-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase.mk
More file actions
195 lines (163 loc) · 5.25 KB
/
base.mk
File metadata and controls
195 lines (163 loc) · 5.25 KB
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
## base.mk: 2807b2a, see https://github.com/jmesmon/trifles.git
# Usage:
#
# == For use by the one who runs 'make' ==
# $(V) when defined, prints the commands that are run.
# $(CFLAGS) expected to be overridden by the user or build system.
# $(LDFLAGS) same as CFLAGS, except for LD.
#
# == Required in the makefile ==
# all:: place this target at the top.
# $(obj-sometarget) the list of objects (generated by CC) that make up a target
# (in the list TARGET).
# $(TARGETS) a list of binaries (the output of LD).
#
# == Optional (for use in the makefile) ==
# $(NO_INSTALL) when defined, no install target is emitted.
# $(ALL_CFLAGS) non-overriden flags. Append (+=) things that are absolutely
# required for the build to work into this.
# $(ALL_LDFLAGS) same as ALL_CFLAGS, except for LD.
# example for adding some library:
#
# sometarget: ALL_LDFLAGS += -lrt
#
# $(CROSS_COMPILE) a prefix on gcc. "CROSS_COMPILE=arm-linux-" (note the trailing '-')
#
# $(ldflags-sometarget)
# $(CFLAGS_someobject)
#
# == How to use with FLEX + BISON support ==
#
# obj-foo = name.tab.o name.ll.o
# name.ll.o : name.tab.h
# TRASH += name.ll.c name.tab.c name.tab.h
# # Optionally
# PP_name = not_quite_name_
#
# TODO:
# - install disable per target.
# - flag tracking per target.'.obj.o.cmd'
# - profile guided optimization support.
# - output directory support ("make O=blah")
# - build with different flags placed into different output directories.
# - library building (shared & static)
# - per-target CFLAGS (didn't I hack this in already?)
# - will TARGETS always be outputs from Linking?
# Delete the default suffixes
.SUFFIXES:
O = .
#VPATH = $(O)
$(foreach target,$(TARGETS),$(eval vpath $(target) $(O)))
.PHONY: all FORCE
all:: $(TARGETS)
# FIXME: overriding in Makefile is tricky
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
LD = $(CC)
RM = rm -f
FLEX = flex
BISON = bison
ifdef DEBUG
OPT=-O0
else
OPT=-Os
endif
ifndef NO_LTO
ALL_CFLAGS ?= -flto
ALL_LDFLAGS ?= $(ALL_CFLAGS) $(OPT) -fuse-linker-plugin
else
ALL_CFLAGS ?= $(OPT)
endif
ALL_CFLAGS += -ggdb3
COMMON_CFLAGS += -Wall
COMMON_CFLAGS += -Wundef -Wshadow
COMMON_CFLAGS += -pipe
COMMON_CFLAGS += -Wcast-align
COMMON_CFLAGS += -Wwrite-strings
# -Wnormalized=id not supported by clang
# -Wunsafe-loop-optimizations not supported by clang
ALL_CFLAGS += -std=gnu99
ALL_CFLAGS += -Wbad-function-cast
ALL_CFLAGS += -Wstrict-prototypes -Wmissing-prototypes
ALL_CFLAGS += $(COMMON_CFLAGS) $(CFLAGS)
ALL_CXXFLAGS += $(COMMON_CFLAGS) $(CXXFLAGS)
ALL_LDFLAGS += -Wl,--build-id
ALL_LDFLAGS += $(LDFLAGS)
ifndef V
QUIET_CC = @ echo ' CC ' $@;
QUIET_CXX = @ echo ' CXX ' $@;
QUIET_LINK = @ echo ' LINK ' $@;
QUIET_LSS = @ echo ' LSS ' $@;
QUIET_SYM = @ echo ' SYM ' $@;
QUIET_FLEX = @ echo ' FLEX ' $@;
QUIET_BISON = @ echo ' BISON' $*.tab.c $*.tab.h;
endif
# Avoid deleting .o files
.SECONDARY:
obj-to-dep = $(foreach obj,$(1),$(dir $(obj)).$(notdir $(obj)).d)
target-dep = $(addprefix $(O)/,$(call obj-to-dep,$(obj-$(1))))
target-obj = $(addprefix $(O)/,$(obj-$(1)))
# flags-template flag-prefix vars message
# Defines a target '.TRACK-$(flag-prefix)FLAGS'.
# if $(ALL_$(flag-prefix)FLAGS) or $(var) changes, any rules depending on this
# target are rebuilt.
vpath .TRACK_%FLAGS $(O)
define flags-template
TRACK_$(1)FLAGS = $$($(2)):$$(subst ','\'',$$(ALL_$(1)FLAGS))
$(O)/.TRACK-$(1)FLAGS: FORCE
@FLAGS='$$(TRACK_$(1)FLAGS)'; \
if test x"$$$$FLAGS" != x"`cat $(O)/.TRACK-$(1)FLAGS 2>/dev/null`" ; then \
echo 1>&2 " * new $(3)"; \
echo "$$$$FLAGS" >$(O)/.TRACK-$(1)FLAGS; \
fi
TRASH += $(O)/.TRACK-$(1)FLAGS
endef
$(eval $(call flags-template,C,CC,c build flags))
$(eval $(call flags-template,CXX,CXX,c++ build flags))
$(eval $(call flags-template,LD,LD,link flags))
obj-cflags = CFLAGS_$(1)
parser-prefix = $(if $(PP_$*),$(PP_$*),$*_)
$(O)/%.tab.h $(O)/%.tab.c : %.y
$(QUIET_BISON)$(BISON) --locations -d \
-p '$(parser-prefix)' -k -b $* $<
$(O)/%.ll.c : %.l
$(QUIET_FLEX)$(FLEX) -P '$(parser-prefix)' --bison-locations --bison-bridge -o $@ $<
$(O)/%.o: %.c .TRACK-CFLAGS
$(QUIET_CC)$(CC) -MMD -MF $(call obj-to-dep,$@) -c -o $@ $< $(ALL_CFLAGS)
$(O)/%.o: %.cc .TRACK-CXXFLAGS
$(QUIET_CXX)$(CXX) -MMD -MF $(call obj-to-dep,$@) -c -o $@ $< $(call obj-clfags,$*) $(ALL_CXXFLAGS)
define BIN-LINK
$(1)/$(2) : .TRACK-LDFLAGS $(obj-$(2))
$$(QUIET_LINK)$(LD) -o $$@ $(call target-obj,$(2)) $(ALL_LDFLAGS) $(ldflags-$(2))
endef
$(foreach target,$(TARGETS),$(eval $(call BIN-LINK,$(O),$(target))))
ifndef NO_INSTALL
PREFIX ?= $(HOME) # link against things here
DESTDIR ?= $(PREFIX) # install into here
BINDIR ?= $(DESTDIR)/bin
.PHONY: install %.install
%.install: %
install $* $(BINDIR)/$*
install: $(foreach target,$(TARGETS),$(target).install)
endif
.PHONY: clean %.clean
%.clean :
$(RM) $(call target-obj,$*) $(O)/$* $(TRASH) $(call target-dep,$*)
clean: $(addsuffix .clean,$(TARGETS))
.PHONY: watch
watch:
@while true; do \
make -rR --no-print-directory; \
inotifywait -q \
\
-- $$(find . \
-name '*.c' \
-or -name '*.h' \
-or -name 'Makefile' \
-or -name '*.mk' ); \
echo "Rebuilding..."
done
show-targets:
@echo $(TARGETS)
deps = $(foreach target,$(TARGETS),$(call target-dep,$(target)))
-include $(deps)