Merge tag 'v3.0.0-rc1'
[qemu/ar7.git] / rules.mak
blob8851ab4e794a0e71e97ba1c3b79a356abe1a75d0
2 # These are used when we want to do substitutions without confusing Make
3 NULL :=
4 SPACE := $(NULL) #
5 COMMA := ,
7 # Don't use implicit rules or variables
8 # we have explicit rules for everything
9 MAKEFLAGS += -rR
11 # Files with this suffixes are final, don't try to generate them
12 # using implicit rules
13 %/trace-events:
14 %.hx:
15 %.py:
16 %.objs:
17 %.d:
18 %.h:
19 %.c:
20 %.cc:
21 %.cpp:
22 %.m:
23 %.mak:
24 clean-target:
26 # Flags for C++ compilation
27 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))
29 # Flags for dependency generation
30 QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
32 # Compiler searches the source file dir first, but in vpath builds
33 # we need to make it search the build dir too, before any other
34 # explicit search paths. There are two search locations in the build
35 # dir, one absolute and the other relative to the compiler working
36 # directory. These are the same for target-independent files, but
37 # different for target-dependent ones.
38 QEMU_LOCAL_INCLUDES = -iquote $(BUILD_DIR)/$(@D) -iquote $(@D)
40 WL_U := -Wl,-u,
41 find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
42 defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}')
43 undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}')
45 # All the .mo objects in -m variables are also added into corresponding -y
46 # variable in unnest-vars, but filtered out here, when LINK is called.
48 # The .mo objects are supposed to be linked as a DSO, for module build. So here
49 # they are only used as a placeholders to generate those "archive undefined"
50 # symbol options (-Wl,-u,$symbol_name), which are the archive functions
51 # referenced by the code in the DSO.
53 # Also the presence in -y variables will also guarantee they are built before
54 # linking executables that will load them. So we can look up symbol reference
55 # in LINK.
57 # This is necessary because the exectuable itself may not use the function, in
58 # which case the function would not be linked in. Then the DSO loading will
59 # fail because of the missing symbol.
60 process-archive-undefs = $(filter-out %.a %.mo,$1) \
61 $(addprefix $(WL_U), \
62 $(filter $(call defined-symbols,$(filter %.a, $1)), \
63 $(call undefined-symbols,$(filter %.mo,$1)))) \
64 $(filter %.a,$1)
66 extract-libs = $(strip $(foreach o,$(filter-out %.mo,$1),$($o-libs)))
67 expand-objs = $(strip $(sort $(filter %.o,$1)) \
68 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
69 $(filter-out %.o %.mo,$1))
71 %.o: %.c
72 $(call quiet-command,$(CC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
73 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
74 -c -o $@ $<,"CC","$(TARGET_DIR)$@")
75 %.o: %.rc
76 $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@")
78 # If we have a CXX we might have some C++ objects, in which case we
79 # must link with the C++ compiler, not the plain C compiler.
80 LINKPROG = $(or $(CXX),$(CC))
82 LINK = $(call quiet-command, $(LINKPROG) $(QEMU_LDFLAGS) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
83 $(call process-archive-undefs, $1) \
84 $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
86 %.o: %.S
87 $(call quiet-command,$(CCAS) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
88 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) \
89 -c -o $@ $<,"CCAS","$(TARGET_DIR)$@")
91 %.o: %.cc
92 $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
93 $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
94 -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
96 %.o: %.cpp
97 $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
98 $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
99 -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
101 %.o: %.m
102 $(call quiet-command,$(OBJCC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
103 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
104 -c -o $@ $<,"OBJC","$(TARGET_DIR)$@")
106 %.o: %.dtrace
107 $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@")
109 ifdef CONFIG_WIN32
110 DSO_OBJ_CFLAGS := -DBUILD_DSO
111 else
112 DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
113 endif
114 module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
115 %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
116 %$(DSOSUF): %.mo
117 $(call LINK,$^)
118 @# Copy to build root so modules can be loaded when program started without install
119 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@),"CP","$(subst /,-,$@)"))
122 LD_REL := $(CC) -nostdlib $(LD_REL_FLAGS)
124 %.mo:
125 $(call quiet-command,$(LD_REL) -o $@ $^,"LD","$(TARGET_DIR)$@")
127 .PHONY: modules
128 modules:
130 %$(EXESUF): %.o
131 $(call LINK,$(filter %.o %.a %.mo, $^))
133 %.a:
134 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"AR","$(TARGET_DIR)$@")
136 # Usage: $(call quiet-command,command and args,"NAME","args to print")
137 # This will run "command and args", and either:
138 # if V=1 just print the whole command and args
139 # otherwise print the 'quiet' output in the format " NAME args to print"
140 # NAME should be a short name of the command, 7 letters or fewer.
141 # If called with only a single argument, will print nothing in quiet mode.
142 quiet-command = $(if $(V),$1,$(if $(2),@printf " %-7s %s\n" $2 $3 && $1, @$1))
144 # cc-option
145 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
147 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
148 >/dev/null 2>&1 && echo OK), $2, $3)
149 cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
150 >/dev/null 2>&1 && echo OK), $2, $3)
152 VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
153 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
155 # install-prog list, dir
156 define install-prog
157 $(INSTALL_DIR) "$2"
158 $(INSTALL_PROG) $1 "$2"
159 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
160 endef
162 # find-in-path
163 # Usage: $(call find-in-path, prog)
164 # Looks in the PATH if the argument contains no slash, else only considers one
165 # specific directory. Returns an # empty string if the program doesn't exist
166 # there.
167 find-in-path = $(if $(findstring /, $1), \
168 $(wildcard $1), \
169 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
171 # Logical functions (for operating on y/n values like CONFIG_FOO vars)
172 # Inputs to these must be either "y" (true) or "n" or "" (both false)
173 # Output is always either "y" or "n".
174 # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
175 # Logical NOT
176 lnot = $(if $(subst n,,$1),n,y)
177 # Logical AND
178 land = $(if $(findstring yy,$1$2),y,n)
179 # Logical OR
180 lor = $(if $(findstring y,$1$2),y,n)
181 # Logical XOR (note that this is the inverse of leqv)
182 lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
183 # Logical equivalence (note that leqv "","n" is true)
184 leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
185 # Logical if: like make's $(if) but with an leqv-like test
186 lif = $(if $(subst n,,$1),$2,$3)
188 # String testing functions: inputs to these can be any string;
189 # the output is always either "y" or "n". Leading and trailing whitespace
190 # is ignored when comparing strings.
191 # String equality
192 eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
193 # String inequality
194 ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
195 # Emptiness/non-emptiness tests:
196 isempty = $(if $1,n,y)
197 notempty = $(if $1,y,n)
199 # Generate files with tracetool
200 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
202 # Generate timestamp files for .h include files
204 config-%.h: config-%.h-timestamp
205 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
207 config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
208 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@,"GEN","$(TARGET_DIR)config-$*.h")
210 .PHONY: clean-timestamp
211 clean-timestamp:
212 rm -f *.timestamp
213 clean: clean-timestamp
215 # will delete the target of a rule if commands exit with a nonzero exit status
216 .DELETE_ON_ERROR:
218 # save-vars
219 # Usage: $(call save-vars, vars)
220 # Save each variable $v in $vars as save-vars-$v, save their object's
221 # variables, then clear $v. saved-vars-$v contains the variables that
222 # where saved for the objects, in order to speedup load-vars.
223 define save-vars
224 $(foreach v,$1,
225 $(eval save-vars-$v := $(value $v))
226 $(eval saved-vars-$v := $(foreach o,$($v), \
227 $(if $($o-cflags), $o-cflags $(eval save-vars-$o-cflags := $($o-cflags))$(eval $o-cflags := )) \
228 $(if $($o-libs), $o-libs $(eval save-vars-$o-libs := $($o-libs))$(eval $o-libs := )) \
229 $(if $($o-objs), $o-objs $(eval save-vars-$o-objs := $($o-objs))$(eval $o-objs := ))))
230 $(eval $v := ))
231 endef
233 # load-vars
234 # Usage: $(call load-vars, vars, add_var)
235 # Load the saved value for each variable in @vars, and the per object
236 # variables.
237 # Append @add_var's current value to the loaded value.
238 define load-vars
239 $(eval $2-new-value := $(value $2))
240 $(foreach v,$1,
241 $(eval $v := $(value save-vars-$v))
242 $(foreach o,$(saved-vars-$v),
243 $(eval $o := $(save-vars-$o)) $(eval save-vars-$o := ))
244 $(eval save-vars-$v := )
245 $(eval saved-vars-$v := ))
246 $(eval $2 := $(value $2) $($2-new-value))
247 endef
249 # fix-paths
250 # Usage: $(call fix-paths, obj_path, src_path, vars)
251 # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
252 # directories in @vars.
253 define fix-paths
254 $(foreach v,$3,
255 $(foreach o,$($v),
256 $(if $($o-libs),
257 $(eval $1$o-libs := $($o-libs)))
258 $(if $($o-cflags),
259 $(eval $1$o-cflags := $($o-cflags)))
260 $(if $($o-objs),
261 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
262 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
263 $(addprefix $2,$(filter %/,$($v)))))
264 endef
266 # unnest-var-recursive
267 # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
269 # Unnest @var by including subdir Makefile.objs, while protect others in @vars
270 # unchanged.
272 # @obj_prefix is the starting point of object path prefix.
274 define unnest-var-recursive
275 $(eval dirs := $(sort $(filter %/,$($3))))
276 $(eval $3 := $(filter-out %/,$($3)))
277 $(foreach d,$(dirs:%/=%),
278 $(call save-vars,$2)
279 $(eval obj := $(if $1,$1/)$d)
280 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
281 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
282 $(call load-vars,$2,$3)
283 $(call unnest-var-recursive,$1,$2,$3))
284 endef
286 # unnest-vars
287 # Usage: $(call unnest-vars, obj_prefix, vars)
289 # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
290 # ending '/'.
292 # @vars: the list of variable names to unnest.
294 # This macro will scan subdirectories's Makefile.objs, include them, to build
295 # up each variable listed in @vars.
297 # Per object and per module cflags and libs are saved with relative path fixed
298 # as well, those variables include -libs, -cflags and -objs. Items in -objs are
299 # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
301 # All nested variables postfixed by -m in names are treated as DSO variables,
302 # and will be built as modules, if enabled.
304 # A simple example of the unnest:
306 # obj_prefix = ..
307 # vars = hot cold
308 # hot = fire.o sun.o season/
309 # cold = snow.o water/ season/
311 # Unnest through a faked source directory structure:
313 # SRC_PATH
314 # ├── water
315 # │ └── Makefile.objs──────────────────┐
316 # │ │ hot += steam.o │
317 # │ │ cold += ice.mo │
318 # │ │ ice.mo-libs := -licemaker │
319 # │ │ ice.mo-objs := ice1.o ice2.o │
320 # │ └──────────────────────────────┘
321 # │
322 # └── season
323 # └── Makefile.objs──────┐
324 # │ hot += summer.o │
325 # │ cold += winter.o │
326 # └──────────────────┘
328 # In the end, the result will be:
330 # hot = ../fire.o ../sun.o ../season/summer.o
331 # cold = ../snow.o ../water/ice.mo ../season/winter.o
332 # ../water/ice.mo-libs = -licemaker
333 # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
335 # Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
336 # included.
338 define unnest-vars
339 # In the case of target build (i.e. $1 == ..), fix path for top level
340 # Makefile.objs objects
341 $(if $1,$(call fix-paths,$1/,,$2))
343 # Descend and include every subdir Makefile.objs
344 $(foreach v, $2,
345 $(call unnest-var-recursive,$1,$2,$v)
346 # Pass the .mo-cflags and .mo-libs along to its member objects
347 $(foreach o, $(filter %.mo,$($v)),
348 $(foreach p,$($o-objs),
349 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
350 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
352 # For all %.mo objects that are directly added into -y, just expand them
353 $(foreach v,$(filter %-y,$2),
354 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
356 $(foreach v,$(filter %-m,$2),
357 # All .o found in *-m variables are single object modules, create .mo
358 # for them
359 $(foreach o,$(filter %.o,$($v)),
360 $(eval $(o:%.o=%.mo)-objs := $o))
361 # Now unify .o in -m variable to .mo
362 $(eval $v := $($v:%.o=%.mo))
363 $(eval modules-m += $($v))
365 # For module build, build shared libraries during "make modules"
366 # For non-module build, add -m to -y
367 $(if $(CONFIG_MODULES),
368 $(foreach o,$($v),
369 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
370 $(eval $o: $($o-objs)))
371 $(eval $(patsubst %-m,%-y,$v) += $($v))
372 $(eval modules: $($v:%.mo=%$(DSOSUF))),
373 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
375 # Post-process all the unnested vars
376 $(foreach v,$2,
377 $(foreach o, $(filter %.mo,$($v)),
378 # Find all the .mo objects in variables and add dependency rules
379 # according to .mo-objs. Report error if not set
380 $(if $($o-objs),
381 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
382 $(error $o added in $v but $o-objs is not set)))
383 $(shell mkdir -p ./ $(sort $(dir $($v))))
384 # Include all the .d files
385 $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
386 $(eval $v := $(filter-out %/,$($v))))
387 endef
389 TEXI2MAN = $(call quiet-command, \
390 perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $(TEXI2PODFLAGS) $< $@.pod && \
391 $(POD2MAN) --section=$(subst .,,$(suffix $@)) --center=" " --release=" " $@.pod > $@, \
392 "GEN","$@")
394 %.1:
395 $(call TEXI2MAN)
396 %.7:
397 $(call TEXI2MAN)
398 %.8:
399 $(call TEXI2MAN)