]> git.ipfire.org Git - thirdparty/qemu.git/blame - rules.mak
Update version for 2.8.1.1 release
[thirdparty/qemu.git] / rules.mak
CommitLineData
17759187 1
2f4e4dc2
FZ
2COMMA := ,
3
5ab28867
JQ
4# Don't use implicit rules or variables
5# we have explicit rules for everything
6MAKEFLAGS += -rR
7
8# Files with this suffixes are final, don't try to generate them
9# using implicit rules
10%.d:
11%.h:
12%.c:
c3dc9fd5 13%.cc:
83f73fce 14%.cpp:
5ab28867
JQ
15%.m:
16%.mak:
dced7eec 17clean-target:
5ab28867 18
83f73fce
TS
19# Flags for C++ compilation
20QEMU_CXXFLAGS = -D__STDC_LIMIT_MACROS $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wold-style-declaration -Wold-style-definition -Wredundant-decls, $(QEMU_CFLAGS))
21
7ebf54bc 22# Flags for dependency generation
998b7b1d 23QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
02d5467e 24
9d9199a0 25# Same as -I$(SRC_PATH) -I., but for the nested source/object directories
7e7da8e3 26QEMU_INCLUDES += -I$(<D) -I$(@D)
9d9199a0 27
c261d774 28WL_U := -Wl,-u,
4852ee95 29find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
c261d774
FZ
30defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}')
31undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}')
32
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.
35#
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.
40#
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.
44#
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.
48process-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)
53
5b1b6dbd 54extract-libs = $(strip $(foreach o,$(filter-out %.mo,$1),$($o-libs)))
17969268
FZ
55expand-objs = $(strip $(sort $(filter %.o,$1)) \
56 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
57 $(filter-out %.o %.mo,$1))
5c0d52bc 58
0e8c9214 59%.o: %.c
0bdb12c7 60 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CC","$(TARGET_DIR)$@")
6821cdc7 61%.o: %.rc
0bdb12c7 62 $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@")
17759187 63
3144f78b
PM
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.
66LINKPROG = $(or $(CXX),$(CC))
67
1c33ac57 68LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
c261d774 69 $(call process-archive-undefs, $1) \
0bdb12c7 70 $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
44dc0ca3 71
5f6f0e27 72%.o: %.S
0bdb12c7 73 $(call quiet-command,$(CCAS) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<,"CCAS","$(TARGET_DIR)$@")
17759187 74
c3dc9fd5 75%.o: %.cc
0bdb12c7 76 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
c3dc9fd5 77
83f73fce 78%.o: %.cpp
0bdb12c7 79 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
83f73fce 80
17759187 81%.o: %.m
0bdb12c7 82 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<,"OBJC","$(TARGET_DIR)$@")
17759187 83
2c13ec50 84%.o: %.dtrace
0bdb12c7 85 $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@")
2c13ec50 86
d24697e1
FZ
87DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
88module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
17969268 89%$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
c261d774 90%$(DSOSUF): %.mo
17969268 91 $(call LINK,$^)
e26110cf 92 @# Copy to build root so modules can be loaded when program started without install
0bdb12c7 93 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@),"CP","$(subst /,-,$@)"))
17969268 94
c261d774 95
7ecf44a5 96LD_REL := $(CC) -nostdlib $(LD_REL_FLAGS)
c261d774
FZ
97
98%.mo:
0bdb12c7 99 $(call quiet-command,$(LD_REL) -o $@ $^,"LD","$(TARGET_DIR)$@")
c261d774 100
17969268
FZ
101.PHONY: modules
102modules:
103
3aa892d7 104%$(EXESUF): %.o
cefa2bbd 105 $(call LINK,$(filter %.o %.a %.mo, $^))
4f188f88 106
93a0dba7 107%.a:
0bdb12c7 108 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"AR","$(TARGET_DIR)$@")
93a0dba7 109
0bdb12c7
PM
110# Usage: $(call quiet-command,command and args,"NAME","args to print")
111# This will run "command and args", and either:
112# if V=1 just print the whole command and args
113# otherwise print the 'quiet' output in the format " NAME args to print"
114# NAME should be a short name of the command, 7 letters or fewer.
115# If called with only a single argument, will print nothing in quiet mode.
116quiet-command = $(if $(V),$1,$(if $(2),@printf " %-7s %s\n" $2 $3 && $1, @$1))
70071e17
JQ
117
118# cc-option
8a2e6ab5 119# Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
70071e17 120
fc3baad7
TM
121cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
122 >/dev/null 2>&1 && echo OK), $2, $3)
036999e9
PB
123cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
124 >/dev/null 2>&1 && echo OK), $2, $3)
1215c6e7 125
c3dc9fd5 126VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
288e7bcc 127set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
076d2471 128
0d659426
MT
129# install-prog list, dir
130define install-prog
131 $(INSTALL_DIR) "$2"
132 $(INSTALL_PROG) $1 "$2"
133 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
134endef
135
2b2e59e6
PB
136# find-in-path
137# Usage: $(call find-in-path, prog)
138# Looks in the PATH if the argument contains no slash, else only considers one
139# specific directory. Returns an # empty string if the program doesn't exist
140# there.
88071589 141find-in-path = $(if $(findstring /, $1), \
2b2e59e6
PB
142 $(wildcard $1), \
143 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
144
837a2e26
PM
145# Logical functions (for operating on y/n values like CONFIG_FOO vars)
146# Inputs to these must be either "y" (true) or "n" or "" (both false)
147# Output is always either "y" or "n".
148# Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
149# Logical NOT
150lnot = $(if $(subst n,,$1),n,y)
151# Logical AND
152land = $(if $(findstring yy,$1$2),y,n)
153# Logical OR
154lor = $(if $(findstring y,$1$2),y,n)
155# Logical XOR (note that this is the inverse of leqv)
156lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
157# Logical equivalence (note that leqv "","n" is true)
158leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
159# Logical if: like make's $(if) but with an leqv-like test
160lif = $(if $(subst n,,$1),$2,$3)
161
9ef622e3
PM
162# String testing functions: inputs to these can be any string;
163# the output is always either "y" or "n". Leading and trailing whitespace
164# is ignored when comparing strings.
165# String equality
166eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
167# String inequality
168ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
169# Emptiness/non-emptiness tests:
170isempty = $(if $1,n,y)
171notempty = $(if $1,y,n)
172
c0424934
LV
173# Generate files with tracetool
174TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
175
1215c6e7
JQ
176# Generate timestamp files for .h include files
177
4b25966a
MT
178config-%.h: config-%.h-timestamp
179 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
1215c6e7 180
55335015 181config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
0bdb12c7 182 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@,"GEN","$(TARGET_DIR)config-$*.h")
7dbbbb0c 183
7586317b
MT
184.PHONY: clean-timestamp
185clean-timestamp:
186 rm -f *.timestamp
187clean: clean-timestamp
188
7dbbbb0c
MT
189# will delete the target of a rule if commands exit with a nonzero exit status
190.DELETE_ON_ERROR:
e05804ee 191
1c33ac57
FZ
192# save-vars
193# Usage: $(call save-vars, vars)
194# Save each variable $v in $vars as save-vars-$v, save their object's
195# variables, then clear $v.
196define save-vars
197 $(foreach v,$1,
198 $(eval save-vars-$v := $(value $v))
199 $(foreach o,$($v),
200 $(foreach k,cflags libs objs,
201 $(if $($o-$k),
202 $(eval save-vars-$o-$k := $($o-$k))
203 $(eval $o-$k := ))))
204 $(eval $v := ))
5c0d52bc
FZ
205endef
206
1c33ac57
FZ
207# load-vars
208# Usage: $(call load-vars, vars, add_var)
209# Load the saved value for each variable in @vars, and the per object
210# variables.
211# Append @add_var's current value to the loaded value.
212define load-vars
213 $(eval $2-new-value := $(value $2))
214 $(foreach v,$1,
215 $(eval $v := $(value save-vars-$v))
216 $(foreach o,$($v),
217 $(foreach k,cflags libs objs,
218 $(if $(save-vars-$o-$k),
219 $(eval $o-$k := $(save-vars-$o-$k))
220 $(eval save-vars-$o-$k := ))))
221 $(eval save-vars-$v := ))
222 $(eval $2 := $(value $2) $($2-new-value))
e05804ee
PB
223endef
224
1c33ac57
FZ
225# fix-paths
226# Usage: $(call fix-paths, obj_path, src_path, vars)
227# Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
228# directories in @vars.
229define fix-paths
230 $(foreach v,$3,
231 $(foreach o,$($v),
232 $(if $($o-libs),
233 $(eval $1$o-libs := $($o-libs)))
234 $(if $($o-cflags),
235 $(eval $1$o-cflags := $($o-cflags)))
236 $(if $($o-objs),
237 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
238 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
239 $(addprefix $2,$(filter %/,$($v)))))
e05804ee
PB
240endef
241
1c33ac57
FZ
242# unnest-var-recursive
243# Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
244#
245# Unnest @var by including subdir Makefile.objs, while protect others in @vars
246# unchanged.
247#
248# @obj_prefix is the starting point of object path prefix.
249#
250define unnest-var-recursive
251 $(eval dirs := $(sort $(filter %/,$($3))))
252 $(eval $3 := $(filter-out %/,$($3)))
253 $(foreach d,$(dirs:%/=%),
254 $(call save-vars,$2)
255 $(eval obj := $(if $1,$1/)$d)
256 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
257 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
258 $(call load-vars,$2,$3)
259 $(call unnest-var-recursive,$1,$2,$3))
17969268
FZ
260endef
261
1c33ac57
FZ
262# unnest-vars
263# Usage: $(call unnest-vars, obj_prefix, vars)
264#
265# @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
266# ending '/'.
267#
268# @vars: the list of variable names to unnest.
269#
270# This macro will scan subdirectories's Makefile.objs, include them, to build
271# up each variable listed in @vars.
272#
273# Per object and per module cflags and libs are saved with relative path fixed
274# as well, those variables include -libs, -cflags and -objs. Items in -objs are
275# also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
276#
277# All nested variables postfixed by -m in names are treated as DSO variables,
278# and will be built as modules, if enabled.
279#
280# A simple example of the unnest:
281#
282# obj_prefix = ..
283# vars = hot cold
284# hot = fire.o sun.o season/
285# cold = snow.o water/ season/
286#
287# Unnest through a faked source directory structure:
288#
289# SRC_PATH
290# ├── water
291# │ └── Makefile.objs──────────────────┐
292# │ │ hot += steam.o │
293# │ │ cold += ice.mo │
294# │ │ ice.mo-libs := -licemaker │
295# │ │ ice.mo-objs := ice1.o ice2.o │
296# │ └──────────────────────────────┘
297# │
298# └── season
299# └── Makefile.objs──────┐
300# │ hot += summer.o │
301# │ cold += winter.o │
302# └──────────────────┘
303#
304# In the end, the result will be:
305#
306# hot = ../fire.o ../sun.o ../season/summer.o
307# cold = ../snow.o ../water/ice.mo ../season/winter.o
308# ../water/ice.mo-libs = -licemaker
309# ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
310#
311# Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
312# included.
313#
e05804ee 314define unnest-vars
1c33ac57
FZ
315 # In the case of target build (i.e. $1 == ..), fix path for top level
316 # Makefile.objs objects
317 $(if $1,$(call fix-paths,$1/,,$2))
318
319 # Descend and include every subdir Makefile.objs
c88f68ec
FZ
320 $(foreach v, $2,
321 $(call unnest-var-recursive,$1,$2,$v)
322 # Pass the .mo-cflags and .mo-libs along to its member objects
323 $(foreach o, $(filter %.mo,$($v)),
324 $(foreach p,$($o-objs),
325 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
326 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
327
328 # For all %.mo objects that are directly added into -y, just expand them
329 $(foreach v,$(filter %-y,$2),
330 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
1c33ac57
FZ
331
332 $(foreach v,$(filter %-m,$2),
333 # All .o found in *-m variables are single object modules, create .mo
334 # for them
335 $(foreach o,$(filter %.o,$($v)),
336 $(eval $(o:%.o=%.mo)-objs := $o))
337 # Now unify .o in -m variable to .mo
338 $(eval $v := $($v:%.o=%.mo))
339 $(eval modules-m += $($v))
340
341 # For module build, build shared libraries during "make modules"
342 # For non-module build, add -m to -y
343 $(if $(CONFIG_MODULES),
c261d774 344 $(foreach o,$($v),
d24697e1 345 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
c261d774
FZ
346 $(eval $o: $($o-objs)))
347 $(eval $(patsubst %-m,%-y,$v) += $($v))
1c33ac57
FZ
348 $(eval modules: $($v:%.mo=%$(DSOSUF))),
349 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
350
351 # Post-process all the unnested vars
352 $(foreach v,$2,
353 $(foreach o, $(filter %.mo,$($v)),
354 # Find all the .mo objects in variables and add dependency rules
355 # according to .mo-objs. Report error if not set
356 $(if $($o-objs),
357 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
c88f68ec 358 $(error $o added in $v but $o-objs is not set)))
1c33ac57
FZ
359 $(shell mkdir -p ./ $(sort $(dir $($v))))
360 # Include all the .d files
27fa7479 361 $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
1c33ac57 362 $(eval $v := $(filter-out %/,$($v))))
e05804ee 363endef