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