-
Notifications
You must be signed in to change notification settings - Fork 9
/
makefile
498 lines (418 loc) · 15.2 KB
/
makefile
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
# Makefile by Adam, 2022
################################################################################
# What OS we're compiling on
################################################################################
IS_WSL := 0
ifeq ($(OS),Windows_NT)
HOST_OS = Windows
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
HOST_OS = Linux
# Check if this is WSL. 0 for not WSL, 1 for WSL
IS_WSL := $(shell uname -a | grep -i WSL | wc -l)
else ifeq ($(UNAME_S),Darwin)
HOST_OS = Darwin
endif
endif
################################################################################
# Programs to use
################################################################################
ifeq ($(HOST_OS),Windows)
CC = gcc
else ifeq ($(HOST_OS),Linux)
CC = gcc
else ifeq ($(UNAME_S),Darwin)
CC = gcc
endif
FIND:=find
ifeq ($(HOST_OS),Windows)
FIND:=$(shell cygpath `where find | grep bin | grep -v " "`)
endif
# clang-format may actually be clang-format-17
CLANG_FORMAT:=clang-format
ifeq (, $(shell which $(CLANG_FORMAT)))
CLANG_FORMAT:=clang-format-17
endif
ifeq ($(HOST_OS),Linux)
ifneq (,$(shell getent group plugdev))
UDEV_GROUP:=plugdev
else
UDEV_GROUP:=$(USER)
endif
endif
################################################################################
# Source Files
################################################################################
CNFS_FILE = main/utils/cnfs_image.c
# This is a list of directories to scan for c files recursively
SRC_DIRS_RECURSIVE = emulator/src main
# This is a list of directories to scan for c files not recursively
SRC_DIRS_FLAT = emulator/src-lib
# This is a list of files to compile directly. There's no scanning here
# cnfs_image.c may not exist when the makefile is invoked, explicitly list it
SRC_FILES = $(CNFS_FILE)
# This is all the source directories combined
SRC_DIRS = $(shell $(FIND) $(SRC_DIRS_RECURSIVE) -type d) $(SRC_DIRS_FLAT)
# This is all the source files combined and deduplicated
SOURCES = $(sort $(shell $(FIND) $(SRC_DIRS) -maxdepth 1 -iname "*.[c]") $(SRC_FILES))
SOURCES := $(filter-out main/utils/cnfs.c, $(SOURCES))
# The emulator doesn't build components, but there is a target for formatting them
ALL_FILES = $(shell $(FIND) components $(SRC_DIRS_RECURSIVE) -iname "*.[c|h]")
SUBMODULES = $(shell git config --file .gitmodules --name-only --get-regexp path | sed -nr 's/submodule.(.*).path/\1/p')
################################################################################
# Includes
################################################################################
# Look for folders with .h files in these directories, recursively
INC_DIRS_RECURSIVE = emulator main
# Look for folders named "include" in these directories, recursively
INC_DIRS_INCLUDE = components
# Treat every source directory as one to search for headers in, also add a few more
INC_DIRS = $(shell $(FIND) $(INC_DIRS_RECURSIVE) -type d)
INC_DIRS += $(shell $(FIND) $(INC_DIRS_INCLUDE) -type d -iname "include")
# Prefix the directories for gcc
INC = $(patsubst %, -I%, $(INC_DIRS) )
################################################################################
# Compiler Flags
################################################################################
# These are flags for the compiler, all files
CFLAGS = \
-c \
-g \
-fdiagnostics-color=always \
-ffunction-sections \
-fdata-sections \
-gdwarf-4 \
-ggdb \
-fno-jump-tables \
-finline-functions \
-std=gnu17
ifneq ($(HOST_OS),Darwin)
# Incompatible flags for clang on MacOS
CFLAGS += \
-static-libgcc \
-static-libstdc++ \
-fstrict-volatile-bitfields \
-fno-tree-switch-conversion \
-fno-omit-frame-pointer
else
# Required for OpenGL and some other libraries
CFLAGS += \
-I/opt/X11/include \
-I/opt/homebrew/include \
-mmacosx-version-min=10.0
endif
ifeq ($(HOST_OS),Linux)
CFLAGS += \
-fsanitize=address \
-fsanitize=bounds-strict
ENABLE_GCOV=false
ifeq ($(ENABLE_GCOV),true)
CFLAGS += -fprofile-arcs -ftest-coverage -DENABLE_GCOV
endif
endif
# These are warning flags that the IDF uses
CFLAGS_WARNINGS = \
-Wall \
-Werror=all \
-Wno-error=unused-function \
-Wno-error=unused-variable \
-Wno-error=deprecated-declarations \
-Wextra \
-Wno-unused-parameter \
-Wno-sign-compare \
-Wno-enum-conversion \
-Wno-error=unused-but-set-variable
# These are warning flags that I like
CFLAGS_WARNINGS_EXTRA = \
-Wundef \
-Wformat=2 \
-Winvalid-pch \
-Wmissing-format-attribute \
-Wmissing-include-dirs \
-Wpointer-arith \
-Wunused-local-typedefs \
-Wuninitialized \
-Wshadow \
-Wredundant-decls \
-Wswitch \
-Wcast-align \
-Wformat-nonliteral \
-Wno-switch-default \
-Wunused \
-Wunused-macros \
-Wmissing-declarations \
-Wmissing-prototypes \
-Wcast-qual \
-Wno-switch \
-Wunused-result \
# -Wstrict-prototypes \
# -Wpedantic \
# -Wconversion \
# -Wsign-conversion \
# -Wdouble-promotion
ifneq ($(HOST_OS),Darwin)
# Incompatible warnings for clang on MacOS
CFLAGS_WARNINGS += \
-Wno-old-style-declaration
CFLAGS_WARNINGS_EXTRA += \
-Wlogical-op \
-Wjump-misses-init
endif
################################################################################
# Defines
################################################################################
# Create a variable with the git hash and branch name
GIT_HASH = \"$(shell git rev-parse --short=7 HEAD)\"
# Used by the ESP SDK
DEFINES_LIST = \
CONFIG_ESP_SYSTEM_PANIC=y\
CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME=y\
CONFIG_DEBUG_OUTPUT_USB=y\
CONFIG_HARDWARE_GUNSHIP=y \
CONFIG_IDF_TARGET_ESP32S2=y \
SOC_RMT_CHANNELS_PER_GROUP=4 \
SOC_TOUCH_SENSOR_NUM=15 \
SOC_ULP_SUPPORTED=y \
SOC_PM_SUPPORT_EXT_WAKEUP=y \
SOC_GPIO_SUPPORT_SLP_SWITCH=y \
SOC_TIMER_GROUP_TIMERS_PER_GROUP=2 \
SOC_TIMER_GROUPS=2 \
SOC_I2C_NUM=2 \
SOC_I2C_SUPPORT_SLAVE=y \
SOC_LEDC_CHANNEL_NUM=8 \
SOC_UART_NUM=2 \
SOC_ADC_DIGI_RESULT_BYTES=2 \
CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD=0 \
CONFIG_LOG_MAXIMUM_LEVEL=3 \
CONFIG_GC9307_240x280=y \
CONFIG_TFT_MAX_BRIGHTNESS=200 \
CONFIG_TFT_MIN_BRIGHTNESS=10 \
CONFIG_NUM_LEDS=9 \
configENABLE_FREERTOS_DEBUG_OCDAWARE=1 \
_GNU_SOURCE \
IDF_VER="v5.3.1" \
ESP_PLATFORM \
_POSIX_READER_WRITER_LOCKS \
CFG_TUSB_MCU=OPT_MCU_ESP32S2 \
CONFIG_SOUND_OUTPUT_SPEAKER=y
# If this is not WSL, use OpenGL for rawdraw
ifeq ($(IS_WSL),0)
DEFINES_LIST += CNFGOGL
endif
# Extra defines
DEFINES_LIST += \
GIT_SHA1=${GIT_HASH} \
HAS_XINERAMA=1 \
FULL_SCREEN_STEAL_FOCUS=1
DEFINES = $(patsubst %, -D%, $(DEFINES_LIST))
################################################################################
# Output Objects
################################################################################
# This is the directory in which object files will be stored
OBJ_DIR = emulator/obj
# This is a list of objects to build
OBJECTS = $(patsubst %.c, $(OBJ_DIR)/%.o, $(SOURCES))
################################################################################
# Linker options
################################################################################
# This is a list of libraries to include. Order doesn't matter
ifeq ($(HOST_OS),Windows)
LIBS = opengl32 gdi32 user32 winmm WSock32
endif
ifeq ($(HOST_OS),Linux)
LIBS = m X11 asound pulse rt GL GLX pthread Xext Xinerama
endif
ifeq ($(HOST_OS),Darwin)
LIBS = m X11 GL pthread Xext Xinerama
endif
# These are directories to look for library files in
LIB_DIRS =
# On MacOS we need to ensure that X11 is added for OpenGL and some others
ifeq ($(HOST_OS),Darwin)
LIB_DIRS = /opt/X11/lib /opt/homebrew/lib
endif
# This combines the flags for the linker to find and use libraries
LIBRARY_FLAGS = $(patsubst %, -L%, $(LIB_DIRS)) $(patsubst %, -l%, $(LIBS)) \
-ggdb
# Incompatible flags for clang on MacOS
ifneq ($(HOST_OS),Darwin)
LIBRARY_FLAGS += \
-static-libgcc \
-static-libstdc++
else
LIBRARY_FLAGS += \
-framework Carbon \
-framework Foundation \
-framework CoreFoundation \
-framework CoreMIDI \
-framework AudioToolbox
endif
ifeq ($(HOST_OS),Linux)
LIBRARY_FLAGS += \
-fsanitize=address \
-fsanitize=bounds-strict \
-fno-omit-frame-pointer \
-static-libasan
ifeq ($(ENABLE_GCOV),true)
LIBRARY_FLAGS += -lgcov -fprofile-arcs -ftest-coverage
endif
endif
ifeq ($(HOST_OS),Windows)
LIBRARY_FLAGS += -Wl,-Bstatic -lpthread
endif
################################################################################
# Build Filenames
################################################################################
# These are the files to build
EXECUTABLE = swadge_emulator
################################################################################
# Targets for Building
################################################################################
# This list of targets do not build files which match their name
.PHONY: all assets bundle clean docs format cppcheck firmware clean-firmware $(CNFS_FILE) update-submodules print-%
# Build the executable
all: $(EXECUTABLE)
assets:
$(MAKE) -C ./tools/assets_preprocessor/
./tools/assets_preprocessor/assets_preprocessor -i ./assets/ -o ./assets_image/
# To build the main file, you have to compile the objects
$(EXECUTABLE): $(CNFS_FILE) $(OBJECTS)
$(CC) $(OBJECTS) $(LIBRARY_FLAGS) -o $@
# This compiles each c file into an o file
./$(OBJ_DIR)/%.o: ./%.c
@mkdir -p $(@D) # This creates a directory before building an object in it.
$(CC) $(CFLAGS) $(CFLAGS_WARNINGS) $(CFLAGS_WARNINGS_EXTRA) $(DEFINES) $(INC) $< -o $@
# To create the c file with assets, run these tools
$(CNFS_FILE):
# Sokoban .tmx to bin preprocessor
python ./tools/soko/soko_tmx_preprocessor.py ./assets/soko/ ./assets_image/
$(MAKE) -C ./tools/assets_preprocessor/
./tools/assets_preprocessor/assets_preprocessor -i ./assets/ -o ./assets_image/
$(MAKE) -C ./tools/cnfs/
./tools/cnfs/cnfs_gen assets_image/ main/utils/cnfs_image.c main/utils/cnfs_image.h
bundle: SwadgeEmulator.app
SwadgeEmulator.app: $(EXECUTABLE) build/SwadgeEmulator.icns emulator/resources/Info.plist
rm -rf SwadgeEmulator.app
mkdir -p SwadgeEmulator.app/Contents/{MacOS,Resources,libs}
cat emulator/resources/Info.plist | sed "s/##GIT_HASH##/$(GIT_HASH)/" > SwadgeEmulator.app/Contents/Info.plist
echo "APPLSwadgeEmulator" > SwadgeEmulator.app/Contents/PkgInfo
cp build/SwadgeEmulator.icns SwadgeEmulator.app/Contents/Resources/
vtool -set-build-version macos 10.0 10.0 -replace -output SwadgeEmulator.app/Contents/MacOS/SwadgeEmulator $(EXECUTABLE)
dylibbundler -od -b -x ./SwadgeEmulator.app/Contents/MacOS/SwadgeEmulator -d ./SwadgeEmulator.app/Contents/libs/
build/SwadgeEmulator.icns: emulator/resources/icon.png
rm -rf build/SwadgeEmulator.iconset
mkdir -p build/SwadgeEmulator.iconset
sips -z 16 16 $< --out build/SwadgeEmulator.iconset/icon_16x16.png
sips -z 32 32 $< --out build/SwadgeEmulator.iconset/[email protected]
sips -z 32 32 $< --out build/SwadgeEmulator.iconset/icon_32x32.png
sips -z 64 64 $< --out build/SwadgeEmulator.iconset/[email protected]
sips -z 128 128 $< --out build/SwadgeEmulator.iconset/icon_128x128.png
sips -z 256 256 $< --out build/SwadgeEmulator.iconset/[email protected]
sips -z 256 256 $< --out build/SwadgeEmulator.iconset/icon_256x256.png
sips -z 512 512 $< --out build/SwadgeEmulator.iconset/[email protected]
sips -z 512 512 $< --out build/SwadgeEmulator.iconset/icon_512x512.png
sips -z 1024 1024 $< --out build/SwadgeEmulator.iconset/[email protected]
iconutil -c icns -o build/SwadgeEmulator.icns build/SwadgeEmulator.iconset
rm -r build/SwadgeEmulator.iconset
# This cleans emulator files
clean:
$(MAKE) -C ./tools/assets_preprocessor/ clean
$(MAKE) -C ./tools/cnfs clean
-@rm -f $(OBJECTS) $(EXECUTABLE)
-@rm -rf ./docs/html
-@rm -rf ./main/utils/cnfs/cnfs_image.c
# This cleans everything
fullclean: clean
idf.py fullclean
git clean -dfX
git clean -df
git clean -fX
git clean -f
$(MAKE) -C ./tools/sandbox_test clean
$(MAKE) -C ./tools/hidapi_test clean
$(MAKE) -C ./tools/bootload_reboot_stub clean
$(MAKE) -C ./tools/font_maker clean
$(MAKE) -C ./tools/swadgeterm clean
$(MAKE) -C ./tools/reboot_into_bootloader clean
################################################################################
# Utility targets
################################################################################
docs:
-wget -nc -O plantuml.jar https://github.com/plantuml/plantuml/releases/download/v1.2023.4/plantuml-1.2023.4.jar
doxygen ./Doxyfile
format:
$(CLANG_FORMAT) -i -style=file $(ALL_FILES)
################################################################################
# Firmware targets
################################################################################
clean-firmware:
idf.py clean
$(MAKE) -C ./tools/assets_preprocessor/ clean
-@rm -rf ./docs/html
-@rm -rf ./assets_image/*
firmware:
idf.py build
# For now, works on Linux. You can copy-paste these for Windows.
ifeq ($(HOST_OS),Windows)
usbflash :
tools/reflash_and_monitor.bat
else
usbflash :
# In case we are already in the bootloader...
($(MAKE) -C tools/bootload_reboot_stub reboot)||(true)
# Command reboot out of game into bootloader.
$(MAKE) -C tools/reboot_into_bootloader
idf.py flash
sleep 1.2
$(MAKE) -C tools/bootload_reboot_stub reboot
$(MAKE) -C tools/swadgeterm monitor
endif
monitor :
$(MAKE) -C tools/swadgeterm monitor
/etc/udev/rules.d/99-swadge.rules :
printf "KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", MODE=\"0664\", GROUP=\"%s\", ATTRS{idVendor}==\"1209\", ATTRS{idProduct}==\"4269\"\n" $(UDEV_GROUP) > /tmp/99-swadge.rules
printf "KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"1209\", ATTRS{idProduct}==\"4269\", GROUP=\"%s\", MODE=\"0660\"\n" $(UDEV_GROUP) >> /tmp/99-swadge.rules
printf "KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", MODE=\"0664\", GROUP=\"%s\", ATTRS{idVendor}==\"303a\", ATTRS{idProduct}==\"00??\"\n" $(UDEV_GROUP) >> /tmp/99-swadge.rules
printf "KERNEL==\"hidraw*\", SUBSYSTEM==\"hidraw\", ATTRS{idVendor}==\"303a\", ATTRS{idProduct}==\"00??\", GROUP=\"%s\", MODE=\"0660\"\n" $(UDEV_GROUP) >> /tmp/99-swadge.rules
sudo cp -a /tmp/99-swadge.rules /etc/udev/rules.d/99-swadge.rules
installudev : /etc/udev/rules.d/99-swadge.rules
getent group plugdev >/dev/null && sudo usermod -aG plugdev $(USER) || true
sudo udevadm control --reload
sudo udevadm trigger
################################################################################
# cppcheck targets
################################################################################
CPPCHECK_FLAGS= \
--enable=warning \
--inconclusive \
--library=posix \
--language=c \
--platform=unix32 \
--std=c++17 \
--suppress=missingIncludeSystem \
--output-file=./cppcheck_result.txt \
-j12 \
-D__linux__=1
CPPCHECK_DIRS= \
main \
components \
emulator/src
CPPCHECK_IGNORE= \
$(shell $(FIND) emulator/src-lib -type f) \
$(shell $(FIND) main/asset_loaders -type f -iname "*heatshrink*")
CPPCHECK_IGNORE_FLAGS = $(patsubst %,-i%, $(CPPCHECK_IGNORE))
cppcheck:
cppcheck $(CPPCHECK_FLAGS) $(DEFINES) $(INC) $(CPPCHECK_DIRS) $(CPPCHECK_IGNORE_FLAGS)
gen-coverage:
lcov --capture --directory ./emulator/obj/ --output-file ./coverage.info
genhtml ./coverage.info --output-directory ./coverage
firefox ./coverage/index.html &
update-submodules:
for submodule in $(SUBMODULES) ; do \
echo Updating $$submodule to latest ; \
git -C $$submodule fetch --prune ; \
git -C $$submodule checkout origin/HEAD ; \
done
# Print any value from this makefile
print-% : ; @echo $* = $($*)