projects/aports·blob v2.0.4 · refs

307 lines · 8.0 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
68LD := $(CROSS_COMPILE)ld
69INSTALL := install
70INSTALLDIR := $(INSTALL) -d
71
72CFLAGS ?= -g -O2
73CFLAGS_ALL := -Werror -Wall -Wstrict-prototypes -D_GNU_SOURCE -std=gnu99 -fPIC
74CFLAGS_ALL += $(CFLAGS)
75
76LDFLAGS ?= -g
77LDFLAGS_ALL += $(LDFLAGS)
78
79export CC LD INSTALL INSTALLDIR CFLAGS_ALL LDFLAGS_ALL
80
81build :=
82
83endif
84
85##
86# Reset all variables.
87ifneq ($(origin targets),file)
88targets :=
89endif
90
91src :=
92obj :=
93
94src += $(build)
95obj := $(build)
96
97##
98# Include directory specific stuff
99
100ifneq ($(build),)
101$(build)/Makefile: ;
102include $(build)/Makefile
103endif
104
105##
106# Rules and helpers
107
108PHONY += all compile install clean FORCE
109
110# Convinient variables
111comma := ,
112squote := '
113empty :=
114space := $(empty) $(empty)
115
116# The temporary file to save gcc -MD generated dependencies must not
117# contain a comma
118depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
119
120build-dir = $(patsubst %/,%,$(dir $@))
121target-dir = $(dir $@)
122
123##
124# Build rules
125
126ifneq ($(NOCMDDEP),1)
127# Check if both arguments has same arguments. Result in empty string if equal
128# User may override this check using make NOCMDDEP=1
129# Check if both arguments has same arguments. Result is empty string if equal.
130# User may override this check using make KBUILD_NOCMDDEP=1
131arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
132 $(filter-out $(cmd_$@), $(cmd_$(1))) )
133endif
134
135# echo command.
136# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
137echo-cmd = $(if $($(quiet)cmd_$(1)),\
138 echo ' $(call escsq,$($(quiet)cmd_$(1)))$(echo-why)';)
139
140make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1)))))
141
142# printing commands
143cmd = @$(echo-cmd) $(cmd_$(1))
144
145# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
146dot-target = $(dir $@).$(notdir $@)
147
148# The temporary file to save gcc -MD generated dependencies must not
149# contain a comma
150depfile = $(subst $(comma),_,$(dot-target).d)
151
152# Escape single quote for use in echo statements
153escsq = $(subst $(squote),'\$(squote)',$1)
154
155# Find any prerequisites that is newer than target or that does not exist.
156# PHONY targets skipped in both cases.
157local-target-prereqs = %
158any-prereq = $(filter $(local-target-prereqs), $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^), $^))
159
160# Execute command if command has changed or prerequisite(s) are updated.
161#
162if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
163 @set -e; \
164 $(echo-cmd) $(cmd_$(1)); \
165 echo 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
166
167# Usage: $(call if_changed_rule,foo)
168# Will check if $(cmd_foo) or any of the prerequisites changed,
169# and if so will execute $(rule_foo).
170if_changed_rule = $(if $(strip $(any-prereq) $(arg-check) ), \
171 @set -e; \
172 $(rule_$(1)))
173
174#####
175# Handle options to gcc.
176
177c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS_ALL) $(CFLAGS_EXTRA) \
178 $(CFLAGS_$(notdir $@))
179ld_flags = $(LDFLAGS_ALL) $(LDFLAGS_EXTRA) $(LDFLAGS_$(notdir $@))
180
181#####
182# Compile c-files.
183quiet_cmd_cc_o_c = CC $@
184
185cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
186
187define rule_cc_o_c
188 $(call echo-cmd,cc_o_c) $(cmd_cc_o_c); \
189 (echo 'cmd_$@ := $(call make-cmd,cc_o_c)'; echo; cat $(depfile)) \
190 > $(dot-target).cmd ; \
191 rm $(depfile)
192endef
193
194$(obj)/%.o: override local-target-prereqs=%
195
196$(obj)/%.o: $(src)/%.c FORCE
197 $(call if_changed_rule,cc_o_c)
198
199#####
200# Link shared libraries
201#
202__shlibs := $(addprefix $(obj)/,$(sort $(shlibs-y)))
203shobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(shlibs-y),$($(m)-objs))))
204
205# link shared library
206quiet_cmd_shlib = LD -shared $@
207 cmd_shlib = $(CC) $(ld_flags) -shared -o $@ \
208 $(addprefix $(obj)/,$($(@F)-objs)) \
209 $(LIBS) $(LIBS_$(@F))
210
211$(__shlibs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs))
212
213$(__shlibs): $(obj)/%: $(shobjs) FORCE
214 $(call if_changed,shlib)
215
216targets += $(__shlibs) $(shobjs)
217#####
218# Link programs
219
220# Link an executable based on list of .o files, all plain c
221# host-cmulti -> executable
222__progs := $(addprefix $(obj)/,$(sort $(progs-y)))
223cobjs := $(addprefix $(obj)/,$(sort $(foreach m,$(progs-y),$($(m)-objs))))
224
225quiet_cmd_ld = LD $@
226 cmd_ld = $(CC) $(ld_flags) -o $@ \
227 $(addprefix $(obj)/,$($(@F)-objs)) \
228 $(LIBS) $(LIBS_$(@F))
229
230$(__progs): override local-target-prereqs=$(addprefix $(obj)/,$($(*F)-objs))
231
232$(__progs): $(obj)/%: $(cobjs) FORCE
233 $(call if_changed,ld)
234
235targets += $(__progs) $(cobjs)
236
237###
238# why - tell why a a target got build
239ifeq ($(VERBOSE),2)
240why = \
241 $(if $(filter $@, $(PHONY)),- due to target is PHONY, \
242 $(if $(wildcard $@), \
243 $(if $(strip $(any-prereq)),- due to: $(any-prereq), \
244 $(if $(arg-check), \
245 $(if $(cmd_$@),- due to command line change: $(arg-check), \
246 $(if $(filter $@, $(targets)), \
247 - due to missing .cmd file, \
248 - due to $(notdir $@) not in $$(targets) \
249 ) \
250 ) \
251 ) \
252 ), \
253 - due to target missing \
254 ) \
255 )
256
257echo-why = $(call escsq, $(strip $(why)))
258endif
259
260##
261# Top level rules.
262
263%/: FORCE
264 $(Q)$(MAKE) -f Make.rules build=$(build-dir) $(MAKECMDGOALS)
265
266compile: $(targets)
267 @:
268
269install: $(targets) FORCE
270
271clean: $(filter %/,$(targets))
272ifeq ($(toplevelrun),yes)
273 $(Q)find . $(RCS_FIND_IGNORE) \
274 \( -name '*.[oas]' -o -name '.*.cmd' -o -name '.*.d' \) \
275 -type f -print | xargs rm -f
276endif
277 $(Q)rm -rf $(addprefix $(obj)/,$(sort $(progs-y) $(progs-n) $(progs-) $(shlibs-y) $(shlibs-n) $(shlibs-)))
278
279ifeq ($(origin VERSION),command line)
280DIST_VERSION=$(VERSION)
281else
282DIST_VERSION=$(FULL_VERSION)
283endif
284
285dist:
286 git archive --format tar --prefix=$(PACKAGE)-$(DIST_VERSION)/ \
287 $(TAGPREFIX)$(DIST_VERSION) \
288 | bzip2 -9 > $(PACKAGE)-$(DIST_VERSION).tar.bz2
289
290FORCE:
291
292# Read all saved command lines and dependencies for the $(targets) we
293# may be building above, using $(if_changed{,_dep}). As an
294# optimization, we don't need to read them if the target does not
295# exist, we will rebuild anyway in that case.
296
297targets := $(wildcard $(sort $(targets)))
298cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
299
300ifneq ($(cmd_files),)
301 include $(cmd_files)
302endif
303
304# Declare the contents of the .PHONY variable as phony. We keep that
305# information in a variable se we can use it in if_changed and friends.
306
307.PHONY: $(PHONY)