forked from nesbox/TIC-80
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
1232 lines (969 loc) · 36 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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
cmake_minimum_required(VERSION 3.3)
set(VERSION_MAJOR 1)
set(VERSION_MINOR 0)
set(VERSION_REVISION 0)
set(VERSION_STATUS "-dev")
string(TIMESTAMP VERSION_YEAR "%Y")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(VERSION_BUILD ".dbg" )
endif()
if(CMAKE_BUILD_TYPE)
string(TOUPPER ${CMAKE_BUILD_TYPE} BUILD_TYPE_UC)
endif()
find_package(Git)
if(Git_FOUND)
execute_process(
COMMAND ${GIT_EXECUTABLE} status
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
ERROR_VARIABLE RESULT_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(LENGTH "${RESULT_STRING}" LENGTH_RESULT_STRING)
if(${LENGTH_RESULT_STRING} EQUAL 0)
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --format=%H
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(SUBSTRING ${GIT_COMMIT_HASH} 0 7 GIT_COMMIT_HASH)
set(VERSION_HASH ${GIT_COMMIT_HASH} )
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-list HEAD --count
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_REVISION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
endif()
if(APPLE)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "Minimum OS X deployment version" FORCE)
endif()
project(TIC-80 VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION} LANGUAGES C CXX)
message("Building for target : ${CMAKE_SYSTEM_NAME}")
message("PROJECT_VERSION: ${PROJECT_VERSION}${VERSION_STATUS}")
message("VERSION_HASH: ${VERSION_HASH}")
configure_file("${PROJECT_SOURCE_DIR}/version.h.in" "${CMAKE_CURRENT_BINARY_DIR}/version.h")
if(ANDROID OR EMSCRIPTEN OR N3DS OR RPI)
set(BUILD_LIBRETRO_DEFAULT OFF)
set(BUILD_DEMO_CARTS_DEFAULT OFF)
set(BUILD_PLAYER_DEFAULT OFF)
else()
set(BUILD_LIBRETRO_DEFAULT ON)
set(BUILD_DEMO_CARTS_DEFAULT ON)
set(BUILD_PLAYER_DEFAULT ON)
endif()
set(BUILD_TOUCH_INPUT_DEFAULT ${ANDROID})
option(BUILD_SDL "SDL Enabled" ON)
option(BUILD_SDLGPU "SDL GPU Enabled" OFF)
option(BUILD_SOKOL "Sokol Enabled" OFF)
option(BUILD_LIBRETRO "libretro Enabled" ${BUILD_LIBRETRO_DEFAULT})
option(BUILD_DEMO_CARTS "Demo Carts Enabled" ${BUILD_DEMO_CARTS_DEFAULT})
option(BUILD_PRO "Build PRO version" FALSE)
option(BUILD_PLAYER "Build standalone players" ${BUILD_PLAYER_DEFAULT})
option(BUILD_TOUCH_INPUT "Build with touch input support" ${BUILD_TOUCH_INPUT_DEFAULT})
option(BUILD_STUB "Build stub without editors" OFF)
if(NOT BUILD_SDL)
set(BUILD_SDLGPU OFF)
endif()
message("BUILD_SDLGPU: ${BUILD_SDLGPU}")
message("BUILD_TOUCH_INPUT: ${BUILD_TOUCH_INPUT}")
message("BUILD_STUB: ${BUILD_STUB}")
if (N3DS)
set(BUILD_SDL off)
endif()
if (BAREMETALPI)
set(BUILD_SDL OFF)
set(BUILD_DEMO_CARTS OFF)
endif()
if(UNIX AND NOT APPLE AND NOT EMSCRIPTEN AND NOT ANDROID AND NOT N3DS)
set(LINUX TRUE)
endif()
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if (NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
if(MSVC)
add_definitions("/D\"_CRT_SECURE_NO_WARNINGS\"")
add_definitions("/D\"_CRT_NONSTDC_NO_DEPRECATE\"")
foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} )
string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/lib )
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/lib )
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/bin )
# use static runtime
# !TODO: use set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>") from cmake 3.15 instead
if(CMAKE_C_FLAGS_${OUTPUTCONFIG} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" CMAKE_C_FLAGS_${OUTPUTCONFIG} "${CMAKE_C_FLAGS_${OUTPUTCONFIG}}")
endif()
if(CMAKE_CXX_FLAGS_${OUTPUTCONFIG} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_${OUTPUTCONFIG} "${CMAKE_CXX_FLAGS_${OUTPUTCONFIG}}")
endif()
endforeach()
else()
set(CMAKE_C_STANDARD 11)
endif()
if(RPI)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu11")
endif()
if(ANDROID)
set(CMAKE_SYSTEM_VERSION 24)
endif()
set(THIRDPARTY_DIR ${CMAKE_SOURCE_DIR}/vendor)
include(ExternalProject)
################################
# LUA
################################
set(LUA_DIR ${THIRDPARTY_DIR}/lua)
set(LUA_SRC
${LUA_DIR}/lapi.c
${LUA_DIR}/lcode.c
${LUA_DIR}/lctype.c
${LUA_DIR}/ldebug.c
${LUA_DIR}/ldo.c
${LUA_DIR}/ldump.c
${LUA_DIR}/lfunc.c
${LUA_DIR}/lgc.c
${LUA_DIR}/llex.c
${LUA_DIR}/lmem.c
${LUA_DIR}/lobject.c
${LUA_DIR}/lopcodes.c
${LUA_DIR}/lparser.c
${LUA_DIR}/lstate.c
${LUA_DIR}/lstring.c
${LUA_DIR}/ltable.c
${LUA_DIR}/ltm.c
${LUA_DIR}/lundump.c
${LUA_DIR}/lvm.c
${LUA_DIR}/lzio.c
${LUA_DIR}/lauxlib.c
${LUA_DIR}/lbaselib.c
${LUA_DIR}/lcorolib.c
${LUA_DIR}/ldblib.c
${LUA_DIR}/liolib.c
${LUA_DIR}/lmathlib.c
${LUA_DIR}/loslib.c
${LUA_DIR}/lstrlib.c
${LUA_DIR}/ltablib.c
${LUA_DIR}/lutf8lib.c
${LUA_DIR}/loadlib.c
${LUA_DIR}/linit.c
)
add_library(lua STATIC ${LUA_SRC})
target_compile_definitions(lua PRIVATE LUA_COMPAT_5_2)
target_include_directories(lua INTERFACE ${THIRDPARTY_DIR}/lua)
if(N3DS)
target_compile_definitions(lua PUBLIC LUA_32BITS)
endif()
################################
# LPEG
################################
set(LPEG_DIR ${THIRDPARTY_DIR}/lpeg)
set(LPEG_SRC
${LPEG_DIR}/lpcap.c
${LPEG_DIR}/lpcode.c
${LPEG_DIR}/lpprint.c
${LPEG_DIR}/lptree.c
${LPEG_DIR}/lpvm.c
)
add_library(lpeg STATIC ${LPEG_SRC})
target_include_directories(lpeg PRIVATE ${LUA_DIR})
################################
# WREN
################################
set(WREN_DIR ${THIRDPARTY_DIR}/wren/src)
set(WREN_SRC
${WREN_DIR}/optional/wren_opt_meta.c
${WREN_DIR}/optional/wren_opt_random.c
${WREN_DIR}/vm/wren_compiler.c
${WREN_DIR}/vm/wren_core.c
${WREN_DIR}/vm/wren_debug.c
${WREN_DIR}/vm/wren_primitive.c
${WREN_DIR}/vm/wren_utils.c
${WREN_DIR}/vm/wren_value.c
${WREN_DIR}/vm/wren_vm.c
)
add_library(wren STATIC ${WREN_SRC})
target_include_directories(wren PUBLIC ${THIRDPARTY_DIR}/wren/src/include)
target_include_directories(wren PRIVATE ${THIRDPARTY_DIR}/wren/src/optional)
target_include_directories(wren PRIVATE ${THIRDPARTY_DIR}/wren/src/vm)
################################
# MRUBY
################################
find_program(RUBY ruby)
find_program(RAKE rake)
if(NOT RAKE)
set(BUILD_WITH_MRUBY_DEFAULT FALSE)
else()
set(BUILD_WITH_MRUBY_DEFAULT TRUE)
endif()
option(BUILD_WITH_MRUBY "mruby Enabled" ${BUILD_WITH_MRUBY_DEFAULT})
message("BUILD_WITH_MRUBY: ${BUILD_WITH_MRUBY}")
if(BUILD_WITH_MRUBY)
set(MRUBY_BUILDDIR ${CMAKE_SOURCE_DIR}/build/mruby)
set(MRUBY_DIR ${THIRDPARTY_DIR}/mruby)
if(ANDROID)
set(MRUBY_CONFIG ${MRUBY_BUILDDIR}/tic_android.rb)
else()
set(MRUBY_CONFIG ${MRUBY_BUILDDIR}/tic_default.rb)
endif()
set(MRUBY_LIB ${MRUBY_DIR}/build/target/lib/libmruby.a)
if(MSVC)
set(MRUBY_TOOLCHAIN visualcpp)
elseif(APPLE OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(MRUBY_TOOLCHAIN clang)
else()
set(MRUBY_TOOLCHAIN gcc)
endif()
if(APPLE)
exec_program(xcrun ARGS --sdk macosx --show-sdk-path OUTPUT_VARIABLE MRUBY_SYSROOT)
endif()
if(ANDROID_NDK_HOME)
set(MRUBY_RAKE_EXTRA_OPTS "${MRUBY_RAKE_EXTRA_OPTS} ANDROID_NDK_HOME=${ANDROID_NDK_HOME}")
endif()
ExternalProject_Add(mruby_vendor
SOURCE_DIR ${MRUBY_DIR}
CONFIGURE_COMMAND ""
BUILD_IN_SOURCE TRUE
BUILD_COMMAND
${RAKE} clean all "MRUBY_CONFIG=${MRUBY_CONFIG}"
"TARGET_CC=\"${CMAKE_C_COMPILER}\""
"TARGET_AR=\"${CMAKE_AR}\""
"TARGET_CFLAGS=${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${BUILD_TYPE_UC}}"
"TARGET_LDFLAGS=${CMAKE_EXE_LINKER_FLAGS} ${CMAKE_LINKER_FLAGS_${BUILD_TYPE_UC}}"
"BUILD_TYPE=${BUILD_TYPE_UC}"
"MRUBY_SYSROOT=${MRUBY_SYSROOT}"
"MRUBY_TOOLCHAIN=${MRUBY_TOOLCHAIN}"
"ANDROID_ARCH=${CMAKE_ANDROID_ARCH_ABI}"
"ANDROID_PLATFORM=android-${CMAKE_SYSTEM_VERSION}"
${MRUBY_RAKE_EXTRA_OPTS}
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${MRUBY_LIB}
)
add_library(mruby STATIC IMPORTED GLOBAL)
set_property(TARGET mruby APPEND
PROPERTY IMPORTED_LOCATION ${MRUBY_LIB}
)
set_property(TARGET mruby APPEND
PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${MRUBY_DIR}/include
)
add_dependencies(mruby mruby_vendor)
endif()
################################
# SQUIRREL
################################
set(SQUIRREL_DIR ${THIRDPARTY_DIR}/squirrel)
set(SQUIRREL_SRC
${SQUIRREL_DIR}/squirrel/sqapi.cpp
${SQUIRREL_DIR}/squirrel/sqbaselib.cpp
${SQUIRREL_DIR}/squirrel/sqclass.cpp
${SQUIRREL_DIR}/squirrel/sqcompiler.cpp
${SQUIRREL_DIR}/squirrel/sqdebug.cpp
${SQUIRREL_DIR}/squirrel/sqfuncstate.cpp
${SQUIRREL_DIR}/squirrel/sqlexer.cpp
${SQUIRREL_DIR}/squirrel/sqmem.cpp
${SQUIRREL_DIR}/squirrel/sqobject.cpp
${SQUIRREL_DIR}/squirrel/sqstate.cpp
${SQUIRREL_DIR}/squirrel/sqtable.cpp
${SQUIRREL_DIR}/squirrel/sqvm.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdaux.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdblob.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdio.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdmath.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdrex.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdstream.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdstring.cpp
${SQUIRREL_DIR}/sqstdlib/sqstdsystem.cpp
)
add_library(squirrel STATIC ${SQUIRREL_SRC})
set_target_properties(squirrel PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(squirrel PUBLIC ${SQUIRREL_DIR}/include)
target_include_directories(squirrel PRIVATE ${SQUIRREL_DIR}/squirrel)
target_include_directories(squirrel PRIVATE ${SQUIRREL_DIR}/sqstdlib)
################################
# GIFLIB
################################
set(GIFLIB_DIR ${THIRDPARTY_DIR}/giflib)
set(GIFLIB_SRC
${GIFLIB_DIR}/dgif_lib.c
${GIFLIB_DIR}/egif_lib.c
${GIFLIB_DIR}/gif_err.c
${GIFLIB_DIR}/gif_font.c
${GIFLIB_DIR}/gif_hash.c
${GIFLIB_DIR}/gifalloc.c
${GIFLIB_DIR}/openbsd-reallocarray.c
)
add_library(giflib STATIC ${GIFLIB_SRC})
target_include_directories(giflib
PRIVATE ${GIFLIB_DIR}
INTERFACE ${THIRDPARTY_DIR}/giflib)
################################
# Blipbuf
################################
add_library(blipbuf STATIC ${THIRDPARTY_DIR}/blip-buf/blip_buf.c)
target_include_directories(blipbuf INTERFACE ${THIRDPARTY_DIR}/blip-buf)
################################
# Duktape
################################
add_library(duktape STATIC ${THIRDPARTY_DIR}/duktape/src/duktape.c)
target_include_directories(duktape INTERFACE ${THIRDPARTY_DIR}/duktape/src)
################################
# TIC-80 core
################################
macro(MACRO_CORE SCRIPT DEFINE BUILD_DEPRECATED)
set(TIC80CORE_DIR ${CMAKE_SOURCE_DIR}/src)
set(TIC80CORE_SRC
${TIC80CORE_DIR}/core/core.c
${TIC80CORE_DIR}/core/languages.c
${TIC80CORE_DIR}/core/draw.c
${TIC80CORE_DIR}/core/io.c
${TIC80CORE_DIR}/core/sound.c
${TIC80CORE_DIR}/api/js.c
${TIC80CORE_DIR}/api/lua.c
${TIC80CORE_DIR}/api/moonscript.c
${TIC80CORE_DIR}/api/fennel.c
${TIC80CORE_DIR}/api/wren.c
${TIC80CORE_DIR}/api/squirrel.c
${TIC80CORE_DIR}/api/mruby.c
${TIC80CORE_DIR}/tic.c
${TIC80CORE_DIR}/cart.c
${TIC80CORE_DIR}/tools.c
${TIC80CORE_DIR}/tilesheet.c
)
if(${BUILD_DEPRECATED})
set(TIC80CORE_SRC ${TIC80CORE_SRC} ${TIC80CORE_DIR}/ext/gif.c)
endif()
add_library(tic80core${SCRIPT} STATIC ${TIC80CORE_SRC})
target_include_directories(tic80core${SCRIPT}
PRIVATE
${THIRDPARTY_DIR}/moonscript
${THIRDPARTY_DIR}/fennel
PUBLIC
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src)
target_link_libraries(tic80core${SCRIPT}
lua
lpeg
wren
squirrel
duktape
blipbuf
zlib
)
if(BUILD_WITH_MRUBY)
target_link_libraries(tic80core${SCRIPT} mruby)
endif()
if(${BUILD_DEPRECATED})
target_compile_definitions(tic80core${SCRIPT} PRIVATE DEPRECATED_CHUNKS)
target_link_libraries(tic80core${SCRIPT} giflib)
endif()
if(LINUX)
target_link_libraries(tic80core${SCRIPT} m)
endif()
target_compile_definitions(tic80core${SCRIPT} PUBLIC ${DEFINE})
endmacro()
MACRO_CORE("" "" TRUE)
if(BUILD_STUB)
MACRO_CORE(lua TIC_BUILD_WITH_LUA FALSE)
MACRO_CORE(moon TIC_BUILD_WITH_MOON FALSE)
MACRO_CORE(fennel TIC_BUILD_WITH_FENNEL FALSE)
MACRO_CORE(js TIC_BUILD_WITH_JS FALSE)
MACRO_CORE(wren TIC_BUILD_WITH_WREN FALSE)
MACRO_CORE(squirrel TIC_BUILD_WITH_SQUIRREL FALSE)
if(BUILD_WITH_MRUBY)
MACRO_CORE(ruby TIC_BUILD_WITH_MRUBY FALSE)
endif()
endif()
if(BUILD_WITH_MRUBY)
target_compile_definitions(tic80core PUBLIC TIC_BUILD_WITH_MRUBY=1)
endif()
################################
# SDL2
################################
if(BUILD_SDL AND NOT EMSCRIPTEN AND NOT RPI)
if(WIN32)
set(HAVE_LIBC TRUE)
endif()
if(ANDROID)
include_directories(${ANDROID_NDK}/sources/android/cpufeatures)
endif()
set(SDL_SHARED OFF CACHE BOOL "" FORCE)
add_subdirectory(${THIRDPARTY_DIR}/sdl2)
endif()
################################
# SDL2 standalone cart player
################################
if(BUILD_SDL AND BUILD_PLAYER AND NOT RPI)
add_executable(player-sdl WIN32 ${CMAKE_SOURCE_DIR}/src/system/sdl/player.c)
target_include_directories(player-sdl PRIVATE
${THIRDPARTY_DIR}/sdl2/include
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src)
if(MINGW)
target_link_libraries(player-sdl mingw32)
target_link_options(player-sdl PRIVATE -static)
endif()
target_link_libraries(player-sdl tic80core SDL2-static SDL2main)
endif()
################################
# Sokol
################################
if(BUILD_SOKOL)
set(SOKOL_LIB_SRC ${CMAKE_SOURCE_DIR}/src/system/sokol/sokol_gfx.c)
if(APPLE)
set(SOKOL_LIB_SRC ${SOKOL_LIB_SRC} ${CMAKE_SOURCE_DIR}/src/system/sokol/sokol_impl.m)
else()
set(SOKOL_LIB_SRC ${SOKOL_LIB_SRC} ${CMAKE_SOURCE_DIR}/src/system/sokol/sokol_impl.c)
endif()
add_library(sokol STATIC ${SOKOL_LIB_SRC})
if(APPLE)
target_compile_definitions(sokol PRIVATE SOKOL_METAL)
elseif(WIN32)
target_compile_definitions(sokol PRIVATE SOKOL_D3D11 SOKOL_D3D11_SHADER_COMPILER)
elseif(LINUX)
target_compile_definitions(sokol PRIVATE SOKOL_GLCORE33)
endif()
if(APPLE)
set_property (TARGET sokol APPEND_STRING PROPERTY
COMPILE_FLAGS "-fobjc-arc")
target_link_libraries(sokol
"-framework Cocoa"
"-framework QuartzCore"
"-framework Metal"
"-framework MetalKit"
"-framework AudioToolbox"
)
elseif(WIN32)
target_link_libraries(sokol D3D11)
if(MINGW)
target_link_libraries(sokol D3dcompiler_47)
endif()
elseif(LINUX)
find_package (Threads)
target_link_libraries(sokol X11 GL m dl asound ${CMAKE_THREAD_LIBS_INIT})
endif()
target_include_directories(sokol PRIVATE ${THIRDPARTY_DIR}/sokol)
endif()
################################
# Sokol standalone cart player
################################
if(BUILD_PLAYER AND BUILD_SOKOL)
add_executable(player-sokol WIN32 ${CMAKE_SOURCE_DIR}/src/system/sokol/player.c)
if(MINGW)
target_link_libraries(player-sokol mingw32)
target_link_options(player-sokol PRIVATE -static)
endif()
target_include_directories(player-sokol PRIVATE
${CMAKE_SOURCE_DIR}/include
${THIRDPARTY_DIR}/sokol
${CMAKE_SOURCE_DIR}/src)
target_link_libraries(player-sokol tic80core sokol)
endif()
################################
# libretro renderer example
################################
if(BUILD_LIBRETRO)
set(LIBRETRO_DIR ${TIC80CORE_DIR}/system/libretro)
set(LIBRETRO_SRC
${LIBRETRO_DIR}/tic80_libretro.c
)
if (LIBRETRO_STATIC)
add_library(tic80_libretro STATIC
${LIBRETRO_SRC}
)
if(EMSCRIPTEN)
set(LIBRETRO_EXTENSION "bc")
else()
set(LIBRETRO_EXTENSION "a")
endif()
set_target_properties(tic80_libretro PROPERTIES SUFFIX "_partial.a")
add_custom_command(TARGET tic80_libretro
POST_BUILD
COMMAND ${CMAKE_SOURCE_DIR}/build/libretro/merge_static.sh $(AR) ${CMAKE_BINARY_DIR}/lib/tic80_libretro${LIBRETRO_SUFFIX}.${LIBRETRO_EXTENSION} ${CMAKE_BINARY_DIR}/lib/tic80_libretro_partial.a ${CMAKE_BINARY_DIR}/lib/libtic80core.a ${CMAKE_BINARY_DIR}/lib/liblua.a ${CMAKE_BINARY_DIR}/lib/libblipbuf.a ${CMAKE_BINARY_DIR}/lib/libduktape.a ${CMAKE_BINARY_DIR}/lib/libwren.a ${CMAKE_BINARY_DIR}/lib/libsquirrel.a ${CMAKE_BINARY_DIR}/lib/libgiflib.a ${CMAKE_BINARY_DIR}/lib/liblpeg.a ${MRUBY_LIB})
else()
add_library(tic80_libretro SHARED
${LIBRETRO_SRC}
)
endif()
target_include_directories(tic80_libretro PRIVATE
${CMAKE_CURRENT_BINARY_DIR}
${TIC80CORE_DIR})
if(MINGW)
target_link_libraries(tic80_libretro mingw32)
endif()
if(ANDROID)
set_target_properties(tic80_libretro PROPERTIES SUFFIX "_android.so")
endif()
# MSYS2 builds libretro to ./bin, despite it being a DLL. This forces it to ./lib.
set_target_properties(tic80_libretro PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
)
target_link_libraries(tic80_libretro tic80core)
set_target_properties(tic80_libretro PROPERTIES PREFIX "")
endif()
################################
# ZLIB
################################
if (NOT N3DS)
set(ZLIB_DIR ${THIRDPARTY_DIR}/zlib)
set(ZLIB_SRC
${ZLIB_DIR}/adler32.c
${ZLIB_DIR}/compress.c
${ZLIB_DIR}/crc32.c
${ZLIB_DIR}/deflate.c
${ZLIB_DIR}/inflate.c
${ZLIB_DIR}/infback.c
${ZLIB_DIR}/inftrees.c
${ZLIB_DIR}/inffast.c
${ZLIB_DIR}/trees.c
${ZLIB_DIR}/uncompr.c
${ZLIB_DIR}/zutil.c
)
add_library(zlib STATIC ${ZLIB_SRC})
target_include_directories(zlib INTERFACE ${THIRDPARTY_DIR}/zlib)
else ()
add_library(zlib STATIC IMPORTED)
set_target_properties( zlib PROPERTIES IMPORTED_LOCATION ${DEVKITPRO}/portlibs/3ds/lib/libz.a )
target_include_directories(zlib INTERFACE ${DEVKITPRO}/portlibs/3ds/include)
endif ()
################################
# ZIP
################################
set(CMAKE_DISABLE_TESTING ON CACHE BOOL "" FORCE)
add_subdirectory(${THIRDPARTY_DIR}/zip)
################################
# bin2txt cart2prj prj2cart xplode
################################
if(BUILD_DEMO_CARTS)
set(TOOLS_DIR ${CMAKE_SOURCE_DIR}/build/tools)
add_executable(cart2prj ${TOOLS_DIR}/cart2prj.c ${CMAKE_SOURCE_DIR}/src/studio/project.c)
target_include_directories(cart2prj PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(cart2prj tic80core)
add_executable(prj2cart ${TOOLS_DIR}/prj2cart.c ${CMAKE_SOURCE_DIR}/src/studio/project.c)
target_include_directories(prj2cart PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(prj2cart tic80core)
add_executable(bin2txt ${TOOLS_DIR}/bin2txt.c)
target_link_libraries(bin2txt zlib)
add_executable(xplode
${TOOLS_DIR}/xplode.c
${CMAKE_SOURCE_DIR}/src/ext/gif.c
${CMAKE_SOURCE_DIR}/src/ext/png.c
${CMAKE_SOURCE_DIR}/src/studio/project.c)
target_include_directories(xplode PRIVATE ${CMAKE_SOURCE_DIR}/src ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(xplode tic80core png giflib)
if(LINUX)
target_link_libraries(xplode m)
endif()
file(GLOB DEMO_CARTS
${CMAKE_SOURCE_DIR}/demos/*.*
${CMAKE_SOURCE_DIR}/demos/bunny/*.*)
list(APPEND DEMO_CARTS
${CMAKE_SOURCE_DIR}/config.lua
)
set(DEMO_CARTS_OUT)
foreach(CART_FILE ${DEMO_CARTS})
get_filename_component(CART_NAME ${CART_FILE} NAME_WE)
set(OUTNAME ${CMAKE_SOURCE_DIR}/build/assets/${CART_NAME}.tic.dat)
set(OUTPRJ ${CMAKE_SOURCE_DIR}/build/${CART_NAME}.tic)
list(APPEND DEMO_CARTS_OUT ${OUTNAME})
add_custom_command(OUTPUT ${OUTNAME}
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/prj2cart ${CART_FILE} ${OUTPRJ} && ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/bin2txt ${OUTPRJ} ${OUTNAME} -z
DEPENDS bin2txt prj2cart ${CART_FILE}
)
endforeach(CART_FILE)
add_custom_command(OUTPUT ${CMAKE_SOURCE_DIR}/build/assets/cart.png.dat
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/bin2txt ${CMAKE_SOURCE_DIR}/build/cart.png ${CMAKE_SOURCE_DIR}/build/assets/cart.png.dat
DEPENDS bin2txt ${CMAKE_SOURCE_DIR}/build/cart.png)
endif()
################################
# Wave writer
################################
add_library(wave_writer STATIC ${THIRDPARTY_DIR}/blip-buf/wave_writer.c)
target_include_directories(wave_writer INTERFACE ${THIRDPARTY_DIR}/blip-buf)
################################
# ArgParse lib
################################
add_library(argparse STATIC ${THIRDPARTY_DIR}/argparse/argparse.c)
target_include_directories(argparse INTERFACE ${THIRDPARTY_DIR}/argparse)
################################
# CURL
################################
if(NOT N3DS AND NOT EMSCRIPTEN AND NOT BAREMETALPI)
set(USE_CURL TRUE)
endif()
if (USE_CURL)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "")
if(WIN32 AND NOT MINGW)
set(CURL_STATIC_CRT ON CACHE BOOL "")
endif()
set(CMAKE_USE_OPENSSL OFF CACHE BOOL "" )
set(CMAKE_USE_LIBSSH2 OFF CACHE BOOL "")
set(HTTP_ONLY ON CACHE BOOL "")
set(BUILD_CURL_EXE OFF CACHE BOOL "")
set(CURL_CA_BUNDLE "none" CACHE STRING "")
set(CURL_CA_PATH "none" CACHE STRING "")
set(CURL_DISABLE_TESTS ON CACHE BOOL "")
if(RPI)
set(CURL_ZLIB OFF CACHE BOOL "" )
endif()
add_subdirectory(${THIRDPARTY_DIR}/curl)
endif()
################################
# libuv
################################
if(USE_LIBUV)
add_subdirectory(${THIRDPARTY_DIR}/libuv)
endif()
################################
# HTTP parser
################################
if(USE_LIBUV)
add_library(http_parser STATIC ${THIRDPARTY_DIR}/http-parser/http_parser.c)
target_include_directories(http_parser INTERFACE ${THIRDPARTY_DIR}/http-parser)
endif()
################################
# PNG
################################
set(LIBPNG_DIR ${THIRDPARTY_DIR}/libpng)
set(LIBPNG_SRC
${LIBPNG_DIR}/png.c
${LIBPNG_DIR}/pngerror.c
${LIBPNG_DIR}/pngget.c
${LIBPNG_DIR}/pngmem.c
${LIBPNG_DIR}/pngpread.c
${LIBPNG_DIR}/pngread.c
${LIBPNG_DIR}/pngrio.c
${LIBPNG_DIR}/pngrtran.c
${LIBPNG_DIR}/pngrutil.c
${LIBPNG_DIR}/pngset.c
${LIBPNG_DIR}/pngtrans.c
${LIBPNG_DIR}/pngwio.c
${LIBPNG_DIR}/pngwrite.c
${LIBPNG_DIR}/pngwtran.c
${LIBPNG_DIR}/pngwutil.c
)
configure_file(${LIBPNG_DIR}/scripts/pnglibconf.h.prebuilt ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
add_library(png STATIC ${LIBPNG_SRC})
target_compile_definitions(png PRIVATE PNG_ARM_NEON_OPT=0)
target_include_directories(png
PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
PRIVATE ${THIRDPARTY_DIR}/zlib
INTERFACE ${THIRDPARTY_DIR}/libpng)
################################
# TIC-80 studio
################################
set(TIC80LIB_DIR ${CMAKE_SOURCE_DIR}/src)
set(TIC80STUDIO_SRC
${TIC80LIB_DIR}/studio/screens/console.c
${TIC80LIB_DIR}/studio/screens/run.c
${TIC80LIB_DIR}/studio/screens/menu.c
${TIC80LIB_DIR}/studio/screens/main_menu.c
${TIC80LIB_DIR}/studio/screens/surf.c
${TIC80LIB_DIR}/studio/screens/start.c
${TIC80LIB_DIR}/studio/editors/code.c
${TIC80LIB_DIR}/studio/editors/sprite.c
${TIC80LIB_DIR}/studio/editors/map.c
${TIC80LIB_DIR}/studio/editors/world.c
${TIC80LIB_DIR}/studio/editors/sfx.c
${TIC80LIB_DIR}/studio/editors/music.c
${TIC80LIB_DIR}/studio/studio.c
${TIC80LIB_DIR}/studio/config.c
${TIC80LIB_DIR}/studio/demos.c
${TIC80LIB_DIR}/studio/fs.c
${TIC80LIB_DIR}/studio/net.c
${TIC80LIB_DIR}/ext/md5.c
${TIC80LIB_DIR}/ext/history.c
${TIC80LIB_DIR}/ext/gif.c
${TIC80LIB_DIR}/ext/png.c
)
if(${BUILD_PRO})
set(TIC80STUDIO_SRC ${TIC80STUDIO_SRC} ${TIC80LIB_DIR}/studio/project.c)
endif()
set(TIC80_OUTPUT tic80)
add_library(tic80studio STATIC
${TIC80STUDIO_SRC}
${DEMO_CARTS_OUT}
${CMAKE_SOURCE_DIR}/build/assets/cart.png.dat)
if(WIN32)
target_include_directories(tic80studio PRIVATE ${THIRDPARTY_DIR}/dirent/include)
endif()
target_include_directories(tic80studio PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(tic80studio tic80core zip wave_writer argparse giflib png)
if(USE_CURL)
target_link_libraries(tic80studio libcurl)
endif()
if(USE_LIBUV)
target_compile_definitions(tic80studio PRIVATE USE_LIBUV)
target_link_libraries(tic80studio uv_a http_parser)
endif()
if(BUILD_PRO)
target_compile_definitions(tic80studio PRIVATE TIC80_PRO)
endif()
if(BUILD_SDLGPU)
target_compile_definitions(tic80studio PUBLIC CRT_SHADER_SUPPORT)
endif()
target_compile_definitions(tic80studio PUBLIC BUILD_EDITORS)
################################
# SDL GPU
################################
if(BUILD_SDLGPU)
set(SDLGPU_DIR ${THIRDPARTY_DIR}/sdl-gpu/src)
set(SDLGPU_SRC
${SDLGPU_DIR}/renderer_GLES_2.c
${SDLGPU_DIR}/SDL_gpu.c
${SDLGPU_DIR}/SDL_gpu_matrix.c
${SDLGPU_DIR}/SDL_gpu_renderer.c
${SDLGPU_DIR}/externals/stb_image/stb_image.c
${SDLGPU_DIR}/externals/stb_image_write/stb_image_write.c
)
if(NOT ANDROID)
list(APPEND SDLGPU_SRC
${SDLGPU_DIR}/renderer_GLES_1.c
${SDLGPU_DIR}/renderer_GLES_3.c
${SDLGPU_DIR}/renderer_OpenGL_1.c
${SDLGPU_DIR}/renderer_OpenGL_1_BASE.c
${SDLGPU_DIR}/renderer_OpenGL_2.c
${SDLGPU_DIR}/renderer_OpenGL_3.c
${SDLGPU_DIR}/renderer_OpenGL_4.c
${SDLGPU_DIR}/SDL_gpu_shapes.c
${SDLGPU_DIR}/externals/glew/glew.c
)
endif()
add_library(sdlgpu STATIC ${SDLGPU_SRC})
if(EMSCRIPTEN OR ANDROID)
target_compile_definitions(sdlgpu PRIVATE GLEW_STATIC SDL_GPU_DISABLE_GLES_1 SDL_GPU_DISABLE_GLES_3 SDL_GPU_DISABLE_OPENGL)
else()
target_compile_definitions(sdlgpu PRIVATE GLEW_STATIC SDL_GPU_DISABLE_GLES SDL_GPU_DISABLE_OPENGL_3 SDL_GPU_DISABLE_OPENGL_4)
endif()
target_include_directories(sdlgpu PUBLIC ${THIRDPARTY_DIR}/sdl-gpu/include)
target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/glew)
target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/glew/GL)
target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/stb_image)
target_include_directories(sdlgpu PRIVATE ${THIRDPARTY_DIR}/sdl-gpu/src/externals/stb_image_write)
if(WIN32)
target_link_libraries(sdlgpu opengl32)
endif()
if(LINUX)
target_link_libraries(sdlgpu GL)
endif()
if(APPLE)
find_library(OPENGL_LIBRARY OpenGL)
target_link_libraries(sdlgpu ${OPENGL_LIBRARY})
endif()
if(ANDROID)
find_library( ANDROID_LOG_LIBRARY log )
find_library( ANDROID_GLES2_LIBRARY GLESv2 )
find_library( ANDROID_GLES1_LIBRARY GLESv1_CM )
target_link_libraries(sdlgpu
${ANDROID_LOG_LIBRARY}
${ANDROID_GLES2_LIBRARY}
${ANDROID_GLES1_LIBRARY}
)
endif()
if(NOT EMSCRIPTEN)
target_link_libraries(sdlgpu SDL2-static)
endif()
endif()
################################
# TIC-80 app
################################
if(BUILD_SDL)
set(TIC80_SRC src/system/sdl/main.c)
if(WIN32)
configure_file("${PROJECT_SOURCE_DIR}/build/windows/tic80.rc.in" "${PROJECT_SOURCE_DIR}/build/windows/tic80.rc")
set(TIC80_SRC ${TIC80_SRC} "${PROJECT_SOURCE_DIR}/build/windows/tic80.rc")
add_executable(tic80 ${SYSTEM_TYPE} ${TIC80_SRC})
elseif(ANDROID)
set(TIC80_SRC ${TIC80_SRC} ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)
add_library(tic80 SHARED ${TIC80_SRC})
target_link_libraries(tic80 hidapi)
else()
add_executable(tic80 ${TIC80_SRC})
endif()
if(MINGW)
target_link_libraries(tic80 mingw32)
target_link_options(tic80 PRIVATE -static -mconsole)
endif()
if(EMSCRIPTEN)
set_target_properties(tic80 PROPERTIES LINK_FLAGS "-s WASM=1 -s USE_SDL=2 -s ALLOW_MEMORY_GROWTH=1 -s FETCH=1 --pre-js ${CMAKE_SOURCE_DIR}/build/html/prejs.js -lidbfs.js")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_SDL=2")
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s ASSERTIONS=1")