blob: 99cd0b3371c00396439a5100d7798c18e60c083b [file] [log] [blame]
aliguori17759182009-01-21 18:12:52 +00001
Fam Zheng2f4e4dc2016-06-01 12:25:15 +08002COMMA := ,
3
Juan Quintela5ab28862009-10-06 21:11:14 +02004# 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:
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000013%.cc:
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040014%.cpp:
Juan Quintela5ab28862009-10-06 21:11:14 +020015%.m:
16%.mak:
17
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040018# 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
Stefan Weil7ebf54b2009-11-19 20:07:52 +010021# Flags for dependency generation
Victor Kaplansky998b7b12015-08-09 12:39:53 +030022QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
malc02d54672009-10-11 17:08:57 +040023
Paolo Bonzini9d9199a2012-09-17 10:21:52 +020024# Same as -I$(SRC_PATH) -I., but for the nested source/object directories
Dunrong Huang7e7da8e2013-04-29 22:52:12 +080025QEMU_INCLUDES += -I$(<D) -I$(@D)
Paolo Bonzini9d9199a2012-09-17 10:21:52 +020026
Fam Zhengc261d772014-09-01 18:35:10 +080027WL_U := -Wl,-u,
Stefan Weil4852ee92014-09-18 21:55:08 +020028find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
Fam Zhengc261d772014-09-01 18:35:10 +080029defined-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
Paolo Bonzinif2770152014-06-16 16:43:25 +020053extract-libs = $(strip $(foreach o,$1,$($o-libs)))
Fam Zheng17969262014-02-10 14:48:56 +080054expand-objs = $(strip $(sort $(filter %.o,$1)) \
55 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
56 $(filter-out %.o %.mo,$1))
Fam Zheng5c0d52b2014-02-10 14:48:53 +080057
Andreas Färber0e8c9212010-01-06 20:24:05 +010058%.o: %.c
Fam Zheng5c0d52b2014-02-10 14:48:53 +080059 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CC $(TARGET_DIR)$@")
Paolo Bonzini6821cdc2013-04-27 00:25:31 +020060%.o: %.rc
61 $(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000062
Peter Maydell3144f782014-02-05 17:27:27 +000063# 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
Fam Zheng1c33ac52014-05-27 15:54:19 +080067LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
Fam Zhengc261d772014-09-01 18:35:10 +080068 $(call process-archive-undefs, $1) \
69 $(version-obj-y) $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@")
Alon Levy44dc0ca2011-05-15 11:51:28 +030070
Richard Henderson5f6f0e22016-06-23 10:39:18 -070071%.o: %.S
Richard Hendersoncdbd7272016-07-07 21:49:36 -070072 $(call quiet-command,$(CCAS) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," CCAS $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000073
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000074%.o: %.cc
Paolo Bonzini0db564e2014-05-08 14:34:09 +020075 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000076
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040077%.o: %.cpp
Paolo Bonzini0db564e2014-05-08 14:34:09 +020078 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040079
aliguori17759182009-01-21 18:12:52 +000080%.o: %.m
Paolo Bonzini0db564e2014-05-08 14:34:09 +020081 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000082
Paolo Bonzini2c13ec52012-12-21 09:23:18 +010083%.o: %.dtrace
84 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
85
Fam Zhengd24697e2015-05-07 14:55:15 +080086DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
87module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
Fam Zheng17969262014-02-10 14:48:56 +080088%$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
Fam Zhengc261d772014-09-01 18:35:10 +080089%$(DSOSUF): %.mo
Fam Zheng17969262014-02-10 14:48:56 +080090 $(call LINK,$^)
Fam Zhenge26110c2014-02-10 14:48:57 +080091 @# Copy to build root so modules can be loaded when program started without install
92 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@), " CP $(subst /,-,$@)"))
Fam Zheng17969262014-02-10 14:48:56 +080093
Fam Zhengc261d772014-09-01 18:35:10 +080094
James Clarke6969ec62016-06-06 12:02:50 +010095LD_REL := $(CC) -nostdlib -Wl,-r $(LD_REL_FLAGS)
Fam Zhengc261d772014-09-01 18:35:10 +080096
97%.mo:
98 $(call quiet-command,$(LD_REL) -o $@ $^," LD -r $(TARGET_DIR)$@")
99
Fam Zheng17969262014-02-10 14:48:56 +0800100.PHONY: modules
101modules:
102
aliguori3aa892d2009-01-21 18:13:02 +0000103%$(EXESUF): %.o
Michael S. Tsirkincefa2bb2016-02-17 16:59:36 +0200104 $(call LINK,$(filter %.o %.a %.mo, $^))
aliguori4f188f82009-01-21 18:13:09 +0000105
aliguori93a0dba2009-01-21 18:13:16 +0000106%.a:
aliguori28c699a2009-01-26 17:07:46 +0000107 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
aliguori93a0dba2009-01-21 18:13:16 +0000108
aliguori28c699a2009-01-26 17:07:46 +0000109quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
Juan Quintela70071e12009-07-21 14:11:18 +0200110
111# cc-option
Juan Quintela8a2e6ab2009-08-31 00:48:45 +0200112# Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
Juan Quintela70071e12009-07-21 14:11:18 +0200113
Thomas Monjalonfc3baad2009-09-11 18:45:40 +0200114cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
115 >/dev/null 2>&1 && echo OK), $2, $3)
Paolo Bonzini036999e2016-07-20 21:36:49 +0200116cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
117 >/dev/null 2>&1 && echo OK), $2, $3)
Juan Quintela1215c6e2009-10-07 02:40:58 +0200118
Peter Maydellc3dc9fd2014-02-05 17:27:27 +0000119VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
Nathan Froyd288e7bc2010-04-26 14:52:23 -0700120set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
Paolo Bonzini076d2472009-12-21 10:06:55 +0100121
Michael Tokarev0d659422014-06-22 10:55:23 +0400122# install-prog list, dir
123define install-prog
124 $(INSTALL_DIR) "$2"
125 $(INSTALL_PROG) $1 "$2"
126 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
127endef
128
Paolo Bonzini2b2e59e2010-10-21 10:18:40 +0200129# find-in-path
130# Usage: $(call find-in-path, prog)
131# Looks in the PATH if the argument contains no slash, else only considers one
132# specific directory. Returns an # empty string if the program doesn't exist
133# there.
134find-in-path = $(if $(find-string /, $1), \
135 $(wildcard $1), \
136 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
137
Peter Maydell837a2e22013-09-13 18:25:51 +0100138# Logical functions (for operating on y/n values like CONFIG_FOO vars)
139# Inputs to these must be either "y" (true) or "n" or "" (both false)
140# Output is always either "y" or "n".
141# Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
142# Logical NOT
143lnot = $(if $(subst n,,$1),n,y)
144# Logical AND
145land = $(if $(findstring yy,$1$2),y,n)
146# Logical OR
147lor = $(if $(findstring y,$1$2),y,n)
148# Logical XOR (note that this is the inverse of leqv)
149lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
150# Logical equivalence (note that leqv "","n" is true)
151leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
152# Logical if: like make's $(if) but with an leqv-like test
153lif = $(if $(subst n,,$1),$2,$3)
154
Peter Maydell9ef622e2013-09-13 18:25:52 +0100155# String testing functions: inputs to these can be any string;
156# the output is always either "y" or "n". Leading and trailing whitespace
157# is ignored when comparing strings.
158# String equality
159eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
160# String inequality
161ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
162# Emptiness/non-emptiness tests:
163isempty = $(if $1,n,y)
164notempty = $(if $1,y,n)
165
Lluís Vilanovac0424932012-04-18 20:15:45 +0200166# Generate files with tracetool
167TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
168
Juan Quintela1215c6e2009-10-07 02:40:58 +0200169# Generate timestamp files for .h include files
170
Michael S. Tsirkin4b259662013-01-15 13:12:35 +0200171config-%.h: config-%.h-timestamp
172 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
Juan Quintela1215c6e2009-10-07 02:40:58 +0200173
Paolo Bonzini55335012016-06-07 13:25:24 +0200174config-%.h-timestamp: config-%.mak $(SRC_PATH)/scripts/create_config
Michael S. Tsirkin4b259662013-01-15 13:12:35 +0200175 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
Michael S. Tsirkin7dbbbb02009-12-07 21:04:52 +0200176
Michael S. Tsirkin75863172013-01-15 13:27:54 +0200177.PHONY: clean-timestamp
178clean-timestamp:
179 rm -f *.timestamp
180clean: clean-timestamp
181
Michael S. Tsirkin7dbbbb02009-12-07 21:04:52 +0200182# will delete the target of a rule if commands exit with a nonzero exit status
183.DELETE_ON_ERROR:
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200184
Fam Zheng1c33ac52014-05-27 15:54:19 +0800185# save-vars
186# Usage: $(call save-vars, vars)
187# Save each variable $v in $vars as save-vars-$v, save their object's
188# variables, then clear $v.
189define save-vars
190 $(foreach v,$1,
191 $(eval save-vars-$v := $(value $v))
192 $(foreach o,$($v),
193 $(foreach k,cflags libs objs,
194 $(if $($o-$k),
195 $(eval save-vars-$o-$k := $($o-$k))
196 $(eval $o-$k := ))))
197 $(eval $v := ))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200198endef
199
Fam Zheng1c33ac52014-05-27 15:54:19 +0800200# load-vars
201# Usage: $(call load-vars, vars, add_var)
202# Load the saved value for each variable in @vars, and the per object
203# variables.
204# Append @add_var's current value to the loaded value.
205define load-vars
206 $(eval $2-new-value := $(value $2))
207 $(foreach v,$1,
208 $(eval $v := $(value save-vars-$v))
209 $(foreach o,$($v),
210 $(foreach k,cflags libs objs,
211 $(if $(save-vars-$o-$k),
212 $(eval $o-$k := $(save-vars-$o-$k))
213 $(eval save-vars-$o-$k := ))))
214 $(eval save-vars-$v := ))
215 $(eval $2 := $(value $2) $($2-new-value))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200216endef
217
Fam Zheng1c33ac52014-05-27 15:54:19 +0800218# fix-paths
219# Usage: $(call fix-paths, obj_path, src_path, vars)
220# Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
221# directories in @vars.
222define fix-paths
223 $(foreach v,$3,
224 $(foreach o,$($v),
225 $(if $($o-libs),
226 $(eval $1$o-libs := $($o-libs)))
227 $(if $($o-cflags),
228 $(eval $1$o-cflags := $($o-cflags)))
229 $(if $($o-objs),
230 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
231 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
232 $(addprefix $2,$(filter %/,$($v)))))
Fam Zheng5c0d52b2014-02-10 14:48:53 +0800233endef
234
Fam Zheng1c33ac52014-05-27 15:54:19 +0800235# unnest-var-recursive
236# Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
237#
238# Unnest @var by including subdir Makefile.objs, while protect others in @vars
239# unchanged.
240#
241# @obj_prefix is the starting point of object path prefix.
242#
243define unnest-var-recursive
244 $(eval dirs := $(sort $(filter %/,$($3))))
245 $(eval $3 := $(filter-out %/,$($3)))
246 $(foreach d,$(dirs:%/=%),
247 $(call save-vars,$2)
248 $(eval obj := $(if $1,$1/)$d)
249 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
250 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
251 $(call load-vars,$2,$3)
252 $(call unnest-var-recursive,$1,$2,$3))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200253endef
254
Fam Zheng1c33ac52014-05-27 15:54:19 +0800255# unnest-vars
256# Usage: $(call unnest-vars, obj_prefix, vars)
257#
258# @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
259# ending '/'.
260#
261# @vars: the list of variable names to unnest.
262#
263# This macro will scan subdirectories's Makefile.objs, include them, to build
264# up each variable listed in @vars.
265#
266# Per object and per module cflags and libs are saved with relative path fixed
267# as well, those variables include -libs, -cflags and -objs. Items in -objs are
268# also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
269#
270# All nested variables postfixed by -m in names are treated as DSO variables,
271# and will be built as modules, if enabled.
272#
273# A simple example of the unnest:
274#
275# obj_prefix = ..
276# vars = hot cold
277# hot = fire.o sun.o season/
278# cold = snow.o water/ season/
279#
280# Unnest through a faked source directory structure:
281#
282# SRC_PATH
283# ├── water
284# │ └── Makefile.objs──────────────────┐
285# │ │ hot += steam.o │
286# │ │ cold += ice.mo │
287# │ │ ice.mo-libs := -licemaker │
288# │ │ ice.mo-objs := ice1.o ice2.o │
289# │ └──────────────────────────────┘
290# │
291# └── season
292# └── Makefile.objs──────┐
293# │ hot += summer.o │
294# │ cold += winter.o │
295# └──────────────────┘
296#
297# In the end, the result will be:
298#
299# hot = ../fire.o ../sun.o ../season/summer.o
300# cold = ../snow.o ../water/ice.mo ../season/winter.o
301# ../water/ice.mo-libs = -licemaker
302# ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
303#
304# Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
305# included.
306#
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200307define unnest-vars
Fam Zheng1c33ac52014-05-27 15:54:19 +0800308 # In the case of target build (i.e. $1 == ..), fix path for top level
309 # Makefile.objs objects
310 $(if $1,$(call fix-paths,$1/,,$2))
311
312 # Descend and include every subdir Makefile.objs
Fam Zhengc88f68e2015-01-12 12:43:09 +0800313 $(foreach v, $2,
314 $(call unnest-var-recursive,$1,$2,$v)
315 # Pass the .mo-cflags and .mo-libs along to its member objects
316 $(foreach o, $(filter %.mo,$($v)),
317 $(foreach p,$($o-objs),
318 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
319 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
320
321 # For all %.mo objects that are directly added into -y, just expand them
322 $(foreach v,$(filter %-y,$2),
323 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
Fam Zheng1c33ac52014-05-27 15:54:19 +0800324
325 $(foreach v,$(filter %-m,$2),
326 # All .o found in *-m variables are single object modules, create .mo
327 # for them
328 $(foreach o,$(filter %.o,$($v)),
329 $(eval $(o:%.o=%.mo)-objs := $o))
330 # Now unify .o in -m variable to .mo
331 $(eval $v := $($v:%.o=%.mo))
332 $(eval modules-m += $($v))
333
334 # For module build, build shared libraries during "make modules"
335 # For non-module build, add -m to -y
336 $(if $(CONFIG_MODULES),
Fam Zhengc261d772014-09-01 18:35:10 +0800337 $(foreach o,$($v),
Fam Zhengd24697e2015-05-07 14:55:15 +0800338 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
Fam Zhengc261d772014-09-01 18:35:10 +0800339 $(eval $o: $($o-objs)))
340 $(eval $(patsubst %-m,%-y,$v) += $($v))
Fam Zheng1c33ac52014-05-27 15:54:19 +0800341 $(eval modules: $($v:%.mo=%$(DSOSUF))),
342 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
343
344 # Post-process all the unnested vars
345 $(foreach v,$2,
346 $(foreach o, $(filter %.mo,$($v)),
347 # Find all the .mo objects in variables and add dependency rules
348 # according to .mo-objs. Report error if not set
349 $(if $($o-objs),
350 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
Fam Zhengc88f68e2015-01-12 12:43:09 +0800351 $(error $o added in $v but $o-objs is not set)))
Fam Zheng1c33ac52014-05-27 15:54:19 +0800352 $(shell mkdir -p ./ $(sort $(dir $($v))))
353 # Include all the .d files
Victor Kaplansky27fa7472015-08-09 12:39:59 +0300354 $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$($v))))
Fam Zheng1c33ac52014-05-27 15:54:19 +0800355 $(eval $v := $(filter-out %/,$($v))))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200356endef