Skip to content

Commit

Permalink
cleanup the cmake files
Browse files Browse the repository at this point in the history
reduce redandent settings and options
  • Loading branch information
ClausKlein committed May 5, 2022
1 parent 388a778 commit 5085bd4
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 64 deletions.
35 changes: 28 additions & 7 deletions CMakeLists.txt
Expand Up @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.16...3.23)
# set a default CXX standard for the tools and targets that do not specify them.
# If commented, the latest supported standard for your compiler is automatically set.
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)

# Add project_options v0.21.0
# https://github.com/aminya/project_options
Expand All @@ -13,7 +14,7 @@ FetchContent_MakeAvailable(_project_options)
include(${_project_options_SOURCE_DIR}/Index.cmake)

# Define the features of the project
include("./Features.cmake")
include("Features.cmake")

# install vcpkg dependencies: - should be called before defining project()
run_vcpkg()
Expand All @@ -29,10 +30,12 @@ project(
# enable sanitizers and clang-tidy if running the tests
set(ENABLE_CLANG_TIDY OFF)
set(ENABLE_CPPCHECK OFF)
set(ENABLE_COVERAGE OFF)
set(ENABLE_SANITIZER_ADDRESS OFF)
if(FEATURE_TESTS)
set(ENABLE_CLANG_TIDY "ENABLE_CLANG_TIDY")
set(ENABLE_CPPCHECK "ENABLE_CPPCHECK")
#XXX set(ENABLE_CPPCHECK "ENABLE_CPPCHECK")
set(ENABLE_COVERAGE "ENABLE_COVERAGE")

string(FIND "$ENV{PATH}" "$ENV{VSINSTALLDIR}" index_of_vs_install_dir)
if(# not windows
Expand All @@ -52,6 +55,9 @@ else()
set(ENABLE_DOXYGEN OFF)
endif()

# to get CMAKE_INSTALL_INCLUDEDIR
include(GNUInstallDirs)

# Initialize project_options variable related to this project
# This overwrites `project_options` and sets `project_warnings`
# uncomment to enable the options. Some of them accept one or more inputs:
Expand All @@ -62,7 +68,7 @@ project_options(
# ENABLE_INTERPROCEDURAL_OPTIMIZATION
# ENABLE_NATIVE_OPTIMIZATION
${ENABLE_DOXYGEN}
# ENABLE_COVERAGE
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
# ENABLE_SANITIZER_UNDEFINED_BEHAVIOR
# ENABLE_SANITIZER_LEAK
Expand All @@ -77,9 +83,24 @@ project_options(
# ENABLE_UNITY
)

add_subdirectory(./my_exe)
add_subdirectory(./my_lib)
add_subdirectory(./my_header_lib)
# use xml reporter if coverage is enabled
if(${ENABLE_COVERAGE})
set(COVERAGE_ARGS REPORTER xml)
endif()

enable_testing()

add_subdirectory(my_exe)
add_subdirectory(my_lib)
add_subdirectory(my_header_lib)

if(NOT DEFINED my_header_lib_DEPENDENCIES_CONFIGURED)
message(WARNING "missing my_header_lib_DEPENDENCIES_CONFIGURED!")
endif()

if(NOT DEFINED my_header_lib_INCLUDE_DIR)
message(WARNING "missing my_header_lib_INCLUDE_DIR!")
endif()

# Package the project
package_project(
Expand All @@ -90,7 +111,7 @@ package_project(
project_options
project_warnings
INTERFACE_DEPENDENCIES_CONFIGURED
${my_header_lib_DEPENDENCIES_CONFIGURED}
fmt # FIXME ${my_header_lib_DEPENDENCIES_CONFIGURED}
INTERFACE_INCLUDES
${my_header_lib_INCLUDE_DIR}
PUBLIC_INCLUDES
Expand Down
29 changes: 16 additions & 13 deletions Makefile
Expand Up @@ -2,31 +2,34 @@
# - list all the task under PHONY
.PHONY: build test install test_release docs format clean

# see https://cmake.org/cmake/help/latest/manual/cmake-env-variables.7.html
CMAKE_GENERATOR?="Ninja Multi-Config"
export CMAKE_GENERATOR
export CMAKE_EXPORT_COMPILE_COMMANDS=YES
export CTEST_OUTPUT_ON_FAILURE=YES

build:
cmake ./ -B ./build -G "Ninja Multi-Config" -D CMAKE_BUILD_TYPE:STRING=Release -D FEATURE_TESTS:BOOL=OFF -D FEATURE_DOCS:BOOL=OFF
cmake -B ./build -G $(CMAKE_GENERATOR) -D CMAKE_BUILD_TYPE:STRING=Release -D FEATURE_TESTS:BOOL=OFF
cmake --build ./build --config Release

install: test build
# NOTE: it is important to not export a build with enabled FEATURE_TESTS! CK
install: test_release build
DESTDIR=${HOME}/.local cmake --build ./build --config Release --target install

test:
cmake ./ -B ./build -G "Ninja Multi-Config" -D CMAKE_BUILD_TYPE:STRING=Debug -D FEATURE_TESTS:BOOL=ON
cmake -B ./build -G $(CMAKE_GENERATOR) -D CMAKE_BUILD_TYPE:STRING=Debug -D FEATURE_TESTS:BOOL=ON
cmake --build ./build --config Debug

(cd build/my_exe/test && ctest -C Debug --output-on-failure)
(cd build/my_header_lib/test && ctest -C Debug --output-on-failure)
(cd build/my_lib/test && ctest -C Debug --output-on-failure)
cmake --build ./build --config Debug --target test
gcovr -r .

test_release:
cmake ./ -B ./build -G "Ninja Multi-Config" -D CMAKE_BUILD_TYPE:STRING=RelWithDebInfo -D FEATURE_TESTS:BOOL=ON
cmake -B ./build -G $(CMAKE_GENERATOR) -D CMAKE_BUILD_TYPE:STRING=RelWithDebInfo -D FEATURE_TESTS:BOOL=ON
cmake --build ./build --config RelWithDebInfo

(cd build/my_exe/test && ctest -C RelWithDebInfo --output-on-failure)
(cd build/my_header_lib/test && ctest -C RelWithDebInfo --output-on-failure)
(cd build/my_lib/test && ctest -C RelWithDebInfo --output-on-failure)
cmake --build ./build --config RelWithDebInfo --target test
gcovr -r .

docs:
cmake ./ -B ./build -G "Ninja Multi-Config" -D CMAKE_BUILD_TYPE:STRING=Debug -D FEATURE_DOCS:BOOL=ON -D FEATURE_TESTS:BOOL=OFF
cmake -B ./build -G $(CMAKE_GENERATOR) -D CMAKE_BUILD_TYPE:STRING=Debug -D FEATURE_DOCS:BOOL=ON -D FEATURE_TESTS:BOOL=OFF
cmake --build ./build --target doxygen-docs --config Debug

format:
Expand Down
4 changes: 2 additions & 2 deletions my_exe/CMakeLists.txt
@@ -1,4 +1,4 @@
add_executable(my_exe "./src/main.cpp")
add_executable(my_exe "src/main.cpp")
target_link_libraries(my_exe PRIVATE project_options project_warnings) # link project_options/warnings

# Includes
Expand All @@ -18,5 +18,5 @@ set(my_exe_LINKED_LIBRARIES fmt::fmt)
target_link_system_libraries(my_exe PRIVATE ${my_exe_LINKED_LIBRARIES})

if(FEATURE_TESTS)
add_subdirectory("./test")
add_subdirectory("test")
endif()
8 changes: 2 additions & 6 deletions my_exe/test/CMakeLists.txt
Expand Up @@ -5,7 +5,6 @@ foreach(DEPENDENCY ${tests_DEPENDENCIES_CONFIGURED})
find_package(${DEPENDENCY} CONFIG REQUIRED)
endforeach()

include(CTest)
include(Catch)

# calling my_exe executable directly
Expand All @@ -15,7 +14,7 @@ add_test(
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

# testing helpers
add_executable(my_exe_helpers_tests "./tests.cpp")
add_executable(my_exe_helpers_tests "tests.cpp")

# because my_exe helpers and dependencies are private, we should link them here again
target_include_directories(my_exe_helpers_tests PRIVATE "${my_exe_INCLUDE_DIR}")
Expand All @@ -30,7 +29,4 @@ target_link_libraries(
# generate a main function for the test executable
target_compile_definitions(my_exe_helpers_tests PRIVATE CATCH_CONFIG_MAIN)

# use xml reporter if coverage is enabled
if(${ENABLE_COVERAGE})
set(COVERAGE_ARGS REPORTER xml)
endif()
catch_discover_tests(my_exe_helpers_tests ${COVERAGE_ARGS})
15 changes: 8 additions & 7 deletions my_header_lib/CMakeLists.txt
Expand Up @@ -2,21 +2,22 @@ add_library(my_header_lib INTERFACE)
target_link_libraries(my_header_lib INTERFACE project_options project_warnings) # link project_options/warnings

# Includes
set(my_header_lib_INCLUDE_DIR "./include") # must be relative paths
target_include_directories(
my_header_lib INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${my_header_lib_INCLUDE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
set(my_header_lib_INCLUDE_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/include"
PARENT_SCOPE)
target_include_directories(my_header_lib INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

# Find dependencies:
set(my_header_lib_DEPENDENCIES_CONFIGURED fmt)
set(my_header_lib_DEPENDENCIES_CONFIGURED fmt) # FIXME: PARENT_SCOPE)

foreach(DEPENDENCY ${my_header_lib_DEPENDENCIES_CONFIGURED})
find_package(${DEPENDENCY} CONFIG REQUIRED)
endforeach()

# Link dependencies:
target_link_system_libraries(my_header_lib INTERFACE fmt::fmt)
target_link_system_libraries(my_header_lib INTERFACE fmt::fmt-header-only)

if(FEATURE_TESTS)
add_subdirectory("./test")
add_subdirectory("test")
endif()
11 changes: 2 additions & 9 deletions my_header_lib/test/CMakeLists.txt
Expand Up @@ -5,12 +5,10 @@ foreach(DEPENDENCY ${tests_DEPENDENCIES_CONFIGURED})
find_package(${DEPENDENCY} CONFIG REQUIRED)
endforeach()

include(CTest)
include(Catch)

# test executable

add_executable(my_header_lib_tests "./tests.cpp")
add_executable(my_header_lib_tests "tests.cpp")

target_link_libraries(
my_header_lib_tests
Expand All @@ -21,13 +19,8 @@ target_link_libraries(
# generate a main function for the test executable
target_compile_definitions(my_header_lib_tests PRIVATE CATCH_CONFIG_MAIN)

# use xml reporter if coverage is enabled
if(${ENABLE_COVERAGE})
set(COVERAGE_ARGS REPORTER xml)
endif()

# automatically discover tests that are defined in catch based test files you can modify the tests
catch_discover_tests(my_header_lib_tests ${COVERAGE_ARGS})

# constexpr tests
add_subdirectory("./constexpr")
add_subdirectory("constexpr")
6 changes: 3 additions & 3 deletions my_header_lib/test/constexpr/CMakeLists.txt
@@ -1,5 +1,5 @@
# Add a file containing a set of constexpr tests
add_executable(my_header_lib_constexpr_tests "./constexpr_tests.cpp")
add_executable(my_header_lib_constexpr_tests "constexpr_tests.cpp")

target_link_libraries(
my_header_lib_constexpr_tests
Expand All @@ -13,7 +13,7 @@ catch_discover_tests(my_header_lib_constexpr_tests ${COVERAGE_ARGS})

# Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when
# things go wrong with the constexpr testing
add_executable(my_header_lib_relaxed_constexpr_tests "./constexpr_tests.cpp")
add_executable(my_header_lib_relaxed_constexpr_tests "constexpr_tests.cpp")

target_link_libraries(
my_header_lib_relaxed_constexpr_tests
Expand All @@ -22,6 +22,6 @@ target_link_libraries(
project_options
Catch2::Catch2)
target_compile_definitions(my_header_lib_relaxed_constexpr_tests PRIVATE CATCH_CONFIG_MAIN)
target_compile_definitions(my_header_lib_relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE)
target_compile_definitions(my_header_lib_relaxed_constexpr_tests PRIVATE CATCH_CONFIG_RUNTIME_STATIC_REQUIRE)

catch_discover_tests(my_header_lib_relaxed_constexpr_tests ${COVERAGE_ARGS})
16 changes: 11 additions & 5 deletions my_lib/CMakeLists.txt
@@ -1,10 +1,16 @@
add_library(my_lib "./src/my_lib/lib.cpp")
add_library(my_lib "src/my_lib/lib.cpp")
target_link_libraries(my_lib PRIVATE project_options project_warnings) # link project_options/warnings

if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
message(FATAL_ERROR "missing CMAKE_INSTALL_INCLUDEDIR!")
endif()

# Includes
set(my_lib_INCLUDE_DIR "include") # must be relative paths
target_include_directories(my_lib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${my_lib_INCLUDE_DIR}>"
"$<INSTALL_INTERFACE:./${CMAKE_INSTALL_INCLUDEDIR}>")
set(my_lib_INCLUDE_DIR
"${CMAKE_CURRENT_SOURCE_DIR}/include"
PARENT_SCOPE)
target_include_directories(my_lib PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

# Find dependencies:
set(my_lib_DEPENDENCIES_CONFIGURED fmt)
Expand All @@ -17,5 +23,5 @@ endforeach()
target_link_system_libraries(my_lib PRIVATE fmt::fmt)

if(FEATURE_TESTS)
add_subdirectory("./test")
add_subdirectory("test")
endif()
11 changes: 2 additions & 9 deletions my_lib/test/CMakeLists.txt
Expand Up @@ -5,12 +5,10 @@ foreach(DEPENDENCY ${tests_DEPENDENCIES_CONFIGURED})
find_package(${DEPENDENCY} CONFIG REQUIRED)
endforeach()

include(CTest)
include(Catch)

# test executable

add_executable(my_lib_tests "./tests.cpp")
add_executable(my_lib_tests "tests.cpp")

target_link_libraries(
my_lib_tests
Expand All @@ -21,13 +19,8 @@ target_link_libraries(
# generate a main function for the test executable
target_compile_definitions(my_lib_tests PRIVATE CATCH_CONFIG_MAIN)

# use xml reporter if coverage is enabled
if(${ENABLE_COVERAGE})
set(COVERAGE_ARGS REPORTER xml)
endif()

# automatically discover tests that are defined in catch based test files you can modify the tests
catch_discover_tests(my_lib_tests ${COVERAGE_ARGS})

# constexpr tests
add_subdirectory("./constexpr")
add_subdirectory("constexpr")
6 changes: 3 additions & 3 deletions my_lib/test/constexpr/CMakeLists.txt
@@ -1,5 +1,5 @@
# Add a file containing a set of constexpr tests
add_executable(my_lib_constexpr_tests "./constexpr_tests.cpp")
add_executable(my_lib_constexpr_tests "constexpr_tests.cpp")

target_link_libraries(
my_lib_constexpr_tests
Expand All @@ -13,7 +13,7 @@ catch_discover_tests(my_lib_constexpr_tests ${COVERAGE_ARGS})

# Disable the constexpr portion of the test, and build again this allows us to have an executable that we can debug when
# things go wrong with the constexpr testing
add_executable(my_lib_relaxed_constexpr_tests "./constexpr_tests.cpp")
add_executable(my_lib_relaxed_constexpr_tests "constexpr_tests.cpp")

target_link_libraries(
my_lib_relaxed_constexpr_tests
Expand All @@ -22,6 +22,6 @@ target_link_libraries(
project_options
Catch2::Catch2)
target_compile_definitions(my_lib_relaxed_constexpr_tests PRIVATE CATCH_CONFIG_MAIN)
target_compile_definitions(my_lib_relaxed_constexpr_tests PRIVATE -DCATCH_CONFIG_RUNTIME_STATIC_REQUIRE)
target_compile_definitions(my_lib_relaxed_constexpr_tests PRIVATE CATCH_CONFIG_RUNTIME_STATIC_REQUIRE)

catch_discover_tests(my_lib_relaxed_constexpr_tests ${COVERAGE_ARGS})

0 comments on commit 5085bd4

Please sign in to comment.