-
Notifications
You must be signed in to change notification settings - Fork 35
/
CMakeLists.txt
321 lines (279 loc) · 10.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
cmake_minimum_required(VERSION 3.7.2)
project(OpenOMF C)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake-scripts)
INCLUDE(CheckFunctionExists)
INCLUDE(CheckSymbolExists)
INCLUDE(FetchContent)
FetchContent_Declare(
libfixmath
GIT_REPOSITORY https://github.com/PetteriAimonen/libfixmath.git
GIT_TAG d308e466e1a09118d03f677c52e5fbf402f6fdd0
)
FetchContent_MakeAvailable(libfixmath)
set(VERSION_MAJOR "0")
set(VERSION_MINOR "6")
set(VERSION_PATCH "6")
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
# Options
OPTION(USE_TESTS "Build unittests" OFF)
OPTION(USE_TOOLS "Build tools" OFF)
OPTION(USE_SANITIZERS "Enable Asan and Ubsan" OFF)
OPTION(USE_TIDY "Use clang-tidy for checks" OFF)
OPTION(USE_FORMAT "Use clang-format for checks" OFF)
# These flags are used for all builds
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wformat -pedantic")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -DDEBUGMODE -Werror -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO} -g -O2 -fno-omit-frame-pointer -DNDEBUG")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O2 -DNDEBUG")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -Os -DNDEBUG")
add_definitions(-DV_MAJOR=${VERSION_MAJOR} -DV_MINOR=${VERSION_MINOR} -DV_PATCH=${VERSION_PATCH})
# See if we have Git, and use it to fetch current SHA1 hash
find_package(Git)
if(GIT_FOUND)
message(STATUS "Git found: ${GIT_EXECUTABLE}")
execute_process(
COMMAND ${GIT_EXECUTABLE} "rev-parse" "HEAD"
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE SHA1_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "Git SHA1 Hash: ${SHA1_HASH}")
add_definitions(-DSHA1_HASH="${SHA1_HASH}")
endif()
# System packages (hard dependencies)
find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)
find_package(xmp)
find_package(OpenGL REQUIRED)
find_package(Epoxy REQUIRED)
find_package(enet)
find_package(confuse)
find_package(argtable2)
find_package(argtable3)
find_package(PNG)
find_package(ZLIB)
# Check functions and generate platform configuration file
check_symbol_exists(strdup "string.h" HAVE_STD_STRDUP)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/platform.h.in ${CMAKE_CURRENT_BINARY_DIR}/src/platform.h)
# If tests are enabled, find CUnit
if(USE_TESTS)
find_package(CUnit)
endif()
# Only strip on GCC (clang does not appreciate)
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -Wl,-s")
set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL} -Wl,-s")
endif()
# Find OpenOMF core sources
file(GLOB_RECURSE OPENOMF_SRC RELATIVE ${CMAKE_SOURCE_DIR} "src/*/*.c")
set(COREINCS
src
${CMAKE_CURRENT_BINARY_DIR}/src/
${CMAKE_CURRENT_BINARY_DIR}/_deps/libfixmath-src
${SDL2_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIR}
${EPOXY_INCLUDE_DIR}
${CONFUSE_INCLUDE_DIR}
${XMP_INCLUDE_DIR}
${ENET_INCLUDE_DIR}
${PNG_INCLUDE_DIR}
${ZLIB_INCLUDE_DIR}
)
set(CORELIBS
${CMAKE_CURRENT_BINARY_DIR}/_deps/libfixmath-build/liblibfixmath.a
${CONFUSE_LIBRARY}
${XMP_LIBRARY}
${ENET_LIBRARY}
${PNG_LIBRARY}
${ZLIB_LIBRARY}
)
if(ARGTABLE2_FOUND)
set(COREINCS ${COREINCS} ${ARGTABLE2_INCLUDE_DIR})
add_compile_definitions(ARGTABLE2_FOUND)
elseif(ARGTABLE3_FOUND)
set(COREINCS ${COREINCS} ${ARGTABLE3_INCLUDE_DIR})
add_compile_definitions(ARGTABLE3_FOUND)
else()
message(FATAL_ERROR "Neither argtable2 or argtable3 was found")
endif()
if(ARGTABLE2_FOUND)
set(CORELIBS ${CORELIBS} ${ARGTABLE2_LIBRARY})
elseif(ARGTABLE3_FOUND)
set(CORELIBS ${CORELIBS} ${ARGTABLE3_LIBRARY})
endif()
# MingW build should add mingw32 lib
if(MINGW)
set(CORELIBS mingw32 ${CORELIBS})
endif()
# On windows, add winsock2 and winmm
if(WIN32)
set(CORELIBS ${CORELIBS} ws2_32 winmm)
endif()
# On unix platforms, add libm (sometimes needed, it seems)
if(UNIX)
SET(CORELIBS ${CORELIBS} m)
endif()
# Set include directories for all builds
include_directories(${COREINCS})
# Build core sources first as an object library
# this can then be reused in tests and main executable to speed things up
add_library(openomf_core OBJECT ${OPENOMF_SRC})
set(CORELIBS openomf_core ${CORELIBS})
# Set icon for windows executable
if(WIN32)
SET(ICON_RESOURCE "resources/icons/openomf.rc")
endif()
# Build the game binary
add_executable(openomf src/main.c src/engine.c ${ICON_RESOURCE})
# This is used to copy shader files from source directory to the binary output directory during compile.
add_custom_target(
copy_shaders ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_SOURCE_DIR}/shaders ${CMAKE_BINARY_DIR}/shaders
)
add_dependencies(openomf copy_shaders)
# Build tools if requested
set(TOOL_TARGET_NAMES)
if (USE_TOOLS)
add_executable(bktool tools/bktool/main.c
tools/shared/animation_misc.c
tools/shared/conversions.c)
add_executable(aftool tools/aftool/main.c
tools/shared/animation_misc.c
tools/shared/conversions.c)
add_executable(soundtool tools/soundtool/main.c)
add_executable(languagetool tools/languagetool/main.c)
add_executable(afdiff tools/afdiff/main.c)
add_executable(rectool tools/rectool/main.c tools/shared/pilot.c)
add_executable(pcxtool tools/pcxtool/main.c)
add_executable(pictool tools/pictool/main.c)
add_executable(scoretool tools/scoretool/main.c)
add_executable(trntool tools/trntool/main.c tools/shared/pilot.c)
add_executable(altpaltool tools/altpaltool/main.c)
add_executable(chrtool tools/chrtool/main.c tools/shared/pilot.c)
add_executable(setuptool tools/setuptool/main.c tools/shared/pilot.c)
add_executable(stringparser tools/stringparser/main.c)
list(APPEND TOOL_TARGET_NAMES
bktool
aftool
soundtool
languagetool
afdiff
rectool
pcxtool
pictool
scoretool
trntool
altpaltool
chrtool
setuptool
stringparser
)
message(STATUS "Development: CLI tools enabled")
else()
message(STATUS "Development: CLI tools disabled")
endif()
# Linting via clang-tidy
if(USE_TIDY)
set_target_properties(openomf PROPERTIES C_CLANG_TIDY "clang-tidy")
set_target_properties(openomf_core PROPERTIES C_CLANG_TIDY "clang-tidy")
foreach(TARGET ${TOOL_TARGET_NAMES})
set_target_properties(${TARGET} PROPERTIES C_CLANG_TIDY "clang-tidy")
endforeach()
message(STATUS "Development: clang-tidy enabled")
else()
message(STATUS "Development: clang-tidy disabled")
endif()
# Formatting via clang-format
if(USE_FORMAT)
include(ClangFormat)
file(
GLOB_RECURSE
SRC_FILES
RELATIVE ${CMAKE_SOURCE_DIR}
"src/*.h"
"src/*.c"
"tools/*.c"
"tools/*.h"
"testing/*.c"
"testing/*.h"
)
clangformat_setup(${SRC_FILES})
message(STATUS "Development: clang-format enabled")
else()
message(STATUS "Development: clang-format disabled")
endif()
# Enable AddressSanitizer if requested (these libs need to be first on the list!)
if(USE_SANITIZERS)
set(CORELIBS asan ubsan ${CORELIBS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,undefined")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address,undefined")
message(STATUS "Development: Asan and Ubsan enabled")
else()
message(STATUS "Development: Asan and Ubsan disabled")
endif()
if(MINGW)
# Don't show console on mingw in release builds (but enable if debug mode)
if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
set_target_properties(openomf PROPERTIES LINK_FLAGS "-mwindows")
else()
set_target_properties(openomf PROPERTIES LINK_FLAGS "-mconsole")
endif()
# Make sure console apps are always in terminal output mode.
foreach(TARGET ${TOOL_TARGET_NAMES})
set_target_properties(${TARGET} PROPERTIES LINK_FLAGS "-mconsole")
endforeach()
# Use static libgcc when on mingw
target_link_options(openomf PRIVATE -static-libgcc)
foreach(TARGET ${TOOL_TARGET_NAMES})
target_link_options(${TARGET} PRIVATE -static-libgcc)
endforeach()
endif()
# Make sure libraries are linked
target_link_libraries(openomf ${CORELIBS})
target_link_libraries(openomf SDL2::Main SDL2::Mixer OpenGL::GL Epoxy::Main)
target_link_libraries(openomf libfixmath)
foreach(TARGET ${TOOL_TARGET_NAMES})
target_link_libraries(${TARGET} ${CORELIBS})
target_link_libraries(${TARGET} SDL2::Main SDL2::Mixer OpenGL::GL Epoxy::Main)
endforeach()
# Testing stuff
if(CUNIT_FOUND)
enable_testing()
include_directories(${CUNIT_INCLUDE_DIR} testing/ src/)
SET(CORELIBS ${CORELIBS} ${CUNIT_LIBRARY})
file(GLOB_RECURSE TEST_SRC RELATIVE ${CMAKE_SOURCE_DIR} "testing/*.c")
add_executable(openomf_test_main ${TEST_SRC})
# This makes loading test resources possible
target_compile_definitions(openomf_test_main PRIVATE
TESTS_ROOT_DIR="${CMAKE_SOURCE_DIR}/testing")
target_link_libraries(openomf_test_main ${CORELIBS} SDL2::Main SDL2::Mixer OpenGL::GL Epoxy::Main)
if(MINGW)
# Always build as a console executable with mingw
set_target_properties(openomf_test_main PROPERTIES LINK_FLAGS "-mconsole")
endif()
add_test(main openomf_test_main)
message(STATUS "Development: Unit-tests are enabled")
else()
message(STATUS "Development: Unit-tests are disabled")
endif()
# Copy some resources to destination resources directory to ease development setup.
file(COPY resources/openomf.bk resources/gamecontrollerdb.txt DESTINATION resources)
# Installation
if(WIN32)
# On windows, generate a flat directory structure under openomf/ subdir.
# This way we have an "openomf/" root directory inside the zip file.
install(TARGETS openomf RUNTIME DESTINATION openomf/ COMPONENT Binaries)
install(FILES resources/openomf.bk resources/gamecontrollerdb.txt DESTINATION openomf/resources/ COMPONENT Data)
install(DIRECTORY shaders/ DESTINATION openomf/shaders COMPONENT Data)
install(FILES README.md BUILD.md LICENSE DESTINATION openomf/ COMPONENT Data)
else()
# On unixy systems, follow standard.
install(TARGETS openomf RUNTIME DESTINATION bin COMPONENT Binaries)
install(FILES resources/openomf.bk resources/gamecontrollerdb.txt resources/icons/openomf.png
DESTINATION share/games/openomf/
COMPONENT Data
)
install(DIRECTORY shaders/ DESTINATION share/games/openomf/shaders COMPONENT Data)
endif()