-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
373 lines (332 loc) · 12.2 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
cmake_minimum_required(VERSION 2.8)
Project("PGR2Examples")
#####################################################################################
# Set CPU architectures
#
if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
set ( ARCH "x64" CACHE STRING "CPU Architecture" )
message( STATUS "Building for x64." )
else ()
message( STATUS "Building for x86." )
set ( ARCH "x86" CACHE STRING "CPU Architecture" )
endif()
#####################################################################################
# General compile flags
#
#####################################################################################
# Project directories
#
SET( BASE_PATH ${CMAKE_CURRENT_SOURCE_DIR} )
SET( SHARED_DATA_PATH ${BASE_PATH}/shared )
SET( RESOURCE_DIRECTORY ${SHARED_DATA_PATH}/resources )
set( EXECUTABLE_OUTPUT_PATH ${BASE_PATH}/bin_${ARCH} CACHE PATH "Directory where executables will be stored" )
set( CMAKE_FIND_ROOT_PATH "" )
set( CMAKE_MODULE_PATH ${SHARED_DATA_PATH}/cmake )
#####################################################################################
# Set default build to Release
#
if( NOT CMAKE_BUILD_TYPE )
set( CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE )
endif( NOT CMAKE_BUILD_TYPE )
#####################################################################################
# OS related defines and settings
#
if( UNIX )
set( OS "linux" )
add_definitions( -DLINUX -g -std=c++11 )
else( UNIX )
if( WIN32 )
set( OS "win" )
add_definitions( -DNOMINMAX )
endif( WIN32 )
endif( UNIX )
#####################################################################################
# Macro for adding new project to the solution
#
macro(_add_project_definitions name)
add_definitions(-DPROJECT_DIRECTORY="${CMAKE_CURRENT_SOURCE_DIR}/")
add_definitions(-DPROJECT_NAME="${name}")
endmacro(_add_project_definitions)
#####################################################################################
# Macro for loading shared library
#
macro( _find_shared_library target folder libName )
if( ARCH STREQUAL "x86" )
message( STATUS "Looking for ${folder}x86/${libName}" )
file( GLOB SHARED_LIBRARIES "${folder}x86/${libName}" )
else()
message( STATUS "Looking for ${folder}x64/${libName}" )
file( GLOB SHARED_LIBRARIES "${folder}x64/${libName}" )
endif()
list( LENGTH SHARED_LIBRARIES NUMD_LIBRARIES )
if( NUMD_LIBRARIES EQUAL 0 )
if( ARCH STREQUAL "x86" )
message( STATUS "Following file was found but requested ${folder}x86/${libName}" )
else()
message( STATUS "Following file was found but requested ${folder}x64/${libName}" )
endif()
set ( ${target} "NOTFOUND" )
else()
list( GET SHARED_LIBRARIES 0 ${target} )
endif()
endmacro()
#####################################################################################
# Macro for copying shared files (libraries) to output directory
#
macro( _copy_files_to_target target fileList )
if( WIN32 )
foreach ( _file ${fileList} )
if( EXISTS "${_file}" )
add_custom_command(
TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${_file}
$<TARGET_FILE_DIR:${target}>
VERBATIM
)
endif()
endforeach()
endif()
endmacro()
#####################################################################################
# Macro that copies various binaries that need to be close to the exe files
#
macro( _copy_binaries_to_target PROJECT_NAME )
if( GLFW_FOUND )
_copy_files_to_target( ${PROJECT_NAME} "${GLFW_DLL}" )
install( FILES "${GLFW_DLL}" DESTINATION bin )
endif()
if(GLEW_FOUND)
_copy_files_to_target( ${PROJECT_NAME} "${GLEW_DLL}")
install(FILES "${GLEW_DLL}" DESTINATION bin)
endif()
if(OPENCL_DLL)
_copy_files_to_target( ${PROJECT_NAME} "${OPENCL_DLL}")
install(FILES "${OPENCL_DLL}" DESTINATION bin)
endif()
install(TARGETS ${PROJECT_NAME} DESTINATION bin CONFIGURATIONS Release)
install(TARGETS ${PROJECT_NAME} DESTINATION bin CONFIGURATIONS Debug)
endmacro()
#####################################################################################
# GLFW
#
macro( _add_package_GLFW )
find_package( GLFW )
if( GLFW_FOUND )
Message( STATUS "--> using package GLFW" )
include_directories( ${GLFW_INCLUDE_DIR} )
LIST( APPEND PACKAGE_SOURCE_FILES ${GLFW_HEADERS} )
LIST( APPEND LIBRARIES_OPTIMIZED ${GLFW_LIB} )
LIST( APPEND LIBRARIES_DEBUG ${GLFW_LIB} )
source_group( GLFW FILES ${GLFW_HEADERS} )
else()
Message( STATUS "--> NOT using package GLFW" )
endif()
endmacro()
#####################################################################################
# GLEW
#
macro( _add_package_GLEW )
find_package( GLEW )
if( GLEW_FOUND )
Message( STATUS "--> using package GLEW" )
include_directories( ${GLEW_INCLUDE_DIR} )
LIST( APPEND PACKAGE_SOURCE_FILES ${GLEW_HEADERS} )
LIST( APPEND LIBRARIES_OPTIMIZED ${GLEW_LIB} )
LIST( APPEND LIBRARIES_DEBUG ${GLEW_LIB} )
source_group( GLEW FILES ${GLEW_HEADERS} )
else()
Message( STATUS "--> NOT using package GLEW" )
endif()
endmacro()
#####################################################################################
# Optional VulkanSDK package
#
macro( _add_package_VulkanSDK )
find_package( VulkanSDK )
if( VULKANSDK_FOUND )
Message( STATUS "--> using package VulkanSDK" )
add_definitions( -DUSEVULKANSDK )
include_directories( ${VULKANSDK_INCLUDE_DIR} )
LIST( APPEND LIBRARIES_OPTIMIZED ${VULKAN_LIB} )
LIST( APPEND LIBRARIES_DEBUG ${VULKAN_LIB} )
LIST( APPEND PACKAGE_SOURCE_FILES ${VULKANSDK_HEADERS} )
source_group( OPTIX FILES ${VULKANSDK_HEADERS} )
else()
Message( STATUS "--> NOT using package VulkanSDK" )
endif()
endmacro()
#####################################################################################
# Optional CUDA package
# WARNING: also used for OpenCL at this point. Rather clunky but
# CUDA contains OpenCL stuff...
#
macro( _add_package_CUDA )
find_package( CUDA )
if( CUDA_FOUND )
Message( STATUS "--> using package CUDA" )
add_definitions( -DUSECUDA )
include_directories( ${CUDA_INCLUDE_DIR} )
LIST( APPEND LIBRARIES_OPTIMIZED ${CUDA_LIB} )
LIST( APPEND LIBRARIES_DEBUG ${CUDA_LIB} )
LIST( APPEND PACKAGE_SOURCE_FILES ${CUDA_HEADERS} )
source_group( CUDA FILES ${CUDA_HEADERS} )
else()
Message( STATUS "--> NOT using package CUDA" )
endif()
endmacro()
#####################################################################################
# Optional OpenCL package
#
macro( _add_package_OpenCL )
find_package( OpenCL )
if( OpenCL_FOUND )
Message( STATUS "--> using package OpenCL : ${OpenCL_LIBRARIES}" )
add_definitions( -DUSEOPENCL )
include_directories( ${OpenCL_INCLUDE_DIRS} )
# just do the copy only if we pointed to the local OpenCL package
string( FIND ${OpenCL_INCLUDE_DIRS} "shared" OFFSET )
if( ( OFFSET GREATER -1 ) AND WIN32 )
if( ARCH STREQUAL "x86" )
set( OPENCL_DLL ${SHARED_DATA_PATH}/OpenCL/lib/x86/OpenCL.dll )
else()
set( OPENCL_DLL ${SHARED_DATA_PATH}/OpenCL/lib/x64/OpenCL.dll )
endif()
endif()
LIST( APPEND LIBRARIES_OPTIMIZED ${OpenCL_LIBRARIES} )
LIST( APPEND LIBRARIES_DEBUG ${OpenCL_LIBRARIES} )
else()
Message( STATUS "--> NOT using package OpenCL" )
endif()
endmacro()
#####################################################################################
# Macro to download a file from a URL
#
macro( _download_file _URL _TARGET _FORCE )
if( ${_FORCE} OR (NOT EXISTS ${_TARGET}) )
Message( STATUS "downloading ${_URL} ==> ${_TARGET}" )
file( DOWNLOAD ${_URL} ${_TARGET} SHOW_PROGRESS )
else()
Message( STATUS "model ${_TARGET} already loaded..." )
endif()
endmacro()
#
# example: _download_files("${FILELIST}" "http://..." "${SHARED_DATA_PATH}/..." ${MODELS_DOWNLOAD_FORCE} )
#
macro( _download_files _FILELIST _URL _TARGET _FORCE )
foreach( _FILE ${_FILELIST} )
if( ${_FORCE} OR ( NOT EXISTS "${_TARGET}/${_FILE}" ) )
Message( STATUS "*******************************************" )
Message( STATUS "downloading ${_URL}/${_FILE}\n ==>\n ${_TARGET}" )
Message( STATUS "*******************************************" )
file( DOWNLOAD ${_URL}/${_FILE} ${_TARGET}/${_FILE} SHOW_PROGRESS )
else()
Message( STATUS "model ${_FILE} already loaded..." )
endif()
endforeach( _FILE )
endmacro()
#####################################################################################
# Macro to add custom build for Spir-V
# _SOURCE can be more than one file (.vert + .frag)
# _OUTPUT is the .spv file, resulting from the linkage
#
macro( _compile_GLSL _SOURCE _OUTPUT SOURCE_LIST )
LIST( APPEND ${SOURCE_LIST} ${_SOURCE} )
Message( STATUS "${GLSLANGVALIDATOR} -o ${_OUTPUT} -V ${_SOURCE}" )
Message( STATUS "${_OUTPUT} : ${_SOURCE}" )
if( GLSLANGVALIDATOR )
add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/${_OUTPUT}
COMMAND echo ${GLSLANGVALIDATOR} -o ${_OUTPUT} -V ${_SOURCE}
COMMAND ${GLSLANGVALIDATOR} -o ${_OUTPUT} -V ${_SOURCE}
MAIN_DEPENDENCY ${_SOURCE}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
else( GLSLANGVALIDATOR )
MESSAGE( WARNING "could not find GLSLANGVALIDATOR to compile shaders" )
endif( GLSLANGVALIDATOR )
endmacro()
#####################################################################################
# OpenGL libraries
#
find_package(OpenGL)
#####################################################################################
# Create list of source files
include_directories(
${SHARED_DATA_PATH}
)
set( PLATFORM_LIBRARIES )
if( UNIX )
LIST( APPEND PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
"-lpthread -lm"
"-ldl"
)
else( UNIX )
LIST( APPEND PLATFORM_LIBRARIES
${OPENGL_LIBRARY}
)
endif( UNIX )
set( COMMON_SOURCE_FILES )
LIST( APPEND COMMON_SOURCE_FILES
${SHARED_DATA_PATH}/common.h
${SHARED_DATA_PATH}/tools.h
${SHARED_DATA_PATH}/resources.h
${SHARED_DATA_PATH}/resources.rc
)
file(GLOB MODEL_SOURCE_FILES
${SHARED_DATA_PATH}/models/*.cpp
${SHARED_DATA_PATH}/models/*.h
)
# ImGui & ImPlot source files
set( GUI_SOURCE_FILES )
LIST( APPEND GUI_SOURCE_FILES
${SHARED_DATA_PATH}/imgui/imconfig.h
${SHARED_DATA_PATH}/imgui/imgui.cpp
${SHARED_DATA_PATH}/imgui/imgui.h
${SHARED_DATA_PATH}/imgui/imgui_draw.cpp
${SHARED_DATA_PATH}/imgui/imgui_impl_glfw.cpp
${SHARED_DATA_PATH}/imgui/imgui_impl_glfw.h
${SHARED_DATA_PATH}/imgui/imgui_impl_opengl3.cpp
${SHARED_DATA_PATH}/imgui/imgui_impl_opengl3.h
${SHARED_DATA_PATH}/imgui/imgui_internal.h
${SHARED_DATA_PATH}/imgui/imgui_widgets.cpp
${SHARED_DATA_PATH}/imgui/imstb_rectpack.h
${SHARED_DATA_PATH}/imgui/imstb_textedit.h
${SHARED_DATA_PATH}/imgui/imstb_truetype.h
${SHARED_DATA_PATH}/implot/implot.cpp
${SHARED_DATA_PATH}/implot/implot.h
${SHARED_DATA_PATH}/implot/implot_internal.h
${SHARED_DATA_PATH}/implot/implot_items.cpp
)
#####################################################################################
# Process PGR2 projects
FILE (GLOB samples "./*")
FOREACH( sample ${samples} )
if( IS_DIRECTORY ${sample} )
if( EXISTS ${sample}/CMakeLists.txt )
# extract the name from the path
string( REGEX REPLACE "^.*/([^/]*)$" "\\1" SAMPLE_NAME ${sample} )
if( (NOT (${SAMPLE_NAME} MATCHES "^__")) AND (NOT(${SAMPLE_NAME} STREQUAL "build_all")) AND (NOT(${SAMPLE_NAME} STREQUAL "shared")) AND (NOT(${SAMPLE_NAME} STREQUAL "downloaded_resources")))
# build the option to include the project or not
set( "BUILD_${SAMPLE_NAME}" TRUE CACHE BOOL "Include the ${SAMPLE_NAME} sample" )
# add the project
if( BUILD_${SAMPLE_NAME})
add_subdirectory( ./${SAMPLE_NAME} ./${SAMPLE_NAME})
endif()
endif()
endif()
endif()
ENDFOREACH( sample ${samples} )
#####################################################################################
# Create defines
#
add_definitions( -DRESOURCE_DIRECTORY="${RESOURCE_DIRECTORY}/" )
if( MSVC )
add_definitions( -D_CRT_SECURE_NO_WARNINGS )
endif( MSVC )
#####################################################################################
# Print cmake configuration info
#
Message( STATUS "BASE_PATH = ${BASE_PATH}" )
Message( STATUS "CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}" )