-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
229 lines (192 loc) · 11.1 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
cmake_minimum_required(VERSION 3.2...3.5)
# Generators using Ninja can tell if the build files are generated as a side effect of running the command, which can help avoid unnecessary rebuilds.
cmake_policy(SET CMP0058 NEW)
# Prefers to use GLVND (OpenGL Vendor Neutral Dispatch) libraries for OpenGL.
cmake_policy(SET CMP0072 NEW)
# The following thread-related commands must be before project()
if(WIN32)
# Use native Win32 threads on Windows
set(CMAKE_USE_WIN32_THREADS_INIT ON)
set(CMAKE_USE_PTHREADS_INIT OFF)
else()
# Use pthreads on Unix-like systems
set(CMAKE_THREAD_LIBS_INIT "-pthread")
set(CMAKE_HAVE_THREADS_LIBRARY 1)
set(CMAKE_USE_WIN32_THREADS_INIT OFF)
set(CMAKE_USE_PTHREADS_INIT ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
endif()
project(maze_builder)
set(MAZE_BUILDER_OUTFILE "maze_builder")
set(MAZE_BUILDER_TESTS_OUTFILE "maze_builder_tests")
set(MAZE_BUILDER_LIB "maze_builder_lib")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
option(BUILD_MAZE_TESTS "Build ${PROJECT_NAME} tests" OFF)
set(DEAR_IMGUI_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui_demo.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui_draw.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui_tables.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui_widgets.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/backends/imgui_impl_sdl3.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/backends/imgui_impl_opengl3.cpp)
set(DEAR_IMGUI_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imconfig.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imgui_internal.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imstb_rectpack.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imstb_textedit.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/imstb_truetype.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/backends/imgui_impl_sdl3.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/backends/imgui_impl_opengl3.h
${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui/backends/imgui_impl_opengl3_loader.h)
set(CPP_BASE64_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/deps/cpp-base64/base64.cpp)
set(GLAD_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/deps/glad/src/glad_3_0.c)
set(NOISE_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/deps/noise/noise.c)
set(SQLITE3_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/deps/sqlite/sqlite3.c)
set(MAZE_BUILDER_LIB_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/sources/args_builder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/sidewinder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/binary_tree.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/sidewinder.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/grid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/cell.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/writer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/maze_thread_safe.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/maze_factory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/distances.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/distance_grid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/colored_grid.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/dfs.cpp)
set(CRAFT_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/sources/craft.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/cube.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/db.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/item.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/map.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/matrix.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/ring.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/sign.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/craft_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sources/world.cpp)
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
message(INFO ": ${CMAKE_BUILD_TYPE} - enabled ${PROJECT_NAME} to use -DMAZE_DEBUG flag.")
add_compile_definitions("MAZE_DEBUG")
endif()
add_library(${MAZE_BUILDER_LIB} STATIC ${MAZE_BUILDER_LIB_SOURCES})
target_compile_features(${MAZE_BUILDER_LIB} PUBLIC cxx_std_17)
target_include_directories(${MAZE_BUILDER_LIB} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps)
add_executable(${MAZE_BUILDER_OUTFILE} ${CMAKE_CURRENT_SOURCE_DIR}/sources/main.cpp ${DEAR_IMGUI_SRCS} ${CPP_BASE64_SRCS} ${GLAD_SOURCES} ${NOISE_SOURCES} ${SQLITE3_SOURCES} ${CRAFT_SOURCES})
target_compile_features(${MAZE_BUILDER_OUTFILE} PUBLIC cxx_std_17)
target_include_directories(${MAZE_BUILDER_OUTFILE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps/glad/include)
target_include_directories(${MAZE_BUILDER_OUTFILE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps)
target_include_directories(${MAZE_BUILDER_OUTFILE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps/emscripten_local)
target_include_directories(${MAZE_BUILDER_OUTFILE} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/deps/dearimgui)
include(FetchContent)
function(fetch_lib_from_git name_of_lib tag git_repo)
FetchContent_Declare(
${name_of_lib}
GIT_REPOSITORY ${git_repo}
GIT_TAG ${tag}
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)
FetchContent_MakeAvailable(${name_of_lib})
endfunction()
if(EMSCRIPTEN)
message(INFO ": Building SDL app for Web!")
# Check if CMAKE_TOOLCHAIN_FILE is set, throw a fatal error if not
if(NOT DEFINED CMAKE_TOOLCHAIN_FILE)
message(FATAL_ERROR "CMAKE_TOOLCHAIN_FILE is not set. Please specify the path to the Emscripten toolchain file.")
endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -matomics -mbulk-memory")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -matomics -mbulk-memory")
set_target_properties(${MAZE_BUILDER_OUTFILE} PROPERTIES SUFFIX ".html" LINK_FLAGS "-lembind")
# SDL3 isn't available as an Emscripten port (yet), so fetch it
message(INFO ": Getting SDL via FetchContent")
fetch_lib_from_git(SDL preview-3.1.3 https:/libsdl-org/SDL.git)
set(SDL_SHARED FALSE CACHE BOOL "Build a SDL shared library (if available)")
set(SDL_STATIC TRUE CACHE BOOL "Build a SDL static library (if available)")
set_property(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/_deps/sdl-src" PROPERTY EXCLUDE_FROM_ALL TRUE)
target_link_libraries(${MAZE_BUILDER_OUTFILE} SDL3::SDL3 ${CMAKE_THREAD_LIBS_INIT} ${MAZE_BUILDER_LIB})
target_link_options(${MAZE_BUILDER_OUTFILE} PRIVATE
"SHELL:-s PTHREAD_POOL_SIZE=6 -s PTHREAD_POOL_SIZE_STRICT=2 -s WASM=1 -s USE_PTHREADS=1 -s ALLOW_MEMORY_GROWTH=1 -s STACK_SIZE=323232"
"SHELL:-s FULL_ES3=1 -s MAX_WEBGL_VERSION=2 -s MODULARIZE=1 -s EXPORT_ES6=1 -s ASYNCIFY"
"SHELL:--shell-file ${CMAKE_CURRENT_SOURCE_DIR}/deps/emscripten_local/shell_minimal.html"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/block_vertex.es.glsl@/shaders/es/block_vertex.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/block_fragment.es.glsl@/shaders/es/block_fragment.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/line_vertex.es.glsl@/shaders/es/line_vertex.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/line_fragment.es.glsl@/shaders/es/line_fragment.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/sky_vertex.es.glsl@/shaders/es/sky_vertex.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/sky_fragment.es.glsl@/shaders/es/sky_fragment.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/text_vertex.es.glsl@/shaders/es/text_vertex.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/shaders/es/text_fragment.es.glsl@/shaders/es/text_fragment.es.glsl"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/textures/texture.png@/textures/texture.png"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/textures/font.png@/textures/font.png"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/textures/sign.png@/textures/sign.png"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/textures/sky.png@/textures/sky.png"
"SHELL:--preload-file ${CMAKE_CURRENT_SOURCE_DIR}/textures/maze_in_green_32x32.bmp@/textures/maze_in_green_32x32.bmp")
if(CMAKE_BUILD_TYPE STREQUAL "Debug" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
target_link_options(${MAZE_BUILDER_OUTFILE} PRIVATE "SHELL:-s STACK_OVERFLOW_CHECK=2 -s SAFE_HEAP=2 -s ASSERTIONS=2 -gsource-map")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_link_options(${MAZE_BUILDER_OUTFILE} PRIVATE "SHELL:-O3")
endif()
elseif(APPLE)
target_link_libraries(${MAZE_BUILDER_OUTFILE} OpenGL::GL SDL3::SDL3 ${CMAKE_THREAD_LIBS_INIT} ${MAZE_BUILDER_LIB})
elseif(UNIX)
message(INFO ": Building SDL app for Desktop UNIX!")
find_package(OpenGL REQUIRED)
# Find/Configure SDL
find_package(SDL3 QUIET)
if(SDL3_FOUND)
message(INFO ": Getting SDL via find_package")
endif()
# If all else fails, download SDL
if(NOT SDL3_FOUND)
# Call custom function to fetch from git
fetch_lib_from_git(SDL preview-3.1.3 https:/libsdl-org/SDL.git)
set_property(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/_deps/sdl-src" PROPERTY EXCLUDE_FROM_ALL TRUE)
endif()
target_link_libraries(${MAZE_BUILDER_OUTFILE} dl OpenGL::GL SDL3::SDL3 ${CMAKE_THREAD_LIBS_INIT} ${MAZE_BUILDER_LIB})
elseif(MINGW OR WIN32)
message(INFO ": Building SDL app for Desktop WINDOWS!")
find_package(OpenGL REQUIRED)
# Find/Configure SDL
find_package(SDL3 QUIET)
if(SDL3_FOUND)
message(INFO ": Getting SDL via find_package")
endif()
# If all else fails, download SDL
if(NOT SDL3_FOUND)
# Call custom function to fetch from git
fetch_lib_from_git(SDL preview-3.1.3 https:/libsdl-org/SDL.git)
set_property(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/_deps/sdl-src" PROPERTY EXCLUDE_FROM_ALL TRUE)
endif()
target_link_libraries(${MAZE_BUILDER_OUTFILE} ws2_32.lib OpenGL::GL SDL3::SDL3 ${CMAKE_THREAD_LIBS_INIT} ${MAZE_BUILDER_LIB})
endif()
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/shaders" DESTINATION "${CMAKE_BINARY_DIR}")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/textures" DESTINATION "${CMAKE_BINARY_DIR}")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/scripts/secure_http_server.py" DESTINATION "${CMAKE_BINARY_DIR}")
if(BUILD_MAZE_TESTS)
find_package(Catch2 3 QUIET)
if(CATCH2_FOUND)
message(INFO ": Getting Catch2 via find_package")
endif()
if(NOT CATCH2_FOUND)
message(INFO ": Getting Catch2 via FetchContent")
fetch_lib_from_git(Catch2 v3.7.0 https:/catchorg/Catch2.git)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
endif()
enable_testing()
message(INFO ": Building ${PROJECT_NAME} tests")
file(GLOB TEST_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/test_*.cpp)
# These tests can use the Catch2-provided main
add_executable(${MAZE_BUILDER_TESTS_OUTFILE} ${TEST_FILES})
add_test(NAME RUN_MAZE_TESTS COMMAND ${MAZE_BUILDER_TESTS_OUTFILE})
target_compile_features(${MAZE_BUILDER_TESTS_OUTFILE} PUBLIC cxx_std_17)
target_link_libraries(${MAZE_BUILDER_TESTS_OUTFILE} PRIVATE ${MAZE_BUILDER_LIB} Catch2::Catch2WithMain)
target_include_directories(${MAZE_BUILDER_TESTS_OUTFILE} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/sources")
endif()