This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCMakeLists.txt
235 lines (200 loc) · 7.02 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# Disable in-source builds to prevent source tree corruption
if("${CMAKE_CURRENT_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}")
message(
FATAL_ERROR
"
FATAL: In-source builds are not allowed.
You should create a separate directory for build files.
"
)
endif()
cmake_minimum_required(VERSION 3.21)
set(CMAKE_MODULE_PATH
${CMAKE_MODULE_PATH}
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules"
)
project(TrajoptLib LANGUAGES CXX)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# Use, i.e. don't skip the full RPATH for the build tree
set(CMAKE_SKIP_BUILD_RPATH FALSE)
# When building, don't use the install RPATH already (but later on when
# installing)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
# Add the automatically determined parts of the RPATH which point to directories
# outside the build tree to the install RPATH
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# The RPATH to be used when installing, but only if it's not a system directory
list(
FIND
CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES
"${CMAKE_INSTALL_PREFIX}/lib"
isSystemDir
)
if("${isSystemDir}" STREQUAL "-1")
list(APPEND CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
endif()
# Set default build type to release with debug info (i.e. release mode
# optimizations are performed, but debug info still exists).
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "" FORCE)
endif()
# Generate compile_commands.json by default
if(NOT CMAKE_EXPORT_COMPILE_COMMANDS)
set(CMAKE_EXPORT_COMPILE_COMMANDS "YES" CACHE STRING "" FORCE)
endif()
# Control where the static and shared libraries are built so that on Windows,
# we don't need to tinker with the path to run the executable
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS FALSE)
option(BUILD_EXAMPLES "Build examples" OFF)
include(CompilerFlags)
file(GLOB_RECURSE TrajoptLib_src src/*.cpp)
list(FILTER TrajoptLib_src EXCLUDE REGEX RustFFI.cpp)
add_library(TrajoptLib ${TrajoptLib_src})
compiler_flags(TrajoptLib)
target_include_directories(TrajoptLib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set_target_properties(TrajoptLib PROPERTIES DEBUG_POSTFIX "d")
set_property(TARGET TrajoptLib PROPERTY FOLDER "libraries")
target_compile_definitions(TrajoptLib PRIVATE TRAJOPT_EXPORTS)
include(CTest)
include(FetchContent)
if(BUILD_TESTING)
# Catch2 dependency
fetchcontent_declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.6.0
CMAKE_ARGS
)
fetchcontent_makeavailable(Catch2)
endif()
set(BUILD_TESTING_SAVE ${BUILD_TESTING})
set(BUILD_EXAMPLES_SAVE ${BUILD_EXAMPLES})
set(BUILD_TESTING OFF)
set(BUILD_EXAMPLES OFF)
fetchcontent_declare(
Sleipnir
GIT_REPOSITORY https://github.com/SleipnirGroup/Sleipnir.git
# main on 2024-06-23
GIT_TAG 346ef361d306b60fa9cf8d96912c37cfaecf78b7
PATCH_COMMAND
git apply ${CMAKE_CURRENT_SOURCE_DIR}/cmake/0001-Downgrade-to-C-20.patch
UPDATE_DISCONNECTED 1
)
fetchcontent_makeavailable(Sleipnir)
set(BUILD_TESTING ${BUILD_TESTING_SAVE})
set(BUILD_EXAMPLES ${BUILD_EXAMPLES_SAVE})
target_link_libraries(TrajoptLib PUBLIC Sleipnir)
target_include_directories(
TrajoptLib
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
install(
TARGETS TrajoptLib
EXPORT TrajoptLibTargets
LIBRARY
DESTINATION lib
ARCHIVE
DESTINATION lib
RUNTIME
DESTINATION bin
INCLUDES DESTINATION include
)
export(TARGETS TrajoptLib FILE TrajoptLib.cmake NAMESPACE TrajoptLib::)
install(DIRECTORY include/ COMPONENT TrajoptLib DESTINATION "include")
install(
EXPORT TrajoptLibTargets
FILE TrajoptLib.cmake
NAMESPACE TrajoptLib::
DESTINATION lib/cmake/TrajoptLib
)
include(CMakePackageConfigHelpers)
# Generate the config file that includes the exports
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/TrajoptLibConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/TrajoptLibConfig.cmake
INSTALL_DESTINATION "lib/cmake/TrajoptLib"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
# Install the config file
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/TrajoptLibConfig.cmake
COMPONENT TrajoptLib
DESTINATION lib/cmake/TrajoptLib
)
if(BUILD_TESTING)
enable_testing()
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
include(Catch)
endif()
# Build TrajoptLib tests
if(BUILD_TESTING)
file(GLOB_RECURSE TrajoptLib_test_src test/src/*.cpp)
add_executable(TrajoptLibTest ${TrajoptLib_test_src})
compiler_flags(TrajoptLibTest)
target_include_directories(
TrajoptLibTest
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${CMAKE_CURRENT_SOURCE_DIR}/test/include
)
target_link_libraries(
TrajoptLibTest
PRIVATE TrajoptLib Catch2::Catch2WithMain
)
if(NOT CMAKE_TOOLCHAIN_FILE)
# https://github.com/catchorg/Catch2/issues/2873
catch_discover_tests(TrajoptLibTest PROPERTIES SKIP_RETURN_CODE 4)
endif()
endif()
# Build examples and example tests
if(BUILD_EXAMPLES)
include(SubdirList)
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})
compiler_flags(${example})
target_include_directories(
${example}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/examples/${example}/include
)
target_link_libraries(${example} PRIVATE TrajoptLib)
# 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
)
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
)
target_link_libraries(
${example}Test
PRIVATE TrajoptLib Catch2::Catch2WithMain
)
if(NOT CMAKE_TOOLCHAIN_FILE)
# https://github.com/catchorg/Catch2/issues/2873
catch_discover_tests(${example}Test PROPERTIES SKIP_RETURN_CODE 4)
endif()
endif()
endforeach()
endif()