Skip to content

Commit

Permalink
Move the cmake options into the main cmake file
Browse files Browse the repository at this point in the history
  • Loading branch information
TrentHouliston committed Aug 12, 2024
1 parent 852d2d7 commit b770f69
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 81 deletions.
11 changes: 9 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
35 changes: 15 additions & 20 deletions docs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
111 changes: 52 additions & 59 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 "$<LINK_LIBRARY:WHOLE_ARCHIVE,NUClear::nuclear>")
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 "$<LINK_LIBRARY:WHOLE_ARCHIVE,NUClear::nuclear>")
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})

0 comments on commit b770f69

Please sign in to comment.