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