-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
617 lines (544 loc) · 26.3 KB
/
Copy pathCMakeLists.txt
File metadata and controls
617 lines (544 loc) · 26.3 KB
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
cmake_minimum_required(VERSION 3.21)
if(PLATFORM STREQUAL "ps3")
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ppu.cmake)
endif()
project(butterscotch C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
add_compile_options(-Wall -Wextra -Werror)
set(PLATFORM "desktop" CACHE STRING "Platform backend")
set(DESKTOP_BACKEND "" CACHE STRING "Desktop platform backend")
set(AUDIO_BACKEND "" CACHE STRING "Audio backend")
if(PLATFORM STREQUAL "desktop" AND DESKTOP_BACKEND STREQUAL "")
message(FATAL_ERROR "You need to set a DESKTOP_BACKEND!")
endif()
# Build metadata
set(BUTTERSCOTCH_COMMIT_HASH "unknown" CACHE STRING "Git commit hash")
set(BUTTERSCOTCH_COMMIT_DATE "unknown" CACHE STRING "Git commit date")
add_compile_definitions(
BUTTERSCOTCH_COMMIT_HASH="${BUTTERSCOTCH_COMMIT_HASH}"
BUTTERSCOTCH_COMMIT_DATE="${BUTTERSCOTCH_COMMIT_DATE}"
)
# Audio backend defaults
if(AUDIO_BACKEND STREQUAL "")
if(PLATFORM STREQUAL "web")
set(AUDIO_BACKEND "web")
elseif(PLATFORM STREQUAL "web-meta")
set(AUDIO_BACKEND "none")
elseif(PLATFORM STREQUAL "ps3")
set(AUDIO_BACKEND "openal")
elseif(PLATFORM STREQUAL "ps2")
set(AUDIO_BACKEND "ps2")
else()
set(AUDIO_BACKEND "miniaudio")
endif()
endif()
if(AUDIO_BACKEND STREQUAL "openal")
file(GLOB AUDIO_SOURCES src/audio/openal/*.c)
elseif(AUDIO_BACKEND STREQUAL "miniaudio")
file(GLOB AUDIO_SOURCES src/audio/miniaudio/*.c)
elseif(AUDIO_BACKEND STREQUAL "web")
file(GLOB AUDIO_SOURCES src/audio/web/*.c src/audio/miniaudio/*.c)
elseif(AUDIO_BACKEND STREQUAL "ps2")
file(GLOB AUDIO_SOURCES src/audio/ps2/*.c)
elseif(NOT AUDIO_BACKEND STREQUAL "none")
message(FATAL_ERROR "Invalid AUDIO_BACKEND!")
endif()
# To avoid unused code in production builds that are only used for a single game (example: PlayStation 2 target), we can enable/disable specific features
#
# If only a single WAD version is enabled, the checks will be collapsed and removed by the compiler during build time, reducing code size and improving
# performance on low end hardware!
option(ENABLE_WAD14 "Enable support for WAD Version 14 and below" ON)
option(ENABLE_WAD16 "Enable support for WAD Version 15/16" ON)
option(ENABLE_WAD17 "Enable support for WAD Version 17" ON)
option(ENABLE_LEGACY_GL "Enable the legacy OpenGL renderer" ON)
option(ENABLE_MODERN_GL "Enable the modern OpenGL renderer" ON)
if(NOT ENABLE_WAD14 AND NOT ENABLE_WAD16 AND NOT ENABLE_WAD17)
message(FATAL_ERROR "You need to build Butterscotch with at least one WAD version enabled!")
endif()
if(ENABLE_WAD14)
add_compile_definitions(ENABLE_WAD14)
endif()
if(ENABLE_WAD16)
add_compile_definitions(ENABLE_WAD16)
endif()
if(ENABLE_WAD17)
add_compile_definitions(ENABLE_WAD17)
endif()
if(ENABLE_LEGACY_GL)
add_compile_definitions(ENABLE_LEGACY_GL)
endif()
if(ENABLE_MODERN_GL)
add_compile_definitions(ENABLE_MODERN_GL)
endif()
if(PLATFORM STREQUAL "web-meta")
# Metadata-only build: just the data.win parser and its direct deps. No VM, no runner, no I/O subsystems.
set(SOURCES
src/data_win.c
src/binary_reader.c
)
else()
file(GLOB SOURCES src/*.c)
endif()
# Platform specific files builds
file(GLOB PLATFORM_SOURCES src/${PLATFORM}/*.c)
# OpenGL ES 3.0 / WebGL2 compatibility
option(ENABLE_GLES "Build the gl_renderer against OpenGL ES 3.0 / WebGL2 instead of desktop GL 4.1 Core" OFF)
if (ENABLE_GLES AND ENABLE_LEGACY_GL)
set(ENABLE_LEGACY_GL OFF)
endif()
add_executable(butterscotch ${SOURCES} ${PLATFORM_SOURCES} ${AUDIO_SOURCES})
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src)
if(AUDIO_BACKEND STREQUAL "openal")
target_include_directories(butterscotch PUBLIC src/audio/openal)
add_compile_definitions(USE_OPENAL)
if(APPLE)
set(AUDIO_LIBRARIES "-framework OpenAL")
elseif(NOT PLATFORM STREQUAL "ps3")
find_package(OpenAL REQUIRED)
target_include_directories(butterscotch PUBLIC "${OPENAL_INCLUDE_DIR}")
set(AUDIO_LIBRARIES "${OPENAL_LIBRARY}")
endif()
elseif(AUDIO_BACKEND STREQUAL "miniaudio")
target_include_directories(butterscotch PUBLIC vendor/miniaudio)
target_include_directories(butterscotch PUBLIC src/audio/miniaudio)
add_compile_definitions(USE_MINIAUDIO)
set(AUDIO_LIBRARIES pthread)
elseif(AUDIO_BACKEND STREQUAL "web")
target_include_directories(butterscotch PUBLIC vendor/miniaudio)
target_include_directories(butterscotch PUBLIC src/audio/web)
target_include_directories(butterscotch PUBLIC src/audio/miniaudio)
add_compile_definitions(USE_WEB_AUDIO)
elseif(AUDIO_BACKEND STREQUAL "ps2")
target_include_directories(butterscotch PUBLIC src/audio/ps2)
add_compile_definitions(USE_PS2_AUDIO)
endif()
# stb_ds
add_library(stb_ds INTERFACE)
target_include_directories(stb_ds SYSTEM INTERFACE ${CMAKE_SOURCE_DIR}/vendor/stb/ds)
# stb_vorbis
add_library(stb_vorbis INTERFACE)
target_include_directories(stb_vorbis SYSTEM INTERFACE ${CMAKE_SOURCE_DIR}/vendor/stb/vorbis)
# RFC 1321 reference MD5 implementation
target_sources(butterscotch PRIVATE vendor/md5/md5.c)
target_include_directories(butterscotch PUBLIC vendor/md5)
# Steve Reid's SHA1 implementation
add_library(sha1 STATIC vendor/sha1/sha1.c)
target_include_directories(sha1 SYSTEM PUBLIC vendor/sha1)
target_compile_options(sha1 PRIVATE -w)
# Base64 implementation
target_sources(butterscotch PRIVATE vendor/base64/base64.c)
target_include_directories(butterscotch PUBLIC vendor/base64)
# bzip2
add_library(bzip2 STATIC
vendor/bzip2/blocksort.c
vendor/bzip2/bzlib.c
vendor/bzip2/compress.c
vendor/bzip2/crctable.c
vendor/bzip2/decompress.c
vendor/bzip2/huffman.c
vendor/bzip2/randtable.c
)
target_include_directories(bzip2 PUBLIC vendor/bzip2)
target_compile_options(bzip2 PRIVATE -w)
if(PLATFORM STREQUAL "desktop")
# Matches the GitHub Actions container flags (FORTIFY requires building with optimizations enabled!)
target_compile_options(butterscotch PRIVATE "$<$<CONFIG:Release,RelWithDebInfo>:-U_FORTIFY_SOURCE;-D_FORTIFY_SOURCE=2>")
if(NOT ENABLE_LEGACY_GL AND NOT ENABLE_MODERN_GL)
message(FATAL_ERROR "You must enable at least one renderer!")
endif()
file(GLOB GL_SOURCES src/image/*.c src/gl_common/*.c)
if(ENABLE_LEGACY_GL)
file(GLOB GL_SOURCES ${GL_SOURCES} src/gl_legacy/*.c)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl_legacy)
endif()
if(ENABLE_MODERN_GL)
file(GLOB GL_SOURCES ${GL_SOURCES} src/gl/*.c)
endif()
target_sources(butterscotch PRIVATE ${GL_SOURCES})
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl_common)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/image)
# Butterscotch VM/interpreter profiler
option(ENABLE_VM_GML_PROFILER "Enable Butterscotch VM/interpreter profiler" ON)
if(ENABLE_VM_GML_PROFILER)
add_compile_definitions(ENABLE_VM_GML_PROFILER)
endif()
# Butterscotch VM/interpreter tracing
option(ENABLE_VM_TRACING "Enable Butterscotch VM/interpreter tracing checks" ON)
if(ENABLE_VM_TRACING)
add_compile_definitions(ENABLE_VM_TRACING)
endif()
# Butterscotch VM opcode count profiler
option(ENABLE_VM_OPCODE_PROFILER "Enable Butterscotch VM opcode count profiler" ON)
if(ENABLE_VM_OPCODE_PROFILER)
add_compile_definitions(ENABLE_VM_OPCODE_PROFILER)
endif()
# Spatial grid logs
option(ENABLE_SPATIAL_GRID_LOGS "Enable Spatial Grid logs" OFF)
if(ENABLE_SPATIAL_GRID_LOGS)
add_compile_definitions(ENABLE_SPATIAL_GRID_LOGS)
endif()
# VM unknown/stubbed function logs
option(ENABLE_VM_STUB_LOGS "Enable VM unknown/stubbed function logs" ON)
if(ENABLE_VM_STUB_LOGS)
add_compile_definitions(ENABLE_VM_STUB_LOGS)
endif()
# GLAD
if(ENABLE_GLES)
add_compile_definitions(ENABLE_GLES)
add_library(glad STATIC vendor/glad-gles/src/glad.c)
target_include_directories(glad PUBLIC vendor/glad-gles/include)
else()
add_library(glad STATIC vendor/glad/src/glad.c)
target_include_directories(glad PUBLIC vendor/glad/include)
endif()
# stb_image
target_include_directories(butterscotch PUBLIC vendor/stb/image)
set(PLATFORM_LIBRARIES glad m bzip2 stb_ds sha1 stb_vorbis)
if(MINGW)
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} opengl32 gdi32 winmm)
elseif(NOT CMAKE_SYSTEM_NAME STREQUAL "Haiku")
set(PLATFORM_LIBRARIES ${PLATFORM_LIBRARIES} dl)
endif()
if(DESKTOP_BACKEND STREQUAL "glfw3")
add_compile_definitions(USE_GLFW3)
if(MINGW)
find_package(glfw3 REQUIRED)
# Target Windows 7+ (avoids referencing newer APIs like MapViewOfFileNuma2 that MinGW import libs don't provide)
# https://github.com/mirror/mingw-w64/blob/master/mingw-w64-headers/include/sdkddkver.h
target_compile_definitions(butterscotch PRIVATE _WIN32_WINNT=0x0601 NTDDI_VERSION=0x06010000 WIN32_LEAN_AND_MEAN)
target_link_options(butterscotch PRIVATE -static)
set(BACKEND_LIBRARIES glfw3 opengl32 gdi32 winmm)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW3 REQUIRED glfw3)
target_include_directories(butterscotch PRIVATE ${GLFW3_INCLUDE_DIRS})
target_link_directories(butterscotch PRIVATE ${GLFW3_LIBRARY_DIRS})
set(BACKEND_LIBRARIES ${GLFW3_LIBRARIES})
endif()
#gamecontrollerdb
add_custom_command(TARGET butterscotch POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${CMAKE_SOURCE_DIR}/vendor/gamecontrollerdb.txt
$<TARGET_FILE_DIR:butterscotch>/gamecontrollerdb.txt
)
elseif(DESKTOP_BACKEND STREQUAL "glfw2")
add_compile_definitions(USE_GLFW2)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW2 REQUIRED libglfw)
target_include_directories(butterscotch PRIVATE ${GLFW2_INCLUDE_DIRS})
target_link_directories(butterscotch PRIVATE ${GLFW2_LIBRARY_DIRS})
set(BACKEND_LIBRARIES ${GLFW2_LIBRARIES})
elseif(DESKTOP_BACKEND STREQUAL "sdl1")
add_compile_definitions(USE_SDL1)
find_package(SDL REQUIRED)
target_include_directories(butterscotch PUBLIC ${SDL_INCLUDE_DIR})
set(BACKEND_LIBRARIES ${SDL_LIBRARY})
elseif(DESKTOP_BACKEND STREQUAL "sdl2")
add_compile_definitions(USE_SDL2)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL2 REQUIRED sdl2)
target_include_directories(butterscotch PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_directories(butterscotch PRIVATE ${SDL2_LIBRARY_DIRS})
set(BACKEND_LIBRARIES ${SDL2_LIBRARIES})
elseif(DESKTOP_BACKEND STREQUAL "sdl3")
add_compile_definitions(USE_SDL3)
find_package(PkgConfig REQUIRED)
pkg_check_modules(SDL3 REQUIRED sdl3)
target_include_directories(butterscotch PRIVATE ${SDL3_INCLUDE_DIRS})
target_link_directories(butterscotch PRIVATE ${SDL3_LIBRARY_DIRS})
set(BACKEND_LIBRARIES ${SDL3_LIBRARIES})
endif()
target_link_libraries(butterscotch PRIVATE ${BACKEND_LIBRARIES} ${AUDIO_LIBRARIES} ${PLATFORM_LIBRARIES})
target_sources(butterscotch PRIVATE src/desktop/backends/${DESKTOP_BACKEND}.c)
# Enable AddressSanitizer by default in Debug builds
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(ENABLE_ASAN_DEFAULT ON)
else()
set(ENABLE_ASAN_DEFAULT OFF)
endif()
set(ENABLE_ASAN ${ENABLE_ASAN_DEFAULT} CACHE BOOL "Enable AddressSanitizer")
if(ENABLE_ASAN AND NOT MINGW AND NOT CMAKE_SYSTEM_NAME STREQUAL "Haiku")
target_compile_options(butterscotch PRIVATE -fsanitize=address -fno-omit-frame-pointer)
target_link_options(butterscotch PRIVATE -fsanitize=address)
endif()
elseif(PLATFORM STREQUAL "web")
if (NOT ENABLE_MODERN_GL)
message(FATAL_ERROR "Web requires modern gl!")
endif()
file(GLOB GL_SOURCES src/gl/*.c src/gl_common/*.c src/image/*.c)
target_sources(butterscotch PRIVATE ${GL_SOURCES})
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl_common)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/image)
add_compile_definitions(ENABLE_GLES)
# stb_image
target_include_directories(butterscotch PUBLIC vendor/stb/image)
# bzip2
target_link_libraries(butterscotch PRIVATE bzip2 stb_ds sha1 stb_vorbis)
set_target_properties(butterscotch PROPERTIES SUFFIX ".mjs")
target_link_options(
butterscotch
PRIVATE
"-sMODULARIZE=1"
"-sEXPORT_ES6=1"
"-sENVIRONMENT=worker"
"-sWASMFS=1"
"-sEXPORTED_RUNTIME_METHODS=['HEAP8', 'HEAPU8', 'HEAPF32', 'ccall', 'cwrap', 'specialHTMLTargets', 'UTF8ToString']"
"-sEXPORTED_FUNCTIONS=['_main', '_mountOpfs', '_startRunner', '_stopRunner', '_malloc', '_free', '_getKeyDownPtr', '_getKeyUpPtr', '_getKeyCount', '_setAudioSampleRate', '_pullAudioFrames']"
"-sOFFSCREENCANVAS_SUPPORT=1"
"-sOFFSCREEN_FRAMEBUFFER=1"
"-sMIN_WEBGL_VERSION=2"
"-sMAX_WEBGL_VERSION=2"
"-sFULL_ES3=1"
"-sALLOW_MEMORY_GROWTH"
"-sSHARED_MEMORY=1"
"-sPTHREAD_POOL_SIZE=4"
"-pthread"
)
target_compile_options(butterscotch PRIVATE "-pthread")
elseif(PLATFORM STREQUAL "web-meta")
set_target_properties(butterscotch PROPERTIES
SUFFIX ".mjs"
OUTPUT_NAME "butterscotch-meta"
)
target_link_libraries(butterscotch PRIVATE stb_ds)
target_link_options(
butterscotch
PRIVATE
"-sMODULARIZE=1"
"-sEXPORT_ES6=1"
"-sENVIRONMENT=web"
"-sEXPORTED_RUNTIME_METHODS=['HEAPU8', 'ccall', 'cwrap', 'UTF8ToString', 'FS']"
"-sEXPORTED_FUNCTIONS=['_main', '_parseDataWin', '_freeDataWin', '_getGameName', '_getGameDisplayName', '_getMajorVersion', '_getMinorVersion', '_getRelease', '_getBuild', '_getDefaultWindowWidth', '_getDefaultWindowHeight', '_malloc', '_free']"
"-sALLOW_MEMORY_GROWTH=1"
"-sFORCE_FILESYSTEM=1"
)
elseif(PLATFORM STREQUAL "ps2")
file(GLOB DEBUG_FONT_SOURCES src/debug_font/*.c)
target_sources(butterscotch PRIVATE ${DEBUG_FONT_SOURCES})
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/debug_font)
# The PS2 EE has a single-precision FPU only, so double is software-emulated
add_compile_definitions(USE_FLOAT_REALS)
# By disabling Int64 support, we can fit the entire RValue struct in 8 bytes, improving performance and memory usage
add_compile_definitions(NO_RVALUE_INT64)
# Butterscotch VM/interpreter profiler
option(ENABLE_VM_GML_PROFILER "Enable Butterscotch VM/interpreter profiler" OFF)
if(ENABLE_VM_GML_PROFILER)
add_compile_definitions(ENABLE_VM_GML_PROFILER)
endif()
# Butterscotch VM/interpreter tracing
option(ENABLE_VM_TRACING "Enable Butterscotch VM/interpreter tracing checks" OFF)
if(ENABLE_VM_TRACING)
add_compile_definitions(ENABLE_VM_TRACING)
endif()
# Butterscotch VM opcode count profiler
option(ENABLE_VM_OPCODE_PROFILER "Enable Butterscotch VM opcode count profiler" OFF)
if(ENABLE_VM_OPCODE_PROFILER)
add_compile_definitions(ENABLE_VM_OPCODE_PROFILER)
endif()
# Spatial grid logs
option(ENABLE_SPATIAL_GRID_LOGS "Enable Spatial Grid logs" OFF)
if(ENABLE_SPATIAL_GRID_LOGS)
add_compile_definitions(ENABLE_SPATIAL_GRID_LOGS)
endif()
# VM unknown/stubbed function logs
option(ENABLE_VM_STUB_LOGS "Enable VM unknown/stubbed function logs" OFF)
if(ENABLE_VM_STUB_LOGS)
add_compile_definitions(ENABLE_VM_STUB_LOGS)
endif()
# Platform identifier for PS2-specific code paths (e.g., scratchpad allocation)
add_compile_definitions(PLATFORM_PS2)
# PS2 renderer hot path logs
option(ENABLE_PS2_RENDERER_LOGS "Enable hot path logs in the PS2 renderer" OFF)
if(ENABLE_PS2_RENDERER_LOGS)
add_compile_definitions(ENABLE_PS2_RENDERER_LOGS)
endif()
# -fsingle-precision-constant: Treat unsuffixed float constants as single-precision to avoid accidental float->double promotions on platforms with no hardware double
# -Werror=double-promotion: Warn when something is being promoted to a double
# -fomit-frame-pointer: frees a register on a target with tight register pressure
# -ffast-math: enables aggressive floating point optimizations
# -Wno-format: because there are too many formatting issues that, at the end of the day, do not matter
target_compile_options(butterscotch PRIVATE -fsingle-precision-constant -Werror=double-promotion -fomit-frame-pointer -ffast-math -Wno-format)
if(PS2)
# Compiling on the Docker container
target_include_directories(butterscotch PRIVATE $ENV{PS2SDK}/ee/include $ENV{PS2SDK}/common/include $ENV{PS2DEV}/ee/include $ENV{GSKIT}/include)
target_link_directories(butterscotch PRIVATE $ENV{PS2SDK}/ee/lib $ENV{GSKIT}/lib)
target_link_libraries(${AUDIO_LIBRARIES} butterscotch gskit dmakit audsrv pad mc kbd patches kernel c m stb_ds sha1)
# Embed ps2sdk IRX modules (freesio2, freepad) as C arrays via bin2c
# This avoids loading from rom0: which has compatibility issues between fat/slim PS2
set(IRX_DIR $ENV{PS2SDK}/iop/irx)
set(IRX_GEN_DIR ${CMAKE_BINARY_DIR}/generated/irx)
file(MAKE_DIRECTORY ${IRX_GEN_DIR})
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/freesio2_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/freesio2.irx ${IRX_GEN_DIR}/freesio2_irx.c freesio2_irx
DEPENDS ${IRX_DIR}/freesio2.irx
COMMENT "Embedding freesio2.irx"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/mcman_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/mcman.irx ${IRX_GEN_DIR}/mcman_irx.c mcman_irx
DEPENDS ${IRX_DIR}/mcman.irx
COMMENT "Embedding mcman.irx"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/mcserv_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/mcserv.irx ${IRX_GEN_DIR}/mcserv_irx.c mcserv_irx
DEPENDS ${IRX_DIR}/mcserv.irx
COMMENT "Embedding mcserv.irx"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/freepad_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/freepad.irx ${IRX_GEN_DIR}/freepad_irx.c freepad_irx
DEPENDS ${IRX_DIR}/freepad.irx
COMMENT "Embedding freepad.irx"
)
# We need the USB mass storage IRX modules for writing gmon.out when not running from host: and for keyboard support
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/usbd_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/usbd.irx ${IRX_GEN_DIR}/usbd_irx.c usbd_irx
DEPENDS ${IRX_DIR}/usbd.irx
COMMENT "Embedding usbd.irx"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/ps2kbd_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/ps2kbd.irx ${IRX_GEN_DIR}/ps2kbd_irx.c ps2kbd_irx
DEPENDS ${IRX_DIR}/ps2kbd.irx
COMMENT "Embedding ps2kbd.irx"
)
if(AUDIO_BACKEND STREQUAL "ps2")
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/freesd_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/freesd.irx ${IRX_GEN_DIR}/freesd_irx.c freesd_irx
DEPENDS ${IRX_DIR}/freesd.irx
COMMENT "Embedding freesd.irx"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/audsrv_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/audsrv.irx ${IRX_GEN_DIR}/audsrv_irx.c audsrv_irx
DEPENDS ${IRX_DIR}/audsrv.irx
COMMENT "Embedding audsrv.irx"
)
endif()
target_sources(butterscotch PRIVATE ${IRX_GEN_DIR}/freesio2_irx.c ${IRX_GEN_DIR}/mcman_irx.c ${IRX_GEN_DIR}/mcserv_irx.c ${IRX_GEN_DIR}/freepad_irx.c ${IRX_GEN_DIR}/usbd_irx.c ${IRX_GEN_DIR}/ps2kbd_irx.c)
if(AUDIO_BACKEND STREQUAL "ps2")
target_sources(butterscotch PRIVATE ${IRX_GEN_DIR}/freesd_irx.c ${IRX_GEN_DIR}/audsrv_irx.c)
endif()
target_include_directories(butterscotch PRIVATE ${IRX_GEN_DIR})
# ===[ gprof Profiling Support ]===
option(ENABLE_GPROF_PROFILING "Enable gprof profiling for PS2" OFF)
if(ENABLE_GPROF_PROFILING)
add_compile_definitions(GPROF_PROFILING)
target_compile_options(butterscotch PRIVATE -pg)
target_link_options(butterscotch PRIVATE -pg)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/bdm_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/bdm.irx ${IRX_GEN_DIR}/bdm_irx.c bdm_irx
DEPENDS ${IRX_DIR}/bdm.irx
COMMENT "Embedding bdm.irx (profiler)"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/bdmfs_fatfs_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/bdmfs_fatfs.irx ${IRX_GEN_DIR}/bdmfs_fatfs_irx.c bdmfs_fatfs_irx
DEPENDS ${IRX_DIR}/bdmfs_fatfs.irx
COMMENT "Embedding bdmfs_fatfs.irx (profiler)"
)
add_custom_command(
OUTPUT ${IRX_GEN_DIR}/usbmass_bd_irx.c
COMMAND $ENV{PS2SDK}/bin/bin2c ${IRX_DIR}/usbmass_bd.irx ${IRX_GEN_DIR}/usbmass_bd_irx.c usbmass_bd_irx
DEPENDS ${IRX_DIR}/usbmass_bd.irx
COMMENT "Embedding usbmass_bd.irx (profiler)"
)
target_sources(butterscotch PRIVATE
${IRX_GEN_DIR}/bdm_irx.c
${IRX_GEN_DIR}/bdmfs_fatfs_irx.c
${IRX_GEN_DIR}/usbmass_bd_irx.c
)
endif()
else()
# This block is for when we are developing Butterscotch with CLion!
# ps2sdk and gsKit scatter headers across per-module subdirectories;
# the flat include/ layout only exists after building the SDK.
# We use file(GLOB) to discover all include dirs automatically.
file(GLOB PS2SDK_EE_INCLUDES ${CMAKE_SOURCE_DIR}/ps2sdk/ee/*/include)
file(GLOB PS2SDK_EE_RPC_INCLUDES ${CMAKE_SOURCE_DIR}/ps2sdk/ee/rpc/*/include)
file(GLOB PS2SDK_EE_NETWORK_INCLUDES ${CMAKE_SOURCE_DIR}/ps2sdk/ee/network/*/include)
file(GLOB GSKIT_INCLUDES ${CMAKE_SOURCE_DIR}/gsKit/ee/*/include)
# _EE is normally set by the PS2 toolchain; without it, tamtypes.h
# refuses to define u64/u32/etc. We set it so CLion can resolve them.
target_compile_definitions(butterscotch PRIVATE _EE)
target_include_directories(butterscotch PRIVATE
${PS2SDK_EE_INCLUDES}
${PS2SDK_EE_RPC_INCLUDES}
${PS2SDK_EE_NETWORK_INCLUDES}
${CMAKE_SOURCE_DIR}/ps2sdk/common/include
${GSKIT_INCLUDES}
# Newlib and GCC built-in headers are build artifacts (not in any repo),
# so these are still extracted from Docker by setup.sh.
${CMAKE_SOURCE_DIR}/ps2dev-headers/newlib-include
${CMAKE_SOURCE_DIR}/ps2dev-headers/gcc-include
)
endif()
elseif(PLATFORM STREQUAL "ps3")
if (NOT ENABLE_LEGACY_GL)
message(FATAL_ERROR "PS3 requires legacy gl!")
endif()
file(GLOB GL_SOURCES src/gl_common/*.c src/gl_legacy/*.c src/image/*.c)
file(GLOB DEBUG_FONT_SOURCES src/debug_font/*.c)
target_sources(butterscotch PRIVATE ${GL_SOURCES} ${DEBUG_FONT_SOURCES})
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl_legacy)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/image)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/debug_font)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/ps3)
target_include_directories(butterscotch PRIVATE ${CMAKE_SOURCE_DIR}/src/gl_common)
# ps3gl
file(GLOB PS3GL_SOURCES vendor/ps3gl/*.c)
add_library(ps3gl STATIC ${PS3GL_SOURCES})
target_include_directories(ps3gl SYSTEM PUBLIC vendor/ps3gl)
target_compile_options(ps3gl PRIVATE -w)
# mojoal
add_library(mojoal STATIC vendor/mojoal/mojoal.c)
target_include_directories(mojoal SYSTEM PUBLIC vendor/mojoal)
target_compile_definitions(mojoal PRIVATE AL_LIBTYPE_STATIC)
target_compile_options(mojoal PRIVATE -w)
# Butterscotch VM/interpreter profiler
option(ENABLE_VM_GML_PROFILER "Enable Butterscotch VM/interpreter profiler" OFF)
if(ENABLE_VM_GML_PROFILER)
add_compile_definitions(ENABLE_VM_GML_PROFILER)
endif()
# Butterscotch VM/interpreter tracing
option(ENABLE_VM_TRACING "Enable Butterscotch VM/interpreter tracing checks" OFF)
if(ENABLE_VM_TRACING)
add_compile_definitions(ENABLE_VM_TRACING)
endif()
# Butterscotch VM opcode count profiler
option(ENABLE_VM_OPCODE_PROFILER "Enable Butterscotch VM opcode count profiler" OFF)
if(ENABLE_VM_OPCODE_PROFILER)
add_compile_definitions(ENABLE_VM_OPCODE_PROFILER)
endif()
# Spatial grid logs
option(ENABLE_SPATIAL_GRID_LOGS "Enable Spatial Grid logs" OFF)
if(ENABLE_SPATIAL_GRID_LOGS)
add_compile_definitions(ENABLE_SPATIAL_GRID_LOGS)
endif()
# VM unknown/stubbed function logs
option(ENABLE_VM_STUB_LOGS "Enable VM unknown/stubbed function logs" OFF)
if(ENABLE_VM_STUB_LOGS)
add_compile_definitions(ENABLE_VM_STUB_LOGS)
endif()
# stb_image
target_include_directories(butterscotch PUBLIC vendor/stb/image)
# Platform identifier for PS3-specific code paths
add_compile_definitions(PLATFORM_PS3)
add_compile_definitions(AL_LIBTYPE_STATIC)
set(CMAKE_EXE_LINKER_FLAGS "-mhard-float -fmodulo-sched -Wl,--gc-sections -L$ENV{PS3DEV}/ppu/lib -L$ENV{PS3DEV}/portlibs/ppu/lib")
find_package(SDL2 REQUIRED)
target_include_directories(butterscotch PRIVATE ${SDL2_INCLUDE_DIRS})
# mojoal calls SDL atomics/threads, so SDL2 must link after it (static link order)
target_link_libraries(mojoal PUBLIC ${SDL2_LIBRARIES})
target_link_libraries(butterscotch PRIVATE ${SDL2_LIBRARIES} m gcm_sys rsx sysutil io audio rt lv2 io audio bzip2 stb_ds sha1 ps3gl mojoal stb_vorbis)
else()
message(FATAL_ERROR "Unknown platform! ${PLATFORM}")
endif()