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)