Merge tag 'v2.8.0-rc3'
[qemu/ar7.git] / rules.mak
blobc94904d59de1c64b78612f37a2dc352fd4687281
2 COMMA := ,
4 # Don't use implicit rules or variables
5 # we have explicit rules for everything
6 MAKEFLAGS += -rR
8 # Files with this suffixes are final, don't try to generate them
9 # using implicit rules
10 %.d:
11 %.h:
12 %.c:
13 %.cc:
14 %.cpp:
15 %.m:
16 %.mak:
17 clean-target:
19 # Flags for C++ compilation
20 QEMU_CXXFLAGS = -D__STDC_LIMIT_MACROS $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wno-override-init -Wold-style-declaration -Wold-style-definition -Wredundant-decls, $(QEMU_CFLAGS))
22 # Flags for dependency generation
23 QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
25 # Same as -I$(SRC_PATH) -I., but for the nested source/object directories
26 QEMU_INCLUDES += -I$(<D) -I$(@D)
28 WL_U := -Wl,-u,
29 find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
30 defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}')
31 undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}')
33 # All the .mo objects in -m variables are also added into corresponding -y
34 # variable in unnest-vars, but filtered out here, when LINK is called.
36 # The .mo objects are supposed to be linked as a DSO, for module build. So here
37 # they are only used as a placeholders to generate those "archive undefined"
38 # symbol options (-Wl,-u,$symbol_name), which are the archive functions
39 # referenced by the code in the DSO.
41 # Also the presence in -y variables will also guarantee they are built before
42 # linking executables that will load them. So we can look up symbol reference
43 # in LINK.
45 # This is necessary because the exectuable itself may not use the function, in
46 # which case the function would not be linked in. Then the DSO loading will
47 # fail because of the missing symbol.
48 process-archive-undefs = $(filter-out %.a %.mo,$1) \
49 $(addprefix $(WL_U), \
50 $(filter $(call defined-symbols,$(filter %.a, $1)), \
51 $(call undefined-symbols,$(filter %.mo,$1)))) \
52 $(filter %.a,$1)
54 extract-libs = $(strip $(foreach o,$(filter-out %.mo,$1),$($o-libs)))
55 expand-objs = $(strip $(sort $(filter %.o,$1)) \
56 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
57 $(filter-out %.o %.mo,$1))
59 %.o: %.c
60 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CC","$(TARGET_DIR)$@")
61 %.o: %.rc
62 $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@")
64 # If we have a CXX we might have some C++ objects, in which case we
65 # must link with the C++ compiler, not the plain C compiler.
66 LINKPROG = $(or $(CXX),$(CC))
68 LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
69 $(call process-archive-undefs, $1) \
70 $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
72 %.o: %.S
73 $(call quiet-command,$(CCAS) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<,"CCAS","$(TARGET_DIR)$@")
75 %.o: %.cc
76 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
78 %.o: %.cpp
79 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
81 %.o: %.m
82 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"OBJC","$(TARGET_DIR)$@")
84 %.o: %.dtrace
85 $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@")
87 ifdef CONFIG_WIN32
88 DSO_OBJ_CFLAGS := -DBUILD_DSO
89 else
90 DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
91 endif
92 module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
93 %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
94 %$(DSOSUF): %.mo
95 $(call LINK,$^)
96 @# Copy to build root so modules can be loaded when program started without install
97 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@),"CP","$(subst /,-,$@)"))
100 LD_REL := $(CC) -nostdlib $(LD_REL_FLAGS)
102 %.mo:
103 $(call quiet-command,$(LD_REL) -o $@ $^,"LD","$(TARGET_DIR)$@")
105 .PHONY: modules
106 modules:
108 %$(EXESUF): %.o
109 $(call LINK,$(filter %.o %.a %.mo, $^))
111 %.a:
112 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"AR","$(TARGET_DIR)$@")
114 # Usage: $(call quiet-command,command and args,"NAME","args to print")
115 # This will run "command and args", and either:
116 # if V=1 just print the whole command and args
117 # otherwise print the 'quiet' output in the format " NAME args to print"
118 # NAME should be a short name of the command, 7 letters or fewer.
119 # If called with only a single argument, will print nothing in quiet mode.
120 quiet-command = $(if $(V),$1,$(if $(2),@printf " %-7s %s\n" $2 $3 && $1, @$1))
122 # cc-option
123 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
125 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
126 >/dev/null 2>&1 && echo OK), $2, $3)
127 cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
128 >/dev/null 2>&1 && echo OK), $2, $3)
130 VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
131 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
133 # install-prog list, dir
134 define install-prog
135 $(INSTALL_DIR) "$2"
136 $(INSTALL_PROG) $1 "$2"
137 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
138 endef
140 # find-in-path
141 # Usage: $(call find-in-path, prog)
142 # Looks in the PATH if the argument contains no slash, else only considers one
143 # specific directory. Returns an # empty string if the program doesn't exist
144 # there.
145 find-in-path = $(if $(findstring /, $1), \
146 $(wildcard $1), \
147 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
149 # Logical functions (for operating on y/n values like CONFIG_FOO vars)
150 # Inputs to these must be either "y" (true) or "n" or "" (both false)
151 # Output is always either "y" or "n".
152 # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
153 # Logical NOT
154 lnot = $(if $(subst n,,$1),n,y)
155 # Logical AND
156 land = $(if $(findstring yy,$1$2),y,n)
157 # Logical OR
158 lor = $(if $(findstring y,$1$2),y,n)
159 # Logical XOR (note that this is the inverse of leqv)
160 lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
161 # Logical equivalence (note that leqv "","n" is true)
162 leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
163 # Logical if: like make's $(if) but with an leqv-like test
164 lif = $(if $(subst n,,$1),$2,$3)
166 # String testing functions: inputs to these can be any string;
167 # the output is always either "y" or "n". Leading and trailing whitespace
168 # is ignored when comparing strings.
169 # String equality
170 eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
171 # String inequality
172 ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
173 # Emptiness/non-emptiness tests:
174 isempty = $(if $1,n,y)
175 notempty = $(if $1,y,n)
177 # Generate files with tracetool
178 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
180 # Generate timestamp files for .h include files
182 config-%.h: config-%.h-timestamp
183 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
185 config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
186 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@,"GEN","$(TARGET_DIR)config-$*.h")
188 .PHONY: clean-timestamp
189 clean-timestamp:
190 rm -f *.timestamp
191 clean: clean-timestamp
193 # will delete the target of a rule if commands exit with a nonzero exit status
194 .DELETE_ON_ERROR:
196 # save-vars
197 # Usage: $(call save-vars, vars)
198 # Save each variable $v in $vars as save-vars-$v, save their object's
199 # variables, then clear $v.
200 define save-vars
201 $(foreach v,$1,
202 $(eval save-vars-$v := $(value $v))
203 $(foreach o,$($v),
204 $(foreach k,cflags libs objs,
205 $(if $($o-$k),
206 $(eval save-vars-$o-$k := $($o-$k))
207 $(eval $o-$k := ))))
208 $(eval $v := ))
209 endef
211 # load-vars
212 # Usage: $(call load-vars, vars, add_var)
213 # Load the saved value for each variable in @vars, and the per object
214 # variables.
215 # Append @add_var's current value to the loaded value.
216 define load-vars
217 $(eval $2-new-value := $(value $2))
218 $(foreach v,$1,
219 $(eval $v := $(value save-vars-$v))
220 $(foreach o,$($v),
221 $(foreach k,cflags libs objs,
222 $(if $(save-vars-$o-$k),
223 $(eval $o-$k := $(save-vars-$o-$k))
224 $(eval save-vars-$o-$k := ))))
225 $(eval save-vars-$v := ))
226 $(eval $2 := $(value $2) $($2-new-value))
227 endef
229 # fix-paths
230 # Usage: $(call fix-paths, obj_path, src_path, vars)
231 # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
232 # directories in @vars.
233 define fix-paths
234 $(foreach v,$3,
235 $(foreach o,$($v),
236 $(if $($o-libs),
237 $(eval $1$o-libs := $($o-libs)))
238 $(if $($o-cflags),
239 $(eval $1$o-cflags := $($o-cflags)))
240 $(if $($o-objs),
241 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
242 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
243 $(addprefix $2,$(filter %/,$($v)))))
244 endef
246 # unnest-var-recursive
247 # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
249 # Unnest @var by including subdir Makefile.objs, while protect others in @vars
250 # unchanged.
252 # @obj_prefix is the starting point of object path prefix.
254 define unnest-var-recursive
255 $(eval dirs := $(sort $(filter %/,$($3))))
256 $(eval $3 := $(filter-out %/,$($3)))
257 $(foreach d,$(dirs:%/=%),
258 $(call save-vars,$2)
259 $(eval obj := $(if $1,$1/)$d)
260 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
261 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
262 $(call load-vars,$2,$3)
263 $(call unnest-var-recursive,$1,$2,$3))
264 endef
266 # unnest-vars
267 # Usage: $(call unnest-vars, obj_prefix, vars)
269 # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
270 # ending '/'.
272 # @vars: the list of variable names to unnest.
274 # This macro will scan subdirectories's Makefile.objs, include them, to build
275 # up each variable listed in @vars.
277 # Per object and per module cflags and libs are saved with relative path fixed
278 # as well, those variables include -libs, -cflags and -objs. Items in -objs are
279 # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
281 # All nested variables postfixed by -m in names are treated as DSO variables,
282 # and will be built as modules, if enabled.
284 # A simple example of the unnest:
286 # obj_prefix = ..
287 # vars = hot cold
288 # hot = fire.o sun.o season/
289 # cold = snow.o water/ season/
291 # Unnest through a faked source directory structure:
293 # SRC_PATH
294 # ├── water
295 # │ └── Makefile.objs──────────────────┐
296 # │ │ hot += steam.o │
297 # │ │ cold += ice.mo │
298 # │ │ ice.mo-libs := -licemaker │
299 # │ │ ice.mo-objs := ice1.o ice2.o │
300 # │ └──────────────────────────────┘
301 # │
302 # └── season
303 # └── Makefile.objs──────┐
304 # │ hot += summer.o │
305 # │ cold += winter.o │
306 # └──────────────────┘
308 # In the end, the result will be:
310 # hot = ../fire.o ../sun.o ../season/summer.o
311 # cold = ../snow.o ../water/ice.mo ../season/winter.o
312 # ../water/ice.mo-libs = -licemaker
313 # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
315 # Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
316 # included.
318 define unnest-vars
319 # In the case of target build (i.e. $1 == ..), fix path for top level
320 # Makefile.objs objects
321 $(if $1,$(call fix-paths,$1/,,$2))
323 # Descend and include every subdir Makefile.objs
324 $(foreach v, $2,
325 $(call unnest-var-recursive,$1,$2,$v)
326 # Pass the .mo-cflags and .mo-libs along to its member objects
327 $(foreach o, $(filter %.mo,$($v)),
328 $(foreach p,$($o-objs),
329 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
330 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
332 # For all %.mo objects that are directly added into -y, just expand them
333 $(foreach v,$(filter %-y,$2),
334 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
336 $(foreach v,$(filter %-m,$2),
337 # All .o found in *-m variables are single object modules, create .mo
338 # for them
339 $(foreach o,$(filter %.o,$($v)),
340 $(eval $(o:%.o=%.mo)-objs := $o))
341 # Now unify .o in -m variable to .mo
342 $(eval $v := $($v:%.o=%.mo))
343 $(eval modules-m += $($v))
345 # For module build, build shared libraries during "make modules"
346 # For non-module build, add -m to -y
347 $(if $(CONFIG_MODULES),
348 $(foreach o,$($v),
349 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
350 $(eval $o: $($o-objs)))
351 $(eval $(patsubst %-m,%-y,$v) += $($v))
352 $(eval modules: $($v:%.mo=%$(DSOSUF))),
353 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
355 # Post-process all the unnested vars
356 $(foreach v,$2,
357 $(foreach o, $(filter %.mo,$($v)),
358 # Find all the .mo objects in variables and add dependency rules
359 # according to .mo-objs. Report error if not set
360 $(if $($o-objs),
361 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
362 $(error $o added in $v but $o-objs is not set)))
363 $(shell mkdir -p ./ $(sort $(dir $($v))))
364 # Include all the .d files
365 $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
366 $(eval $v := $(filter-out %/,$($v))))
367 endef