build: factor per-frontend CMake into essim_add_frontend()

Every frontend repeated the same target wiring (glob sources minus main.cpp →
essim_<name> 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 <noreply@anthropic.com>
This commit is contained in:
2026-06-03 20:46:10 +02:00
parent 091ef6fe4b
commit e561c0f960
3 changed files with 36 additions and 19 deletions

View File

@@ -11,6 +11,10 @@ project(essim
include(FetchContent) 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 # ----------------------------------------------------------------- core deps
# libbsdl — standalone BSDL parser (LGPL-2.1), dynamically linked (EUPL-1.2, # libbsdl — standalone BSDL parser (LGPL-2.1), dynamically linked (EUPL-1.2,
# which the LGPL permits). Override its path with -DBSDL_DIR=... # which the LGPL permits). Override its path with -DBSDL_DIR=...

30
cmake/EssimFrontend.cmake Normal file
View File

@@ -0,0 +1,30 @@
# essim_add_frontend(<name> [LIBS <toolkit link targets>...])
#
# Builds the boilerplate shared by every frontend under src/frontends/<name>/:
# * a static library essim_<name> 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_<name> 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()

View File

@@ -18,22 +18,5 @@ FetchContent_Declare(ftxui
) )
FetchContent_MakeAvailable(ftxui) FetchContent_MakeAvailable(ftxui)
# Frontend library = every .cpp here except the entry point. # Library essim_tui (sources here minus main.cpp) + the `essim` binary.
file(GLOB TUI_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp") essim_add_frontend(tui LIBS ftxui::screen ftxui::dom ftxui::component)
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}")