From 01e0fc1b50ebe975775b929273f6f8479f8ee3c5 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Sun, 25 Feb 2024 23:12:31 -0800 Subject: [PATCH] Allow disabling example build (#408) --- CMakeLists.txt | 87 +++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4748ab22..8add1227 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,10 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" ON) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS FALSE) +option(BUILD_BENCHMARKING "Build CasADi and Sleipnir benchmarks" ON) +option(BUILD_EXAMPLES "Build examples" ON) +option(BUILD_PYTHON "Build Python module" OFF) + include(SleipnirCompilerFlags) file(GLOB_RECURSE Sleipnir_src src/*.cpp) @@ -190,8 +194,6 @@ install( DESTINATION lib/cmake/Sleipnir ) -option(BUILD_BENCHMARKING "Build CasADi and Sleipnir benchmarks" ON) - # Add benchmark executables if CasADi exists if(BUILD_BENCHMARKING) find_package(casadi QUIET) @@ -246,54 +248,53 @@ if(BUILD_TESTING) endif() # Build examples and example tests -include(SleipnirSubdirList) -sleipnir_subdir_list(EXAMPLES ${CMAKE_CURRENT_SOURCE_DIR}/examples) -foreach(example ${EXAMPLES}) - # Build example - file(GLOB_RECURSE sources examples/${example}/src/*.cpp) - add_executable(${example} ${sources}) - sleipnir_compiler_flags(${example}) - target_include_directories( - ${example} - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/include - ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/units/include - ) - target_link_libraries(${example} PRIVATE Sleipnir) - - # Build example test if files exist for it - if( - BUILD_TESTING - AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/test - ) - file(GLOB_RECURSE test_sources examples/${example}/test/*.cpp) - add_executable(${example}Test ${sources} ${test_sources}) - target_include_directories( - ${example}Test - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/src - ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/test - ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/units/include - ) - sleipnir_compiler_flags(${example}Test) - target_compile_definitions(${example}Test PUBLIC RUNNING_TESTS) +if(BUILD_EXAMPLES) + include(SleipnirSubdirList) + sleipnir_subdir_list(EXAMPLES ${CMAKE_CURRENT_SOURCE_DIR}/examples) + foreach(example ${EXAMPLES}) + # Build example + file(GLOB_RECURSE sources examples/${example}/src/*.cpp) + add_executable(${example} ${sources}) + sleipnir_compiler_flags(${example}) target_include_directories( - ${example}Test + ${example} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/include ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/units/include ) - target_link_libraries( - ${example}Test - PRIVATE Sleipnir Catch2::Catch2WithMain + target_link_libraries(${example} PRIVATE Sleipnir) + + # Build example test if files exist for it + if( + BUILD_TESTING + AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/test ) - if(NOT CMAKE_TOOLCHAIN_FILE) - catch_discover_tests(${example}Test) + file(GLOB_RECURSE test_sources examples/${example}/test/*.cpp) + add_executable(${example}Test ${sources} ${test_sources}) + target_include_directories( + ${example}Test + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/src + ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/test + ) + sleipnir_compiler_flags(${example}Test) + target_compile_definitions(${example}Test PUBLIC RUNNING_TESTS) + target_include_directories( + ${example}Test + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/include + ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/units/include + ) + target_link_libraries( + ${example}Test + PRIVATE Sleipnir Catch2::Catch2WithMain + ) + if(NOT CMAKE_TOOLCHAIN_FILE) + catch_discover_tests(${example}Test) + endif() endif() - endif() -endforeach() - -option(BUILD_PYTHON "Build Python module" OFF) + endforeach() +endif() if(BUILD_PYTHON) find_package(Python REQUIRED COMPONENTS Interpreter Development)