-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
102 lines (83 loc) · 2.65 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
# MAKEFLAGS += --jobs=$(shell nproc)
# CC = cc
# CC = gcc
# CC = musl-gcc
# CC = clang
# CC = tcc
RCPDIR = ./rcp
SRCDIR = ./src
TMPDIR = ./tmp/$(CC)
BINDIR = ./bin/$(CC)
SRCS = $(wildcard $(SRCDIR)/*.c)
OBJS = $(patsubst $(SRCDIR)/%.c,$(TMPDIR)/%.o,$(SRCS))
EXE = $(BINDIR)/$(shell basename $$(pwd))
ifeq ($(OS),Windows_NT)
EXE = $(BINDIR)/$(shell basename $$(pwd)).exe
else
EXE = $(BINDIR)/$(shell basename $$(pwd))
endif
# CFLAGS = @$(RCPDIR)/flags_gcc.txt
# CFLAGS = @$(RCPDIR)/flags_clang.txt
# CFLAGS = @$(RCPDIR)/flags_tcc.txt
# CFLAGS += -pipe -Og -ggdb3 -D_FORTIFY_SOURCE=3
# CFLAGS += -Ofast -static -s -flto -DDBG_DISABLED -DNDEBUG
# SANDBOX = $(shell cat $(RCPDIR)/sandbox_gdb.txt)
# SANDBOX = $(shell cat $(RCPDIR)/sandbox_rr.txt)
# SANDBOX = $(shell cat $(RCPDIR)/sandbox_perf.txt)
# SANDBOX = $(shell cat $(RCPDIR)/sandbox_valgrind.txt)
.PHONY: default
# default: run compile_commands.json
# default: build compile_commands.json
default: build
.PHONY: run
run: build
@echo "Running $(EXE) ..."
@$(SANDBOX) $(EXE)
.PHONY: build
build: $(EXE)
.PHONY: clean
clean:
@echo "Cleaning ..."
@rm -frv $(shell cat .gitignore)
.PHONY: always
always: ;
$(EXE): $(TMPDIR)/Makefile.mk always $(BINDIR) $(TMPDIR)
@echo "Building $@ ..."
@$(MAKE) CC='$(CC)' CFLAGS='$(CFLAGS)' EXE='$(EXE)' --file='$<' --no-print-directory
# TODO: '|| (rm $@ && exit 1)' is a bit ugly but we have to use it
# because tcc does not support gcc's '-MT'
# Maybe it would be nice to contribute '-MT' to tcc someday!
# FIXME: I think I missunderstood '-MT' with '-MF'
.PRECIOUS: $(TMPDIR)/%.mk
$(TMPDIR)/%.mk: $(SRCDIR)/%.c | $(TMPDIR)
@echo "Creating $@ ..."
@printf "$(TMPDIR)/" > $@
@$(CC) $(CFLAGS) -MM $< >> $@ || (rm $@ && exit 1)
@echo " @echo \" Compiling \$$@ ...\"" >> $@
@echo " @\$$(CC) \$$(CFLAGS) -c \$$< -o \$$@" >> $@
$(TMPDIR)/Makefile.mk: $(patsubst $(SRCDIR)/%.c,$(TMPDIR)/%.mk,$(SRCS)) | $(TMPDIR)
@echo "Creating $@ ..."
@echo "\$$(EXE): $(OBJS)" > $@
@echo " @echo \" Linking \$$@ ...\"" >> $@
@echo " @\$$(CC) \$$(CFLAGS) \$$^ -o \$$@" >> $@
@echo " @ln -sfv \$$(EXE) ./\$$(shell basename \$$(EXE))" >> $@
@for file in $^; do \
echo "" >> $@; \
cat $$file >> $@; \
done \
;
$(TMPDIR) $(BINDIR):
@mkdir -pv $@
# https://clang.llvm.org/docs/JSONCompilationDatabase.html
compile_commands.json: $(SRCS)
@echo "Creating $@ ..."
@echo "[" > $@
@for file in $^; do \
echo " {" >> $@; \
echo " \"file\": \"$$file\"," >> $@; \
echo " \"command\": \"clang $$file @$(RCPDIR)/flags_clang.txt -c\"," >> $@; \
echo " \"directory\": \"$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))\"," >> $@; \
echo " }," >> $@; \
done \
;
@echo "]" >> $@