-
Notifications
You must be signed in to change notification settings - Fork 20
/
CMakeLists.txt
executable file
·570 lines (516 loc) · 12.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
cmake_minimum_required(VERSION 3.12)
###########
# Options #
###########
set(BACKEND "SDL2" CACHE STRING "Which backend to use (SDL2)")
option(REV01 "Compile REV01 ROM" ON)
option(JAPANESE "Compile Japanese ROM" OFF)
option(FIX_BUGS "Fix bugs (completely screwed up code, not gameplay bugs)" OFF)
option(SPLASH "Enable the SSRG splash screen (for my own demo releases)" OFF)
option(SANITIZE "Enable sanitization" OFF)
option(LTO "Enable link-time optimization" OFF)
option(MSVC_LINK_STATIC_RUNTIME "Link the static MSVC runtime library (Visual Studio only)" OFF)
#########
# Setup #
#########
# Define project, source, and includes
project(SoniCPort LANGUAGES C)
add_executable(SoniCPort WIN32
# Game
"src/Main.c"
"src/Constants.h"
"src/Macros.h"
"src/Kosinski.c"
"src/Kosinski.h"
"src/Nemesis.c"
"src/Nemesis.h"
"src/MathUtil.c"
"src/MathUtil.h"
"src/Game.c"
"src/Game.h"
"src/Demo.c"
"src/Demo.h"
"src/Video.c"
"src/Video.h"
"src/Palette.c"
"src/Palette.h"
"src/PLC.c"
"src/PLC.h"
"src/PaletteCycle.c"
"src/PaletteCycle.h"
"src/GM_Sega.c"
"src/GM_Sega.h"
"src/GM_Title.c"
"src/GM_Title.h"
"src/GM_Level.c"
"src/GM_Level.h"
"src/GM_Special.c"
"src/GM_Special.h"
"src/Level.c"
"src/Level.h"
"src/LevelDraw.c"
"src/LevelDraw.h"
"src/LevelScroll.c"
"src/LevelScroll.h"
"src/LevelCollision.c"
"src/LevelCollision.h"
"src/SpecialStage.c"
"src/SpecialStage.h"
"src/HUD.c"
"src/HUD.h"
"src/Object.c"
"src/Object.h"
"src/Object/Sonic.c"
"src/Object/Sonic.h"
"src/Object/SpecialSonic.c"
"src/Object/Signpost.c"
"src/Object/TitleSonic.c"
"src/Object/PSB.c"
"src/Object/GHZBridge.c"
"src/Object/Crabmeat.c"
"src/Object/HUD.c"
"src/Object/BuzzBomber.c"
"src/Object/Ring.c"
"src/Object/Monitor.c"
"src/Object/Explosion.c"
"src/Object/Chopper.c"
"src/Object/TitleCard.c"
"src/Object/Spikes.c"
"src/Object/ShieldInvincibility.c"
"src/Object/GameOverCard.c"
"src/Object/GHZRock.c"
"src/Object/Motobug.c"
"src/Object/Spring.c"
"src/Object/Newtron.c"
"src/Object/GHZEdge.c"
"src/Object/Credits.c"
# Backend
"src/Backend/MegaDrive.c"
"src/Backend/MegaDrive.h"
"src/Backend/VDP.c"
"src/Backend/VDP.h"
"src/Backend/Joypad.c"
"src/Backend/Joypad.h"
)
set(RESOURCES
"Icon"
"Art/SegaREV00"
"Art/SegaREV01"
"Art/JapaneseCredits"
"Art/TitleFG"
"Art/Sonic"
"Art/TitleSonic"
"Art/TitleTM"
"Art/TitleCard"
"Art/GameOver"
"Art/CreditsFont"
"Art/Lamppost"
"Art/HUD"
"Art/HUDLife"
"Art/Ring"
"Art/Points"
"Art/Monitor"
"Art/Shield"
"Art/Invincibility"
"Art/Explosion"
"Art/GHZStalk"
"Art/GHZRock"
"Art/Crabmeat"
"Art/BuzzBomber"
"Art/Chopper"
"Art/Newtron"
"Art/Motobug"
"Art/Spikes"
"Art/SpringH"
"Art/SpringV"
"Art/GHZSwing"
"Art/GHZBridge"
"Art/GHZLog"
"Art/GHZBall"
"Art/GHZWall1"
"Art/GHZWall2"
"Art/Bumper"
"Art/GHZ1"
"Art/GHZ2"
"Art/LZ"
"Art/MZ"
"Art/SLZ"
"Art/SYZ"
"Art/SBZ"
"Art/Signpost"
"Art/HiddenBonus"
"Art/BigFlash"
"Art/GHZWaterfall"
"Art/GHZFlowerLarge"
"Art/GHZFlowerSmall"
"Art/HUDNum"
"Art/LifeNum"
"Art/Text"
"Art/SSClouds"
"Art/SSBack"
"Art/SSWall"
"Art/SSGoal"
"Art/SSSpeed"
"Art/SSRotate"
"Art/SSLife"
"Art/SSTwinkle"
"Art/SSChecker"
"Art/SSGhost"
"Art/SSWarp"
"Art/SSGlass"
"Art/SSEmerald"
"Art/SSZone1"
"Art/SSZone2"
"Art/SSZone3"
"Art/SSZone4"
"Art/SSZone5"
"Art/SSZone6"
"Art/SSResultEmerald"
"Tilemap/SegaREV00"
"Tilemap/SegaREV01"
"Tilemap/JapaneseCredits"
"Tilemap/TitleFG"
"Palette/SegaBG"
"Palette/Title"
"Palette/LevelSel"
"Palette/Sonic"
"Palette/GHZ"
"Palette/LZ"
"Palette/MZ"
"Palette/SYZ"
"Palette/SLZ"
"Palette/SBZ1"
"Palette/Special"
"Palette/LZWater"
"Palette/SBZ3"
"Palette/SBZ3Water"
"Palette/SBZ2"
"Palette/SonicLZ"
"Palette/SonicSBZ"
"Palette/SSResults"
"Palette/Continue"
"Palette/Ending"
"Palette/Sega1"
"Palette/Sega2"
"Palette/TitleCycle"
"Palette/GHZCycle"
"Animation/Sonic"
"Animation/Signpost"
"Animation/TitleSonic"
"Animation/PSB"
"Animation/Crabmeat"
"Animation/BuzzBomber"
"Animation/BuzzMissile"
"Animation/Ring"
"Animation/Monitor"
"Animation/Chopper"
"Animation/ShieldInvincibility"
"Animation/Motobug"
"Animation/Spring"
"Animation/Newtron"
"Mappings/Sonic"
"Mappings/SonicDPLC"
"Mappings/Signpost"
"Mappings/TitleSonic"
"Mappings/PSB"
"Mappings/GHZBridge"
"Mappings/Crabmeat"
"Mappings/HUD"
"Mappings/BuzzBomber"
"Mappings/BuzzMissile"
"Mappings/BuzzExplode"
"Mappings/RingREV00"
"Mappings/RingREV01"
"Mappings/Monitor"
"Mappings/Explosion"
"Mappings/Chopper"
"Mappings/TitleCard"
"Mappings/Spikes"
"Mappings/ShieldInvincibility"
"Mappings/GameOver"
"Mappings/GHZRock"
"Mappings/Motobug"
"Mappings/Spring"
"Mappings/Newtron"
"Mappings/GHZEdge"
"Mappings/Bumper"
"Mappings/Credits"
"Mappings/SSRotate"
"Mappings/SSGlass"
"Mappings/SSUp"
"Mappings/SSDown"
"Mappings/SSEmerald"
"Mappings/SSWall"
"Mappings/SSResultEmerald"
"Layout/GHZ1"
"Layout/GHZ2"
"Layout/GHZ3"
"Layout/GHZBG"
"Layout/LZ1"
"Layout/LZ2"
"Layout/LZ3"
"Layout/LZBG"
"Layout/MZ1"
"Layout/MZ1BG"
"Layout/MZ2"
"Layout/MZ2BG"
"Layout/MZ3"
"Layout/MZ3BG"
"Layout/SLZ1"
"Layout/SLZ2"
"Layout/SLZ3"
"Layout/SLZBG"
"Layout/SYZ1"
"Layout/SYZ2"
"Layout/SYZ3"
"Layout/SYZBGREV00"
"Layout/SYZBGREV01"
"Layout/SBZ1"
"Layout/SBZ1BG"
"Layout/SBZ2"
"Layout/SBZ2BG"
"Layout/SBZ3"
"Layout/Ending"
"ObjectLayout/GHZ1"
"ObjectLayout/GHZ2"
"ObjectLayout/GHZ3REV00"
"ObjectLayout/GHZ3REV01"
"ObjectLayout/LZ1PF1"
"ObjectLayout/LZ1PF2"
"ObjectLayout/LZ1REV00"
"ObjectLayout/LZ1REV01"
"ObjectLayout/LZ2"
"ObjectLayout/LZ2PF1"
"ObjectLayout/LZ2PF2"
"ObjectLayout/LZ3PF1"
"ObjectLayout/LZ3PF2"
"ObjectLayout/LZ3REV00"
"ObjectLayout/LZ3REV01"
"ObjectLayout/MZ1REV00"
"ObjectLayout/MZ1REV01"
"ObjectLayout/MZ2"
"ObjectLayout/MZ3"
"ObjectLayout/SLZ1"
"ObjectLayout/SLZ2"
"ObjectLayout/SLZ3"
"ObjectLayout/SYZ1"
"ObjectLayout/SYZ2"
"ObjectLayout/SYZ3REV00"
"ObjectLayout/SYZ3REV01"
"ObjectLayout/SBZ1PF1"
"ObjectLayout/SBZ1PF2"
"ObjectLayout/SBZ1PF3"
"ObjectLayout/SBZ1PF4"
"ObjectLayout/SBZ1PF5"
"ObjectLayout/SBZ1PF6"
"ObjectLayout/SBZ1REV00"
"ObjectLayout/SBZ1REV01"
"ObjectLayout/SBZ2"
"ObjectLayout/SBZ3"
"ObjectLayout/FZ"
"ObjectLayout/Ending"
"Map16/GHZ"
"Map16/LZ"
"Map16/MZ"
"Map16/SBZ"
"Map16/SLZ"
"Map16/SYZ"
"Map256/GHZ"
"Map256/LZ"
"Map256/MZREV00"
"Map256/MZREV01"
"Map256/SBZREV00"
"Map256/SBZREV01"
"Map256/SLZ"
"Map256/SYZ"
"CollisionIndex/GHZ"
"CollisionIndex/LZ"
"CollisionIndex/MZ"
"CollisionIndex/SLZ"
"CollisionIndex/SYZ"
"CollisionIndex/SBZ"
"Collision/AngleMap"
"Collision/HeightMap"
"Collision/WidthMap"
"Demo/IntroGHZ"
"Demo/IntroMZ"
"Demo/IntroSYZ"
"Demo/IntroSS"
"Demo/EndingGHZ1"
"Demo/EndingMZ"
"Demo/EndingSYZ"
"Demo/EndingLZ"
"Demo/EndingSLZ"
"Demo/EndingSBZ1"
"Demo/EndingSBZ2"
"Demo/EndingGHZ2"
"SSLayout/1"
"SSLayout/2"
"SSLayout/3"
"SSLayout/4"
"SSLayout/5REV00"
"SSLayout/5REV01"
"SSLayout/6REV00"
"SSLayout/6REV01"
)
target_include_directories(SoniCPort PRIVATE "src")
##################
# Compiler flags #
##################
# Revision
if(REV01)
target_compile_definitions(SoniCPort PRIVATE SCP_REV01)
else()
target_compile_definitions(SoniCPort PRIVATE SCP_REV00)
endif()
# Region
if(JAPANESE)
target_compile_definitions(SoniCPort PRIVATE SCP_JP)
endif()
# Bug fixes
if(FIX_BUGS)
target_compile_definitions(SoniCPort PRIVATE SCP_FIX_BUGS)
endif()
# Sanitization
if(SANITIZE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Og -ggdb3 -fsanitize=address")
endif()
# Splash
if(SPLASH)
target_compile_definitions(SoniCPort PRIVATE SCP_SPLASH)
target_sources(SoniCPort PRIVATE
"src/GM_SSRG.c"
"src/GM_SSRG.h"
)
list(APPEND RESOURCES
"SSRG/ArtLink"
"SSRG/ArtMain"
"SSRG/ArtSonic"
"SSRG/ArtSquare"
"SSRG/MapLink"
"SSRG/MapMain"
"SSRG/MapSquare"
)
endif()
# Strip release builds
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
# Force warnings
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(SoniCPort PRIVATE /W4)
else()
target_compile_options(SoniCPort PRIVATE -Wall -Wextra -pedantic)
endif()
# Use LTO if requested
if(LTO)
include(CheckIPOSupported)
check_ipo_supported(RESULT result)
if(result)
set_target_properties(SoniCPort PROPERTIES INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()
endif()
##############################
# Other compile-time defines #
##############################
# Determine endianness
INCLUDE(TestBigEndian)
TEST_BIG_ENDIAN(ENDIAN)
if(ENDIAN)
target_compile_definitions(SoniCPort PRIVATE SCP_BIG_ENDIAN)
else()
target_compile_definitions(SoniCPort PRIVATE SCP_LIL_ENDIAN)
endif()
########
# MSVC #
########
# This is messy as hell, and has been replaced by CMAKE_MSVC_RUNTIME_LIBRARY,
# but that's a very recent CMake addition, so we're still doing it this way for now
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC" AND MSVC_LINK_STATIC_RUNTIME)
# Statically-link the CRT (vcpkg static libs do this)
foreach(flag_var CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
if(${flag_var} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
endif()
endforeach()
endif()
# Do some other MSVC fixes
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
# Disable warnings that normally fire up on MSVC when using "unsafe" functions instead of using MSVC's "safe" _s functions
target_compile_definitions(SoniCPort PRIVATE _CRT_SECURE_NO_WARNINGS)
# Make it so source files are recognized as UTF-8 by MSVC
target_compile_options(SoniCPort PRIVATE "/utf-8")
# Use `main` instead of `WinMain`
set_target_properties(SoniCPort PROPERTIES LINK_FLAGS "/ENTRY:mainCRTStartup")
endif()
##########################
# Further compiler setup #
##########################
# Force strict C99
set_target_properties(SoniCPort PROPERTIES
C_STANDARD 99
C_STANDARD_REQUIRED ON
C_EXTENSIONS OFF
)
# Set debug executable name
set_target_properties(SoniCPort PROPERTIES DEBUG_OUTPUT_NAME "SoniCPort_debug")
# Set output directory
set(BUILD_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
set_target_properties(SoniCPort PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${BUILD_DIRECTORY}
RUNTIME_OUTPUT_DIRECTORY_RELEASE ${BUILD_DIRECTORY}
RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL ${BUILD_DIRECTORY}
RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO ${BUILD_DIRECTORY}
RUNTIME_OUTPUT_DIRECTORY_DEBUG ${BUILD_DIRECTORY}
)
############
# Backends #
############
# Find PkgConfig for dependency linking
find_package(PkgConfig QUIET)
if(BACKEND MATCHES "SDL2")
target_compile_definitions(SoniCPort PRIVATE SCP_BACKEND_SDL2)
target_sources(SoniCPort PRIVATE
"src/Backend/SDL2/System.c"
"src/Backend/SDL2/Render.c"
"src/Backend/SDL2/Input.c"
)
# Compile and link SDL2
set(SDL_SHARED_ENABLED_BY_DEFAULT OFF)
if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(LIBC ON CACHE INTERNAL "" FORCE) # Needed to prevent possible 'symbol already defined' errors
endif()
add_subdirectory("lib/SDL" EXCLUDE_FROM_ALL)
target_link_libraries(SoniCPort PRIVATE SDL2-static)
endif()
#######################
# Resource conversion #
#######################
# Build bin2h externally, so it isn't cross-compiled when SoniCPort is (Emscripten, cross-GCC, MinGW on Linux, etc.)
include(ExternalProject)
ExternalProject_Add(bin2h
SOURCE_DIR "${CMAKE_SOURCE_DIR}/bin2h"
DOWNLOAD_COMMAND ""
UPDATE_COMMAND ""
BUILD_BYPRODUCTS "<INSTALL_DIR>/bin/bin2h"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE=Release
INSTALL_COMMAND
${CMAKE_COMMAND} --build . --config Release --target install
)
ExternalProject_Get_Property(bin2h INSTALL_DIR)
add_executable(bin2h_tool IMPORTED)
add_dependencies(bin2h_tool bin2h)
set_target_properties(bin2h_tool PROPERTIES IMPORTED_LOCATION "${INSTALL_DIR}/bin/bin2h")
# Convert resources to header files
foreach(FILENAME IN LISTS RESOURCES)
set(IN_DIR "${CMAKE_CURRENT_SOURCE_DIR}/res")
set(OUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/Resource")
get_filename_component(DIRECTORY "${FILENAME}" DIRECTORY)
add_custom_command(
OUTPUT "${OUT_DIR}/${FILENAME}.h"
COMMAND ${CMAKE_COMMAND} -E make_directory "${OUT_DIR}/${DIRECTORY}"
COMMAND bin2h_tool "${IN_DIR}/${FILENAME}" "${OUT_DIR}/${FILENAME}.h"
DEPENDS bin2h_tool "${IN_DIR}/${FILENAME}"
)
target_sources(SoniCPort PRIVATE "${OUT_DIR}/${FILENAME}.h")
endforeach()