-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
386 lines (310 loc) · 13 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
# xrlib
# Copyright 2024 Rune Berg (http://runeberg.io | https://github.com/1runeberg)
# Licensed under Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0)
# SPDX-License-Identifier: Apache-2.0
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
set(CMAKE_SUPPRESS_REGENERATION true)
######################
# PROJECT DEFINITION #
######################
# Set project variables
set(XRLIB "xrlib")
project("${XRLIB}" VERSION 1.0.0.0)
# Set project directories
set(XRLIB_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")
set(XRLIB_SRC "${XRLIB_ROOT}/src")
set(XRLIB_INCLUDE "${XRLIB_ROOT}/include")
set(XRLIB_BIN_OUT "${XRLIB_ROOT}/bin")
set(XRLIB_LIB_OUT "${XRLIB_ROOT}/lib")
set(THIRD_PARTY "${XRLIB_ROOT}/third_party")
set(XRLIB_EXT_INCLUDE "${XRLIB_INCLUDE}/${XRLIB}/ext")
set(XRLIB_EXT_SRC "${XRLIB_SRC}/${XRLIB}/ext")
set(XRVK_INCLUDE "${XRLIB_INCLUDE}/xrvk")
set(XRVK_SRC "${XRLIB_SRC}/xrvk")
set(XRVK_SHADERS_SRC "${XRLIB_ROOT}/res/shaders/src")
set(XRVK_SHADERS_BIN "${XRLIB_ROOT}/res/shaders/bin")
# Set project configuration files
set(XRLIB_CONFIG_IN "${XRLIB_ROOT}/project_config.h.in")
set(XRLIB_CONFIG_OUT "${XRLIB_INCLUDE}/project_config.h")
# Set project wide options
option(BUILD_AS_STATIC "Build as static library" OFF)
option(BUILD_SHADERS "Build shaders in resource directory" ON)
option(ENABLE_XRVK "Compile xrvk - pbr render module" ON)
option(ENABLE_RENDERDOC "Enable renderdoc for render debugs" OFF)
option(ENABLE_VULKAN_DEBUG "Enable vulkan debugging" OFF)
# For windows, we need to override openxr's resource script, so define the rc files here which will be used during binary pre-build
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(SRC_RC "${CMAKE_CURRENT_SOURCE_DIR}/win_dll_metadata.rc")
set(DST_RC "${CMAKE_CURRENT_SOURCE_DIR}/third_party/openxr/src/loader/loader.rc")
execute_process(COMMAND ${CMAKE_COMMAND} -E copy ${SRC_RC} ${DST_RC})
endif()
# Set config files
file(GLOB XRLIB_CONFIG
"${XRLIB_ROOT}/.clang-format"
"${XRLIB_ROOT}/.gitignore"
"${XRLIB_ROOT}/README.md"
"${XRLIB_ROOT}/LICENSE"
"${XRLIB_ROOT}/CMakeLists.txt"
"${XRLIB_ROOT}/*.h.in"
)
# Set CI files
file(GLOB XRLIB_CI
"${XRLIB_ROOT}/.github/workflows/*.yml"
)
# Set headers
file(GLOB_RECURSE XRLIB_HEADERS
"${XRLIB_INCLUDE}/*.h*"
"${XRLIB_INCLUDE}/{$XRLIB}/*.h*"
"${XRLIB_SRC}/*.h*"
)
file(GLOB_RECURSE XRVK_HEADERS
"${XRVK_INCLUDE}/*.h*"
"${XRVK_SRC}/*.h*"
)
# Set source code
file(GLOB_RECURSE XRLIB_SOURCE
"${XRLIB_SRC}/*.c*"
)
file(GLOB_RECURSE XRVK_SOURCE
"${XRVK_SRC}/*.c*"
)
# Exclude extensions (option)
option(EXCLUDE_KHR_VISIBILITY_MASK "EXCLUDE_KHR_VISIBILITY_MASK" OFF)
set(FILES_KHR_VISIBILITY_MASK "KHR/visibility_mask")
option(EXCLUDE_EXT_HAND_TRACKING "EXCLUDE_EXT_HAND_TRACKING" OFF)
set (FILES_EXT_HAND_TRACKING "EXT/hand_tracking" )
option(EXCLUDE_FB_DISPLAY_REFRESH "EXCLUDE_FB_DISPLAY_REFRESH" OFF)
set (FILES_FB_DISPLAY_REFRESH "FB/display_refresh_rate" )
option(EXCLUDE_FB_PASSTHROUGH "EXCLUDE_FB_PASSTHROUGH" OFF)
set (FILES_FB_PASSTHROUGH "FB/passthrough" "FB/triangle_mesh")
if (${EXCLUDE_KHR_VISIBILITY_MASK} STREQUAL "ON")
foreach( f IN ITEMS ${FILES_KHR_VISIBILITY_MASK} )
list(REMOVE_ITEM XRLIB_HEADERS "${XRLIB_EXT_INCLUDE}/${f}.hpp" )
list(REMOVE_ITEM XRLIB_SOURCE "${XRLIB_EXT_SRC}/${f}.cpp" )
endforeach()
endif()
if (${EXCLUDE_EXT_HAND_TRACKING})
foreach(f IN ITEMS ${FILE_EXT_HAND_TRACKING})
list(REMOVE_ITEM XRLIB_HEADERS "${XRLIB_EXT_INCLUDE}/${f}.hpp")
list(REMOVE_ITEM XRLIB_SOURCE "${XRLIB_EXT_SRC}/${f}.cpp")
endforeach()
endif()
if (${EXCLUDE_FB_DISPLAY_REFRESH})
foreach(f IN ITEMS ${FILES_FB_DISPLAY_REFRESH} )
list(REMOVE_ITEM XRLIB_HEADERS "${XRLIB_EXT_INCLUDE}/${f}.hpp")
list(REMOVE_ITEM XRLIB_SOURCE "${XRLIB_EXT_SRC}/${f}.cpp")
endforeach()
endif()
if (${EXCLUDE_FB_PASSTHROUGH})
foreach(f IN ITEMS ${FILES_FB_PASSTHROUGH} )
list(REMOVE_ITEM XRLIB_HEADERS "${XRLIB_EXT_INCLUDE}/${f}.hpp")
list(REMOVE_ITEM XRLIB_SOURCE "${XRLIB_EXT_SRC}/${f}.cpp")
endforeach()
endif()
# Set shaders
file(GLOB_RECURSE XRVK_SHADERS
"${XRVK_SHADERS_SRC}/*.frag"
"${XRVK_SHADERS_SRC}/*.vert"
"${XRVK_SHADERS_SRC}/*.tesc"
"${XRVK_SHADERS_SRC}/*.tese"
"${XRVK_SHADERS_SRC}/*.geom"
"${XRVK_SHADERS_SRC}/*.comp"
)
# Compile shaders
if(NOT ANDROID AND BUILD_SHADERS)
set(SHADER_OUTPUT_FILES)
foreach(SHADER ${XRVK_SHADERS})
# Get the filename with extension
get_filename_component(SHADER_NAME ${SHADER} NAME)
# Define the output .spv file path
set(OUTPUT_FILE "${XRVK_SHADERS_BIN}/${SHADER_NAME}.spv")
# Add custom command to run glslc for each shader file
add_custom_command(
OUTPUT ${OUTPUT_FILE}
COMMAND glslc ${SHADER} -o ${OUTPUT_FILE}
COMMENT "Compiling ${SHADER} to ${OUTPUT_FILE}"
VERBATIM
DEPENDS ${SHADER} # Check if the shader file has changed
)
# Collect all output files
list(APPEND SHADER_OUTPUT_FILES ${OUTPUT_FILE})
endforeach()
# Create a custom target that depends on all shader output files
add_custom_target(_compile_shaders ALL DEPENDS ${SHADER_OUTPUT_FILES})
endif()
# Set openxr
set(OPENXR_ROOT "${THIRD_PARTY}/openxr")
set(OPENXR_INCLUDE "${OPENXR_ROOT}/include")
######################################
# SET PROJECT TECHNICAL REQUIREMENTS #
######################################
# C++ standard for this project
set(CPP_STD 20)
set(CMAKE_CXX_STANDARD ${CPP_STD})
set(CMAKE_CXX_STANDARD_REQUIRED True)
message(STATUS "[${XRLIB}] Project language set to C++ ${CPP_STD}")
# Check platform architecture
if(NOT PLATFORM)
if(CMAKE_SIZEOF_VOID_P MATCHES 8)
set(PLATFORM 64)
else()
message(FATAL_ERROR "[${XRLIB}] ERROR: Only 64-bit platforms are supported.")
endif()
endif()
# OS specific
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
add_definitions(-D_WIN32)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS True)
endif()
# Debug settings
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
# Assertions enabled
message(STATUS "[${XRLIB}] Debug build: Assertions enabled.")
else()
# Disable assertions by defining NDEBUG
add_definitions(-DNDEBUG)
message(STATUS "[${XRLIB}] Release build: Assertions disabled.")
endif()
# Advanced debug options (desktop only)
if(NOT ANDROID)
if(ENABLE_RENDERDOC)
add_definitions(-DRENDERDOC_ENABLE)
message(STATUS "[${XRLIB}] DEBUG: Renderdoc frame capture enabled.")
endif()
if(ENABLE_VULKAN_DEBUG)
add_definitions(-DXRVK_VULKAN_VALIDATION_ENABLE -DXRVK_VULKAN_DEBUG_ENABLE )
message(STATUS "[${XRLIB}] DEBUG: Vulkan validation and debugging enabled.")
endif()
endif()
##################################
# BUILD THIRD PARTY DEPENDENCIES #
##################################
# Add cmake known packages
find_package(Vulkan)
if(NOT Vulkan_FOUND)
message(FATAL_ERROR "[${XRLIB}] ERROR: Unable to find Vulkan library - install the Vulkan SDK from https://vulkan.lunarg.com/")
else()
message(STATUS "[${XRLIB}] Vulkan library loaded: ${Vulkan_LIBRARY}")
endif()
add_subdirectory(${OPENXR_ROOT})
#####################
# BINARY DEFINITION #
#####################
# Organize source folders
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
source_group(CI FILES ${XRLIB_CI})
source_group(config FILES ${XRLIB_CONFIG})
source_group(TREE ${XRLIB_INCLUDE} PREFIX include FILES ${XRLIB_HEADERS})
source_group(TREE ${XRLIB_SRC} PREFIX src FILES ${XRLIB_SOURCE})
source_group(TREE ${XRVK_INCLUDE} PREFIX xrvk_include FILES ${XRVK_HEADERS})
source_group(TREE ${XRVK_SRC} PREFIX xrvk_src FILES ${XRVK_SOURCE})
source_group(TREE ${XRVK_SHADERS_SRC} PREFIX xrvk_shaders FILES ${XRVK_SHADERS})
# Set project config header which contains this project's current version number
configure_file(${XRLIB_CONFIG_IN} ${XRLIB_CONFIG_OUT})
message(STATUS "[${XRLIB}] Project version is ${CMAKE_PROJECT_VERSION}")
# Assemble all source code (and optional modules/code) that will be compiled for the library
# See "Set Compile Options" section
set (ALLSOURCE
${XRLIB_CONFIG}
${XRLIB_CI}
${XRLIB_HEADERS}
${XRLIB_SOURCE}
$<TARGET_OBJECTS:openxr_loader>
)
if(ENABLE_XRVK)
list(APPEND ALLSOURCE
${XRVK_HEADERS}
${XRVK_SOURCE}
${XRVK_SHADERS}
)
add_compile_definitions(XRVK_ENABLED)
message(STATUS "[${XRLIB}] Optional module included: xrvk (test with macro XRVK_ENABLED)")
endif()
# Add project header and source files to project (including statically linking openxr_loader object)
if(BUILD_AS_STATIC)
add_library(${XRLIB} STATIC ${ALLSOURCE})
message(STATUS "[${XRLIB}] Project library defined. Will build as: STATIC")
else()
add_library(${XRLIB} SHARED ${ALLSOURCE})
message(STATUS "[${XRLIB}] Project library defined. Will build as: SHARED")
endif()
# For shared library builds, set project version
if(NOT BUILD_AS_STATIC)
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set_target_properties(${XRLIB} PROPERTIES VERSION ${PROJECT_VERSION})
# else()
# cmake 3.22 builds multiple copies with version and so version for desktops. commenting for now to avoid CI build duplicates.
# set_target_properties(${XRLIB} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR})
endif()
endif()
# Set project public include headers
target_include_directories(${XRLIB} PUBLIC "${XRLIB_SRC}"
"${XRLIB_INCLUDE}"
"${XRLIB_ROOT}"
"${THIRD_PARTY}"
"${OPENXR_INCLUDE}"
"${Vulkan_INCLUDE_DIRS}"
)
# For Android, add the native app glue NDK directory
if(ANDROID)
message(STATUS "[${XRLIB}] Android NDK path: ${ANDROID_NDK}")
# Add native app glue
find_path(ANDROID_NATIVE_APP_GLUE NAMES android_native_app_glue.h PATHS "${ANDROID_NDK}/sources/android/native_app_glue" NO_DEFAULT_PATH)
if(NOT ANDROID_NATIVE_APP_GLUE)
message(WARNING "[${XRLIB}] WARNING: Unable to find native_app_glue in: **${ANDROID_NDK}/sources/android/native_app_glue** setting manually.")
set(ANDROID_NATIVE_APP_GLUE "${ANDROID_NDK}/sources/android/native_app_glue")
endif()
target_include_directories(${XRLIB} PUBLIC ${ANDROID_NATIVE_APP_GLUE})
endif()
message(STATUS "[${XRLIB}] Public include directories defined.")
# Generate export headers
include(GenerateExportHeader)
generate_export_header(${XRLIB})
message(STATUS "[${XRLIB}] Export headers generated.")
###########################################
# LINK THIRD PARTY DEPENDENCIES TO BINARY #
###########################################
# Add external libraries
if(ANDROID)
target_compile_definitions(${XRLIB} PRIVATE XR_USE_PLATFORM_ANDROID=1)
find_library(ANDROID_LIBRARY NAMES android)
find_library(ANDROID_LOG_LIBRARY NAMES log)
target_link_libraries(${XRLIB} PUBLIC
${Vulkan_LIBRARY}
${ANDROID_LIBRARY}
${ANDROID_LOG_LIBRARY}
)
else()
target_link_libraries(${XRLIB} PUBLIC
${Vulkan_LIBRARY}
)
endif()
message(STATUS "[${XRLIB}] Third party libraries linked.")
################
# BUILD BINARY #
################
# Set output directories
set_target_properties(${XRLIB} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${XRLIB_LIB_OUT}"
LIBRARY_OUTPUT_DIRECTORY_DEBUG "${XRLIB_LIB_OUT}"
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${XRLIB_BIN_OUT}"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${XRLIB_LIB_OUT}"
LIBRARY_OUTPUT_DIRECTORY_RELEASE "${XRLIB_LIB_OUT}"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${XRLIB_BIN_OUT}"
)
message(STATUS "[${XRLIB}] Current build/working directory is: ${CMAKE_CURRENT_BINARY_DIR}")
message(STATUS "[${XRLIB}] Project archives will be built in: ${XRLIB_LIB_OUT}")
message(STATUS "[${XRLIB}] Project libraries will be built in: ${XRLIB_LIB_OUT}")
message(STATUS "[${XRLIB}] Project binaries will be built in: ${XRLIB_BIN_OUT}")
# Post-Build
add_custom_command(TARGET ${XRLIB} POST_BUILD
# Create output directories
COMMAND ${CMAKE_COMMAND} -E make_directory "${XRLIB_LIB_OUT}"
COMMAND ${CMAKE_COMMAND} -E make_directory "${XRLIB_BIN_OUT}"
)
# Compile shaders only if we're not in an android build
if(NOT ANDROID AND BUILD_SHADERS)
add_custom_command(TARGET ${XRLIB} POST_BUILD
COMMAND ${CMAKE_COMMAND} --build . --target _compile_shaders
)
endif()