-
Notifications
You must be signed in to change notification settings - Fork 11
/
CMakeLists.txt
189 lines (164 loc) · 7.29 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
cmake_minimum_required(VERSION 3.20)
project(Ungar VERSION 1.0.1)
add_library(ungar INTERFACE)
add_library(ungar::ungar ALIAS ungar)
target_compile_features(ungar INTERFACE cxx_std_20)
# ##############################################################################
# Set up CMake options.
# ##############################################################################
include(CMakeDependentOption)
option(
UNGAR_USE_SYSTEM_LIBRARIES
"Prevent automatic download of missing external dependencies. To selectively use system-wide packages, provide their installation prefixes as CMake variables of the form '<PackageName>_ROOT', where <PackageName> is the (case-preserved) name given to the find_package() call and _ROOT is literal."
OFF)
option(UNGAR_BUILD_TESTS "Add targets for unit testing." OFF)
option(UNGAR_BUILD_EXAMPLES "Add targets showing Ungar's features." OFF)
option(UNGAR_INSTALL "Install CMake targets during install step." OFF)
option(UNGAR_ENABLE_OPTIMIZATION
"Enable Ungar's nonlinear programming interface." OFF)
option(UNGAR_ENABLE_PINOCCHIO
"Enable Ungar's interface to Pinocchio's rigid body dynamics algorithms."
OFF)
cmake_dependent_option(
UNGAR_ENABLE_LOGGING
"Enable Ungar's logging functionalities. Automatically enabled if any of the flags 'UNGAR_ENABLE_AUTODIFF', 'UNGAR_ENABLE_OPTIMIZATION', and 'UNGAR_BUILD_EXAMPLES' is set to 'ON'."
OFF
"NOT UNGAR_ENABLE_AUTODIFF;NOT UNGAR_ENABLE_OPTIMIZATION;NOT UNGAR_BUILD_EXAMPLES"
ON)
cmake_dependent_option(
UNGAR_ENABLE_AUTODIFF
"Enable Ungar's autodiff interface. Automatically enabled if 'UNGAR_ENABLE_OPTIMIZATION' is set to 'ON'."
OFF
"NOT UNGAR_ENABLE_OPTIMIZATION"
ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
message(STATUS "--------------------------------------------------")
message(STATUS "Configuring Ungar with the following options:")
message(STATUS " Enable logging : ${UNGAR_ENABLE_LOGGING}")
message(STATUS " Enable autodiff : ${UNGAR_ENABLE_AUTODIFF}")
message(STATUS " Enable optimization : ${UNGAR_ENABLE_OPTIMIZATION}")
message(STATUS " Enable Pinocchio : ${UNGAR_ENABLE_PINOCCHIO}")
message(STATUS " Build examples : ${UNGAR_BUILD_EXAMPLES}")
message(STATUS " Build tests : ${UNGAR_BUILD_TESTS}")
message(STATUS " Install : ${UNGAR_INSTALL}")
message(STATUS " Use system libraries : ${UNGAR_USE_SYSTEM_LIBRARIES}")
# Add external dependencies.
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1)
add_subdirectory(external)
message(STATUS "--------------------------------------------------")
# ##############################################################################
# Add compile options.
# ##############################################################################
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") # Clang.
target_compile_options(ungar INTERFACE "-pedantic" "$<$<CONFIG:RELEASE>:-O2>")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") # GNU Compiler Collection (GCC).
target_compile_options(
ungar
INTERFACE "-pedantic"
"-ftemplate-backtrace-limit=1"
"-fconstexpr-depth=2147483647"
"-fconstexpr-loop-limit=2147483647"
"-fconstexpr-cache-depth=2147483647"
"-fconstexpr-ops-limit=2147483647"
"$<$<CONFIG:RELEASE>:-O3>")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Intel") # Intel C++ Compiler.
message(WARNING "Intel C++ Compiler is not supported.")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # MSVC.
target_compile_options(ungar INTERFACE "/W4" "$<$<CONFIG:RELEASE>:/O2>")
else()
message(WARNING "${CMAKE_CXX_COMPILER_ID} is not supported.")
endif()
# ##############################################################################
# Set Ungar's include directories and link libraries.
# ##############################################################################
# Core modules.
list(APPEND UNGAR_INCLUDE_DIRECTORIES
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
list(APPEND UNGAR_LINK_LIBRARIES Eigen3::Eigen hana Boost::preprocessor)
# Optional modules.
if(UNGAR_ENABLE_LOGGING)
list(APPEND UNGAR_LINK_LIBRARIES spdlog::spdlog)
endif()
if(UNGAR_ENABLE_AUTODIFF)
list(APPEND UNGAR_LINK_LIBRARIES CppAD::CppAD CppADCodeGen::CppADCodeGen
${CMAKE_DL_LIBS} finitediff::finitediff)
endif()
if(UNGAR_ENABLE_OPTIMIZATION)
list(APPEND UNGAR_LINK_LIBRARIES osqp-cpp::osqp-cpp)
endif()
if(UNGAR_ENABLE_PINOCCHIO)
list(APPEND UNGAR_LINK_LIBRARIES pinocchio::pinocchio Boost::boost
"-Wl,--disable-new-dtags")
endif()
target_include_directories(ungar INTERFACE ${UNGAR_INCLUDE_DIRECTORIES})
target_link_libraries(ungar INTERFACE ${UNGAR_LINK_LIBRARIES})
# ##############################################################################
# Set Ungar's compile definitions.
# ##############################################################################
target_compile_definitions(
ungar
INTERFACE $<$<CONFIG:Debug>:_GLIBCXX_ASSERTIONS>
$<$<CONFIG:RelWithDebInfo>:_GLIBCXX_ASSERTIONS>
$<$<CONFIG:MinSizeRel>:UNGAR_CONFIG_ENABLE_RELEASE_MODE>
$<$<CONFIG:Release>:UNGAR_CONFIG_ENABLE_RELEASE_MODE>)
if(UNGAR_ENABLE_LOGGING)
target_compile_definitions(ungar INTERFACE -DUNGAR_CONFIG_ENABLE_LOGGING)
endif()
if(UNGAR_ENABLE_AUTODIFF)
target_compile_definitions(
ungar
INTERFACE -DUNGAR_CONFIG_ENABLE_AUTODIFF
INTERFACE "UNGAR_CODEGEN_FOLDER=\"${CMAKE_BINARY_DIR}/ungar_codegen\"")
endif()
if(UNGAR_ENABLE_OPTIMIZATION)
target_compile_definitions(ungar INTERFACE -DUNGAR_CONFIG_ENABLE_OPTIMIZATION)
endif()
if(UNGAR_ENABLE_PINOCCHIO)
target_compile_definitions(
ungar
INTERFACE -DUNGAR_CONFIG_ENABLE_PINOCCHIO
INTERFACE "UNGAR_DATA_FOLDER=\"${CMAKE_CURRENT_SOURCE_DIR}/data\"")
endif()
# ##############################################################################
# Add optional targets.
# ##############################################################################
if(UNGAR_BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif()
if(UNGAR_BUILD_EXAMPLES)
add_subdirectory(example)
endif()
if(UNGAR_INSTALL)
include(GNUInstallDirs)
# Install Ungar headers.
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# Install Ungar binaries.
install(
TARGETS ungar
EXPORT ${PROJECT_NAME}Targets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
# Install Ungar configuration files.
include(CMakePackageConfigHelpers)
configure_package_config_file(
UngarConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ungar)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY SameMajorVersion)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ungar)
# Perform installation.
install(
EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ungar::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ungar)
endif(UNGAR_INSTALL)