blob: 9dda9f760f76c2c33e99a542fe6f93ffbee91c61 [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
Fam Zheng17969262014-02-10 14:48:56 +080025maybe-add = $(filter-out $1, $2) $1
26extract-libs = $(strip $(sort $(foreach o,$1,$($o-libs)) \
27 $(foreach o,$(call expand-objs,$1),$($o-libs))))
28expand-objs = $(strip $(sort $(filter %.o,$1)) \
29 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
30 $(filter-out %.o %.mo,$1))
Fam Zheng5c0d52b2014-02-10 14:48:53 +080031
Andreas Färber0e8c9212010-01-06 20:24:05 +010032%.o: %.c
Fam Zheng5c0d52b2014-02-10 14:48:53 +080033 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CC $(TARGET_DIR)$@")
Paolo Bonzini6821cdc2013-04-27 00:25:31 +020034%.o: %.rc
35 $(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000036
Peter Maydell3144f782014-02-05 17:27:27 +000037# If we have a CXX we might have some C++ objects, in which case we
38# must link with the C++ compiler, not the plain C compiler.
39LINKPROG = $(or $(CXX),$(CC))
40
Alon Levy44dc0ca2011-05-15 11:51:28 +030041ifeq ($(LIBTOOL),)
Peter Maydell3144f782014-02-05 17:27:27 +000042LINK = $(call quiet-command,$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
Fam Zheng17969262014-02-10 14:48:56 +080043 $(call expand-objs,$1) $(version-obj-y) \
44 $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@")
Alon Levy44dc0ca2011-05-15 11:51:28 +030045else
Paolo Bonzinif141ccf2012-12-20 18:32:53 +010046LIBTOOL += $(if $(V),,--quiet)
Alon Levy44dc0ca2011-05-15 11:51:28 +030047%.lo: %.c
Paolo Bonzinif141ccf2012-12-20 18:32:53 +010048 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," lt CC $@")
Paolo Bonzini6821cdc2013-04-27 00:25:31 +020049%.lo: %.rc
50 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=RC $(WINDRES) -I. -o $@ $<,"lt RC $(TARGET_DIR)$@")
Paolo Bonzini2c13ec52012-12-21 09:23:18 +010051%.lo: %.dtrace
52 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@")
53
Paolo Bonzini21655882012-12-20 18:57:45 +010054LINK = $(call quiet-command,\
Fam Zheng17969262014-02-10 14:48:56 +080055 $(if $(filter %.lo %.la,$1),$(LIBTOOL) --mode=link --tag=CC \
Peter Maydell3144f782014-02-05 17:27:27 +000056 )$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
Fam Zheng17969262014-02-10 14:48:56 +080057 $(call expand-objs,$1) \
58 $(if $(filter %.lo %.la,$1),$(version-lobj-y),$(version-obj-y)) \
59 $(if $(filter %.lo %.la,$1),$(LIBTOOLFLAGS)) \
60 $(call extract-libs,$1) $(LIBS),$(if $(filter %.lo %.la,$1),"lt LINK ", " LINK ")"$(TARGET_DIR)$@")
Alon Levy44dc0ca2011-05-15 11:51:28 +030061endif
62
Blue Swirl3dd46c72013-01-05 10:10:27 +000063%.asm: %.S
64 $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@")
65
66%.o: %.asm
67 $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000068
Peter Maydellc3dc9fd2014-02-05 17:27:27 +000069%.o: %.cc
70 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," CXX $(TARGET_DIR)$@")
71
Tomoki Sekiyama83f73fc2013-08-07 11:39:36 -040072%.o: %.cpp
73 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," CXX $(TARGET_DIR)$@")
74
aliguori17759182009-01-21 18:12:52 +000075%.o: %.m
Peter Maydell3c4a4d02012-08-11 22:34:40 +010076 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
aliguori17759182009-01-21 18:12:52 +000077
Paolo Bonzini2c13ec52012-12-21 09:23:18 +010078%.o: %.dtrace
79 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
80
Fam Zheng17969262014-02-10 14:48:56 +080081DSO_CFLAGS := -fPIC -DBUILD_DSO
82%$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
83%$(DSOSUF): %.mo libqemustub.a
84 $(call LINK,$^)
Fam Zhenge26110c2014-02-10 14:48:57 +080085 @# Copy to build root so modules can be loaded when program started without install
86 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@), " CP $(subst /,-,$@)"))
Fam Zheng17969262014-02-10 14:48:56 +080087
88.PHONY: modules
89modules:
90
aliguori3aa892d2009-01-21 18:13:02 +000091%$(EXESUF): %.o
Anthony Liguori0bfe3ca2009-05-14 19:29:53 +010092 $(call LINK,$^)
aliguori4f188f82009-01-21 18:13:09 +000093
aliguori93a0dba2009-01-21 18:13:16 +000094%.a:
aliguori28c699a2009-01-26 17:07:46 +000095 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
aliguori93a0dba2009-01-21 18:13:16 +000096
aliguori28c699a2009-01-26 17:07:46 +000097quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
Juan Quintela70071e12009-07-21 14:11:18 +020098
99# cc-option
Juan Quintela8a2e6ab2009-08-31 00:48:45 +0200100# Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
Juan Quintela70071e12009-07-21 14:11:18 +0200101
Thomas Monjalonfc3baad2009-09-11 18:45:40 +0200102cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
103 >/dev/null 2>&1 && echo OK), $2, $3)
Juan Quintela1215c6e2009-10-07 02:40:58 +0200104
Peter Maydellc3dc9fd2014-02-05 17:27:27 +0000105VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
Nathan Froyd288e7bc2010-04-26 14:52:23 -0700106set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
Paolo Bonzini076d2472009-12-21 10:06:55 +0100107
Paolo Bonzini2b2e59e2010-10-21 10:18:40 +0200108# find-in-path
109# Usage: $(call find-in-path, prog)
110# Looks in the PATH if the argument contains no slash, else only considers one
111# specific directory. Returns an # empty string if the program doesn't exist
112# there.
113find-in-path = $(if $(find-string /, $1), \
114 $(wildcard $1), \
115 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
116
Peter Maydell837a2e22013-09-13 18:25:51 +0100117# Logical functions (for operating on y/n values like CONFIG_FOO vars)
118# Inputs to these must be either "y" (true) or "n" or "" (both false)
119# Output is always either "y" or "n".
120# Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
121# Logical NOT
122lnot = $(if $(subst n,,$1),n,y)
123# Logical AND
124land = $(if $(findstring yy,$1$2),y,n)
125# Logical OR
126lor = $(if $(findstring y,$1$2),y,n)
127# Logical XOR (note that this is the inverse of leqv)
128lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
129# Logical equivalence (note that leqv "","n" is true)
130leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
131# Logical if: like make's $(if) but with an leqv-like test
132lif = $(if $(subst n,,$1),$2,$3)
133
Peter Maydell9ef622e2013-09-13 18:25:52 +0100134# String testing functions: inputs to these can be any string;
135# the output is always either "y" or "n". Leading and trailing whitespace
136# is ignored when comparing strings.
137# String equality
138eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
139# String inequality
140ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
141# Emptiness/non-emptiness tests:
142isempty = $(if $1,n,y)
143notempty = $(if $1,y,n)
144
Lluís Vilanovac0424932012-04-18 20:15:45 +0200145# Generate files with tracetool
146TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
147
Juan Quintela1215c6e2009-10-07 02:40:58 +0200148# Generate timestamp files for .h include files
149
Michael S. Tsirkin4b259662013-01-15 13:12:35 +0200150config-%.h: config-%.h-timestamp
151 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
Juan Quintela1215c6e2009-10-07 02:40:58 +0200152
Michael S. Tsirkin4b259662013-01-15 13:12:35 +0200153config-%.h-timestamp: config-%.mak
154 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
Michael S. Tsirkin7dbbbb02009-12-07 21:04:52 +0200155
Michael S. Tsirkin75863172013-01-15 13:27:54 +0200156.PHONY: clean-timestamp
157clean-timestamp:
158 rm -f *.timestamp
159clean: clean-timestamp
160
Michael S. Tsirkin7dbbbb02009-12-07 21:04:52 +0200161# will delete the target of a rule if commands exit with a nonzero exit status
162.DELETE_ON_ERROR:
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200163
164# magic to descend into other directories
165
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200166define push-var
167$(eval save-$2-$1 = $(value $1))
168$(eval $1 :=)
169endef
170
171define pop-var
172$(eval subdir-$2-$1 := $(if $(filter $2,$(save-$2-$1)),$(addprefix $2,$($1))))
173$(eval $1 = $(value save-$2-$1) $$(subdir-$2-$1))
174$(eval save-$2-$1 :=)
175endef
176
Fam Zheng5c0d52b2014-02-10 14:48:53 +0800177define fix-obj-vars
178$(foreach v,$($1), \
179 $(if $($v-cflags), \
180 $(eval $2$v-cflags := $($v-cflags)) \
181 $(eval $v-cflags := )) \
182 $(if $($v-libs), \
183 $(eval $2$v-libs := $($v-libs)) \
Fam Zheng17969262014-02-10 14:48:56 +0800184 $(eval $v-libs := )) \
185 $(if $($v-objs), \
186 $(eval $2$v-objs := $(addprefix $2,$($v-objs))) \
187 $(eval $v-objs := )))
Fam Zheng5c0d52b2014-02-10 14:48:53 +0800188endef
189
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200190define unnest-dir
191$(foreach var,$(nested-vars),$(call push-var,$(var),$1/))
Fam Zhengba1183d2014-02-10 14:48:52 +0800192$(eval obj-parent-$1 := $(obj))
193$(eval obj := $(if $(obj),$(obj)/$1,$1))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200194$(eval include $(SRC_PATH)/$1/Makefile.objs)
Fam Zheng5c0d52b2014-02-10 14:48:53 +0800195$(foreach v,$(nested-vars),$(call fix-obj-vars,$v,$(if $(obj),$(obj)/)))
Fam Zhengba1183d2014-02-10 14:48:52 +0800196$(eval obj := $(obj-parent-$1))
197$(eval obj-parent-$1 := )
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200198$(foreach var,$(nested-vars),$(call pop-var,$(var),$1/))
199endef
200
201define unnest-vars-1
202$(eval nested-dirs := $(filter-out \
203 $(old-nested-dirs), \
204 $(sort $(foreach var,$(nested-vars), $(filter %/, $($(var)))))))
205$(if $(nested-dirs),
206 $(foreach dir,$(nested-dirs),$(call unnest-dir,$(patsubst %/,%,$(dir))))
207 $(eval old-nested-dirs := $(old-nested-dirs) $(nested-dirs))
208 $(call unnest-vars-1))
209endef
210
Fam Zheng17969262014-02-10 14:48:56 +0800211define process-modules
212$(foreach o,$(filter %.o,$($1)),
213 $(eval $(patsubst %.o,%.mo,$o): $o) \
214 $(eval $(patsubst %.o,%.mo,$o)-objs := $o))
215$(foreach o,$(filter-out $(modules-m), $(patsubst %.o,%.mo,$($1))), \
Fam Zhenge26110c2014-02-10 14:48:57 +0800216 $(eval $o-objs += module-common.o)
Fam Zheng17969262014-02-10 14:48:56 +0800217 $(eval $o: $($o-objs))
218 $(eval modules-objs-m += $($o-objs))
219 $(eval modules-m += $o)
220 $(eval $o:; $$(call quiet-command,touch $$@," GEN $$(TARGET_DIR)$$@"))
221 $(if $(CONFIG_MODULES),$(eval modules: $(patsubst %.mo,%$(DSOSUF),$o)))) \
222$(eval modules-objs-m := $(sort $(modules-objs-m)))
223$(foreach o,$(modules-objs-m), \
224 $(if $(CONFIG_MODULES),$(eval $o-cflags := $(call maybe-add, $(DSO_CFLAGS), $($o-cflags)))))
225$(eval $(patsubst %-m,%-$(call lnot,$(CONFIG_MODULES)),$1) += $($1))
226endef
227
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200228define unnest-vars
Fam Zhengba1183d2014-02-10 14:48:52 +0800229$(eval obj := $1)
230$(eval nested-vars := $2)
231$(eval old-nested-dirs := )
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200232$(call unnest-vars-1)
Fam Zhengba1183d2014-02-10 14:48:52 +0800233$(if $1,$(foreach v,$(nested-vars),$(eval \
234 $v := $(addprefix $1/,$($v)))))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200235$(foreach var,$(nested-vars),$(eval $(var) := $(filter-out %/, $($(var)))))
Paolo Bonzini2dee8d52012-06-04 09:15:43 +0200236$(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
Paolo Bonzini1435ddb2012-07-11 16:40:21 +0200237$(foreach var,$(nested-vars), $(eval \
238 -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
Fam Zheng17969262014-02-10 14:48:56 +0800239$(foreach v,$(filter %-m,$(nested-vars)), \
240 $(call process-modules,$v))
Paolo Bonzinie05804e2012-05-22 13:41:27 +0200241endef