]> git.proxmox.com Git - mirror_qemu.git/blob - rules.mak
acpi: rephrase comment
[mirror_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 extract-libs = $(strip $(foreach o,$1,$($o-libs)))
26 expand-objs = $(strip $(sort $(filter %.o,$1)) \
27 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
28 $(filter-out %.o %.mo,$1))
29
30 %.o: %.c
31 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CC $(TARGET_DIR)$@")
32 %.o: %.rc
33 $(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
34
35 # 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.
37 LINKPROG = $(or $(CXX),$(CC))
38
39 ifeq ($(LIBTOOL),)
40 LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
41 $1 $(version-obj-y) $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@")
42 else
43 LIBTOOL += $(if $(V),,--quiet)
44 %.lo: %.c
45 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($*.o-cflags) -c -o $@ $<," lt CC $@")
46 %.lo: %.rc
47 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=RC $(WINDRES) -I. -o $@ $<,"lt RC $(TARGET_DIR)$@")
48 %.lo: %.dtrace
49 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@")
50
51 LINK = $(call quiet-command,\
52 $(if $(filter %.lo %.la,$1),$(LIBTOOL) --mode=link --tag=CC \
53 )$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $1 \
54 $(if $(filter %.lo %.la,$1),$(version-lobj-y),$(version-obj-y)) \
55 $(if $(filter %.lo %.la,$1),$(LIBTOOLFLAGS)) \
56 $(call extract-libs,$(1:.lo=.o)) $(LIBS),$(if $(filter %.lo %.la,$1),"lt LINK ", " LINK ")"$(TARGET_DIR)$@")
57 endif
58
59 %.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)$@")
64
65 %.o: %.cc
66 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
67
68 %.o: %.cpp
69 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
70
71 %.o: %.m
72 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
73
74 %.o: %.dtrace
75 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
76
77 %$(DSOSUF): CFLAGS += -fPIC -DBUILD_DSO
78 %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
79 %$(DSOSUF):
80 $(call LINK,$^)
81 @# Copy to build root so modules can be loaded when program started without install
82 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@), " CP $(subst /,-,$@)"))
83
84 .PHONY: modules
85 modules:
86
87 %$(EXESUF): %.o
88 $(call LINK,$^)
89
90 %.a:
91 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
92
93 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
94
95 # cc-option
96 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
97
98 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
99 >/dev/null 2>&1 && echo OK), $2, $3)
100
101 VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
102 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
103
104 # 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.
109 find-in-path = $(if $(find-string /, $1), \
110 $(wildcard $1), \
111 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
112
113 # 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
118 lnot = $(if $(subst n,,$1),n,y)
119 # Logical AND
120 land = $(if $(findstring yy,$1$2),y,n)
121 # Logical OR
122 lor = $(if $(findstring y,$1$2),y,n)
123 # Logical XOR (note that this is the inverse of leqv)
124 lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
125 # Logical equivalence (note that leqv "","n" is true)
126 leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
127 # Logical if: like make's $(if) but with an leqv-like test
128 lif = $(if $(subst n,,$1),$2,$3)
129
130 # 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
134 eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
135 # String inequality
136 ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
137 # Emptiness/non-emptiness tests:
138 isempty = $(if $1,n,y)
139 notempty = $(if $1,y,n)
140
141 # Generate files with tracetool
142 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
143
144 # Generate timestamp files for .h include files
145
146 config-%.h: config-%.h-timestamp
147 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
148
149 config-%.h-timestamp: config-%.mak
150 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
151
152 .PHONY: clean-timestamp
153 clean-timestamp:
154 rm -f *.timestamp
155 clean: clean-timestamp
156
157 # will delete the target of a rule if commands exit with a nonzero exit status
158 .DELETE_ON_ERROR:
159
160 # 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.
164 define 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 := ))
173 endef
174
175 # 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.
180 define 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))
191 endef
192
193 # 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.
197 define 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)))))
208 endef
209
210 # 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 #
218 define 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))
228 endef
229
230 # 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 #
282 define unnest-vars
283 # 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))))
321 endef