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