forked from elonafoobar/elonafoobar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
487 lines (402 loc) · 14.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# CMake v3.2 or later
cmake_minimum_required(VERSION 3.2)
# Project
project(Elona_foobar VERSION 0.7.1)
# Platform detection
set(LINUX FALSE)
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
set(LINUX TRUE)
endif()
# Versioning
# Get commit hash from Git
find_package(Git)
if(NOT GIT_FOUND)
message(FATAL_ERROR "Git not Found")
endif()
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE PROJECT_VERSION_REVISION
OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS "Commit hash: ${PROJECT_VERSION_REVISION}")
# Get date and time
string(TIMESTAMP PROJECT_VERSION_TIMESTAMP UTC)
# Get platform
set(PROJECT_VERSION_PLATFORM "${CMAKE_SYSTEM}")
# Versioning file
configure_file("${PROJECT_SOURCE_DIR}/src/version.cpp.in" "${PROJECT_SOURCE_DIR}/src/version.cpp")
# C++17 or later
enable_language(CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
# If a CMake prefix was not provided, assume dependencies are in "./deps"
if("${CMAKE_PREFIX_PATH}" STREQUAL "")
get_filename_component(CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}/deps" ABSOLUTE)
message("No dependency prefix provided, using ${CMAKE_PREFIX_PATH}")
else()
message("Using provided dependency prefix ${CMAKE_PREFIX_PATH}")
endif()
# Options
set(ELONA_BUILD_TARGET "GAME" CACHE STRING "Build executable type (GAME, LAUNCHER, TESTS, or BENCH)")
if((ELONA_BUILD_TARGET STREQUAL "TESTS") OR (ELONA_BUILD_TARGET STREQUAL "BENCH"))
set(SNAIL_BACKEND "HEADLESS")
set(SPIDER_BACKEND "HEADLESS")
else()
set(SNAIL_BACKEND "SDL")
set(SPIDER_BACKEND "BOOST_ASIO")
endif()
# Executable
if(ELONA_BUILD_TARGET STREQUAL "TESTS")
set (CATCH_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/thirdparty/catch2})
add_library(Catch INTERFACE)
target_include_directories(Catch INTERFACE ${CATCH_INCLUDE_DIR})
# Test sources
set(TEST_SOURCES
src/tests/tests.cpp
src/tests/util.cpp
src/tests/config.cpp
src/tests/config_def.cpp
src/tests/filesystem.cpp
src/tests/lua_api.cpp
src/tests/lua_callbacks.cpp
src/tests/lua_events.cpp
src/tests/lua_exports.cpp
src/tests/lua_handles.cpp
src/tests/lua_mod_resolve.cpp
src/tests/lua_mods.cpp
src/tests/lua_data.cpp
src/tests/lua_data_character.cpp
src/tests/lua_data_item.cpp
src/tests/lua_serialization.cpp
src/tests/item.cpp
src/tests/i18n.cpp
src/tests/i18n_builtins.cpp
src/tests/i18n_regressions.cpp
src/tests/keybind_deserializer.cpp
src/tests/keybind_key_names.cpp
src/tests/keybind_manager.cpp
src/tests/keybind_serializer.cpp
src/tests/semver.cpp
src/tests/serialization.cpp
)
add_executable(${PROJECT_NAME} src/version.cpp ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME} Catch)
elseif(ELONA_BUILD_TARGET STREQUAL "BENCH")
add_subdirectory(src/thirdparty/hayai)
# Benchmark sources
set(BENCH_SOURCES
src/bench/ai.cpp
src/bench/generate.cpp
src/bench/i18n.cpp
src/bench/lua_callbacks.cpp
src/bench/magic.cpp
src/bench/serialization.cpp
src/bench/util.cpp
)
add_executable(${PROJECT_NAME} src/version.cpp ${BENCH_SOURCES})
target_link_libraries(${PROJECT_NAME} hayai_main ${LIB_TIMING})
elseif(ELONA_BUILD_TARGET STREQUAL "GAME")
if(WIN32)
set(RESOURCES "${PROJECT_SOURCE_DIR}/resources/icon_windows.rc")
endif()
add_executable(${PROJECT_NAME} WIN32 src/main.cpp src/version.cpp ${RESOURCES})
elseif(ELONA_BUILD_TARGET STREQUAL "LAUNCHER")
if(WIN32)
set(RESOURCES "${PROJECT_SOURCE_DIR}/resources/icon_windows.rc")
endif()
add_executable(${PROJECT_NAME} WIN32 src/launcher_main.cpp src/version.cpp ${RESOURCES})
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "Elona foobar Launcher")
else()
message(FATAL_ERROR "Unknown build mode: ${ELONA_BUILD_TARGET}")
endif()
if (NOT DEFINED ZLIB_INCLUDE_DIR OR NOT DEFINED ZLIB_LIBRARIES)
find_package(ZLIB REQUIRED)
endif()
include_directories(${ZLIB_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
if(WIN32)
# Force FindBoost to look for libraries with -mt-sgd
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
# Set default project to be run when using Visual Studio
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
# Specify dynamic libraries to copy to output folder after build
file(GLOB copy_sources "${CMAKE_PREFIX_PATH}/lib/*${CMAKE_SHARED_LIBRARY_SUFFIX}")
set(copy_dest "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
message("Will copy libraries to output folder.")
# Perform the copy
foreach(copy_source ${copy_sources})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E
copy_if_different ${copy_source} $<TARGET_FILE_DIR:${PROJECT_NAME}>)
endforeach()
endif()
# Thirdparty libraries
find_package(Boost REQUIRED COMPONENTS system filesystem)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
# NOTE: The prebuilt version of lua in the windows dependency
# submodule is patched with wstring support.
if(WIN32)
# FindLua does not support distinguishing debug/release builds.
set(LUA_INCLUDE_DIR ${CMAKE_PREFIX_PATH}/include)
set(LUA_LIBRARIES debug ${CMAKE_PREFIX_PATH}/lib/lua53d.lib optimized ${CMAKE_PREFIX_PATH}/lib/lua53.lib)
else()
if(NOT DEFINED LUA_INCLUDE_DIR OR NOT DEFINED LUA_LIBRARIES)
find_package(Lua REQUIRED)
endif()
endif()
include_directories(${LUA_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${LUA_LIBRARIES})
if(NOT DEFINED SDL2_INCLUDE_DIR OR NOT DEFINED SDL2_LIBRARIES)
find_package(SDL2 REQUIRED)
endif()
include_directories(${SDL2_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
if(NOT DEFINED SDL2_IMAGE_INCLUDE_DIR OR NOT DEFINED SDL2_IMAGE_LIBRARIES)
find_package(SDL2_image REQUIRED)
endif()
include_directories(${SDL2_IMAGE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SDL2_IMAGE_LIBRARIES})
if(NOT DEFINED SDL2_TTF_INCLUDE_DIR OR NOT DEFINED SDL2_TTF_LIBRARIES)
find_package(SDL2_ttf REQUIRED)
endif()
include_directories(${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SDL2_TTF_LIBRARIES})
if(NOT DEFINED SDL2_MIXER_INCLUDE_DIR OR NOT DEFINED SDL2_MIXER_LIBRARIES)
find_package(SDL2_mixer REQUIRED)
endif()
include_directories(${SDL2_MIXER_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${SDL2_MIXER_LIBRARIES})
if(LINUX)
target_link_libraries(${PROJECT_NAME} dl)
endif()
if(WIN32)
else()
target_link_libraries(${PROJECT_NAME} pthread)
endif()
if(NOT DEFINED OPENSSL_INCLUDE_DIR OR NOT DEFINED OPENSSL_LIBRARIES)
find_package(OpenSSL REQUIRED)
endif()
include_directories(${OPENSSL_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${OPENSSL_LIBRARIES})
set(GENERAL_OPTIONS)
set(DEBUG_OPTIONS)
set(RELEASE_OPTIONS)
set(LINKER_OPTIONS)
set(EXTRA_DEFINES)
set(DISABLED_WARNINGS)
set(WARNINGS_AS_ERRORS)
list(APPEND EXTRA_DEFINES
"BOOST_ALLOW_DEPRECATED_HEADERS" # https://github.com/boostorg/random/issues/49
# https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/using.html
# > With MSVC or Borland C++ you may want to add -DBOOST_DATE_TIME_NO_LIB and
# > -DBOOST_REGEX_NO_LIB to your project settings to disable autolinking of
# > the Boost.Date_Time and Boost.Regex libraries respectively.
"BOOST_DATE_TIME_NO_LIB"
"BOOST_REGEX_NO_LIB"
"SOL_CHECK_ARGUMENTS"
)
if(MSVC)
list(APPEND EXTRA_DEFINES "_UNICODE")
list(APPEND GENERAL_OPTIONS
"W4"
"source-charset:utf-8"
"MT"
"EHsc"
)
list(APPEND DEBUG_OPTIONS
"MP8"
"MTd"
"ZI"
"Od"
"bigobj"
)
list(APPEND RELEASE_OPTIONS
"MT"
)
list(APPEND DISABLED_WARNINGS
"4244" # 'conversion' conversion from 'type1' to 'type2', possible loss of data
"4267" # 'var' : conversion from 'size_t' to 'type', possible loss of data
"4456" # declaration of 'identifier' hides previous local declaration
"4459" # declaration of 'identifier' hides global declaration
"4996" # Call to 'std::equal' with parameters that may be unsafe
)
list(APPEND EXTRA_DEFINES
"BOOST_CONFIG_SUPPRESS_OUTDATED_MESSAGE"
)
foreach(opt ${DISABLED_WARNINGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd${opt}")
endforeach()
foreach(opt ${GENERAL_OPTIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /${opt}")
endforeach()
foreach(opt ${DEBUG_OPTIONS})
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /${opt}")
endforeach()
foreach(opt ${RELEASE_OPTIONS})
foreach(flag_var CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
set(${flag_var} "${${flag_var}} /${opt}")
endforeach()
endforeach()
else() # GCC or Clang
list(APPEND GENERAL_OPTIONS
"Wall"
"Wextra"
)
list(APPEND DEBUG_OPTIONS
"g"
"O0"
"DDEBUG"
)
if(ELONA_BUILD_TARGET STREQUAL "BENCH")
list(APPEND GENERAL_OPTIONS "g1")
endif()
list(APPEND DISABLED_WARNINGS
"deprecated-declarations"
)
list(APPEND WARNINGS_AS_ERRORS
"return-type"
)
# Speed up linking on Linux by using split DWARF
if (LINUX)
list(APPEND GENERAL_OPTIONS "gsplit-dwarf")
endif ()
# Use gold linker or lld if available
set(LINKER)
option(USE_GOLD_LINKER "Link with GNU gold" ON)
if (USE_GOLD_LINKER)
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -fuse-ld=gold -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
if(LD_VERSION MATCHES "GNU gold")
set(LINKER "gold")
endif()
endif()
option(USE_LLD_LINKER "Link with lld" ON)
if (USE_LLD_LINKER)
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -fuse-ld=lld -Wl,--version ERROR_QUIET OUTPUT_VARIABLE LD_VERSION)
if(LD_VERSION MATCHES "LLD")
set(LINKER "lld")
endif()
endif()
if (LINKER)
list(APPEND LINKER_OPTIONS "fuse-ld=${LINKER}")
message(STATUS "Using linker: ${LINKER}")
else()
message(STATUS "Using system linker.")
endif()
foreach(opt ${DISABLED_WARNINGS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-${opt}")
endforeach()
foreach(opt ${WARNINGS_AS_ERRORS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=${opt}")
endforeach()
foreach(opt ${GENERAL_OPTIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -${opt}")
endforeach()
foreach(opt ${DEBUG_OPTIONS})
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -${opt}")
endforeach()
foreach(opt ${RELEASE_OPTIONS})
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -${opt}")
endforeach()
foreach(opt ${LINKER_OPTIONS})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -${opt}")
endforeach()
endif()
foreach(opt ${EXTRA_DEFINES})
add_definitions("-D${opt}")
endforeach()
# My libraries: util
add_subdirectory(src/util)
target_link_libraries(${PROJECT_NAME} util)
# My libraries: snail
add_subdirectory(src/snail)
target_link_libraries(${PROJECT_NAME} ${SNAIL_LIBRARY})
# My libraries: spider
add_subdirectory(src/spider)
target_link_libraries(${PROJECT_NAME} ${SPIDER_LIBRARY})
add_subdirectory(src/thirdparty/uri)
target_link_libraries(${PROJECT_NAME} ${NETWORK_URI_LIBRARY})
# My libraries: elona
add_subdirectory(src/elona)
target_link_libraries(${PROJECT_NAME} elona ${SNAIL_LIBRARY} ${SPIDER_LIBRARY} util ${NETWORK_URI_LIBRARY})
if((ELONA_BUILD_TARGET STREQUAL "GAME") OR (ELONA_BUILD_TARGET STREQUAL "LAUNCHER"))
add_subdirectory(src/thirdparty/nfd)
target_link_libraries(${PROJECT_NAME} nfd)
endif()
add_subdirectory(src/thirdparty/minizip)
target_link_libraries(${PROJECT_NAME} minizip)
# Install target
if(APPLE)
set(ELONA_INSTALL_PATH "${CMAKE_CURRENT_LIST_DIR}/bin/bundled")
set(ELONA_INSTALL_APP "${ELONA_INSTALL_PATH}/${PROJECT_NAME}.app")
install(
TARGETS ${PROJECT_NAME}
BUNDLE
DESTINATION ${ELONA_INSTALL_PATH}
COMPONENT Runtime
)
install(CODE "
include(BundleUtilities)
fixup_bundle(\"${ELONA_INSTALL_APP}\" \"\" \"\")
" COMPONENT Runtime)
endif()
function(copy_nonexisting source target)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-Dsourcefile=${source}
-Ddestinationfile=${target}
-P ${CMAKE_MODULE_PATH}/copy_nonexisting.cmake)
endfunction()
function(copy_dir source target)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${source}
${target})
endfunction()
function(copy_mod_dirs mod_root_dir target)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND}
-Dmod_root_dir=${mod_root_dir}
-Dtarget=${target}
-P ${CMAKE_MODULE_PATH}/copy_mod_dirs.cmake)
endfunction()
function(copy_file source target)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${source}
${target})
endfunction()
function(remove_dir dir)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E remove_directory
${dir})
endfunction()
set(ASSET_DIR "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
set(RUNTIME_DIR "${ASSET_DIR}")
copy_dir(${CMAKE_SOURCE_DIR}/runtime/data/ "${RUNTIME_DIR}/data/")
copy_dir(${CMAKE_SOURCE_DIR}/runtime/font/ "${RUNTIME_DIR}/font/")
copy_dir(${CMAKE_SOURCE_DIR}/runtime/graphic/ "${RUNTIME_DIR}/graphic/")
copy_dir(${CMAKE_SOURCE_DIR}/runtime/map/ "${RUNTIME_DIR}/map/")
copy_mod_dirs(${CMAKE_SOURCE_DIR}/runtime/mod/ "${RUNTIME_DIR}/mod/")
copy_dir(${CMAKE_SOURCE_DIR}/runtime/profile/ "${RUNTIME_DIR}/profile/")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
# Overwrite the default config.json with tools/config.json.
copy_file("${CMAKE_SOURCE_DIR}/tools/config.json" "${RUNTIME_DIR}/profile/default/config.json")
endif()
# Copy assets from Elona 1.22 if they are present.
if(EXISTS "${CMAKE_PREFIX_PATH}/elona")
copy_dir("${CMAKE_PREFIX_PATH}/elona/graphic" "${ASSET_DIR}/graphic")
copy_dir("${CMAKE_PREFIX_PATH}/elona/sound" "${ASSET_DIR}/sound")
message("Elona 1.22 distribution found.")
else()
message(WARNING "Elona 1.22 distribution not found at ${CMAKE_PREFIX_PATH}/elona. The game cannot be run without assets. Please manually copy the 'graphic' and 'sound' directories from Elona 1.22 to the ${PROJECT_NAME} output path after building.")
endif()
if((ELONA_BUILD_TARGET STREQUAL "TESTS") OR (ELONA_BUILD_TARGET STREQUAL "BENCH"))
copy_dir(${CMAKE_SOURCE_DIR}/src/tests/data "$<TARGET_FILE_DIR:${PROJECT_NAME}>/tests/data")
endif()
if(ELONA_BUILD_TARGET STREQUAL "TESTS")
copy_dir(${CMAKE_SOURCE_DIR}/src/tests/lua "$<TARGET_FILE_DIR:${PROJECT_NAME}>/tests/lua")
endif()
remove_dir("$<TARGET_FILE_DIR:${PROJECT_NAME}>/profile/testing")