projects/aports·blob v2.10.0 · refs

337 lines · 8.9 KiB

1##
2# A set of makefile rules loosely based on kbuild.
3
4all: compile
5
6ifndef build
7
8toplevelrun:=yes
9
10##
11# Disable default rules and make output pretty.
12
13MAKEFLAGS += -rR --no-print-directory
14
15Makefile: ;
16
17ifdef V
18 ifeq ("$(origin V)", "command line")
19 VERBOSE = $(V)
20 endif
21endif
22ifndef VERBOSE
23 VERBOSE = 0
24endif
25
26ifeq ($(VERBOSE),1)
27 quiet =
28 Q =
29else
30 quiet=quiet_
31 Q = @
32endif
33
34ifneq ($(findstring s,$(MAKEFLAGS)),)
35 quiet=silent_
36endif
37
38export quiet Q VERBOSE
39
40##
41# Recursion helpers.
42srctree := $(CURDIR)
43objtree := $(CURDIR)
44
45export srctree objtree
46
47##
48# Consult SCM for better version string.
49
50TAGPREFIX ?= v
51
52GIT_REV := $(shell test -d .git && git describe || echo exported)
53ifneq ($(GIT_REV), exported)
54FULL_VERSION := $(patsubst $(TAGPREFIX)%,%,$(GIT_REV))
55else
56FULL_VERSION := $(VERSION)
57endif
58
59RCS_FIND_IGNORE := \( -name SCCS -o -name BitKeeper -o -name .svn -o -name CVS -o -name .pc -o -name .hg -o -name .git \) -prune -o
60
61export FULL_VERSION RCS_FIND_IGNORE
62
63##
64# Utilities and default flags for them.
65
66CROSS_COMPILE ?=
67CC := $(CROSS_COMPILE)gcc
68AR := $(CROSS_COMPILE)ar
69LD := $(CROSS_COMPILE)ld
70INSTALL := install
71INSTALLDIR := $(INSTALL) -d
72
73CFLAGS ?= -g -O2
74CFLAGS_ALL := -Werror -Wall -Wstrict-prototypes -D_GNU_SOURCE -std=gnu99 -fPIC
75CFLAGS_ALL += $(CFLAGS)
76
77LDFLAGS ?= -g
78LDFLAGS_ALL += $(LDFLAGS)
79
80export CC AR LD INSTALL INSTALLDIR CFLAGS_ALL LDFLAGS_ALL
81
82build :=
83
84endif
85
86##
87# Reset all variables.
88ifneq ($(origin targets),file)
89targets :=
90endif
91
92src :=
93obj :=
94
95src += $(build)
96obj := $(build)
97
98##
99# Include directory specific stuff
100
101ifneq ($(build),)
102$(build)/Makefile: ;
103include $(build)/Makefile
104endif
105
106##
107# Rules and helpers
108
109PHONY += all compile install clean FORCE
110
111# Convinient variables
112comma := ,
113squote := '
114empty :=
115space := $(empty) $(empty)
116
117# The temporary file to save gcc -MD generated dependencies must not
118# contain a comma
119depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
120
121build-dir = $(patsubst %/,%,$(dir $@))
122target-dir = $(dir $@)
123
124##
125# Build rules
126
127ifneq ($(NOCMDDEP),1)
128# Check if both arguments has same arguments. Result in empty string if equal
129# User may override this check using make NOCMDDEP=1
130# Check if both arguments has same arguments. Result is empty string if equal.
131# User may override this check using make KBUILD_NOCMDDEP=1
132arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
133 $(filter-out $(cmd_$@), $(cmd_$(1))) )
134endif
135
136# echo command.
137# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
138echo-cmd = $(if $($(quiet)cmd_$(1)),\
139 echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
140
141make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
142
143# printing commands
144cmd = @$(echo-cmd) $(cmd_$(1))
145
146# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
147dot-target = $(dir $@).$(notdir $@)
148
149# The temporary file to save gcc -MD generated dependencies must not
150# contain a comma
151depfile = $(subst $(comma),_,$(dot-target).d)
152
153# Escape single quote for use in echo statements
154escsq = $(subst $(squote),'\$(squote)',$1)
155
156# Find any prerequisites that is newer than target or that does not exist.
157# PHONY targets skipped in both cases.
158local-target-prereqs = %
159any-prereq = $(filter $(local-target-prereqs), $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^), $^))
160
161# Execute command if command has changed or prerequisite(s) are updated.
162#
163if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
164 @set -e; \
165 $(echo-cmd) $(cmd_$(1)); \
166 echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
167
168# Usage: $(call if_changed_rule,foo)
169# Will check if $(cmd_foo) or any of the prerequisites changed,
170# and if so will execute $(rule_foo).
171if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
172 @set -e; \
173 $(rule_$(1)))
174
175#####
176# Handle options to gcc.
177
178c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CPPFLAGS) \
179 $(CFLAGS_ALL) $(CFLAGS_EXTRA) $(CFLAGS_$(notdir $@))
180ld_flags = $(LDFLAGS_ALL) $(LDFLAGS_EXTRA) $(LDFLAGS_$(notdir $@))
181
182#####
183# Generated targets
184generate: $(addprefix $(obj)/,$(sort $(generate-y)))
185
186#####
187# Compile c-files.
188quiet_cmd_cc_o_c = CC $@
189
190cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
191
192define rule_cc_o_c
193 $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
194 (echo 'cmd_$@ := $(call make-cmd,cc_o_c)'; echo; cat $(depfile)) \
195 > $(dot-target).cmd ; \
196 rm $(depfile)
197endef
198
199$(obj)/%.o: override local-target-prereqs=%
200
201$(obj)/%.o: $(src)/%.c FORCE | generate
202 $(call if_changed_rule,cc_o_c)
203
204#####
205# Link static libraries
206#
207__arlibs := $(addprefix $(obj)/,$(sort $(libs-y)))
208arobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(libs-y),$($(m)-objs))))
209
210# link shared library
211quiet_cmd_ar = AR $@
212 cmd_ar = $(AR) rcs $@ $(addprefix $(obj)/,$($(@F)-objs))
213
214$(__arlibs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs))
215
216$(__arlibs): $(obj)/%: $(arobjs) FORCE
217 $(call if_changed,ar)
218
219targets += $(__arlibs) $(arobjs)
220
221#####
222# Link shared libraries
223#
224__shlibs := $(addprefix $(obj)/,$(sort $(shlibs-y)))
225shobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(shlibs-y),$($(m)-objs)))) $(sort $(foreach m,$(shlibs-y),$($(m)-libs)))
226
227# link shared library
228quiet_cmd_shlib = LD -shared $@
229 cmd_shlib = $(CC) $(ld_flags) -shared -o $@ \
230 $(addprefix $(obj)/,$($(@F)-objs)) \
231 $($(@F)-libs) \
232 $(LIBS) $(LIBS_$(@F))
233
234$(__shlibs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs)) $($(*F)-libs)
235
236$(__shlibs): $(obj)/%: $(shobjs) FORCE
237 $(call if_changed,shlib)
238
239targets += $(__shlibs) $(shobjs)
240
241#####
242# Link programs
243
244# Link an executable based on list of .o files, all plain c
245# host-cmulti -> executable
246__progs := $(addprefix $(obj)/,$(sort $(progs-y)))
247cobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(progs-y),$($(m)-objs)))) $(sort $(foreach m,$(progs-y),$($(m)-libs)))
248
249quiet_cmd_ld = LD $@
250 cmd_ld = $(CC) $(ld_flags) -o $@ \
251 $(addprefix $(obj)/,$($(@F)-objs)) $($(@F)-libs) \
252 $(LIBS) $(LIBS_$(@F))
253
254$(__progs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs)) $($(*F)-libs)
255
256$(__progs): $(obj)/%: $(cobjs) FORCE
257 $(call if_changed,ld)
258
259targets += $(__progs) $(cobjs)
260
261###
262# why - tell why a a target got build
263ifeq ($(VERBOSE),2)
264why = \
265 $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
266 $(if $(wildcard $@), \
267 $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
268 $(if $(arg-check), \
269 $(if $(cmd_$@),- due to command line change: $(arg-check), \
270 $(if $(filter $@, $(targets)), \
271 - due to missing .cmd file, \
272 - due to $(notdir $@) not in $$(targets) \
273 ) \
274 ) \
275 ) \
276 ), \
277 - due to target missing \
278 ) \
279 )
280
281echo-why = $(call escsq, $(strip $(why)))
282endif
283
284##
285# Top level rules.
286
287%/: FORCE
288 $(Q)$(MAKE) -f Make.rules build=$(build-dir) $(MAKECMDGOALS)
289
290compile: generate $(targets)
291 @:
292
293install: $(targets) FORCE
294
295generate:
296
297clean: $(filter %/,$(targets))
298ifeq ($(toplevelrun),yes)
299 $(Q)find . $(RCS_FIND_IGNORE) \
300 \( -name '*.[oas]' -o -name '.*.cmd' -o -name '.*.d' \) \
301 -type f -print | xargs rm -f
302endif
303 $(Q)rm -rf $(addprefix $(obj)/, \
304 $(sort $(progs-y) $(progs-n) $(progs-) \
305 $(shlibs-y) $(shlibs-n) $(shlibs-) \
306 $(libs-y) $(libs-n) $(libs-) \
307 $(generate-y) $(generate-n) $(generate-)))
308
309ifeq ($(origin VERSION),command line)
310DIST_VERSION=$(VERSION)
311else
312DIST_VERSION=$(FULL_VERSION)
313endif
314
315dist:
316 git archive --format tar --prefix=$(PACKAGE)-$(DIST_VERSION)/ \
317 $(TAGPREFIX)$(DIST_VERSION) \
318 | bzip2 -9 > $(PACKAGE)-$(DIST_VERSION).tar.bz2
319
320FORCE:
321
322# Read all saved command lines and dependencies for the $(targets) we
323# may be building above, using $(if_changed{,_dep}). As an
324# optimization, we don't need to read them if the target does not
325# exist, we will rebuild anyway in that case.
326
327targets := $(wildcard $(sort $(targets)))
328cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
329
330ifneq ($(cmd_files),)
331 include $(cmd_files)
332endif
333
334# Declare the contents of the .PHONY variable as phony. We keep that
335# information in a variable se we can use it in if_changed and friends.
336
337.PHONY: $(PHONY)