From 28fdace0da069179603864c2415b4f269512a540 Mon Sep 17 00:00:00 2001 From: Trent Houliston Date: Mon, 12 Aug 2024 17:05:17 +1000 Subject: [PATCH] Move the cmake options into the main cmake file to enable_testing() in root (#115) Moves the cmake options into the main cmake file. Also runs enable_testing() in the main cmake file so that ctest works in the root build directory rather than just in the subdirectory --- CMakeLists.txt | 11 ++++- docs/CMakeLists.txt | 35 ++++++-------- tests/CMakeLists.txt | 111 ++++++++++++++++++++----------------------- 3 files changed, 76 insertions(+), 81 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index db5483fc8..af0a90edb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -118,7 +118,14 @@ endif() add_subdirectory(src) # Add the tests directory -add_subdirectory(tests) +option(BUILD_TESTS "Builds all of the NUClear unit tests." TRUE) +if(BUILD_TESTS) + enable_testing() + add_subdirectory(tests) +endif() # Add the documentation subdirectory -add_subdirectory(docs) +option(BUILD_DOCUMENTATION "Create and install the HTML based API documentation (requires Doxygen and Sphinx)" FALSE) +if(BUILD_DOCUMENTATION) + add_subdirectory(docs) +endif() diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index 41ace683d..6e5213521 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -19,24 +19,19 @@ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEM COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] -option(BUILD_DOCUMENTATION "Create and install the HTML based API documentation (requires Doxygen and Sphinx)" FALSE) -if(BUILD_DOCUMENTATION) - - find_package(Doxygen REQUIRED) - find_package(Sphinx REQUIRED) - - add_custom_target( - docs - COMMAND ${Sphinx_BINARY} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} - WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - COMMENT "Generating documentation pages using sphinx" - ) - - install( - DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} - DESTINATION share/doc - OPTIONAL - ) - -endif(BUILD_DOCUMENTATION) +find_package(Doxygen REQUIRED) +find_package(Sphinx REQUIRED) + +add_custom_target( + docs + COMMAND ${Sphinx_BINARY} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generating documentation pages using sphinx" +) + +install( + DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + DESTINATION share/doc + OPTIONAL +) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5aaf7361a..6a0954670 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -20,64 +20,57 @@ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER I OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] -option(BUILD_TESTS "Builds all of the NUClear unit tests." TRUE) - -if(BUILD_TESTS) - enable_testing() - - # Get catch2 using FetchContent - include(FetchContent) - FetchContent_Declare( - Catch2 - SYSTEM DOWNLOAD_EXTRACT_TIMESTAMP TRUE - URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.6.0.tar.gz - ) - FetchContent_MakeAvailable(Catch2) - - # Silence clang-tidy warnings from catch2 - get_target_property(catch2_target Catch2::Catch2 ALIASED_TARGET) - set_target_properties(${catch2_target} PROPERTIES CXX_CLANG_TIDY "") - set_target_properties(${catch2_target} PROPERTIES CXX_CLANG_TIDY "") - get_target_property(catch2_with_main_target Catch2::Catch2WithMain ALIASED_TARGET) - set_target_properties(${catch2_with_main_target} PROPERTIES CXX_CLANG_TIDY "") - set_target_properties(${catch2_with_main_target} PROPERTIES CXX_CLANG_TIDY "") - - # Create a test_util library that is used by all tests - file(GLOB_RECURSE test_util_src "test_util/*.cpp") - add_library(test_util OBJECT ${test_util_src}) - target_link_libraries(test_util INTERFACE "$") - target_link_libraries(test_util PUBLIC Catch2::Catch2) - target_include_directories(test_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - target_include_directories( - test_util SYSTEM PUBLIC ${CATCH_INCLUDE_DIRS} ${PROJECT_BINARY_DIR}/include "${PROJECT_SOURCE_DIR}/src" +# Get catch2 using FetchContent +include(FetchContent) +FetchContent_Declare( + Catch2 + SYSTEM DOWNLOAD_EXTRACT_TIMESTAMP TRUE + URL https://github.com/catchorg/Catch2/archive/refs/tags/v3.6.0.tar.gz +) +FetchContent_MakeAvailable(Catch2) + +# Silence clang-tidy warnings from catch2 +get_target_property(catch2_target Catch2::Catch2 ALIASED_TARGET) +set_target_properties(${catch2_target} PROPERTIES CXX_CLANG_TIDY "") +set_target_properties(${catch2_target} PROPERTIES CXX_CLANG_TIDY "") +get_target_property(catch2_with_main_target Catch2::Catch2WithMain ALIASED_TARGET) +set_target_properties(${catch2_with_main_target} PROPERTIES CXX_CLANG_TIDY "") +set_target_properties(${catch2_with_main_target} PROPERTIES CXX_CLANG_TIDY "") + +# Create a test_util library that is used by all tests +file(GLOB_RECURSE test_util_src "test_util/*.cpp") +add_library(test_util OBJECT ${test_util_src}) +target_link_libraries(test_util INTERFACE "$") +target_link_libraries(test_util PUBLIC Catch2::Catch2) +target_include_directories(test_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories( + test_util SYSTEM PUBLIC ${CATCH_INCLUDE_DIRS} ${PROJECT_BINARY_DIR}/include "${PROJECT_SOURCE_DIR}/src" +) + +# Create a test binary for each test file +file(GLOB_RECURSE test_sources "tests/*.cpp") +foreach(test_file ${test_sources}) + get_filename_component(test_name ${test_file} NAME_WE) + get_filename_component(test_dir ${test_file} DIRECTORY) + file(RELATIVE_PATH test_dir "${CMAKE_CURRENT_SOURCE_DIR}/tests" ${test_dir}) + + add_executable(${test_name} ${test_file}) + target_include_directories(${test_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) + target_link_libraries(${test_name} test_util) + target_link_libraries(${test_name} Catch2::Catch2WithMain) + + set_property(TARGET ${test_name} PROPERTY FOLDER "tests/${test_dir}") + set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${test_dir}") + + # Add the test + add_test( + NAME "${test_dir}/${test_name}" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${test_dir}/${test_name} --order rand ) +endforeach() - # Create a test binary for each test file - file(GLOB_RECURSE test_sources "tests/*.cpp") - foreach(test_file ${test_sources}) - get_filename_component(test_name ${test_file} NAME_WE) - get_filename_component(test_dir ${test_file} DIRECTORY) - file(RELATIVE_PATH test_dir ${CMAKE_CURRENT_SOURCE_DIR} ${test_dir}) - - add_executable(${test_name} ${test_file}) - target_include_directories(${test_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) - target_link_libraries(${test_name} test_util) - target_link_libraries(${test_name} Catch2::Catch2WithMain) - - set_property(TARGET ${test_name} PROPERTY FOLDER "tests/${test_dir}") - set_property(TARGET ${test_name} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${test_dir}") - - # Add the test - add_test( - NAME "${test_dir}/${test_name}" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/${test_dir}/${test_name} --order rand - ) - endforeach() - - # Special test binary for testing NUClearNet - add_executable(test_network networktest.cpp) - target_link_libraries(test_network test_util) - target_include_directories(test_network PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - -endif(BUILD_TESTS) +# Special test binary for testing NUClearNet +add_executable(test_network networktest.cpp) +target_link_libraries(test_network test_util) +target_include_directories(test_network PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})