blob: 945484ecb4e168913cef16afb0e104ea6a1e3688 [file] [log] [blame]
aliguori17759182009-01-21 18:12:52 +00001
Juan Quintela5ab28862009-10-06 21:11:14 +02002# Don't use implicit rules or variables
3# we have explicit rules for everything
4MAKEFLAGS += -rR
5
6# Files with this suffixes are final, don't try to generate them
7# using implicit rules
8%.d:
9%.h:
10%.c:
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000011%.cc:
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040012%.cpp:
Juan Quintela5ab28862009-10-06 21:11:14 +020013%.m:
14%.mak:
15
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040016# Flags for C++ compilation
17QEMU_CXXFLAGS = -D__STDC_LIMIT_MACROS $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Wold-style-declaration -Wold-style-definition -Wredundant-decls, $(QEMU_CFLAGS))
18
Stefan Weil7ebf54b2009-11-19 20:07:52 +010019# Flags for dependency generation
Jan Kiszkaa71cd2a2010-05-15 13:03:28 +020020QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(*D)/$(*F).d
malc02d54672009-10-11 17:08:57 +040021
Paolo Bonzini9d9199a2012-09-17 10:21:52 +020022# Same as -I$(SRC_PATH) -I., but for the nested source/object directories
Dunrong Huang7e7da8e2013-04-29 22:52:12 +080023QEMU_INCLUDES += -I$(<D) -I$(@D)
Paolo Bonzini9d9199a2012-09-17 10:21:52 +020024
Paolo Bonzinif2770152014-06-16 16:43:25 +020025extract-libs = $(strip $(foreach o,$1,$($o-libs)))
Fam Zheng17969262014-02-10 14:48:56 +080026expand-objs = $(strip $(sort $(filter %.o,$1)) \
27 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
28 $(filter-out %.o %.mo,$1))
Fam Zheng5c0d52b2014-02-10 14:48:53 +080029
Andreas Färber0e8c9212010-01-06 20:24:05 +010030%.o: %.c
Fam Zheng5c0d52b2014-02-10 14:48:53 +080031 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CC $(TARGET_DIR)$@")
Paolo Bonzini6821cdc2013-04-27 00:25:31 +020032%.o: %.rc
33 $(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000034
Peter Maydell3144f782014-02-05 17:27:27 +000035# If we have a CXX we might have some C++ objects, in which case we
36# must link with the C++ compiler, not the plain C compiler.
37LINKPROG = $(or $(CXX),$(CC))
38
Alon Levy44dc0ca2011-05-15 11:51:28 +030039ifeq ($(LIBTOOL),)
Fam Zheng1c33ac52014-05-27 15:54:19 +080040LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
41 $1 $(version-obj-y) $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@")
Alon Levy44dc0ca2011-05-15 11:51:28 +030042else
Paolo Bonzinif141ccf2012-12-20 18:32:53 +010043LIBTOOL += $(if $(V),,--quiet)
Alon Levy44dc0ca2011-05-15 11:51:28 +030044%.lo: %.c
Paolo Bonzini0db564e2014-05-08 14:34:09 +020045 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($*.o-cflags) -c -o $@ $<," lt CC $@")
Paolo Bonzini6821cdc2013-04-27 00:25:31 +020046%.lo: %.rc
47 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=RC $(WINDRES) -I. -o $@ $<,"lt RC $(TARGET_DIR)$@")
Paolo Bonzini2c13ec52012-12-21 09:23:18 +010048%.lo: %.dtrace
49 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@")
50
Paolo Bonzini21655882012-12-20 18:57:45 +010051LINK = $(call quiet-command,\
Fam Zheng17969262014-02-10 14:48:56 +080052 $(if $(filter %.lo %.la,$1),$(LIBTOOL) --mode=link --tag=CC \
Fam Zheng1c33ac52014-05-27 15:54:19 +080053 )$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $1 \
Fam Zheng17969262014-02-10 14:48:56 +080054 $(if $(filter %.lo %.la,$1),$(version-lobj-y),$(version-obj-y)) \
55 $(if $(filter %.lo %.la,$1),$(LIBTOOLFLAGS)) \
Paolo Bonzini0db564e2014-05-08 14:34:09 +020056 $(call extract-libs,$(1:.lo=.o)) $(LIBS),$(if $(filter %.lo %.la,$1),"lt LINK ", " LINK ")"$(TARGET_DIR)$@")
Alon Levy44dc0ca2011-05-15 11:51:28 +030057endif
58
Blue Swirl3dd46c72013-01-05 10:10:27 +000059%.asm: %.S
60 $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@")
61
62%.o: %.asm
63 $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000064
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000065%.o: %.cc
Paolo Bonzini0db564e2014-05-08 14:34:09 +020066 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000067
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040068%.o: %.cpp
Paolo Bonzini0db564e2014-05-08 14:34:09 +020069 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040070
aliguori17759182009-01-21 18:12:52 +000071%.o: %.m
Paolo Bonzini0db564e2014-05-08 14:34:09 +020072 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000073
Paolo Bonzini2c13ec52012-12-21 09:23:18 +010074%.o: %.dtrace
75 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
76
Fam Zheng1c33ac52014-05-27 15:54:19 +080077%$(DSOSUF): CFLAGS += -fPIC -DBUILD_DSO
Fam Zheng17969262014-02-10 14:48:56 +080078%$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
Fam Zheng1c33ac52014-05-27 15:54:19 +080079%$(DSOSUF):
Fam Zheng17969262014-02-10 14:48:56 +080080 $(call LINK,$^)
Fam Zhenge26110c2014-02-10 14:48:57 +080081 @# Copy to build root so modules can be loaded when program started without install
82 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@), " CP $(subst /,-,$@)"))
Fam Zheng17969262014-02-10 14:48:56 +080083
84.PHONY: modules
85modules:
86
aliguori3aa892d2009-01-21 18:13:02 +000087%$(EXESUF): %.o
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010088 $(call LINK,$^)
aliguori4f188f82009-01-21 18:13:09 +000089
aliguori93a0dba2009-01-21 18:13:16 +000090%.a:
aliguori28c699a2009-01-26 17:07:46 +000091 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
aliguori93a0dba2009-01-21 18:13:16 +000092
aliguori28c699a2009-01-26 17:07:46 +000093quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
Juan Quintela70071e12009-07-21 14:11:18 +020094
95# cc-option
Juan Quintela8a2e6ab2009-08-31 00:48:45 +020096# Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
Juan Quintela70071e12009-07-21 14:11:18 +020097
Thomas Monjalonfc3baad2009-09-11 18:45:40 +020098cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
99 >/dev/null 2>&1 && echo OK), $2, $3)
Juan Quintela1215c6e2009-10-07 02:40:58 +0200100
Peter Maydellc3dc9fd2014-02-05 17:27:27 +0000101VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
Nathan Froyd288e7bc2010-04-26 14:52:23 -0700102set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
Paolo Bonzini076d2472009-12-21 10:06:55 +0100103
Paolo Bonzini2b2e59e2010-10-21 10:18:40 +0200104# find-in-path
105# Usage: $(call find-in-path, prog)
106# Looks in the PATH if the argument contains no slash, else only considers one
107# specific directory. Returns an # empty string if the program doesn't exist
108# there.
109find-in-path = $(if $(find-string /, $1), \
110 $(wildcard $1), \
111 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
112
Peter Maydell837a2e22013-09-13 18:25:51 +0100113# Logical functions (for operating on y/n values like CONFIG_FOO vars)
114# Inputs to these must be either "y" (true) or "n" or "" (both false)
115# Output is always either "y" or "n".
116# Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
117# Logical NOT
118lnot = $(if $(subst n,,$1),n,y)
119# Logical AND
120land = $(if $(findstring yy,$1$2),y,n)
121# Logical OR
122lor = $(if $(findstring y,$1$2),y,n)
123# Logical XOR (note that this is the inverse of leqv)
124lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
125# Logical equivalence (note that leqv "","n" is true)
126leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
127# Logical if: like make's $(if) but with an leqv-like test
128lif = $(if $(subst n,,$1),$2,$3)
129
Peter Maydell9ef622e2013-09-13 18:25:52 +0100130# String testing functions: inputs to these can be any string;
131# the output is always either "y" or "n". Leading and trailing whitespace
132# is ignored when comparing strings.
133# String equality
134eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
135# String inequality
136ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
137# Emptiness/non-emptiness tests:
138isempty = $(if $1,n,y)
139notempty = $(if $1,y,n)
140
Lluís Vilanovac0424932012-04-18 20:15:45 +0200141# Generate files with tracetool
142TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
143
Juan Quintela1215c6e2009-10-07 02:40:58 +0200144# Generate timestamp files for .h include files
145
Michael S. Tsirkin4b259662013-01-15 13:12:35 +0200146config-%.h: config-%.h-timestamp
147 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
Juan Quintela1215c6e2009-10-07 02:40:58 +0200148
Michael S. Tsirkin4b259662013-01-15 13:12:35 +0200149config-%.h-timestamp: config-%.mak
150 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
Michael S. Tsirkin7dbbbb02009-12-07 21:04:52 +0200151
Michael S. Tsirkin75863172013-01-15 13:27:54 +0200152.PHONY: clean-timestamp
153clean-timestamp:
154 rm -f *.timestamp
155clean: clean-timestamp
156
Michael S. Tsirkin7dbbbb02009-12-07 21:04:52 +0200157# will delete the target of a rule if commands exit with a nonzero exit status
158.DELETE_ON_ERROR:
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200159
Fam Zheng1c33ac52014-05-27 15:54:19 +0800160# save-vars
161# Usage: $(call save-vars, vars)
162# Save each variable $v in $vars as save-vars-$v, save their object's
163# variables, then clear $v.
164define save-vars
165 $(foreach v,$1,
166 $(eval save-vars-$v := $(value $v))
167 $(foreach o,$($v),
168 $(foreach k,cflags libs objs,
169 $(if $($o-$k),
170 $(eval save-vars-$o-$k := $($o-$k))
171 $(eval $o-$k := ))))
172 $(eval $v := ))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200173endef
174
Fam Zheng1c33ac52014-05-27 15:54:19 +0800175# load-vars
176# Usage: $(call load-vars, vars, add_var)
177# Load the saved value for each variable in @vars, and the per object
178# variables.
179# Append @add_var's current value to the loaded value.
180define load-vars
181 $(eval $2-new-value := $(value $2))
182 $(foreach v,$1,
183 $(eval $v := $(value save-vars-$v))
184 $(foreach o,$($v),
185 $(foreach k,cflags libs objs,
186 $(if $(save-vars-$o-$k),
187 $(eval $o-$k := $(save-vars-$o-$k))
188 $(eval save-vars-$o-$k := ))))
189 $(eval save-vars-$v := ))
190 $(eval $2 := $(value $2) $($2-new-value))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200191endef
192
Fam Zheng1c33ac52014-05-27 15:54:19 +0800193# fix-paths
194# Usage: $(call fix-paths, obj_path, src_path, vars)
195# Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
196# directories in @vars.
197define fix-paths
198 $(foreach v,$3,
199 $(foreach o,$($v),
200 $(if $($o-libs),
201 $(eval $1$o-libs := $($o-libs)))
202 $(if $($o-cflags),
203 $(eval $1$o-cflags := $($o-cflags)))
204 $(if $($o-objs),
205 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
206 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
207 $(addprefix $2,$(filter %/,$($v)))))
Fam Zheng5c0d52b2014-02-10 14:48:53 +0800208endef
209
Fam Zheng1c33ac52014-05-27 15:54:19 +0800210# unnest-var-recursive
211# Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
212#
213# Unnest @var by including subdir Makefile.objs, while protect others in @vars
214# unchanged.
215#
216# @obj_prefix is the starting point of object path prefix.
217#
218define unnest-var-recursive
219 $(eval dirs := $(sort $(filter %/,$($3))))
220 $(eval $3 := $(filter-out %/,$($3)))
221 $(foreach d,$(dirs:%/=%),
222 $(call save-vars,$2)
223 $(eval obj := $(if $1,$1/)$d)
224 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
225 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
226 $(call load-vars,$2,$3)
227 $(call unnest-var-recursive,$1,$2,$3))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200228endef
229
Fam Zheng1c33ac52014-05-27 15:54:19 +0800230# unnest-vars
231# Usage: $(call unnest-vars, obj_prefix, vars)
232#
233# @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
234# ending '/'.
235#
236# @vars: the list of variable names to unnest.
237#
238# This macro will scan subdirectories's Makefile.objs, include them, to build
239# up each variable listed in @vars.
240#
241# Per object and per module cflags and libs are saved with relative path fixed
242# as well, those variables include -libs, -cflags and -objs. Items in -objs are
243# also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
244#
245# All nested variables postfixed by -m in names are treated as DSO variables,
246# and will be built as modules, if enabled.
247#
248# A simple example of the unnest:
249#
250# obj_prefix = ..
251# vars = hot cold
252# hot = fire.o sun.o season/
253# cold = snow.o water/ season/
254#
255# Unnest through a faked source directory structure:
256#
257# SRC_PATH
258# ├── water
259# │ └── Makefile.objs──────────────────┐
260# │ │ hot += steam.o │
261# │ │ cold += ice.mo │
262# │ │ ice.mo-libs := -licemaker │
263# │ │ ice.mo-objs := ice1.o ice2.o │
264# │ └──────────────────────────────┘
265# │
266# └── season
267# └── Makefile.objs──────┐
268# │ hot += summer.o │
269# │ cold += winter.o │
270# └──────────────────┘
271#
272# In the end, the result will be:
273#
274# hot = ../fire.o ../sun.o ../season/summer.o
275# cold = ../snow.o ../water/ice.mo ../season/winter.o
276# ../water/ice.mo-libs = -licemaker
277# ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
278#
279# Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
280# included.
281#
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200282define unnest-vars
Fam Zheng1c33ac52014-05-27 15:54:19 +0800283 # In the case of target build (i.e. $1 == ..), fix path for top level
284 # Makefile.objs objects
285 $(if $1,$(call fix-paths,$1/,,$2))
286
287 # Descend and include every subdir Makefile.objs
288 $(foreach v, $2, $(call unnest-var-recursive,$1,$2,$v))
289
290 $(foreach v,$(filter %-m,$2),
291 # All .o found in *-m variables are single object modules, create .mo
292 # for them
293 $(foreach o,$(filter %.o,$($v)),
294 $(eval $(o:%.o=%.mo)-objs := $o))
295 # Now unify .o in -m variable to .mo
296 $(eval $v := $($v:%.o=%.mo))
297 $(eval modules-m += $($v))
298
299 # For module build, build shared libraries during "make modules"
300 # For non-module build, add -m to -y
301 $(if $(CONFIG_MODULES),
302 $(eval modules: $($v:%.mo=%$(DSOSUF))),
303 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
304
305 # Post-process all the unnested vars
306 $(foreach v,$2,
307 $(foreach o, $(filter %.mo,$($v)),
308 # Find all the .mo objects in variables and add dependency rules
309 # according to .mo-objs. Report error if not set
310 $(if $($o-objs),
311 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
312 $(error $o added in $v but $o-objs is not set))
313 # Pass the .mo-cflags and .mo-libs along to member objects
314 $(foreach p,$($o-objs),
315 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
316 $(if $($o-libs), $(eval $p-libs += $($o-libs)))))
317 $(shell mkdir -p ./ $(sort $(dir $($v))))
318 # Include all the .d files
319 $(eval -include $(addsuffix *.d, $(sort $(dir $($v)))))
320 $(eval $v := $(filter-out %/,$($v))))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200321endef