Mirai's Miscellaneous Misadventures

M27 / makefile

1# copyright 2022 zamfofex
2# license: AGPLv3 or later
3
4engine ?= mfb
5
6libs =
7
8ifeq ($(engine),sdl2)
9libs += -lSDL2
10endif
11
12ifeq ($(engine),mfb)
13libs += -lminifb -lX11 -lGL
14CFLAGS += -Wno-discarded-qualifiers
15endif
16
17src = \
18	core/behaviors.c \
19	core/sprites.c \
20	core/controls.c \
21	core/cameras.c \
22	core/animations.c \
23	core/displays.c \
24	core/fonts.c core/font.c \
25	core/text.c \
26	core/coroutines.c \
27	core/collisions.c \
28	models/all.c \
29	test/chapter.c
30
31exe = mimimi
32lib = lib$(exe)
33
34ifneq ($(engine),lib)
35src += engines/$(engine).c
36endif
37
38ifeq ($(engine),x86)
39
40$(exe).$(engine).img: $(src:.c=.$(engine).o) $(engine)/boot.$(engine).o $(engine)/main.$(engine).o $(engine)/vbe.$(engine).o
41	@echo ld $@
42	@$(CC) -m16 $(LDFLAGS) -nostdlib -static -T$(engine)/link.ld -o $@ $^ $(libs)
43
44%.$(engine).o: %.c x86/link.ld
45	@echo cc $<
46	@$(CC) -m16 $(CFLAGS) $(CPPFLAGS) -MMD -MF$(<:.c=.d) -MP -Iinclude -ffreestanding -c -o $@ $<
47
48%.$(engine).o: %.s x86/link.ld
49	@echo cc $<
50	@$(CC) -m16 $(ASFLAGS) -c -o $@ $<
51
52else ifeq ($(engine),wasm)
53
54$(exe).wasm: $(src:.c=.wasm)
55	@echo ld $@
56	@$(CC) $(LDFLAGS) -nostdlib -Wl,--no-entry \
57		-Wl,--export=mimimi_wasm_stamp \
58		-Wl,--export=mimimi_wasm_allocator \
59		-Wl,--export=mimimi_wasm_allocate \
60		-Wl,--export=mimimi_test \
61		-Wl,--allow-undefined \
62		-Wl,--export-table,--growable-table \
63		-o $@ $^ $(libs)
64
65$(lib).wasm.a: $(src:.c=.wasm)
66	@echo ar $@
67	@$(AR) rDsc $@ $^
68
69%.wasm: %.c
70	@echo cc $<
71	@$(CC) $(CFLAGS) $(CPPFLAGS) -ffreestanding -MMD -MP -Iinclude -c -o $@ $<
72
73else ifeq ($(engine),psx)
74
75src += psx/malloc.c
76
77libs += -lpsxgpu -lpsxgte -lpsxspu -lpsxcd -lpsxpress -lpsxsio -lpsxetc -lpsxapi -llzp -lc
78
79$(exe).exe: $(src:.c=.o) main/$(engine).o
80	@echo ld $@
81	@$(CC) $(LDFLAGS) -nostdlib -static -o $(exe) $^ $(libs)
82	@elf2x -q $(exe) $@
83
84%.o: %.c
85	@echo cc $<
86	@$(CC) $(CFLAGS) $(CPPFLAGS) -march=r3000 -msoft-float -mabi=32 -mdivide-breaks -ffreestanding -mno-abicalls -MMD -MP -Iinclude -c -o $@ $<
87
88else
89
90src += core/malloc.c
91
92$(exe): $(src:.c=.o) main/$(engine).o
93	@echo ld $@
94	@$(CC) $(LDFLAGS) -o $@ $^ $(libs)
95
96$(lib).a: $(src:.c=.o)
97	@echo ar $@
98	@$(AR) rDsc $@ $^
99
100%.o: %.c
101	@echo cc $<
102	@$(CC) $(CFLAGS) $(CPPFLAGS) -MMD -MP -Iinclude -c -o $@ $<
103
104endif
105
106.PHONY: clean
107clean:
108	$(RM) $(exe) $(exe).exe *.wasm *.img *.a */*.o */*.wasm */*.d
109
110-include $(src:.c=.d)