From 66e986572d65e622e78190aa971313ed7e3c8b9b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 6 Jan 2026 14:19:58 +0100 Subject: build: clean up configuration --- CMakeLists.txt | 27 ++++++++++++++++++++++++--- cmake/Modules/AddExample.cmake | 25 +++++++++++++++++++++++++ examples/CMakeLists.txt | 14 +++----------- lib/CMakeLists.txt | 8 ++++++++ tests/CMakeLists.txt | 40 ++++++++++++++-------------------------- 5 files changed, 74 insertions(+), 40 deletions(-) create mode 100644 cmake/Modules/AddExample.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index ea55749..b46834c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,9 +8,29 @@ project("newtype" # Project Options -option(NEWTYPE_BUILD_DOCS "Build the library documentation" ON) -option(NEWTYPE_BUILD_EXAMPLES "Build the library examples" ON) -option(NEWTYPE_BUILD_TESTS "Build the library tests" ON) +option(NEWTYPE_BUILD_DOCS "Build the library documentation" OFF) +option(NEWTYPE_BUILD_EXAMPLES "Build the library examples" OFF) +option(NEWTYPE_BUILD_TESTS "Build the library tests" OFF) + +# Project Dependencies + +include("FetchContent") + +if(NEWTYPE_BUILD_TESTS) + FetchContent_Declare( + "Catch2" + URL "https://github.com/catchorg/Catch2/archive/refs/tags/v3.7.0.zip" + URL_HASH "SHA256=75b04c94471a70680f10f5d0d985bd1a96b8941d040d6a7bfd43f6c6b1de9daf" + EXCLUDE_FROM_ALL + FIND_PACKAGE_ARGS + "3.7.0" EXACT + COMPONENTS "Catch2WithMain" + ) + + FetchContent_MakeAvailable("Catch2") + + include("Catch") +endif() # Project Components @@ -21,6 +41,7 @@ if(NEWTYPE_BUILD_DOCS) endif() if(NEWTYPE_BUILD_EXAMPLES) + include("cmake/Modules/AddExample.cmake") add_subdirectory("examples") endif() diff --git a/cmake/Modules/AddExample.cmake b/cmake/Modules/AddExample.cmake new file mode 100644 index 0000000..e5a78e6 --- /dev/null +++ b/cmake/Modules/AddExample.cmake @@ -0,0 +1,25 @@ +function(newtype_add_example SOURCE_FILE) + get_filename_component(NAME "${SOURCE_FILE}" NAME_WE) + + add_executable("ex_${NAME}") + + target_sources("ex_${NAME}" PRIVATE + ${SOURCE_FILE} + ) + + target_link_libraries("ex_${NAME}" PRIVATE + "newtype::lib" + ) + + target_compile_options("ex_${NAME}" PRIVATE + "$<$:-Wall>" + "$<$:-Wextra>" + "$<$:-Werror>" + "$<$:-pedantic-errors>" + ) + + set_target_properties("ex_${NAME}" PROPERTIES + CMAKE_CXX_EXTENSIONS OFF + CMAKE_CXX_STANDARD_REQUIRED YES + ) +endfunction() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a25b061..c0834d3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,14 +1,6 @@ -file(GLOB SOURCES - RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" - CONFIGURE_DEPENDS - "src/*.cpp" -) - -foreach(EXAMPLE IN LISTS SOURCES) - get_filename_component(NAME "${EXAMPLE}" NAME_WE) - add_executable("ex_${NAME}" "${EXAMPLE}") - target_link_libraries("ex_${NAME}" "newtype::lib") -endforeach() +newtype_add_example("src/basic_usage.cpp") +newtype_add_example("src/basic_usage_with_show.cpp") +newtype_add_example("src/basic_usage_with_read.cpp") install(DIRECTORY "src/" TYPE DOC diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ec76c2e..3d3060e 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,5 +1,13 @@ add_library("newtype" INTERFACE) +target_sources("newtype" PUBLIC + FILE_SET HEADERS + FILES + "include/newtype/newtype.hpp" + BASE_DIRS + "${CMAKE_CURRENT_SOURCE_DIR}/include" +) + target_include_directories("newtype" INTERFACE "$" "$" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8e5e798..4c6c14c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,30 +1,19 @@ -include("FetchContent") - -FetchContent_Declare( - "Catch2" - URL "https://github.com/catchorg/Catch2/archive/refs/tags/v3.7.0.zip" - URL_HASH "SHA256=75b04c94471a70680f10f5d0d985bd1a96b8941d040d6a7bfd43f6c6b1de9daf" - EXCLUDE_FROM_ALL - FIND_PACKAGE_ARGS - "3.7.0" EXACT - COMPONENTS "Catch2WithMain" -) - -FetchContent_MakeAvailable("Catch2") - -include("Catch") - -file(GLOB SOURCES - RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" - CONFIGURE_DEPENDS - "src/*.cpp" -) - -add_executable("newtype_tests" - ${SOURCES} +add_executable("newtype_tests") + +target_sources("newtype_tests" PRIVATE + "src/arithmetic.cpp" + "src/constructors.cpp" + "src/conversion.cpp" + "src/derivation_clause.cpp" + "src/equality_comparison.cpp" + "src/hash.cpp" + "src/io_operators.cpp" + "src/iterable.cpp" + "src/relational_operators.cpp" + "src/threeway_comparison.cpp" ) -target_link_libraries("newtype_tests" +target_link_libraries("newtype_tests" PRIVATE "newtype::lib" "Catch2::Catch2WithMain" ) @@ -34,7 +23,6 @@ target_compile_options("newtype_tests" PRIVATE "$<$:-Wextra>" "$<$:-Werror>" "$<$:-pedantic-errors>" - "$<$:-fconcepts-diagnostics-depth=5>" ) set_target_properties("newtype_tests" PROPERTIES -- cgit v1.2.3