-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
271 lines (227 loc) · 6.57 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
MKCONF ?= default
$(eval include mkconf/$(MKCONF).mk)
ifeq ($(DEBUG), 1)
CFLAGS += -g -O0
endif
RULES = $(shell find . -name rules.mk)
_TARGETS =
_TARGETS_RULES=
_CROSSTARGETS =
_CROSSTARGETS_RULES =
_BUNDLES =
_FILES =
_FILES_RULES =
$(call sanitize-dir,BUILDDIR)
$(call sanitize-dir,OUTDIR)
define sanitize-dir
$(eval $(1) = $$(patsubst /,,$$(patsubst %,%/,$($(1):%/=%))))
endef
define rm-file
@(rm $(1) 2>/dev/null && \
echo "rm $(1)") || true;
endef
define rm-dir
@(rmdir -p $(1) 2>/dev/null && \
echo "rmdir -p $(1)") || true;
endef
define file-add
$(foreach f, $(1), \
$(eval fout = $(2)$(notdir $(f))) \
$(if $(filter $(fout), $(_FILES)), , \
$(eval _FILES = $(_FILES) $(fout)) \
$(eval _$(fout)_SRC=$(f))) \
)
endef
define clean-env
$(eval $(1)-OUTDIR=) \
$(eval $(1)-CSRC=) \
$(eval $(1)-ASRC=) \
$(eval $(1)-FILE=) \
$(eval $(1)-CFLAGS=) \
$(eval $(1)-INCLUDE=) \
$(eval $(1)-LDFLAGS=) \
$(eval $(1)-LDSCRIPT=) \
$(eval $(1)-DEPS=)
endef
define get-env
$(if $(filter $(2), $($(1))), , \
$(eval $(1) = $($(1)) $(2))) \
$(if $($(2)-OUTDIR), \
$(eval _$(2)_OUTDIR = $($(2)-OUTDIR))) \
$(call sanitize-dir,_$(2)_OUTDIR) \
$(if $($(2)-CSRC), \
$(eval _$(2)_CSRC += \
$($(2)-CSRC:%=$(3)%))) \
$(if $($(2)-ASRC), \
$(eval _$(2)_ASRC += \
$($(2)-ASRC:%=$(3)%))) \
$(if $($(2)-FILE), \
$(eval _$(2)_FILE += \
$($(2)-FILE:%=$(3)%))) \
$(if $($(2)-CFLAGS), \
$(eval _$(2)_CFLAGS += $($(2)-CFLAGS))) \
$(if $($(2)-INCLUDE), \
$(eval _$(2)_CFLAGS += \
$($(2)-INCLUDE:%=-I $(3)%))) \
$(if $($(2)-LDFLAGS), \
$(eval _$(2)_LDFLAGS += $($(2)-LDFLAGS))) \
$(if $($(2)-DEPS), \
$(eval _$(2)_DEPS += $($(2)-DEPS))) \
$(if $($(2)-LDSCRIPT), \
$(eval _$(2)_LDFLAGS += \
$($(2)-LDSCRIPT:%=-T $(3)%))) \
$(call clean-env,$(2))
endef
define get-deps
$(foreach d, $(_$(1)_DEPS), \
$(if $(filter $(_$(1)_DEPS_DONE), $(d)), , \
$(eval _$(1)_DEPS_DONE += $(d)) \
$(call get-deps,$(d)) \
$(if $(_$(d)_COBJ), \
$(eval _$(1)_DEP_OBJ += $(_$(d)_COBJ))) \
$(if $(_$(d)_AOBJ), \
$(eval _$(1)_DEP_OBJ += $(_$(d)_AOBJ))) \
$(if $(_$(d)_DEP_OBJ), \
$(eval _$(1)_DEP_OBJ += $(_$(d)_DEP_OBJ))) \
$(if $(_$(d)_CFLAGS), \
$(eval _$(1)_CFLAGS += $(_$(d)_CFLAGS))) \
$(if $(_$(d)_LDFLAGS), \
$(eval _$(1)_LDFLAGS += $(_$(d)_LDFLAGS))) \
$(call file-add,$(_$(d)_FILE),$(_$(1)_OUTDIR)) \
) \
)
endef
# Aggregate all targets environment variables
$(foreach r, $(RULES), \
$(eval TARGET=) \
$(eval CROSSTARGET=) \
$(eval BUNDLE=) \
$(eval -include $(r)) \
$(foreach t, $(TARGET), \
$(call get-env,_TARGETS,$(t),$(subst ./,,$(dir $(r))))) \
$(foreach t, $(CROSSTARGET), \
$(call get-env,_CROSSTARGETS,$(t),$(subst ./,,$(dir $(r))))) \
$(foreach t, $(BUNDLE), \
$(call get-env,_BUNDLES,$(t),$(subst ./,,$(dir $(r))))) \
)
define file-rule
$(OUTDIR)$(1): $(_$(1)_SRC)
@mkdir -p $$(dir $$@)
cp $$< $$@
$(OUTDIR)$(1)-clean:
$(call rm-file,$$(@:%-clean=%))
$(call rm-dir,$$(dir $$@))
endef
define cobj-rule
$(eval -include $(2:%.o=%.d))
$(2): $(2:$(BUILDDIR)%.o=%.c)
@mkdir -p $$(dir $$@)
$$(CC) -MMD -c $$(CFLAGS) $$(_$(1)_CFLAGS) -o $$@ $$<
$(2)-clean:
$(call rm-file,$$(@:%-clean=%))
$(call rm-file,$$(@:%.o-clean=%.d))
$(call rm-dir,$$(dir $$@))
endef
define aobj-rule
$(2): $(2:$(BUILDDIR)%.o=%.s)
@mkdir -p $$(dir $$@)
$$(CC) -c $$(CFLAGS) $$(_$(1)_CFLAGS) -o $$@ $$<
$(2)-clean:
$(call rm-file,$$(@:%-clean=%))
$(call rm-dir,$$(dir $$@))
endef
define cross-cobj-rule
$(eval -include $(2:%.o=%.d))
$(2): $(2:$(BUILDDIR)%.o=%.c)
@mkdir -p $$(dir $$@)
$$(CROSSCC) -MMD -c $$(CROSSCFLAGS) $$(_$(1)_CFLAGS) -o $$@ $$<
$(2)-clean:
$(call rm-file,$$(@:%-clean=%))
$(call rm-file,$$(@:%.o-clean=%.d))
$(call rm-dir,$$(dir $$@))
endef
define cross-aobj-rule
$(2): $(2:$(BUILDDIR)%.o=%.s)
@mkdir -p $$(dir $$@)
$$(CROSSCC) -c $$(CROSSCFLAGS) $$(_$(1)_CFLAGS) -o $$@ $$<
$(2)-clean:
$(call rm-file,$$(@:%-clean=%))
$(call rm-dir,$$(dir $$@))
endef
define bundle-rule
$(foreach o, $(_$(1)_COBJ), $(call cobj-rule,$(1),$(o)))
$(foreach o, $(_$(1)_AOBJ), $(call aobj-rule,$(1),$(o)))
endef
define target-rule
ifneq ($(_$(1)_COBJ) $(_$(1)_AOBJ) $(_$(1)_DEP_OBJ),)
$(2): $(_$(1)_COBJ) $(_$(1)_AOBJ) $(_$(1)_DEP_OBJ)
@mkdir -p $$(dir $$@)
$$(CC) -o $$@ $$^ $$(LDFLAGS) $$(_$(1)_LDFLAGS)
$(2)-clean: $(_$(1)_COBJ:%=%-clean) $(_$(1)_AOBJ:%=%-clean) \
$(_$(1)_DEP_OBJ:%=%-clean)
$(call rm-file,$$(@:%-clean=%))
$(call rm-dir,$$(dir $$@))
$(foreach o, $(_$(1)_COBJ), $(call cobj-rule,$(1),$(o)))
$(foreach o, $(_$(1)_AOBJ), $(call aobj-rule,$(1),$(o)))
else
$(2):
$(2)-clean:
endif
endef
define cross-target-rule
ifneq ($(_$(1)_COBJ) $(_$(1)_AOBJ) $(_$(1)_DEP_OBJ),)
$(2).elf: $(_$(1)_COBJ) $(_$(1)_AOBJ) $(_$(1)_DEP_OBJ)
@mkdir -p $$(dir $$@)
$$(CROSSCC) -o $$@ $$^ $$(CROSSLDFLAGS) $$(_$(1)_LDFLAGS)
$(2): $(2).elf
$$(CROSSOBJCOPY) -O binary $$< $$@
$(2)-clean: $(_$(1)_COBJ:%=%-clean) $(_$(1)_AOBJ:%=%-clean) \
$(_$(1)_DEP_OBJ:%=%-clean)
$(call rm-file,$$(@:%-clean=%.elf))
$(call rm-file,$$(@:%-clean=%))
$(call rm-dir,$$(dir $$@))
$(foreach o, $(_$(1)_COBJ), $(call cross-cobj-rule,$(1),$(o)))
$(foreach o, $(_$(1)_AOBJ), $(call cross-aobj-rule,$(1),$(o)))
else
$(2):
$(2)-clean:
endif
endef
$(foreach t, $(_BUNDLES), \
$(eval _$(t)_COBJ = $(_$(t)_CSRC:%.c=$(BUILDDIR)%.o)) \
$(eval _$(t)_AOBJ = $(_$(t)_ASRC:%.s=$(BUILDDIR)%.o)) \
)
$(foreach t, $(_TARGETS), \
$(eval _$(t)_RULE = $(t:%=$(OUTDIR)$(_$(t)_OUTDIR)%)) \
$(eval _TARGETS_RULES = $(_TARGETS_RULES) $(_$(t)_RULE)) \
$(call get-deps,$(t)) \
$(eval _$(t)_COBJ = $(_$(t)_CSRC:%.c=$(BUILDDIR)%.o)) \
$(eval _$(t)_AOBJ = $(_$(t)_ASRC:%.s=$(BUILDDIR)%.o)) \
$(call file-add,$(_$(t)_FILE),$(_$(t)_OUTDIR)) \
)
$(foreach t, $(_CROSSTARGETS), \
$(eval _$(t)_RULE = $(t:%=$(OUTDIR)$(_$(t)_OUTDIR)%)) \
$(eval _CROSSTARGETS_RULES = $(_CROSSTARGETS_RULES) $(_$(t)_RULE)) \
$(call get-deps,$(t)) \
$(eval _$(t)_COBJ = $(_$(t)_CSRC:%.c=$(BUILDDIR)%.o)) \
$(eval _$(t)_AOBJ = $(_$(t)_ASRC:%.s=$(BUILDDIR)%.o)) \
$(call file-add,$(_$(t)_FILE),$(_$(t)_OUTDIR)) \
)
$(foreach t, $(_FILES), \
$(eval _$(t)_RULE = $(t:%=$(OUTDIR)%)) \
$(eval _FILES_RULES = $(_FILES_RULES) $(_$(t)_RULE)) \
)
all: $(_TARGETS_RULES) $(_CROSSTARGETS_RULES) $(_FILES_RULES)
$(foreach t, $(_FILES), \
$(eval $(call file-rule,$(t))))
$(foreach t, $(_BUNDLES), \
$(eval $(call bundle-rule,$(t))))
$(foreach t, $(_TARGETS), \
$(eval $(call target-rule,$(t),$(_$(t)_RULE))))
$(foreach t, $(_CROSSTARGETS), \
$(eval $(call cross-target-rule,$(t),$(_$(t)_RULE))))
.PHONY: clean
clean: $(_TARGETS_RULES:%=%-clean) $(_CROSSTARGETS_RULES:%=%-clean) \
$(_FILES_RULES:%=%-clean)
$(call rm-dir,$(BUILDDIR))
$(call rm-dir,$(OUTDIR))