-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
87 lines (67 loc) · 2.87 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
CFLAGS += -std=c11 -Wall -Wextra -Wpedantic
CFLAGS += -Wfloat-conversion -Wno-sign-compare -Werror=int-conversion
CFLAGS += -Werror=switch
CFLAGS += -Werror=incompatible-pointer-types
CFLAGS += -Werror=implicit-function-declaration
CFLAGS += -Werror=discarded-qualifiers
CFLAGS += -Werror=stack-usage=60000
CFLAGS += -Wno-format-truncation # gcc warns about how snprintf truncates, insane lol
CFLAGS += -Wno-missing-field-initializers # it's often handy to leave stuff zeroed
CFLAGS += -DSDL_ASSERT_LEVEL=2 # enable SDL_assert()
CFLAGS += -g
CFLAGS += -Ofast -fno-finite-math-only # https://stackoverflow.com/q/47703436
VENDOR_CFLAGS := $(CFLAGS:-W%=) # no warnings from other people's code please
LDFLAGS += -lSDL2 -lSDL2_ttf
LDFLAGS += -lm
SRC := $(wildcard src/*.c src/*/*.c)
TESTS_SRC := $(wildcard tests/test_*.c)
HEADERS := $(wildcard src/*.h src/*/*.h tests/*.h)
EXEDIR ?= .
COPIED_FILES := $(addprefix $(EXEDIR)/,$(notdir $(FILES_TO_COPY)))
# OBJ order matters, parallelizing make works best when slowly compiling things are first
OBJ := obj/stb_image.o $(SRC:%.c=obj/%.o)
TESTS_OBJ := $(TESTS_SRC:%.c=obj/%.o)
TESTS_LINK_OBJ := $(TESTS_OBJ) $(filter-out obj/src/main.o, $(OBJ))
RUN ?= true
all: $(EXEDIR)/game$(EXESUFFIX) test checkassets
# doesn't use .gitignore because it's sometimes handy to have files being
# ignored but not cleaned on rebuild
.PHONY: clean
clean:
rm -rvf game generated build obj callgrind.out graph.* testrunner
obj/%.o: %.c $(HEADERS)
mkdir -p $(@D) && $(CC) -c -o $@ $< $(CFLAGS)
# "-x c" tells gcc to not treat the file as a header file
obj/stb_image.o: $(wildcard stb/*.h)
mkdir -p $(@D) && \
$(CC) -c -o $@ -x c \
-DSTB_IMAGE_IMPLEMENTATION -DSTBI_WINDOWS_UTF8 stb/stb_image.h $(VENDOR_CFLAGS)
$(EXEDIR)/game$(EXESUFFIX): $(OBJ) $(HEADERS) $(COPIED_FILES)
mkdir -p $(@D) && $(CC) $(CFLAGS) $(OBJ) -o $@ $(LDFLAGS) $(GUI_LDFLAGS)
$(EXEDIR)/testrunner$(EXESUFFIX): tests/main.c $(TESTS_LINK_OBJ) $(HEADERS) $(COPIED_FILES) generated/run_tests.h
mkdir -p $(@D) && $(CC) -o $@ tests/main.c $(TESTS_LINK_OBJ) $(CFLAGS) $(LDFLAGS)
# this is lol
generated/run_tests.h: $(TESTS_OBJ)
mkdir -p $(@D) && strings $(TESTS_OBJ) | grep '^test_[a-z0-9_]*$$' | sort | uniq | awk '{ printf "RUN(%s);", $$1 }' > $@
.PHONY: test
test: $(EXEDIR)/testrunner$(EXESUFFIX)
if [ $(RUN) = true ]; then ./$<; fi
.PHONY: iwyu
iwyu:
$(MAKE) -f Makefile.iwyu
.PHONY: check_assets_sources
checkassets:
scripts/check_assets_sources
$(COPIED_FILES): $(FILES_TO_COPY)
mkdir -p $(@D) && cp -r $(shell printf '%s\n' $^ | grep $(notdir $@)) $(EXEDIR)
# profiling stuff
#
# $ sudo apt install valgrind
# $ python3 -m pip install gprof2dot
# $ make graph.png
callgrind.out: $(EXEDIR)/game$(EXESUFFIX)
valgrind --tool=callgrind --callgrind-out-file=$@ ./$< --no-sound
graph.gv: callgrind.out
gprof2dot $< --format=callgrind --output=$@
graph.png: graph.gv
dot -Tpng $< -o $@