forked from DiligentGraphics/DiligentCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildUtils.cmake
399 lines (334 loc) · 15.5 KB
/
BuildUtils.cmake
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
if(PLATFORM_WIN32 OR PLATFORM_UNIVERSAL_WINDOWS)
function(copy_required_dlls TARGET_NAME)
if(D3D11_SUPPORTED)
list(APPEND ENGINE_DLLS Diligent-GraphicsEngineD3D11-shared)
endif()
if(D3D12_SUPPORTED)
list(APPEND ENGINE_DLLS Diligent-GraphicsEngineD3D12-shared)
endif()
if(GL_SUPPORTED)
list(APPEND ENGINE_DLLS Diligent-GraphicsEngineOpenGL-shared)
endif()
if(VULKAN_SUPPORTED)
list(APPEND ENGINE_DLLS Diligent-GraphicsEngineVk-shared)
endif()
if(METAL_SUPPORTED)
list(APPEND ENGINE_DLLS Diligent-GraphicsEngineMetal-shared)
endif()
foreach(DLL ${ENGINE_DLLS})
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"\"$<TARGET_FILE:${DLL}>\""
"\"$<TARGET_FILE_DIR:${TARGET_NAME}>\"")
endforeach(DLL)
# Copy D3Dcompiler_47.dll, dxcompiler.dll, and dxil.dll
if(MSVC)
if ((D3D11_SUPPORTED OR D3D12_SUPPORTED) AND VS_D3D_COMPILER_PATH)
# Note that VS_D3D_COMPILER_PATH can only be used in a Visual Studio command
# and is not a valid path during CMake configuration
list(APPEND SHADER_COMPILER_DLLS ${VS_D3D_COMPILER_PATH})
endif()
if(D3D12_SUPPORTED AND VS_DXC_COMPILER_PATH AND VS_DXIL_SIGNER_PATH)
# For the compiler to sign the bytecode, you have to have a copy of dxil.dll in
# the same folder as the dxcompiler.dll at runtime.
# Note that VS_DXC_COMPILER_PATH and VS_DXIL_SIGNER_PATH can only be used in a Visual Studio command
# and are not valid paths during CMake configuration
list(APPEND SHADER_COMPILER_DLLS ${VS_DXC_COMPILER_PATH})
list(APPEND SHADER_COMPILER_DLLS ${VS_DXIL_SIGNER_PATH})
endif()
foreach(DLL ${SHADER_COMPILER_DLLS})
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${DLL}
"\"$<TARGET_FILE_DIR:${TARGET_NAME}>\"")
endforeach(DLL)
if(D3D12_SUPPORTED AND EXISTS ${DILIGENT_PIX_EVENT_RUNTIME_DLL_PATH})
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${DILIGENT_PIX_EVENT_RUNTIME_DLL_PATH}
"\"$<TARGET_FILE_DIR:${TARGET_NAME}>\"")
endif()
if(VULKAN_SUPPORTED)
if(NOT DEFINED DILIGENT_DXCOMPILER_FOR_SPIRV_PATH)
message(FATAL_ERROR "DILIGENT_DXCOMPILER_FOR_SPIRV_PATH is undefined, check order of cmake includes")
endif()
if(EXISTS ${DILIGENT_DXCOMPILER_FOR_SPIRV_PATH})
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${DILIGENT_DXCOMPILER_FOR_SPIRV_PATH}
"\"$<TARGET_FILE_DIR:${TARGET_NAME}>/spv_dxcompiler.dll\"")
endif()
endif()
endif()
endfunction()
function(package_required_dlls TARGET_NAME)
if(D3D12_SUPPORTED AND VS_DXC_COMPILER_PATH AND VS_DXIL_SIGNER_PATH)
# Copy the dlls to the project's CMake binary dir
# Note that VS_DXC_COMPILER_PATH and VS_DXIL_SIGNER_PATH can only be used in a Visual Studio command
# and are not valid paths during CMake configuration
add_custom_command(TARGET ${TARGET_NAME} PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${VS_DXC_COMPILER_PATH}
"\"${CMAKE_CURRENT_BINARY_DIR}/dxcompiler.dll\""
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${VS_DXIL_SIGNER_PATH}
"\"${CMAKE_CURRENT_BINARY_DIR}/dxil.dll\"")
set(DLLS "${CMAKE_CURRENT_BINARY_DIR}/dxcompiler.dll" "${CMAKE_CURRENT_BINARY_DIR}/dxil.dll")
# Add the dlls to the target project as source files
target_sources(${TARGET_NAME} PRIVATE ${DLLS})
# Label them as content
set_source_files_properties(${DLLS} PROPERTIES
GENERATED TRUE
VS_DEPLOYMENT_CONTENT 1
VS_DEPLOYMENT_LOCATION ".")
endif()
endfunction()
# Set dll output name by adding _{32|64}{r|d} suffix
function(set_dll_output_name TARGET_NAME OUTPUT_NAME_WITHOUT_SUFFIX)
foreach(DBG_CONFIG ${DEBUG_CONFIGURATIONS})
set_target_properties(${TARGET_NAME} PROPERTIES
OUTPUT_NAME_${DBG_CONFIG} ${OUTPUT_NAME_WITHOUT_SUFFIX}${DLL_DBG_SUFFIX}
)
endforeach()
foreach(REL_CONFIG ${RELEASE_CONFIGURATIONS})
set_target_properties(${TARGET_NAME} PROPERTIES
OUTPUT_NAME_${REL_CONFIG} ${OUTPUT_NAME_WITHOUT_SUFFIX}${DLL_REL_SUFFIX}
)
endforeach()
endfunction()
endif(PLATFORM_WIN32 OR PLATFORM_UNIVERSAL_WINDOWS)
function(set_common_target_properties TARGET)
if(COMMAND custom_pre_configure_target)
custom_pre_configure_target(${TARGET})
if(TARGET_CONFIGURATION_COMPLETE)
return()
endif()
endif()
get_target_property(TARGET_TYPE ${TARGET} TYPE)
if(MSVC)
# For msvc, enable link-time code generation for release builds (I was not able to
# find any way to set these settings through interface library BuildSettings)
if(TARGET_TYPE STREQUAL STATIC_LIBRARY)
foreach(REL_CONFIG ${RELEASE_CONFIGURATIONS})
set_target_properties(${TARGET} PROPERTIES
STATIC_LIBRARY_FLAGS_${REL_CONFIG} /LTCG
)
endforeach()
else()
foreach(REL_CONFIG ${RELEASE_CONFIGURATIONS})
set_target_properties(${TARGET} PROPERTIES
LINK_FLAGS_${REL_CONFIG} "/LTCG /OPT:REF /INCREMENTAL:NO"
)
endforeach()
if(PLATFORM_UNIVERSAL_WINDOWS)
# On UWP, disable incremental link to avoid linker warnings
foreach(DBG_CONFIG ${DEBUG_CONFIGURATIONS})
set_target_properties(${TARGET} PROPERTIES
LINK_FLAGS_${DBG_CONFIG} "/INCREMENTAL:NO"
)
endforeach()
endif()
endif()
else()
set_target_properties(${TARGET} PROPERTIES
CXX_VISIBILITY_PRESET hidden # -fvisibility=hidden
C_VISIBILITY_PRESET hidden # -fvisibility=hidden
VISIBILITY_INLINES_HIDDEN TRUE
# Without -fPIC option GCC fails to link static libraries into dynamic library:
# -fPIC
# If supported for the target machine, emit position-independent code, suitable for
# dynamic linking and avoiding any limit on the size of the global offset table.
POSITION_INDEPENDENT_CODE ON
# It is crucial to set CXX_STANDARD flag to only affect c++ files and avoid failures compiling c-files:
# error: invalid argument '-std=c++14' not allowed with 'C/ObjC'
CXX_STANDARD 14
CXX_STANDARD_REQUIRED ON
C_STANDARD 11
)
if(NOT MINGW_BUILD)
# Do not disable extensions when building with MinGW!
set_target_properties(${TARGET} PROPERTIES
CXX_EXTENSIONS OFF
)
endif()
endif()
if(COMMAND custom_post_configure_target)
custom_post_configure_target(${TARGET})
endif()
endfunction()
function(find_targets_in_directory _RESULT _DIR)
get_property(_subdirs DIRECTORY "${_DIR}" PROPERTY SUBDIRECTORIES)
foreach(_subdir IN LISTS _subdirs)
find_targets_in_directory(${_RESULT} "${_subdir}")
endforeach()
get_property(_SUB_TARGETS DIRECTORY "${_DIR}" PROPERTY BUILDSYSTEM_TARGETS)
set(${_RESULT} ${${_RESULT}} ${_SUB_TARGETS} PARENT_SCOPE)
endfunction()
function(set_directory_root_folder _DIRECTORY _ROOT_FOLDER)
find_targets_in_directory(_TARGETS ${_DIRECTORY})
foreach(_TARGET IN LISTS _TARGETS)
get_target_property(_FOLDER ${_TARGET} FOLDER)
if(_FOLDER)
set_target_properties(${_TARGET} PROPERTIES FOLDER "${_ROOT_FOLDER}/${_FOLDER}")
else()
set_target_properties(${_TARGET} PROPERTIES FOLDER "${_ROOT_FOLDER}")
endif()
endforeach()
endfunction()
# Returns default backend library type (static/dynamic) for the current platform
function(get_backend_libraries_type _LIB_TYPE)
if(PLATFORM_WIN32 OR PLATFORM_LINUX OR PLATFORM_ANDROID OR PLATFORM_UNIVERSAL_WINDOWS OR PLATFORM_MACOS)
set(LIB_TYPE "shared")
elseif(PLATFORM_IOS OR PLATFORM_TVOS)
# Statically link with the engine on iOS and tvOS.
# It is also possible to link dynamically by
# putting the library into the framework.
set(LIB_TYPE "static")
else()
message(FATAL_ERROR "Undefined platform")
endif()
set(${_LIB_TYPE} ${LIB_TYPE} PARENT_SCOPE)
endfunction()
# Adds the list of supported backend targets to variable ${_TARGETS} in parent scope.
# Second argument to the function may override the target type (static/dynamic). If It
# is not given, default target type for the platform is used.
function(get_supported_backends _TARGETS)
if(${ARGC} GREATER 1)
set(LIB_TYPE ${ARGV1})
else()
get_backend_libraries_type(LIB_TYPE)
endif()
if(D3D11_SUPPORTED)
list(APPEND BACKENDS Diligent-GraphicsEngineD3D11-${LIB_TYPE})
endif()
if(D3D12_SUPPORTED)
list(APPEND BACKENDS Diligent-GraphicsEngineD3D12-${LIB_TYPE})
endif()
if(GL_SUPPORTED OR GLES_SUPPORTED)
list(APPEND BACKENDS Diligent-GraphicsEngineOpenGL-${LIB_TYPE})
endif()
if(VULKAN_SUPPORTED)
list(APPEND BACKENDS Diligent-GraphicsEngineVk-${LIB_TYPE})
endif()
if(METAL_SUPPORTED)
list(APPEND BACKENDS Diligent-GraphicsEngineMetal-${LIB_TYPE})
endif()
# ${_TARGETS} == ENGINE_LIBRARIES
# ${${_TARGETS}} == ${ENGINE_LIBRARIES}
set(${_TARGETS} ${${_TARGETS}} ${BACKENDS} PARENT_SCOPE)
endfunction()
# Returns path to the target relative to CMake root
function(get_target_relative_dir _TARGET _DIR)
get_target_property(TARGET_SOURCE_DIR ${_TARGET} SOURCE_DIR)
file(RELATIVE_PATH TARGET_RELATIVE_PATH "${CMAKE_SOURCE_DIR}" "${TARGET_SOURCE_DIR}")
set(${_DIR} ${TARGET_RELATIVE_PATH} PARENT_SCOPE)
endfunction()
# Performs installation steps for the core library
function(install_core_lib _TARGET)
get_target_relative_dir(${_TARGET} TARGET_RELATIVE_PATH)
get_target_property(TARGET_TYPE ${_TARGET} TYPE)
if(TARGET_TYPE STREQUAL STATIC_LIBRARY)
list(APPEND DILIGENT_CORE_INSTALL_LIBS_LIST ${_TARGET})
set(DILIGENT_CORE_INSTALL_LIBS_LIST ${DILIGENT_CORE_INSTALL_LIBS_LIST} CACHE INTERNAL "Core libraries installation list")
elseif(TARGET_TYPE STREQUAL SHARED_LIBRARY)
install(TARGETS ${_TARGET}
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}/${DILIGENT_CORE_DIR}/$<CONFIG>"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}/${DILIGENT_CORE_DIR}/$<CONFIG>"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}/${DILIGENT_CORE_DIR}/$<CONFIG>"
)
if (DILIGENT_INSTALL_PDB)
install(FILES $<TARGET_PDB_FILE:${_TARGET}> DESTINATION "${CMAKE_INSTALL_BINDIR}/${DILIGENT_CORE_DIR}/$<CONFIG>" OPTIONAL)
endif()
endif()
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/interface")
install(DIRECTORY interface
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${TARGET_RELATIVE_PATH}/"
)
endif()
endfunction()
function(install_combined_static_lib COMBINED_LIB_NAME LIBS_LIST CUSTOM_TARGET_NAME CUSTOM_TARGET_FOLDER INSTALL_DESTINATION)
foreach(LIB ${LIBS_LIST})
list(APPEND COMBINED_LIB_TARGET_FILES $<TARGET_FILE:${LIB}>)
endforeach(LIB)
if(MSVC)
add_custom_command(
OUTPUT ${COMBINED_LIB_NAME}
COMMAND lib.exe /OUT:${COMBINED_LIB_NAME} ${COMBINED_LIB_TARGET_FILES}
DEPENDS ${LIBS_LIST}
COMMENT "Combining libraries..."
)
add_custom_target(${CUSTOM_TARGET_NAME} ALL DEPENDS ${COMBINED_LIB_NAME})
else()
if(PLATFORM_WIN32)
# do NOT use stock ar on MinGW
find_program(AR NAMES x86_64-w64-mingw32-gcc-ar)
else()
set(AR ${CMAKE_AR})
endif()
if(AR)
add_custom_command(
OUTPUT ${COMBINED_LIB_NAME}
# Delete all object files from current directory
COMMAND ${CMAKE_COMMAND} -E remove "*${CMAKE_C_OUTPUT_EXTENSION}"
DEPENDS ${LIBS_LIST}
COMMENT "Combining libraries..."
)
# Unpack all object files from all targets to current directory
foreach(LIB_TARGET ${COMBINED_LIB_TARGET_FILES})
add_custom_command(
OUTPUT ${COMBINED_LIB_NAME}
COMMAND ${AR} -x ${LIB_TARGET}
APPEND
)
endforeach()
# Pack object files to a combined library and delete them
add_custom_command(
OUTPUT ${COMBINED_LIB_NAME}
COMMAND ${AR} -crs ${COMBINED_LIB_NAME} "*${CMAKE_C_OUTPUT_EXTENSION}"
COMMAND ${CMAKE_COMMAND} -E remove "*${CMAKE_C_OUTPUT_EXTENSION}"
APPEND
)
add_custom_target(${CUSTOM_TARGET_NAME} ALL DEPENDS ${COMBINED_LIB_NAME})
else()
message("ar command is not found")
endif()
endif()
if(TARGET ${CUSTOM_TARGET_NAME})
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${COMBINED_LIB_NAME}"
DESTINATION ${INSTALL_DESTINATION}
)
set_target_properties(${CUSTOM_TARGET_NAME} PROPERTIES
FOLDER ${CUSTOM_TARGET_FOLDER}
)
else()
message("Unable to find librarian tool. Combined ${COMBINED_LIB_NAME} static library will not be produced.")
endif()
endfunction()
function(add_format_validation_target MODULE_NAME MODULE_ROOT_PATH IDE_FOLDER)
if(${DILIGENT_NO_FORMAT_VALIDATION})
return()
endif()
# Start by copying .clang-format file to the module's root folder
add_custom_target(${MODULE_NAME}-ValidateFormatting ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${DILIGENT_CORE_SOURCE_DIR}/.clang-format" "${MODULE_ROOT_PATH}/.clang-format"
)
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(RUN_VALIDATION_SCRIPT validate_format_win.bat)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux")
set(RUN_VALIDATION_SCRIPT ./validate_format_linux.sh)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin")
set(RUN_VALIDATION_SCRIPT ./validate_format_mac.sh)
else()
message(FATAL_ERROR "Unexpected host system")
endif()
# Run the format validation script
add_custom_command(TARGET ${MODULE_NAME}-ValidateFormatting
COMMAND ${RUN_VALIDATION_SCRIPT}
WORKING_DIRECTORY "${MODULE_ROOT_PATH}/BuildTools/FormatValidation"
COMMENT "Validating ${MODULE_NAME} module's source code formatting..."
VERBATIM
)
if(TARGET ${MODULE_NAME}-ValidateFormatting)
set_target_properties(${MODULE_NAME}-ValidateFormatting PROPERTIES FOLDER ${IDE_FOLDER})
endif()
endfunction()