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