From e561c0f9602f8dd589c579f9ae0f981ad4f69928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Wed, 3 Jun 2026 20:46:10 +0200 Subject: [PATCH] build: factor per-frontend CMake into essim_add_frontend() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every frontend repeated the same target wiring (glob sources minus main.cpp → essim_ lib linking essim_core; essim exe from main.cpp linking the lib + essim_frontend; RUNTIME_OUTPUT_DIRECTORY=build/). Move it into a reusable helper cmake/EssimFrontend.cmake::essim_add_frontend(name LIBS ...), included once at the top level. The tui CMakeLists now just fetches FTXUI and calls essim_add_frontend(tui LIBS ftxui::screen ftxui::dom ftxui::component). A new frontend's CMakeLists is its toolkit setup + one call. No behaviour change; binary stays ./build/essim, tests green. Co-Authored-By: Claude Opus 4.8 --- CMakeLists.txt | 4 ++++ cmake/EssimFrontend.cmake | 30 ++++++++++++++++++++++++++++++ src/frontends/tui/CMakeLists.txt | 21 ++------------------- 3 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 cmake/EssimFrontend.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 278da8f..16caa8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,10 @@ project(essim include(FetchContent) +# Shared CMake helpers (essim_add_frontend — per-frontend target boilerplate). +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") +include(EssimFrontend) + # ----------------------------------------------------------------- core deps # libbsdl — standalone BSDL parser (LGPL-2.1), dynamically linked (EUPL-1.2, # which the LGPL permits). Override its path with -DBSDL_DIR=... diff --git a/cmake/EssimFrontend.cmake b/cmake/EssimFrontend.cmake new file mode 100644 index 0000000..a053815 --- /dev/null +++ b/cmake/EssimFrontend.cmake @@ -0,0 +1,30 @@ +# essim_add_frontend( [LIBS ...]) +# +# Builds the boilerplate shared by every frontend under src/frontends//: +# * a static library essim_ from every .cpp in the current directory +# except main.cpp, linking essim_core plus the frontend's own GUI/TUI +# toolkit (LIBS); +# * the `essim` executable from main.cpp, linking essim_ and the shared, +# toolkit-free launcher essim_frontend, emitted at the top of the build tree +# (./build/essim) whichever frontend produced it. +# +# A per-frontend CMakeLists only sets up its toolkit (FetchContent / +# find_package, and any directory-scoped include dirs / definitions) and then +# calls this with the toolkit's link targets — no target wiring repeated. +function(essim_add_frontend name) + cmake_parse_arguments(FE "" "" "LIBS" ${ARGN}) + + set(dir "${CMAKE_CURRENT_SOURCE_DIR}") + + # Frontend library = every .cpp here except the entry point. + file(GLOB FE_SOURCES "${dir}/*.cpp") + list(REMOVE_ITEM FE_SOURCES "${dir}/main.cpp") + + add_library(essim_${name} STATIC ${FE_SOURCES}) + target_link_libraries(essim_${name} PUBLIC essim_core ${FE_LIBS}) + + add_executable(essim "${dir}/main.cpp") + target_link_libraries(essim PRIVATE essim_${name} essim_frontend) + set_target_properties(essim PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") +endfunction() diff --git a/src/frontends/tui/CMakeLists.txt b/src/frontends/tui/CMakeLists.txt index 9f31b6d..58e3235 100644 --- a/src/frontends/tui/CMakeLists.txt +++ b/src/frontends/tui/CMakeLists.txt @@ -18,22 +18,5 @@ FetchContent_Declare(ftxui ) FetchContent_MakeAvailable(ftxui) -# Frontend library = every .cpp here except the entry point. -file(GLOB TUI_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") -list(REMOVE_ITEM TUI_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") - -add_library(essim_tui STATIC ${TUI_SOURCES}) -target_link_libraries(essim_tui - PUBLIC - essim_core - ftxui::screen - ftxui::dom - ftxui::component -) - -add_executable(essim "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") -target_link_libraries(essim PRIVATE essim_tui essim_frontend) - -# Keep the binary at the top of the build tree (./build/essim), regardless of -# which frontend subdir produced it. -set_target_properties(essim PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}") +# Library essim_tui (sources here minus main.cpp) + the `essim` binary. +essim_add_frontend(tui LIBS ftxui::screen ftxui::dom ftxui::component)