Skip to content

Commit

Permalink
Allow disabling example build (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul authored Feb 26, 2024
1 parent 6a877e4 commit 01e0fc1
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 01e0fc1

Please sign in to comment.