-
Notifications
You must be signed in to change notification settings - Fork 7
/
CMakeLists.txt
414 lines (353 loc) · 16 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
cmake_minimum_required(VERSION 3.24)
# Tell CMake not to set CC and CXX flags. We'll do this ourselves
# https://cmake.org/cmake/help/latest/policy/CMP0132.html
cmake_policy(SET CMP0132 NEW)
# Both CXX and C need to be given otherwise Conan may ignore the CMAKE_C_COMPILER flag
project(
CesiumOmniverse
VERSION 0.0.0
DESCRIPTION "Cesium for Omniverse"
LANGUAGES CXX C)
# Set the CC and CXX environment variables explicitly so that conan picks them up
set(ENV{CC} ${CMAKE_C_COMPILER})
set(ENV{CXX} ${CMAKE_CXX_COMPILER})
# Ensure that all packages are built in the same .conan directory
# Windows Long Paths must be enabled. See instructions in README.
if(MSVC)
set(ENV{CONAN_USER_HOME_SHORT} "None")
endif()
# Just in case we need to pin a revision in AddConanDependencies
set(ENV{CONAN_REVISIONS_ENABLED} "1")
# cxx version
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if(MSVC)
# CMake automatically choses /MD for Release, RelWithDebInfo, MinSizeRel and /MDd for Debug
# Explicitly set the C/CXX flags for conan
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /MDd /MP")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /MD /MP")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} /MD /MP")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} /MD /MP")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd /MP")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD /MP")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /MD /MP")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MD /MP")
endif()
# Tell CMake where to look for our CMake helper files when calling `include`
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# Built-in CMake include for using `cmake_dependent_option`
include(CMakeDependentOption)
# Options:
option(CESIUM_OMNI_ENABLE_TESTS "Unit tests" ON)
option(CESIUM_OMNI_ENABLE_DOCUMENTATION "Generate HTML documentation with Doxygen" ON)
option(CESIUM_OMNI_ENABLE_SANITIZERS "Check for undefined behavior at runtime" OFF)
option(CESIUM_OMNI_ENABLE_LINTERS "Enable clang-format for code formatting and clang-tidy for static code analysis" ON)
# Coverage can only be enabled if tests are also enabled
cmake_dependent_option(
CESIUM_OMNI_ENABLE_COVERAGE
"Code coverage for checking the thoroughness of unit tests - only available if CESIUM_OMNI_ENABLE_TESTS is ON"
ON
CESIUM_OMNI_ENABLE_TESTS
OFF)
cmake_dependent_option(
CESIUM_OMNI_ENABLE_LINTERS_ON_BUILD
"Check linting during the build - only available if CESIUM_OMNI_ENABLE_LINTERS is ON"
OFF
CESIUM_OMNI_ENABLE_LINTERS
OFF)
cmake_dependent_option(
CESIUM_OMNI_ENABLE_LINTERS_ON_COMMIT
"Check formatting and linting before committing code - only available if CESIUM_OMNI_ENABLE_LINTERS is ON"
OFF
CESIUM_OMNI_ENABLE_LINTERS
OFF)
# Change the default build type to Release
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Type of build: Debug Release MinSizeRel RelWithDebInfo." FORCE)
endif()
# Use the same Python version as Omniverse (Python 3.7)
find_package(OmniverseConnectSample REQUIRED)
set(Python3_ROOT_DIR "${OmniverseConnectSample_DIR}/_build/target-deps/python")
find_package(
Python3
COMPONENTS Interpreter Development
REQUIRED)
# Generate compile_commands.json file for use by language servers such as clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# Source directories for formatting and linting
set(LINT_SOURCE_DIRECTORIES "${PROJECT_SOURCE_DIR}/include" "${PROJECT_SOURCE_DIR}/src" "${PROJECT_SOURCE_DIR}/tests")
# Source directories for coverage
set(COVERAGE_SOURCE_DIRECTORIES "${PROJECT_SOURCE_DIR}/include" "${PROJECT_SOURCE_DIR}/src")
# Source directories for documentation
set(DOC_SOURCE_DIRECTORIES "${PROJECT_SOURCE_DIR}/include")
if(CESIUM_OMNI_ENABLE_DOCUMENTATION)
include(Documentation)
setup_doxygen_if_available(
PROJECT_ROOT_DIRECTORY
"${PROJECT_SOURCE_DIR}"
PROJECT_INCLUDE_DIRECTORIES
${DOC_SOURCE_DIRECTORIES}
OUTPUT_DIRECTORY
"${PROJECT_BINARY_DIR}/docs")
endif()
set(SCRIPTS_DIRECTORY "${PROJECT_SOURCE_DIR}/scripts")
if(CESIUM_OMNI_ENABLE_LINTERS)
include(Linters)
setup_linters(
PROJECT_SCRIPTS_DIRECTORY
"${SCRIPTS_DIRECTORY}"
PROJECT_BUILD_DIRECTORY
"${PROJECT_BINARY_DIR}"
PROJECT_SOURCE_DIRECTORIES
${LINT_SOURCE_DIRECTORIES}
ENABLE_LINTERS_ON_BUILD
${CESIUM_OMNI_ENABLE_LINTERS_ON_BUILD})
endif()
include(InstallHooks)
if(CESIUM_OMNI_ENABLE_LINTERS_ON_COMMIT)
install_git_hooks(
PROJECT_ROOT_DIRECTORY
"${PROJECT_SOURCE_DIR}"
GIT_HOOKS_SOURCE_DIRECTORY
"${SCRIPTS_DIRECTORY}")
else()
uninstall_git_hooks(PROJECT_ROOT_DIRECTORY "${PROJECT_SOURCE_DIR}")
endif()
set(CMAKE_LINK_WHAT_YOU_USE ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
# defines CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR, and CMAKE_INSTALL_INCLUDEDIR for OS-dependent build locations.
# See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
include(GNUInstallDirs)
# Add conan third party libraries
include(AddConanDependencies)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
set(CMAKE_DEBUG_POSTFIX
"d"
CACHE FILEPATH "Postfix for Debug binaries")
set(CMAKE_RELWITHDEBINFO_POSTFIX
"rd"
CACHE FILEPATH "Postfix for RelWithDebInfo binaries")
# compiler / preprocessor / linker options passed later to targets in cmake/Macros.cmake
# Debug options are only applied if using the Debug configuration
set(CESIUM_OMNI_CXX_FLAGS "")
set(CESIUM_OMNI_CXX_FLAGS_DEBUG "")
set(CESIUM_OMNI_CXX_DEFINES "")
set(CESIUM_OMNI_CXX_DEFINES_DEBUG "")
set(CESIUM_OMNI_LINKER_FLAGS "")
set(CESIUM_OMNI_LINKER_FLAGS_DEBUG "")
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_VERSION="${PROJECT_VERSION}")
# Add DEBUG definition to be able to #ifdef
set(CESIUM_OMNI_CXX_DEFINES_DEBUG ${CESIUM_OMNI_CXX_DEFINES_DEBUG} CESIUM_OMNI_DEBUG)
if(MSVC)
set(CESIUM_OMNI_CXX_FLAGS
${CESIUM_OMNI_CXX_FLAGS}
# TODO: temporarily disable warnings
#/W4 # enable all warnings
#/WX # error on warnings
/Zc:__cplusplus # make __cplusplus defined correctly
)
set(CESIUM_OMNI_CXX_DEFINES
${CESIUM_OMNI_CXX_DEFINES}
_CRT_SECURE_NO_WARNINGS # Avoid erroring on strcpy
UNICODE # tell Windows to use the unicode version of string functions, as opposed to ASCII (https://docs.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings)
_UNICODE # tell Windows to use the unicode version of string functions, as opposed to ASCII (https://docs.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings)
WIN32_LEAN_AND_MEAN # ignore some unnecessary Windows APIs
NOMINMAX # don't let Windows create macros for MIN and MAX
_SILENCE_CXX17_RESULT_OF_DEPRECATION_WARNING)
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CESIUM_OMNI_CXX_FLAGS
${CESIUM_OMNI_CXX_FLAGS}
# TODO: temporarily disable warnings
# -Werror
# -Wall
# -Wextra
# -Wpedantic
# -Wparentheses
# -Wreturn-type
# -Wmissing-braces
# -Wunknown-pragmas
# -Wshadow
# -Wunused
# -Wuninitialized
# -Wconversion
# -Wno-missing-field-initializers
# -fstrict-aliasing
# -fstrict-enums
)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CESIUM_OMNI_CXX_FLAGS ${CESIUM_OMNI_CXX_FLAGS} -Wno-gnu-anonymous-struct)
endif()
endif()
if(CESIUM_OMNI_ENABLE_SANITIZERS)
if(MSVC)
set(SANITIZER_FLAGS /fsanitize=address /fno-sanitize-address-vcasan-lib)
set(CESIUM_OMNI_CXX_FLAGS ${CESIUM_OMNI_CXX_FLAGS} ${SANITIZER_FLAGS})
else()
set(SANITIZER_FLAGS
-fsanitize=address
-fsanitize=undefined
-fsanitize=float-divide-by-zero
-fsanitize=bounds
-fno-omit-frame-pointer)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
# These flags are only supported in clang
# Can't use -fsanitize=unsigned-integer-overflow since it triggers UB in libstdc++ std::map: https://github.com/google/sanitizers/issues/1346
set(SANITIZER_FLAGS
${SANITIZER_FLAGS}
-fsanitize=implicit-conversion
-fsanitize=local-bounds
-fsanitize=nullability)
endif()
set(CESIUM_OMNI_CXX_FLAGS ${CESIUM_OMNI_CXX_FLAGS} ${SANITIZER_FLAGS})
set(CESIUM_OMNI_LINKER_FLAGS ${CESIUM_OMNI_LINKER_FLAGS} ${SANITIZER_FLAGS})
endif()
endif()
# Enable code coverage if supported on this platform.
if(CESIUM_OMNI_ENABLE_COVERAGE)
# At reconfigure time: Delete any lingering GCDA files in the build folder.
# GCDA files track profiling information and are generated everytime a binary with
# coverage support is executed. If we don't do this we can encounter a
# `cannot merge previous GCDA file: corrupt arc tag` error message (when switching
# compilers).
file(GLOB_RECURSE GCDA_FILES "${PROJECT_BINARY_DIR}/*.gcda")
foreach(GCDA IN LISTS GCDA_FILES)
file(REMOVE "${GCDA}")
endforeach()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CESIUM_OMNI_CXX_FLAGS_DEBUG ${CESIUM_OMNI_CXX_FLAGS_DEBUG} -fprofile-arcs -ftest-coverage)
set(CESIUM_OMNI_LINKER_FLAGS_DEBUG ${CESIUM_OMNI_LINKER_FLAGS_DEBUG} --coverage)
endif()
# We always define the `generate-coverage` target, even if it would fail at runtime
# due to missing tools / incompatible compilers.
add_custom_target(
generate-coverage
COMMAND
${CMAKE_COMMAND} -DCMAKE_CXX_COMPILER_ID=${CMAKE_CXX_COMPILER_ID}
-DCMAKE_CXX_COMPILER_VERSION=${CMAKE_CXX_COMPILER_VERSION} -DPROJECT_ROOT_DIRECTORY="${PROJECT_SOURCE_DIR}"
-DPROJECT_BUILD_DIRECTORY="${PROJECT_BINARY_DIR}"
-DPROJECT_SOURCE_DIRECTORIES=${COVERAGE_SOURCE_DIRECTORIES}
-DOUTPUT_DIRECTORY="${PROJECT_BINARY_DIR}/coverage/" -P "${PROJECT_SOURCE_DIR}/cmake/GenerateCoverage.cmake"
DEPENDS $<TARGET_FILE:tests>)
endif()
# System preprocessor definitions
# CESIUM_OMNI_WINDOWS = The program is being compiled for Windows
# CESIUM_OMNI_MSVC = The program is being compiled for Windows using the Microsoft C++ compiler
if(WIN32)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_WINDOWS)
if(MSVC)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_MSVC)
endif()
elseif(APPLE)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_MACOS)
elseif(UNIX)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_UNIX)
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_GCC)
endif()
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_CLANG)
endif()
# Enable/Disable sanitizers
if(CESIUM_OMNI_ENABLE_SANITIZERS)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_ENABLE_SANITIZERS)
endif()
set(CESIUM_OMNI_CXX_DEFINES
${CESIUM_OMNI_CXX_DEFINES}
GLM_FORCE_XYZW_ONLY # Disable .rgba and .stpq to make it easier to view values from debugger
GLM_FORCE_EXPLICIT_CTOR # Disallow implicit conversions between dvec3 <-> dvec4, dvec3 <-> fvec3, etc
GLM_FORCE_SIZE_T_LENGTH # Make vec.length() and vec[idx] use size_t instead of int
)
# Boost is a dependency of USD. It is dynamically linked so we need to set this flag.
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} BOOST_ALL_DYN_LINK)
# TBB is a dependency of USD
set(CESIUM_OMNI_CXX_DEFINES_DEBUG ${CESIUM_OMNI_CXX_DEFINES_DEBUG} TBB_USE_DEBUG)
# Add this for cesium-native (TODO: should this go in src/CMakeLists?)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} SPDLOG_COMPILED_LIB)
# Add this for cesium-native (TODO: should this go in src/CMakeLists?)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} LIBASYNC_STATIC)
# USD is compiled with the old C++ ABI so we need to compile our own code and external libraries
# with the old ABI. Only relevant for libraries that have std::string or std::list in their
# public interface, which includes cesium-native and cpr.
# See https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} _GLIBCXX_USE_CXX11_ABI=0)
endif()
if(BUILD_SHARED_LIBS)
set(CESIUM_OMNI_CXX_DEFINES ${CESIUM_OMNI_CXX_DEFINES} CESIUM_OMNI_SHARED)
endif()
set(CESIUM_OMNI_CXX_FLAGS
${CESIUM_OMNI_CXX_FLAGS}
CACHE STRING "Compiler flags to use." FORCE)
set(CESIUM_OMNI_CXX_FLAGS_DEBUG
${CESIUM_OMNI_CXX_FLAGS_DEBUG}
CACHE STRING "Compiler flags to use in debug mode." FORCE)
set(CESIUM_OMNI_CXX_DEFINES
${CESIUM_OMNI_CXX_DEFINES}
CACHE STRING "Preprocessor definitions to use." FORCE)
set(CESIUM_OMNI_CXX_DEFINES_DEBUG
${CESIUM_OMNI_CXX_DEFINES_DEBUG}
CACHE STRING "Preprocessor definitions to use in debug mode." FORCE)
set(CESIUM_OMNI_LINKER_FLAGS
${CESIUM_OMNI_LINKER_FLAGS}
CACHE STRING "Linker flags to use." FORCE)
set(CESIUM_OMNI_LINKER_FLAGS_DEBUG
${CESIUM_OMNI_LINKER_FLAGS_DEBUG}
CACHE STRING "Linker flags to use in debug mode." FORCE)
# Tell CMake to search for packages with the correct build type (i.e. Release or Debug) as generated
# by ConfigureConan.cmake with the cmake_find_package_multi generator
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)
find_package(Threads) # System threading library
# Conan libraries
find_package(cpr)
find_package(doctest)
find_package(pybind11)
find_package(ZLIB)
# Third party dependencies not handled by conan
add_subdirectory(extern)
# So that the installed libraries can find shared libraries in the same directory
set(CMAKE_INSTALL_RPATH $ORIGIN)
# Cesium Omniverse library
add_subdirectory(src)
# unit tests
if(CESIUM_OMNI_ENABLE_TESTS)
enable_testing()
add_subdirectory(tests)
endif()
# Ninja and various Makefiles generators support generating compile_commands.json
# https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html
# https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html#makefile-generators
if("${CMAKE_GENERATOR}" MATCHES "Ninja"
OR "${CMAKE_GENERATOR}" MATCHES "Makefiles"
OR "${CMAKE_GENERATOR}" MATCHES "WMake")
# Copy compile_commands.json to the root directory after configuration so that
# extensions like clangd or intellisense use the correct compile flags
add_custom_target(
copy-compile-commands ALL
${CMAKE_COMMAND}
-E
copy_if_different
"${PROJECT_BINARY_DIR}/compile_commands.json"
"${PROJECT_SOURCE_DIR}")
endif()
# Generate ThirdParty.json
execute_process(COMMAND "${Python3_EXECUTABLE}" "${SCRIPTS_DIRECTORY}/generate_third_party_license_json.py" --build-dir
"${PROJECT_BINARY_DIR}" --project-dir "${PROJECT_SOURCE_DIR}" --skip "doctest")
# When calling find_package with `CMAKE_FIND_PACKAGE_PREFER_CONFIG` it adds
# a `<PACKAGE>_DIR` variable to the CMake cache, polluting the CMake GUI.
# Use `mark_as_advanced` on all packages to hide them from the GUI.
# Conan internally does `mark_as_advanced` for `cmake_find_package` generator, but not for
# `cmake_find_package_multi`. Related: https://github.com/conan-io/conan/issues/6167
get_cmake_property(PACKAGE_NAMES PACKAGES_FOUND)
foreach(PACKAGE_NAME ${PACKAGE_NAMES})
mark_as_advanced(${PACKAGE_NAME}_DIR)
endforeach()
# Hide CONAN_CMD_PATH from CMake GUI
mark_as_advanced(CONAN_CMD_PATH)