-
Notifications
You must be signed in to change notification settings - Fork 5
/
CMakeLists.txt
346 lines (299 loc) · 12.9 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
cmake_minimum_required(VERSION 3.13)
if(NOT DEFINED BACKEND)
message(FATAL_ERROR "You must define the variable BACKEND (to either PICO, ESP32, PC, RAWDRAW, ANDROID, THREEDS or TEST)" )
endif()
if(BACKEND STREQUAL "PICO")
include(pico_sdk_import.cmake)
add_definitions(-DPICO_BACKEND)
elseif(BACKEND STREQUAL "ESP32")
add_definitions(-DESP_BACKEND)
set(EXTRA_COMPONENT_DIRS "esp")
set(COMPONENTS "esp")
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
elseif(BACKEND STREQUAL "PC")
add_definitions(-DSDL_BACKEND)
elseif(BACKEND STREQUAL "RAWDRAW")
add_definitions(-DRAWDRAW_BACKEND)
elseif(BACKEND STREQUAL "ANDROID")
add_definitions(-DANDROID_BACKEND)
add_definitions(-DRAWDRAW_BACKEND)
elseif(BACKEND STREQUAL "THREEDS")
add_definitions(-DTHREEDS_BACKEND)
elseif(BACKEND STREQUAL "TEST")
add_definitions(-DTEST_BACKEND)
else()
message( SEND_ERROR "Invalid backend" )
endif()
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
project(my_project C CXX ASM)
# TODO: how do i compile _a_ luac for meta-build
if(NOT BACKEND STREQUAL "ESP32")
add_subdirectory(local_lua)
endif()
add_compile_options(
# -Werror
#-g3
-Wno-unused-variable
)
SET(luac ${CMAKE_CURRENT_BINARY_DIR}/local_lua/luac)
file(GLOB ALL_RESOURCE_FILES CONFIGURE_DEPENDS stdlib/stdlib.lua artifacts/font.lua carts/* scripts/to_c.py)
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/src/generated/static_game_data.h
COMMAND python3 scripts/to_c.py --emit-stdlib --luac ${luac} carts/ > ${CMAKE_SOURCE_DIR}/src/generated/static_game_data.h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${ALL_RESOURCE_FILES}
)
file(GLOB ALL_SOURCE_FILES CONFIGURE_DEPENDS *.c *.h esp/*.c esp/*.h)
add_custom_target(
cppcheck
COMMAND /usr/bin/cppcheck
--enable=warning,performance,portability,information,missingInclude
--language=c++
${ALL_SOURCE_FILES}
)
if(BACKEND STREQUAL "PC")
add_executable(pc_pico
src/generated/static_game_data.h
src/entrypoint.cpp
)
add_subdirectory(SDL_mixer)
add_subdirectory(SDL)
add_subdirectory(lua)
target_link_libraries(pc_pico
my_lua_lib
SDL2::SDL2
SDL2::SDL2main
SDL2_mixer
)
target_include_directories(pc_pico PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
SDL
SDL_mixer
)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -fsingle-precision-constant -Wall -Wshadow -Wundef -fsanitize=address")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
target_compile_options(pc_pico PRIVATE -DSDL_BACKEND=1 -DLUA_USE_LINUX=1 -DW_SCREEN_HEIGHT=512 -DW_SCREEN_WIDTH=512 -DSCREEN_WIDTH=128 -DSCREEN_HEIGHT=128 -DHUD_HEIGHT=12)
add_compile_options(
-g3
-fsingle-precision-constant
)
if(APPLE)
add_custom_command(TARGET pc_pico POST_BUILD COMMAND codesign -s - ./pc_pico)
endif()
elseif(BACKEND STREQUAL "ESP32")
# some day i'll figure out how to merge the 2 projects
elseif(BACKEND STREQUAL "THREEDS")
set(CMAKE_C_COMPILER "/opt/devkitpro/devkitARM/bin/arm-none-eabi-g++")
set(CMAKE_CXX_COMPILER "/opt/devkitpro/devkitARM/bin/arm-none-eabi-g++")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O2 -mword-relocations -ffunction-sections -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -specs=3dsx.specs")
SET(APPNAME "3ds_pico")
link_directories(
/opt/devkitpro/libctru/lib
)
add_executable("${APPNAME}"
src/generated/static_game_data.h
src/entrypoint.cpp
)
add_subdirectory(lua)
target_link_libraries("${APPNAME}"
my_lua_lib
citro2d
citro3d
ctru
m
)
target_include_directories("${APPNAME}" PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
/opt/devkitpro/libctru/include)
target_compile_options("${APPNAME}" PRIVATE -DSCREEN_WIDTH=128 -DSCREEN_HEIGHT=128 -DHUD_HEIGHT=12 -D__3DS__)
add_custom_target(generate_cart ALL
BYPRODUCTS "${APPNAME}.3dsx"
# ${CMAKE_SOURCE_DIR}/artifacts/3ds_icon.png
# in GH actions, getting:
# Could not decode PNG: /__w/PicoPico/PicoPico/artifacts/3ds_icon.png
COMMAND /opt/devkitpro/tools/bin/smdhtool --create "${APPNAME}" "'Very_alpha'" "'David'" "/opt/devkitpro/libctru/default_icon.png" "${APPNANE}.smdh"
COMMAND /opt/devkitpro/tools/bin/3dsxtool "${APPNAME}" "${APPNAME}.3dsx" --smdh=${APPNANE}.smdh #--romfs=/home/david/Downloads/a/romfs
COMMAND rm -f "${APPNANE}.smdh"
)
add_dependencies(generate_cart "${APPNAME}")
elseif(BACKEND STREQUAL "ANDROID")
set(APPNAME "cnfgtest")
# setting vars before `add_subdirectory` is important
# TODO generic vars
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -Os -fdata-sections -Wall -fvisibility=hidden -std=c++11")
# missing -llua, automatic?
add_library("${APPNAME}" SHARED
src/generated/static_game_data.h
src/entrypoint.cpp
rawdrawandroid/android_native_app_glue.c
)
SET(ANDROIDVERSION "30")
SET(ANDROIDTARGET "${ANDROIDVERSION}")
set(LABEL "${APPNAME}")
if(NOT DEFINED ANDROIDSDK)
set(ANDROIDSDK "/home/david/AndroidCrap")
endif()
set(NDK "${ANDROIDSDK}/ndk/21.3.6528147")
SET(OS_NAME "linux-x86_64")
set(PACKAGENAME "org.yourorg.${APPNAME}")
SET(CXX_ARM64 "${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/bin/aarch64-linux-android${ANDROIDVERSION}-clang++")
SET(CC_ARM64 "${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/bin/aarch64-linux-android${ANDROIDVERSION}-clang")
SET(BUILD_TOOLS "${ANDROIDSDK}/build-tools/30.0.2")
SET(AAPT "${BUILD_TOOLS}/aapt")
SET(ADB "${ANDROIDSDK}/platform-tools/adb")
SET(KEYSTOREFILE "${CMAKE_CURRENT_SOURCE_DIR}/rawdrawandroid/my-release-key.keystore")
SET(STOREPASS "password")
SET(ALIASNAME "standkey")
SET(DNAME "CN=example.com, OU=ID, O=Example, L=Doe, S=John, C=GB")
SET(APKFILE "${APPNAME}.apk")
#SET(CC_ARM32 "${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/bin/armv7a-linux-androideabi${ANDROIDVERSION}-clang")
#SET(CC_x86 "${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/bin/i686-linux-android${ANDROIDVERSION}-clang")
#SET(CC_x86_64 "${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/bin/x86_64-linux-android${ANDROIDVERSION}-clang")
set(CMAKE_C_COMPILER "${CC_ARM64}")
set(CMAKE_CXX_COMPILER "${CXX_ARM64}")
add_subdirectory(lua)
target_link_libraries("${APPNAME}"
my_lua_lib
-Wl,--gc-sections -Wl,-Map=output.map
-s
-static-libstdc++
-lm -lGLESv3 -lEGL -landroid -llog
-lOpenSLES
-shared -uANativeActivity_onCreate
)
target_include_directories("${APPNAME}" PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
rawdrawandroid
rawdrawandroid/rawdraw
${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/sysroot/usr/include
${NDK}/toolchains/llvm/prebuilt/${OS_NAME}/sysroot/usr/include/android
)
target_compile_options("${APPNAME}" PRIVATE -DRAWDRAW_BACKEND=1 -DANDROID_BACKEND=1 -DSCREEN_WIDTH=128 -DSCREEN_HEIGHT=128 -DHUD_HEIGHT=12 -DAPPNAME="cnfgtest" -DANDROIDVERSION=${ANDROIDVERSION} -DANDROID)
add_custom_target(render_xml ALL
COMMAND PACKAGENAME=${PACKAGENAME} ANDROIDVERSION=${ANDROIDVERSION} ANDROIDTARGET=${ANDROIDTARGET} APPNAME=${APPNAME} LABEL=${LABEL} envsubst '$$ANDROIDTARGET $$ANDROIDVERSION $$APPNAME $$PACKAGENAME $$LABEL' < "${CMAKE_CURRENT_SOURCE_DIR}/rawdrawandroid/AndroidManifest.xml.template" > AndroidManifest.xml
)
add_custom_command(
OUTPUT "${KEYSTOREFILE}"
COMMAND keytool -genkey -v -keystore ${KEYSTOREFILE} -alias ${ALIASNAME} -keyalg RSA -keysize 2048 -validity 10000 -storepass ${STOREPASS} -keypass ${STOREPASS} -dname ${DNAME}
)
# are you supposed to have a full shell script in here
add_custom_target(makeapk ALL
BYPRODUCTS "${APKFILE}"
BYPRODUCTS "${APKFILE}.idsig"
BYPRODUCTS "activity_name"
DEPENDS "${KEYSTOREFILE}"
COMMAND mkdir -p makecapk/assets makecapk/lib/arm64-v8a/
COMMAND cp "${CMAKE_CURRENT_BINARY_DIR}/lib${APPNAME}.so" makecapk/lib/arm64-v8a/
COMMAND cp -r "${CMAKE_CURRENT_SOURCE_DIR}/rawdrawandroid/Sources/assets/*" makecapk/assets
COMMAND rm -rf temp.apk
COMMAND ${AAPT} package -f -F temp.apk -I ${ANDROIDSDK}/platforms/android-${ANDROIDVERSION}/android.jar -M AndroidManifest.xml -S "${CMAKE_CURRENT_SOURCE_DIR}/rawdrawandroid/Sources/res" -A makecapk/assets -v --target-sdk-version ${ANDROIDTARGET}
COMMAND unzip -o temp.apk -d makecapk
COMMAND rm -rf makecapk.apk
COMMAND cd makecapk && zip -D5r ../makecapk.apk . && zip -D0r ../makecapk.apk ./resources.arsc ./AndroidManifest.xml
COMMAND rm -rf ${APKFILE}
COMMAND ${BUILD_TOOLS}/zipalign -p -v 4 makecapk.apk ${APKFILE}
# #Using the apksigner in this way is only required on Android 30+
COMMAND ${BUILD_TOOLS}/apksigner sign --key-pass pass:${STOREPASS} --ks-pass pass:${STOREPASS} --ks ${KEYSTOREFILE} ${APKFILE}
COMMAND ${AAPT} dump badging ${APKFILE} | grep "launchable-activity" | cut -f 2 -d"'" >activity_name
)
add_dependencies(makeapk "${APPNAME}")
add_custom_target(run
DEPENDS ${APKFILE}
COMMAND ${ADB} install -r ${APKFILE}
COMMAND ${ADB} shell am start -n ${PACKAGENAME}/android.app.NativeActivity
# TODO: android.app.NativeActivity is actually $(cat activity_name)
)
add_dependencies(run makeapk)
elseif(BACKEND STREQUAL "RAWDRAW")
add_executable(rawdraw_pico
src/generated/static_game_data.h
src/entrypoint.cpp
)
add_subdirectory(lua)
target_link_libraries(rawdraw_pico
my_lua_lib
asound
)
target_include_directories(rawdraw_pico PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
rawdraw
)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -fsingle-precision-constant -Wall -Wshadow -Wundef -fsanitize=address")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg -lX11 -lpthread -lXinerama -lXext -lGL -g -lm -ldl")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg ")
target_compile_options(rawdraw_pico PRIVATE -DRAWDRAW_BACKEND=1 -DLUA_USE_LINUX=1 -DW_SCREEN_HEIGHT=512 -DW_SCREEN_WIDTH=512 -DSCREEN_WIDTH=128 -DSCREEN_HEIGHT=128 -DHUD_HEIGHT=12)
add_compile_options(
-g3
-fsingle-precision-constant
)
elseif(BACKEND STREQUAL "TEST")
add_custom_target(
bin_to_png
COMMAND python3 buf_to_png.py --in-file data/hud.bin --out-file data/hud.png --width 128 --height 12
COMMAND python3 buf_to_png.py --in-file data/menu.bin --out-file data/menu.png --width 128 --height 128 --palette
COMMAND python3 buf_to_png.py --in-file data/hello_world.bin --out-file data/hello_world.png --width 128 --height 128 --palette
COMMAND python3 buf_to_png.py --in-file data/test_basic_gfx.bin --out-file data/test_basic_gfx.png --width 128 --height 128 --palette
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests/
)
add_custom_target(
tests_webserver
COMMAND python3 -m http.server
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests/data
)
file(GLOB ALL_REGRESSION_FILES CONFIGURE_DEPENDS tests/regression/*.p8 scripts/to_c.py)
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/tests/regression_static_game_data.h
COMMAND python3 scripts/to_c.py --luac ${luac} --cart-prefix regression tests/regression > ${CMAKE_SOURCE_DIR}/tests/regression_static_game_data.h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${ALL_REGRESSION_FILES}
)
add_custom_command(
OUTPUT ${CMAKE_SOURCE_DIR}/tests/test_static_game_data.h
COMMAND python3 scripts/to_c.py --luac ${luac} --cart-prefix t_ tests/carts > ${CMAKE_SOURCE_DIR}/tests/test_static_game_data.h
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${ALL_REGRESSION_FILES}
)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg -fsingle-precision-constant -Wall -Wshadow -Wundef -fsanitize=address")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pg")
SET(test_compile_options -DTEST_BACKEND=1 -DLUA_USE_LINUX=1 -DSCREEN_WIDTH=128 -DSCREEN_HEIGHT=128 -DHUD_HEIGHT=12 -fprofile-arcs -ftest-coverage)
# -DRAWDRAW_BACKEND=1 -DANDROID_BACKEND=1
enable_testing()
add_subdirectory(lua rawdraw)
file(GLOB TEST_SOURCES
tests/test_hud.cpp
tests/test_menu.cpp
tests/test_hello_world.cpp
tests/test_primitives.cpp
tests/rawdraw_backend_test.cpp
)
foreach(testfile ${TEST_SOURCES})
get_filename_component(EXECUTABLE_NAME ${testfile} NAME_WE)
add_executable(${EXECUTABLE_NAME} src/generated/static_game_data.h tests/test_static_game_data.h ${testfile})
target_link_libraries(${EXECUTABLE_NAME} my_lua_lib gcov)
add_test(NAME ${EXECUTABLE_NAME} COMMAND ${EXECUTABLE_NAME})
set_tests_properties(${EXECUTABLE_NAME} PROPERTIES ENVIRONMENT GMON_OUT_PREFIX=gmon_${EXECUTABLE_NAME})
target_compile_options(${EXECUTABLE_NAME} PRIVATE ${test_compile_options})
target_include_directories(${EXECUTABLE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)
if(APPLE)
add_custom_command(TARGET ${EXECUTABLE_NAME} POST_BUILD COMMAND codesign -s - ${EXECUTABLE_NAME})
endif()
endforeach(testfile ${TEST_SOURCES})
# test regression
add_executable(test_regression src/generated/static_game_data.h tests/regression_static_game_data.h tests/test_lua_regression.cpp)
target_link_libraries(test_regression my_lua_lib gcov)
add_test(NAME test_regression COMMAND test_regression)
target_compile_options(test_regression PRIVATE ${test_compile_options})
include_directories(${CMAKE_CURRENT_SOURCE_DIR} lua
)
add_compile_options(
-g3
-fsingle-precision-constant
)
endif()