-
Notifications
You must be signed in to change notification settings - Fork 20
/
CMakeLists.txt
320 lines (242 loc) · 10.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
# require version 3.10 or higher
cmake_minimum_required(VERSION 3.10)
project(olcSoundWaveEngine)
option(USE_ALSA "Force using ALSA as audio backend (Linux-only)")
option(USE_PULSEAUDIO "Force using PulseAudio as audio backend (Linux-only)")
option(USE_SDL2_MIXER "Force using SDL2_mixer as audio backend")
# Set C++ Standards
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
######################################################################
# Directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
# We need to specify the output for each configuration to make it work
# on Visual Studio solutions.
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_PROFILE "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_PROFILE "${CMAKE_BINARY_DIR}/lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_PROFILE "${CMAKE_BINARY_DIR}/bin")
# set(SOURCE_CXX_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(SOURCE_CXX_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/demos)
set(SOURCE_DATA_DIR ${CMAKE_CURRENT_SOURCE_DIR}/demos/assets)
# Source Files are Curated Here
file(
GLOB_RECURSE _demoSourceFiles
"${SOURCE_CXX_SRC_DIR}/*.cpp"
)
foreach(_demoSource IN LISTS _demoSourceFiles) #######################
get_filename_component(OutputExecutable ${_demoSource} NAME_WE)
add_executable(${OutputExecutable} ${_demoSource})
######################################################################
# MacOS
######################################################################
if(APPLE)
# OpenGL
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
target_link_libraries(${OutputExecutable} ${OpenGL_LIBRARIES} OpenGL::GL)
# Carbon
FIND_LIBRARY(CARBON_LIBRARY Carbon)
target_link_libraries(${OutputExecutable} ${CARBON_LIBRARY})
# GLUT
find_package(GLUT REQUIRED)
target_link_libraries(${OutputExecutable} ${GLUT_LIBRARIES})
# Threads
find_package(Threads REQUIRED)
target_link_libraries(${OutputExecutable} Threads::Threads)
include_directories(${Threads_INCLUDE_DIRS})
# SDL2_mixer
set(USE_SDL2_MIXER ON)
find_package(PNG REQUIRED)
target_link_libraries(${OutputExecutable} PNG::PNG)
include_directories(${PNG_INCLUDE_DIRS})
endif()
######################################################################
# Windows: MinGW
######################################################################
if(WIN32 AND MINGW)
# OpenGL
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
target_link_libraries(${OutputExecutable} ${OpenGL_LIBRARIES} OpenGL::GL)
# GDI+
set(GDIPLUS_LIBRARY gdiplus)
target_link_libraries(${OutputExecutable} ${GDIPLUS_LIBRARY})
# Shlwapi
set(SHLWAPI_LIBRARY shlwapi)
target_link_libraries(${OutputExecutable} ${SHLWAPI_LIBRARY})
# Dwmapi
set(DWMAPI_LIBRARY dwmapi)
target_link_libraries(${OutputExecutable} ${DWMAPI_LIBRARY})
if(NOT USE_SDL2_MIXER)
# winmm
set(WINMM_LIBRARY winmm)
target_link_libraries(${OutputExecutable} ${WINMM_LIBRARY})
endif() # NOT USE_SDL2_MIXER
# stdc++fs
target_link_libraries(${OutputExecutable} stdc++fs)
endif() # WIN32 AND MINGW
######################################################################
# Windows: Visual Studio / MSVC
######################################################################
if(WIN32 AND MSVC)
# OpenGL
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
target_link_libraries(${OutputExecutable} ${OpenGL_LIBRARIES} OpenGL::GL)
# set the startup project to the target executable instead of ALL_BUILD
set_property(
DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
PROPERTY
VS_STARTUP_PROJECT
${OutputExecutable}
)
# set working directory for Visual Studio Debugger
set_target_properties(
${OutputExecutable} PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
# GDI+
set(GDIPLUS_LIBRARY gdiplus)
target_link_libraries(${OutputExecutable} ${GDIPLUS_LIBRARY})
# Shlwapi
set(SHLWAPI_LIBRARY shlwapi)
target_link_libraries(${OutputExecutable} ${SHLWAPI_LIBRARY})
# Dwmapi
set(DWMAPI_LIBRARY dwmapi)
target_link_libraries(${OutputExecutable} ${DWMAPI_LIBRARY})
if(NOT USE_SDL2_MIXER)
# winmm
set(WINMM_LIBRARY winmm)
target_link_libraries(${OutputExecutable} ${WINMM_LIBRARY})
endif()
endif() # WIN32 AND MSVC
######################################################################
# Linux: using anything?
######################################################################
if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN)
# OpenGL
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
target_link_libraries(${OutputExecutable} ${OpenGL_LIBRARIES} OpenGL::GL)
# X11
find_package(X11 REQUIRED)
target_link_libraries(${OutputExecutable} X11::X11)
include_directories(${X11_INCLUDE_DIRS})
# Threads
find_package(Threads REQUIRED)
target_link_libraries(${OutputExecutable} Threads::Threads)
include_directories(${Threads_INCLUDE_DIRS})
# TODO: sanity checks
if(USE_ALSA)
# ALSA
find_package(ALSA REQUIRED)
target_link_libraries(${OutputExecutable} ALSA::ALSA)
include_directories(${ALSA_INCLUDE_DIRS})
add_compile_definitions(SOUNDWAVE_USING_ALSA=1)
elseif(USE_SDL2_MIXER)
# Because SDL2_mixer can be used on multiple platforms, we
# defer it's inclusion until outside of the platform/toolchain
# selection logic.
else() # PulseAudio is Default
# PulseAudio
find_package(PulseAudio REQUIRED)
target_link_libraries(${OutputExecutable} ${PULSEAUDIO_LIBRARY} pulse-simple)
include_directories(${PULSEAUDIO_INCLUDE_DIR})
add_compile_definitions(SOUNDWAVE_USING_PULSE=1)
endif()
find_package(PNG REQUIRED)
target_link_libraries(${OutputExecutable} PNG::PNG)
include_directories(${PNG_INCLUDE_DIRS})
# stdc++fs
target_link_libraries(${OutputExecutable} stdc++fs)
endif() # Linux
######################################################################
# Emscripten
######################################################################
if (EMSCRIPTEN)
# Generate an HTML file
set(CMAKE_EXECUTABLE_SUFFIX .html)
# SDL2_mixer: build cache
execute_process(COMMAND "${EMSCRIPTEN_ROOT_PATH}/embuilder${EMCC_SUFFIX}" build sdl2_mixer)
# libpng: build cache
execute_process(COMMAND "${EMSCRIPTEN_ROOT_PATH}/embuilder${EMCC_SUFFIX}" build libpng)
# zlib, for libpng: build cache
execute_process(COMMAND "${EMSCRIPTEN_ROOT_PATH}/embuilder${EMCC_SUFFIX}" build zlib)
# require libpng
find_package(PNG REQUIRED)
target_link_libraries(${OutputExecutable} PNG::PNG)
include_directories(${PNG_INCLUDE_DIRS})
if(EXISTS "${SOURCE_DATA_DIR}" AND IS_DIRECTORY "${SOURCE_DATA_DIR}")
target_link_options(
${OutputExecutable}
PRIVATE
-sALLOW_MEMORY_GROWTH=1
-sMAX_WEBGL_VERSION=2
-sMIN_WEBGL_VERSION=2
-sUSE_LIBPNG=1
-sUSE_SDL_MIXER=2 # thanks for the s, cstd
-sLLD_REPORT_UNDEFINED
--preload-file ${SOURCE_DATA_DIR}@assets)
else()
target_link_options(
${OutputExecutable}
PRIVATE
-sALLOW_MEMORY_GROWTH=1
-sMAX_WEBGL_VERSION=2
-sMIN_WEBGL_VERSION=2
-sUSE_LIBPNG=1
-sUSE_SDL_MIXER=2 # thanks for the s, cstd
-sLLD_REPORT_UNDEFINED)
endif()
endif() # Emscripten
if(USE_SDL2_MIXER AND NOT EMSCRIPTEN)
# SDL2_mixer
find_package(SDL2_mixer REQUIRED)
target_link_libraries(${OutputExecutable} SDL2_mixer::SDL2_mixer)
add_compile_definitions(SOUNDWAVE_USING_SDLMIXER=1)
endif() # USE_SDL2_MIXER
######################################################################
# Set include directory
######################################################################
if(IS_DIRECTORY ${SOURCE_CXX_INCLUDE_DIR})
include_directories(${SOURCE_CXX_INCLUDE_DIR})
endif()
endforeach() #########################################################
######################################################################
# Copy assets/ directory target
######################################################################
set(DATA_OUTPUT_DIR ${CMAKE_BINARY_DIR}/bin/assets)
file(GLOB_RECURSE src_data_files
RELATIVE ${SOURCE_DATA_DIR}/ "${SOURCE_DATA_DIR}/*.*" "${SOURCE_DATA_DIR}/*")
foreach(fn ${src_data_files})
add_custom_command(
OUTPUT ${DATA_OUTPUT_DIR}/${fn}
COMMAND ${CMAKE_COMMAND} -E copy ${SOURCE_DATA_DIR}/${fn} ${DATA_OUTPUT_DIR}/${fn}
MAIN_DEPENDENCY ${SOURCE_DATA_DIR}/${fn})
list(APPEND out_data_files ${DATA_OUTPUT_DIR}/${fn})
endforeach()
add_custom_target(copy_data DEPENDS ${out_data_files})
# Copy Asset Files, if not Emscripten
if (NOT EMSCRIPTEN)
foreach(_demoSource IN LISTS _demoSourceFiles) #######################
get_filename_component(OutputExecutable ${_demoSource} NAME_WE)
add_dependencies(${OutputExecutable} copy_data)
endforeach()
endif()