-
Notifications
You must be signed in to change notification settings - Fork 8
/
CMakeLists.txt
237 lines (204 loc) · 8.41 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
236
237
cmake_minimum_required(VERSION 3.8)
project(MPPI-Generic VERSION 0.9.0 LANGUAGES C CXX CUDA)
# https://cmake.org/cmake/help/latest/policy/CMP0104.html
if (POLICY CMP0104)
cmake_policy(SET CMP0104 OLD)
endif()
####################################
# Set up options for configuration #
####################################
option(BUILD_TESTS "BUILD_TESTS" OFF)
option(BUILD_EXAMPLES "BUILD_EXAMPLES" OFF)
option(MPPI_USE_CUDA_BARRIERS "Compile MPPI-Generic with cuda barrier support. Turn off for GPUs older than the 20 series." ON)
option(MPPI_EXPORT_PACKAGE "Export this folder as the MPPI installation to the global CMake package repository" OFF)
set(MPPI_CUDA_ARCH_LIST "" CACHE STRING "Specific CUDA Architectures to build for. Leave empty for automatic selection.")
# Configure CMake Cuda Flags for MPPI
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(MPPIGenericToolsConfig)
set(BUILD_FLAGS "-Wuninitialized -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${BUILD_FLAGS}")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -w")
# Allow the lib location to be overwritten from command line
if (NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
get_filename_component(PROJECT_LIBS_DIR ${PROJECT_BINARY_DIR}/lib ABSOLUTE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
else()
get_filename_component(PROJECT_LIBS_DIR ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} ABSOLUTE)
endif()
# set up cnpy
# Don't install cnpy to user package registry
set(TMP_PACKAGE_REGISTRY_EXPORT ${CMAKE_EXPORT_NO_PACKAGE_REGISTRY})
set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)
add_subdirectory(submodules/cnpy)
# Set the no package registry back to default
set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ${TMP_PACKAGE_REGISTRY_EXPORT})
# Find Eigen
find_package(Eigen3 REQUIRED)
# REQUIRED for CUDA to correctly align Eigen member variables inside structures/classes
# https://listengine.tuxfamily.org/lists.tuxfamily.org/eigen/2016/06/msg00006.html
# add_definitions(-DEIGEN_MAX_STATIC_ALIGN_BYTES=0)
# Create a Header-only MPPI Library
add_library(${MPPI_HEADER_LIBRARY_NAME} INTERFACE)
add_library(MPPI::MPPI ALIAS ${MPPI_HEADER_LIBRARY_NAME})
target_include_directories(${MPPI_HEADER_LIBRARY_NAME} INTERFACE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>"
)
target_link_libraries(${MPPI_HEADER_LIBRARY_NAME} INTERFACE
${MPPI_GENERIC_CUDA_EXTRA_LIBS}
${CUDA_LIBRARIES}
cnpy
Eigen3::Eigen
)
target_include_directories(${MPPI_HEADER_LIBRARY_NAME} INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/submodules/cnpy>"
"$<INSTALL_INTERFACE:include/cnpy>"
)
# --display-error-number was added as a compilation flag in CUDA 11.2
if (${CUDA_VERSION} VERSION_GREATER_EQUAL "11.2")
target_compile_options(${MPPI_HEADER_LIBRARY_NAME} INTERFACE
$<$<COMPILE_LANGUAGE:CUDA>:--display-error-number>
)
endif()
# Allow the ability to turn off cuda barriers through CMake. Necessary for GPUs older than
# the 20 series
if (MPPI_USE_CUDA_BARRIERS)
target_compile_definitions(${MPPI_HEADER_LIBRARY_NAME} INTERFACE
CMAKE_USE_CUDA_BARRIERS)
endif()
if (BUILD_TESTS OR BUILD_EXAMPLES)
# find yaml-cpp
find_package(yaml-cpp REQUIRED)
include_directories(${YAML_CPP_INCLUDE_DIR})
endif()
# Install all library header files
install(
DIRECTORY ${PROJECT_SOURCE_DIR}/include/mppi
DESTINATION include
)
###########################################
# Set up CMake configuration installation #
###########################################
set(CMAKE_CONFIG_DEST "lib/cmake/${PROJECT_NAME}")
install(
TARGETS ${MPPI_HEADER_LIBRARY_NAME}
EXPORT ${PROJECT_NAME}-targets
)
install(
EXPORT ${PROJECT_NAME}-targets
NAMESPACE MPPI::
DESTINATION ${CMAKE_CONFIG_DEST}
FILE ${PROJECT_NAME}Targets.cmake
)
install(
EXPORT ${PROJECT_NAME}-targets
DESTINATION ${CMAKE_CONFIG_DEST}
FILE ${PROJECT_NAME}TargetsNoNamespace.cmake
)
# Set up MPPI to export the header library as MPPI in the MPPI namespace
set_target_properties (${MPPI_HEADER_LIBRARY_NAME} PROPERTIES EXPORT_NAME MPPI)
export (TARGETS ${MPPI_HEADER_LIBRARY_NAME}
NAMESPACE MPPI::
FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake
)
# Export the package for use from the build-tree
# (this registers the build-tree with a global CMake-registry ~/.cmake)
if (MPPI_EXPORT_PACKAGE)
export(PACKAGE ${PROJECT_NAME})
endif()
#################################################################################
# Create CMake Config and Version files and install in the appropriate location #
#################################################################################
# Create a CMake variable of the appropriate include directory depending on
# whether it is consider from the build tree or the install tree
set(TMP_MPPI_INCLUDE_DIRS "${PROJECT_SOURCE_DIR}/include")
set(TMP_MPPI_TARGET_CMAKE_FILE "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake")
# Set up build tree config file
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in
${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${PROJECT_BINARY_DIR}
PATH_VARS TMP_MPPI_INCLUDE_DIRS TMP_MPPI_TARGET_CMAKE_FILE
)
# Set up install tree config file
set(TMP_MPPI_INCLUDE_DIRS "include/")
set(TMP_MPPI_TARGET_CMAKE_FILE "${CMAKE_CONFIG_DEST}/${PROJECT_NAME}Targets.cmake")
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/Config.cmake.in
${PROJECT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_CONFIG_DEST}
PATH_VARS TMP_MPPI_INCLUDE_DIRS TMP_MPPI_TARGET_CMAKE_FILE
)
write_basic_package_version_file(
${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMinorVersion
)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/MPPIGenericToolsConfig.cmake
${PROJECT_BINARY_DIR}/MPPIGenericToolsConfig.cmake
COPYONLY
)
install(FILES
${PROJECT_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/${PROJECT_NAME}Config.cmake
${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
${CMAKE_CURRENT_SOURCE_DIR}/cmake/MPPIGenericToolsConfig.cmake
DESTINATION ${CMAKE_CONFIG_DEST}
)
if (BUILD_TESTS OR BUILD_EXAMPLES)
add_subdirectory(src)
endif()
if (BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
# Add custom cmake finds
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH}" "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules")
# Use CMake to download gtest as part of the configure step
###################################################################
# Add gtest
###################################################################
if (NOT DEFINED CMAKE_TOOLCHAIN_FILE AND BUILD_TESTS)
message(STATUS "Building MPPI Generic tests")
enable_testing()
############################################################
# copied from
# https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project
############################################################
# Download and unpack googletest at configure time
if (NOT TARGET gtest_main)
list(GET CMAKE_MODULE_PATH -1 MPPI_GENERIC_MODULES)
configure_file(${MPPI_GENERIC_MODULES}/CMakeLists.txt.gtest.in
${PROJECT_BINARY_DIR}/googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Prevent googletest installation
set(INSTALL_GTEST OFF)
# Add googletest directly to our build. This defines
# the gtest and gtest_main targets.
add_subdirectory(${PROJECT_BINARY_DIR}/googletest-src
${PROJECT_BINARY_DIR}/googletest-build)
endif()
include(GoogleTest)
add_subdirectory(tests)
endif()
# Uninstall
if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY)
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
endif()