]> Git Repo - qemu.git/blame - rules.mak
configure: integrate Meson in the build system
[qemu.git] / rules.mak
CommitLineData
17759187 1
5609c512
AB
2# These are used when we want to do substitutions without confusing Make
3NULL :=
4SPACE := $(NULL) #
2f4e4dc2
FZ
5COMMA := ,
6
5ab28867
JQ
7# Don't use implicit rules or variables
8# we have explicit rules for everything
9MAKEFLAGS += -rR
10
11# Files with this suffixes are final, don't try to generate them
12# using implicit rules
a273f4ce
PB
13%/trace-events:
14%.hx:
15%.py:
16%.objs:
5ab28867
JQ
17%.d:
18%.h:
19%.c:
c3dc9fd5 20%.cc:
83f73fce 21%.cpp:
5ab28867
JQ
22%.m:
23%.mak:
dced7eec 24clean-target:
5ab28867 25
7ebf54bc 26# Flags for dependency generation
998b7b1d 27QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
02d5467e 28
ba78db44
DB
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.
9edc19c9 35QEMU_LOCAL_INCLUDES = -iquote $(BUILD_DIR)/$(@D) -iquote $(@D)
9d9199a0 36
c261d774 37WL_U := -Wl,-u,
4852ee95 38find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
c261d774
FZ
39defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}')
40undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}')
41
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.
44#
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.
49#
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.
53#
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.
57process-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)
62
5b1b6dbd 63extract-libs = $(strip $(foreach o,$(filter-out %.mo,$1),$($o-libs)))
17969268
FZ
64expand-objs = $(strip $(sort $(filter %.o,$1)) \
65 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
66 $(filter-out %.o %.mo,$1))
5c0d52bc 67
0e8c9214 68%.o: %.c
ac76f9d1 69 @mkdir -p $(dir $@)
ba78db44
DB
70 $(call quiet-command,$(CC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
71 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
72 -c -o $@ $<,"CC","$(TARGET_DIR)$@")
6821cdc7 73%.o: %.rc
0bdb12c7 74 $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@")
17759187 75
3144f78b
PM
76# If we have a CXX we might have some C++ objects, in which case we
77# must link with the C++ compiler, not the plain C compiler.
78LINKPROG = $(or $(CXX),$(CC))
79
db5adeaa 80LINK = $(call quiet-command, $(LINKPROG) $(CFLAGS) $(QEMU_LDFLAGS) -o $@ \
c261d774 81 $(call process-archive-undefs, $1) \
0bdb12c7 82 $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
44dc0ca3 83
5f6f0e27 84%.o: %.S
ba78db44
DB
85 $(call quiet-command,$(CCAS) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
86 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) \
87 -c -o $@ $<,"CCAS","$(TARGET_DIR)$@")
17759187 88
c3dc9fd5 89%.o: %.cc
ba78db44 90 $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
086d5f75 91 $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CXXFLAGS) $($@-cflags) \
ba78db44 92 -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
c3dc9fd5 93
83f73fce 94%.o: %.cpp
ba78db44 95 $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
086d5f75 96 $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CXXFLAGS) $($@-cflags) \
ba78db44 97 -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
83f73fce 98
17759187 99%.o: %.m
ba78db44
DB
100 $(call quiet-command,$(OBJCC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
101 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
102 -c -o $@ $<,"OBJC","$(TARGET_DIR)$@")
17759187 103
2c13ec50 104%.o: %.dtrace
0bdb12c7 105 $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@")
2c13ec50 106
d24697e1
FZ
107DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
108module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
db5adeaa 109%$(DSOSUF): QEMU_LDFLAGS += $(LDFLAGS_SHARED)
c261d774 110%$(DSOSUF): %.mo
17969268 111 $(call LINK,$^)
e26110cf 112 @# Copy to build root so modules can be loaded when program started without install
0bdb12c7 113 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@),"CP","$(subst /,-,$@)"))
17969268 114
c261d774 115
7ecf44a5 116LD_REL := $(CC) -nostdlib $(LD_REL_FLAGS)
c261d774
FZ
117
118%.mo:
0bdb12c7 119 $(call quiet-command,$(LD_REL) -o $@ $^,"LD","$(TARGET_DIR)$@")
c261d774 120
17969268
FZ
121.PHONY: modules
122modules:
123
3aa892d7 124%$(EXESUF): %.o
cefa2bbd 125 $(call LINK,$(filter %.o %.a %.mo, $^))
4f188f88 126
93a0dba7 127%.a:
0bdb12c7 128 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"AR","$(TARGET_DIR)$@")
93a0dba7 129
0bdb12c7
PM
130# Usage: $(call quiet-command,command and args,"NAME","args to print")
131# This will run "command and args", and either:
132# if V=1 just print the whole command and args
133# otherwise print the 'quiet' output in the format " NAME args to print"
134# NAME should be a short name of the command, 7 letters or fewer.
135# If called with only a single argument, will print nothing in quiet mode.
9df43317
PB
136quiet-command-run = $(if $(V),,$(if $2,printf " %-7s %s\n" $2 $3 && ))$1
137quiet-@ = $(if $(V),,@)
138quiet-command = $(quiet-@)$(call quiet-command-run,$1,$2,$3)
70071e17
JQ
139
140# cc-option
8a2e6ab5 141# Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
70071e17 142
fc3baad7
TM
143cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
144 >/dev/null 2>&1 && echo OK), $2, $3)
036999e9
PB
145cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
146 >/dev/null 2>&1 && echo OK), $2, $3)
1215c6e7 147
d52c454a 148VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc Kconfig% %.json.in
288e7bcc 149set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
076d2471 150
0d659426
MT
151# install-prog list, dir
152define install-prog
153 $(INSTALL_DIR) "$2"
154 $(INSTALL_PROG) $1 "$2"
155 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
156endef
157
2b2e59e6
PB
158# find-in-path
159# Usage: $(call find-in-path, prog)
160# Looks in the PATH if the argument contains no slash, else only considers one
161# specific directory. Returns an # empty string if the program doesn't exist
162# there.
88071589 163find-in-path = $(if $(findstring /, $1), \
2b2e59e6
PB
164 $(wildcard $1), \
165 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
166
837a2e26
PM
167# Logical functions (for operating on y/n values like CONFIG_FOO vars)
168# Inputs to these must be either "y" (true) or "n" or "" (both false)
169# Output is always either "y" or "n".
170# Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
171# Logical NOT
172lnot = $(if $(subst n,,$1),n,y)
173# Logical AND
174land = $(if $(findstring yy,$1$2),y,n)
175# Logical OR
176lor = $(if $(findstring y,$1$2),y,n)
177# Logical XOR (note that this is the inverse of leqv)
178lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
179# Logical equivalence (note that leqv "","n" is true)
180leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
181# Logical if: like make's $(if) but with an leqv-like test
182lif = $(if $(subst n,,$1),$2,$3)
183
9ef622e3
PM
184# String testing functions: inputs to these can be any string;
185# the output is always either "y" or "n". Leading and trailing whitespace
186# is ignored when comparing strings.
187# String equality
188eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
189# String inequality
190ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
191# Emptiness/non-emptiness tests:
192isempty = $(if $1,n,y)
193notempty = $(if $1,y,n)
194
c0424934
LV
195# Generate files with tracetool
196TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
197
1215c6e7
JQ
198# Generate timestamp files for .h include files
199
4b25966a
MT
200config-%.h: config-%.h-timestamp
201 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
1215c6e7 202
55335015 203config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
0bdb12c7 204 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@,"GEN","$(TARGET_DIR)config-$*.h")
7dbbbb0c 205
7586317b
MT
206.PHONY: clean-timestamp
207clean-timestamp:
208 rm -f *.timestamp
209clean: clean-timestamp
210
7dbbbb0c
MT
211# will delete the target of a rule if commands exit with a nonzero exit status
212.DELETE_ON_ERROR:
e05804ee 213
1c33ac57
FZ
214# save-vars
215# Usage: $(call save-vars, vars)
216# Save each variable $v in $vars as save-vars-$v, save their object's
5ffb3505
PB
217# variables, then clear $v. saved-vars-$v contains the variables that
218# where saved for the objects, in order to speedup load-vars.
1c33ac57
FZ
219define save-vars
220 $(foreach v,$1,
221 $(eval save-vars-$v := $(value $v))
5ffb3505
PB
222 $(eval saved-vars-$v := $(foreach o,$($v), \
223 $(if $($o-cflags), $o-cflags $(eval save-vars-$o-cflags := $($o-cflags))$(eval $o-cflags := )) \
224 $(if $($o-libs), $o-libs $(eval save-vars-$o-libs := $($o-libs))$(eval $o-libs := )) \
225 $(if $($o-objs), $o-objs $(eval save-vars-$o-objs := $($o-objs))$(eval $o-objs := ))))
1c33ac57 226 $(eval $v := ))
5c0d52bc
FZ
227endef
228
1c33ac57
FZ
229# load-vars
230# Usage: $(call load-vars, vars, add_var)
231# Load the saved value for each variable in @vars, and the per object
232# variables.
233# Append @add_var's current value to the loaded value.
234define load-vars
235 $(eval $2-new-value := $(value $2))
236 $(foreach v,$1,
237 $(eval $v := $(value save-vars-$v))
5ffb3505
PB
238 $(foreach o,$(saved-vars-$v),
239 $(eval $o := $(save-vars-$o)) $(eval save-vars-$o := ))
240 $(eval save-vars-$v := )
241 $(eval saved-vars-$v := ))
1c33ac57 242 $(eval $2 := $(value $2) $($2-new-value))
e05804ee
PB
243endef
244
1c33ac57
FZ
245# fix-paths
246# Usage: $(call fix-paths, obj_path, src_path, vars)
247# Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
248# directories in @vars.
249define fix-paths
250 $(foreach v,$3,
251 $(foreach o,$($v),
252 $(if $($o-libs),
253 $(eval $1$o-libs := $($o-libs)))
254 $(if $($o-cflags),
255 $(eval $1$o-cflags := $($o-cflags)))
256 $(if $($o-objs),
257 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
258 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
259 $(addprefix $2,$(filter %/,$($v)))))
e05804ee
PB
260endef
261
1c33ac57
FZ
262# unnest-var-recursive
263# Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
264#
265# Unnest @var by including subdir Makefile.objs, while protect others in @vars
266# unchanged.
267#
268# @obj_prefix is the starting point of object path prefix.
269#
270define unnest-var-recursive
271 $(eval dirs := $(sort $(filter %/,$($3))))
272 $(eval $3 := $(filter-out %/,$($3)))
273 $(foreach d,$(dirs:%/=%),
274 $(call save-vars,$2)
275 $(eval obj := $(if $1,$1/)$d)
276 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
277 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
278 $(call load-vars,$2,$3)
279 $(call unnest-var-recursive,$1,$2,$3))
17969268
FZ
280endef
281
1c33ac57
FZ
282# unnest-vars
283# Usage: $(call unnest-vars, obj_prefix, vars)
284#
285# @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
286# ending '/'.
287#
288# @vars: the list of variable names to unnest.
289#
290# This macro will scan subdirectories's Makefile.objs, include them, to build
291# up each variable listed in @vars.
292#
293# Per object and per module cflags and libs are saved with relative path fixed
294# as well, those variables include -libs, -cflags and -objs. Items in -objs are
295# also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
296#
297# All nested variables postfixed by -m in names are treated as DSO variables,
298# and will be built as modules, if enabled.
299#
300# A simple example of the unnest:
301#
302# obj_prefix = ..
303# vars = hot cold
304# hot = fire.o sun.o season/
305# cold = snow.o water/ season/
306#
307# Unnest through a faked source directory structure:
308#
309# SRC_PATH
310# ├── water
311# │ └── Makefile.objs──────────────────┐
312# │ │ hot += steam.o │
313# │ │ cold += ice.mo │
314# │ │ ice.mo-libs := -licemaker │
315# │ │ ice.mo-objs := ice1.o ice2.o │
316# │ └──────────────────────────────┘
317# │
318# └── season
319# └── Makefile.objs──────┐
320# │ hot += summer.o │
321# │ cold += winter.o │
322# └──────────────────┘
323#
324# In the end, the result will be:
325#
326# hot = ../fire.o ../sun.o ../season/summer.o
327# cold = ../snow.o ../water/ice.mo ../season/winter.o
328# ../water/ice.mo-libs = -licemaker
329# ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
330#
9ab71e5e 331# Note that 'hot' didn't include 'water/' in the input, so 'steam.o' is not
1c33ac57
FZ
332# included.
333#
e05804ee 334define unnest-vars
1c33ac57
FZ
335 # In the case of target build (i.e. $1 == ..), fix path for top level
336 # Makefile.objs objects
337 $(if $1,$(call fix-paths,$1/,,$2))
338
339 # Descend and include every subdir Makefile.objs
c88f68ec
FZ
340 $(foreach v, $2,
341 $(call unnest-var-recursive,$1,$2,$v)
342 # Pass the .mo-cflags and .mo-libs along to its member objects
343 $(foreach o, $(filter %.mo,$($v)),
344 $(foreach p,$($o-objs),
345 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
346 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
347
348 # For all %.mo objects that are directly added into -y, just expand them
349 $(foreach v,$(filter %-y,$2),
350 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
1c33ac57
FZ
351
352 $(foreach v,$(filter %-m,$2),
353 # All .o found in *-m variables are single object modules, create .mo
354 # for them
355 $(foreach o,$(filter %.o,$($v)),
356 $(eval $(o:%.o=%.mo)-objs := $o))
357 # Now unify .o in -m variable to .mo
358 $(eval $v := $($v:%.o=%.mo))
359 $(eval modules-m += $($v))
360
361 # For module build, build shared libraries during "make modules"
362 # For non-module build, add -m to -y
363 $(if $(CONFIG_MODULES),
c261d774 364 $(foreach o,$($v),
d24697e1 365 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
c261d774
FZ
366 $(eval $o: $($o-objs)))
367 $(eval $(patsubst %-m,%-y,$v) += $($v))
1c33ac57
FZ
368 $(eval modules: $($v:%.mo=%$(DSOSUF))),
369 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
370
371 # Post-process all the unnested vars
372 $(foreach v,$2,
373 $(foreach o, $(filter %.mo,$($v)),
374 # Find all the .mo objects in variables and add dependency rules
375 # according to .mo-objs. Report error if not set
376 $(if $($o-objs),
377 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
c88f68ec 378 $(error $o added in $v but $o-objs is not set)))
1c33ac57
FZ
379 $(shell mkdir -p ./ $(sort $(dir $($v))))
380 # Include all the .d files
27fa7479 381 $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
1c33ac57 382 $(eval $v := $(filter-out %/,$($v))))
e05804ee 383endef
76480423
MAL
384
385TEXI2MAN = $(call quiet-command, \
d59157ea 386 perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $(TEXI2PODFLAGS) $< [email protected] && \
76480423
MAL
387 $(POD2MAN) --section=$(subst .,,$(suffix $@)) --center=" " --release=" " [email protected] > $@, \
388 "GEN","$@")
389
390%.1:
391 $(call TEXI2MAN)
56e8bdd4
MAL
392%.7:
393 $(call TEXI2MAN)
76480423
MAL
394%.8:
395 $(call TEXI2MAN)
d52c454a
MAL
396
397GEN_SUBST = $(call quiet-command, \
398 sed -e "s!@libexecdir@!$(libexecdir)!g" < $< > $@, \
399 "GEN","$@")
400
401%.json: %.json.in
402 $(call GEN_SUBST)
e0f3728d
PM
403
404# Support for building multiple output files by atomically executing
405# a single rule which depends on several input files (so the rule
406# will be executed exactly once, not once per output file, and
407# not multiple times in parallel.) For more explanation see:
408# https://www.cmcrossroads.com/article/atomic-rules-gnu-make
409
410# Given a space-separated list of filenames, create the name of
411# a 'sentinel' file to use to indicate that they have been built.
412# We use fixed text on the end to avoid accidentally triggering
413# automatic pattern rules, and . on the start to make the file
414# not show up in ls output.
415sentinel = .$(subst $(SPACE),_,$(subst /,_,$1)).sentinel.
416
417# Define an atomic rule that builds multiple outputs from multiple inputs.
418# To use:
419# $(call atomic,out1 out2 ...,in1 in2 ...)
420# <TAB>rule to do the operation
421#
422# Make 4.3 will have native support for this, and you would be able
423# to instead write:
424# out1 out2 ... &: in1 in2 ...
425# <TAB>rule to do the operation
426#
427# The way this works is that it creates a make rule
428# "out1 out2 ... : sentinel-file ; @:" which says that the sentinel
429# depends on the dependencies, and the rule to do that is "do nothing".
430# Then we have a rule
431# "sentinel-file : in1 in2 ..."
432# whose commands start with "touch sentinel-file" and then continue
433# with the rule text provided by the user of this 'atomic' function.
434# The foreach... is there to delete the sentinel file if any of the
435# output files don't exist, so that we correctly rebuild in that situation.
436atomic = $(eval $1: $(call sentinel,$1) ; @:) \
437 $(call sentinel,$1) : $2 ; @touch $$@ \
438 $(foreach t,$1,$(if $(wildcard $t),,$(shell rm -f $(call sentinel,$1))))
1a28f878
PMD
439
440print-%:
441 @echo '$*=$($*)'
This page took 0.877835 seconds and 4 git commands to generate.