-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
398 lines (361 loc) · 13.1 KB
/
CMakeLists.txt
File metadata and controls
398 lines (361 loc) · 13.1 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
cmake_minimum_required(VERSION 3.21)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Build macOS universal libraries by default
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "")
set(CMAKE_OSX_DEPLOYMENT_TARGET "14.0" CACHE STRING "")
project(Sleipnir LANGUAGES CXX)
include(BuildTypes)
include(CMakePackageConfigHelpers)
include(CTest)
include(CompilerFlags)
include(FetchContent)
# 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)
option(SLEIPNIR_BUILD_BENCHMARKS "Build CasADi and Sleipnir benchmarks" OFF)
option(SLEIPNIR_BUILD_EXAMPLES "Build examples" OFF)
option(SLEIPNIR_BUILD_PYTHON "Build Python module" OFF)
option(
SLEIPNIR_DISABLE_DIAGNOSTICS
"Disable diagnostics support at compile-time"
OFF
)
option(
SLEIPNIR_ENABLE_BOUND_PROJECTION
"Enable projecting the state onto bound inequality constraints inside problem setup"
OFF
)
# Options for using a package manager (e.g., vcpkg) for certain dependencies
option(SLEIPNIR_USE_SYSTEM_EIGEN "Use system eigen" OFF)
option(SLEIPNIR_USE_SYSTEM_NANOBIND "Use system nanobind" OFF)
file(GLOB_RECURSE Sleipnir_src src/*.cpp)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS FALSE)
add_library(Sleipnir ${Sleipnir_src})
add_library(Sleipnir::Sleipnir ALIAS Sleipnir)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
compiler_flags(Sleipnir)
target_compile_definitions(Sleipnir PRIVATE SLEIPNIR_EXPORTS)
target_include_directories(
Sleipnir
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
# Required for std::async()
find_package(Threads)
if(Threads_FOUND)
target_link_libraries(Sleipnir PUBLIC Threads::Threads)
endif()
if(SLEIPNIR_DISABLE_DIAGNOSTICS)
target_compile_definitions(Sleipnir PUBLIC SLEIPNIR_DISABLE_DIAGNOSTICS)
endif()
if(SLEIPNIR_ENABLE_BOUND_PROJECTION)
target_compile_definitions(Sleipnir PUBLIC SLEIPNIR_ENABLE_BOUND_PROJECTION)
endif()
# Eigen dependency
if(NOT SLEIPNIR_USE_SYSTEM_EIGEN)
set(EIGEN_BUILD_CMAKE_PACKAGE TRUE)
FetchContent_Declare(
Eigen3
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
# master on 2026-03-29
GIT_TAG b7f6aed1b9974fd56cce651b4ff059c3bfc67782
SYSTEM
)
FetchContent_MakeAvailable(Eigen3)
else()
find_package(Eigen3 CONFIG REQUIRED)
endif()
target_link_libraries(Sleipnir PUBLIC Eigen3::Eigen)
# small_vector dependency
FetchContent_Declare(
small_vector
GIT_REPOSITORY https://github.com/gharveymn/small_vector.git
# main on 2025-05-15
GIT_TAG b19a9c477cad5e58f8aed59dbe9be1c30e4b9826
SYSTEM
)
FetchContent_MakeAvailable(small_vector)
target_link_libraries(Sleipnir PUBLIC small_vector)
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
# Catch2 dependency
FetchContent_Declare(
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.14.0
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
SYSTEM
)
FetchContent_MakeAvailable(Catch2)
endif()
install(
TARGETS Sleipnir
COMPONENT Sleipnir
EXPORT SleipnirTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
INCLUDES DESTINATION include
)
export(TARGETS Sleipnir FILE Sleipnir.cmake NAMESPACE Sleipnir::)
install(DIRECTORY include/ COMPONENT Sleipnir DESTINATION "include")
install(
EXPORT SleipnirTargets
FILE Sleipnir.cmake
NAMESPACE Sleipnir::
DESTINATION lib/cmake/Sleipnir
)
# Generate the config file that includes the exports
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/SleipnirConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/SleipnirConfig.cmake
INSTALL_DESTINATION "lib/cmake/Sleipnir"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
# Install the config file
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/SleipnirConfig.cmake
COMPONENT Sleipnir
DESTINATION lib/cmake/Sleipnir
)
# Add benchmark executables
if(SLEIPNIR_BUILD_BENCHMARKS)
# Perf benchmark
foreach(benchmark "cart_pole" "flywheel")
file(
GLOB ${benchmark}_perf_benchmark_src
benchmarks/perf/*.cpp
benchmarks/perf/${benchmark}/*.cpp
)
add_executable(
${benchmark}_perf_benchmark
${${benchmark}_perf_benchmark_src}
)
compiler_flags(${benchmark}_perf_benchmark)
target_include_directories(
${benchmark}_perf_benchmark
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/benchmarks
${CMAKE_CURRENT_SOURCE_DIR}/benchmarks/perf
)
target_link_libraries(${benchmark}_perf_benchmark Sleipnir)
endforeach()
# Scalability benchmark (if CasADi exists)
find_package(casadi QUIET)
if(casadi_FOUND)
foreach(benchmark "cart_pole" "flywheel")
file(
GLOB ${benchmark}_scalability_benchmark_src
benchmarks/scalability/*.cpp
benchmarks/scalability/${benchmark}/*.cpp
)
add_executable(
${benchmark}_scalability_benchmark
${${benchmark}_scalability_benchmark_src}
)
compiler_flags(${benchmark}_scalability_benchmark)
target_include_directories(
${benchmark}_scalability_benchmark
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/benchmarks
${CMAKE_CURRENT_SOURCE_DIR}/benchmarks/scalability
)
target_link_libraries(
${benchmark}_scalability_benchmark
Sleipnir
casadi
)
endforeach()
endif()
endif()
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
enable_testing()
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
include(Catch)
# Build Sleipnir tests
file(GLOB_RECURSE sleipnir_test_src test/src/*.cpp)
add_executable(sleipnir_test ${sleipnir_test_src})
compiler_flags(sleipnir_test)
target_include_directories(
sleipnir_test
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test/include
)
target_link_libraries(sleipnir_test Sleipnir Catch2::Catch2WithMain)
if(NOT CMAKE_TOOLCHAIN_FILE)
catch_discover_tests(sleipnir_test)
endif()
endif()
# Build examples and example tests
if(SLEIPNIR_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} Sleipnir)
# Build example test if files exist for it
if(
PROJECT_IS_TOP_LEVEL
AND 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})
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}/examples/${example}/test/include
)
target_link_libraries(
${example}_test
Sleipnir
Catch2::Catch2WithMain
)
if(NOT CMAKE_TOOLCHAIN_FILE)
catch_discover_tests(${example}_test)
endif()
endif()
endforeach()
endif()
if(SLEIPNIR_BUILD_PYTHON)
find_package(
Python
REQUIRED
COMPONENTS Interpreter Development.Module Development.SABIModule
)
if(DEFINED PY_BUILD_CMAKE_MODULE_NAME)
set(PY_DEST ${PY_BUILD_CMAKE_MODULE_NAME})
else()
set(PY_DEST lib/python${Python_VERSION_MAJOR}.${Python_VERSION_MINOR})
endif()
# nanobind dependency
if(NOT SLEIPNIR_USE_SYSTEM_NANOBIND)
FetchContent_Declare(
nanobind
GIT_REPOSITORY https://github.com/wjakob/nanobind.git
GIT_TAG v2.12.0
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
SYSTEM
)
FetchContent_MakeAvailable(nanobind)
else()
find_package(nanobind CONFIG REQUIRED)
endif()
file(GLOB_RECURSE python_ext_src python/cpp/*.cpp)
# Build Sleipnir dependency directly into the wheel to avoid having to
# configure RPATHs
nanobind_add_module(_sleipnir STABLE_ABI ${python_ext_src} ${Sleipnir_src})
compiler_flags(_sleipnir)
target_compile_definitions(_sleipnir PRIVATE SLEIPNIR_PYTHON=1)
target_include_directories(
_sleipnir
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/python/cpp
)
target_link_libraries(
_sleipnir
PUBLIC Threads::Threads Eigen3::Eigen small_vector
)
# Suppress compiler warnings in nanobind
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
# nanobind/include/nanobind/nb_attr.h:212:14: warning: ISO C++ forbids zero-size array
# 212 | arg_data args[Size];
# | ^~~~
target_compile_options(_sleipnir PRIVATE -Wno-pedantic)
endif()
if(
${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang"
OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "AppleClang"
)
target_compile_options(nanobind-static-abi3 PRIVATE -Wno-array-bounds)
target_compile_options(
_sleipnir
PRIVATE -Wno-nested-anon-types -Wno-zero-length-array
)
endif()
install(
TARGETS _sleipnir
COMPONENT python_modules
LIBRARY DESTINATION ${PY_DEST}
)
nanobind_add_stub(
_sleipnir_stub
INSTALL_TIME
MARKER_FILE sleipnir/py.typed
MODULE _sleipnir
OUTPUT sleipnir/__init__.pyi
PYTHON_PATH $<TARGET_FILE_DIR:_sleipnir>
DEPENDS _sleipnir
COMPONENT python_modules
)
nanobind_add_stub(
_sleipnir_autodiff_stub
INSTALL_TIME
MODULE _sleipnir.autodiff
OUTPUT sleipnir/autodiff/__init__.pyi
PYTHON_PATH $<TARGET_FILE_DIR:_sleipnir>
DEPENDS _sleipnir
COMPONENT python_modules
)
nanobind_add_stub(
_sleipnir_optimization_stub
INSTALL_TIME
MODULE _sleipnir.optimization
OUTPUT sleipnir/optimization/__init__.pyi
PYTHON_PATH $<TARGET_FILE_DIR:_sleipnir>
DEPENDS _sleipnir
COMPONENT python_modules
)
# pybind11_mkdoc doesn't support Windows
if(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
# pybind11_mkdoc dependency
FetchContent_Declare(
pybind11_mkdoc
GIT_REPOSITORY https://github.com/pybind/pybind11_mkdoc.git
# master on 2023-02-08
GIT_TAG 42fbf377824185e255b06d68fa70f4efcd569e2d
EXCLUDE_FROM_ALL
SYSTEM
)
FetchContent_MakeAvailable(pybind11_mkdoc)
set(sleipnir_headers
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/expression_type.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/gradient.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/hessian.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/jacobian.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/variable.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/variable_block.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/autodiff/variable_matrix.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/ocp.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/ocp/dynamics_type.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/ocp/timestep_method.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/ocp/transcription_method.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/problem.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/solver/exit_status.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/solver/iteration_info.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/sleipnir/optimization/solver/options.hpp
)
# Generate docs for the Python module
include(Pybind11Mkdoc)
pybind11_mkdoc(_sleipnir "${sleipnir_headers}")
add_dependencies(_sleipnir _sleipnir_docstrings)
endif()
endif()