meson: convert hw/rdma
[qemu/ar7.git] / rules.mak
blob8285fe529aa56decca1bfe682ab432c9a31a837c
2 # These are used when we want to do substitutions without confusing Make
3 NULL :=
4 SPACE := $(NULL) #
5 COMMA := ,
7 # Don't use implicit rules or variables
8 # we have explicit rules for everything
9 MAKEFLAGS += -rR
11 # Files with this suffixes are final, don't try to generate them
12 # using implicit rules
13 %/trace-events:
14 %.hx:
15 %.py:
16 %.objs:
17 %.d:
18 %.h:
19 %.c:
20 %.cc:
21 %.cpp:
22 %.m:
23 %.mak:
24 clean-target:
26 # Flags for dependency generation
27 QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(@D)/$(*F).d
29 # Compiler searches the source file dir first, but in vpath builds
30 # we need to make it search the build dir too, before any other
31 # explicit search paths. There are two search locations in the build
32 # dir, one absolute and the other relative to the compiler working
33 # directory. These are the same for target-independent files, but
34 # different for target-dependent ones.
35 QEMU_LOCAL_INCLUDES = -iquote $(BUILD_DIR) -iquote $(BUILD_DIR)/$(@D) -iquote $(@D)
37 WL_U := -Wl,-u,
38 find-symbols = $(if $1, $(sort $(shell $(NM) -P -g $1 | $2)))
39 defined-symbols = $(call find-symbols,$1,awk '$$2!="U"{print $$1}')
40 undefined-symbols = $(call find-symbols,$1,awk '$$2=="U"{print $$1}')
42 WL := -Wl,
43 ifdef CONFIG_DARWIN
44 whole-archive = $(WL)-force_load,$1
45 else
46 whole-archive = $(WL)--whole-archive $1 $(WL)--no-whole-archive
47 endif
49 # All the .mo objects in -m variables are also added into corresponding -y
50 # variable in unnest-vars, but filtered out here, when LINK is called.
52 # The .mo objects are supposed to be linked as a DSO, for module build. So here
53 # they are only used as a placeholders to generate those "archive undefined"
54 # symbol options (-Wl,-u,$symbol_name), which are the archive functions
55 # referenced by the code in the DSO.
57 # Also the presence in -y variables will also guarantee they are built before
58 # linking executables that will load them. So we can look up symbol reference
59 # in LINK.
61 # This is necessary because the exectuable itself may not use the function, in
62 # which case the function would not be linked in. Then the DSO loading will
63 # fail because of the missing symbol.
64 process-archive-undefs = $(filter-out %.a %.fa %.mo %$(DSOSUF),$1) \
65 $(addprefix $(WL_U), \
66 $(filter $(call defined-symbols,$(filter %.a %.fa, $1)), \
67 $(call undefined-symbols,$(filter %.mo %$(DSOSUF),$1)))) \
68 $(foreach l,$(filter %.fa,$1),$(call whole-archive,$l)) \
69 $(filter %.a,$1)
71 extract-libs = $(strip $(foreach o,$(filter-out %.mo %$(DSOSUF),$1),$($o-libs)))
72 expand-objs = $(strip $(sort $(filter %.o,$1)) \
73 $(foreach o,$(filter %.mo %$(DSOSUF),$1),$($o-objs)) \
74 $(filter-out %.o %.mo %$(DSOSUF),$1))
76 %.o: %.c
77 @mkdir -p $(dir $@)
78 $(call quiet-command,$(CC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
79 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
80 -c -o $@ $<,"CC","$(TARGET_DIR)$@")
81 %.o: %.rc
82 $(call quiet-command,$(WINDRES) -I. -o $@ $<,"RC","$(TARGET_DIR)$@")
84 # If we have a CXX we might have some C++ objects, in which case we
85 # must link with the C++ compiler, not the plain C compiler.
86 LINKPROG = $(or $(CXX),$(CC))
88 LINK = $(call quiet-command, $(LINKPROG) $(CFLAGS) $(QEMU_LDFLAGS) -o $@ \
89 $(call process-archive-undefs, $1) \
90 $(version-obj-y) $(call extract-libs,$1) $(LIBS),"LINK","$(TARGET_DIR)$@")
92 %.o: %.S
93 $(call quiet-command,$(CCAS) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
94 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) \
95 -c -o $@ $<,"CCAS","$(TARGET_DIR)$@")
97 %.o: %.cc
98 $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
99 $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CXXFLAGS) $($@-cflags) \
100 -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
102 %.o: %.cpp
103 $(call quiet-command,$(CXX) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
104 $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CXXFLAGS) $($@-cflags) \
105 -c -o $@ $<,"CXX","$(TARGET_DIR)$@")
107 %.o: %.m
108 $(call quiet-command,$(OBJCC) $(QEMU_LOCAL_INCLUDES) $(QEMU_INCLUDES) \
109 $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) \
110 -c -o $@ $<,"OBJC","$(TARGET_DIR)$@")
112 %.o: %.dtrace
113 $(call quiet-command,dtrace -o $@ -G -s $<,"GEN","$(TARGET_DIR)$@")
115 DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
116 module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
117 %$(DSOSUF): QEMU_LDFLAGS += $(LDFLAGS_SHARED)
118 %$(DSOSUF): %.mo
119 $(call LINK,$^)
120 @# Copy to build root so modules can be loaded when program started without install
121 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@),"CP","$(subst /,-,$@)"))
124 LD_REL := $(CC) -nostdlib $(LD_REL_FLAGS)
126 %.mo:
127 $(call quiet-command,$(LD_REL) -o $@ $^,"LD","$(TARGET_DIR)$@")
129 .PHONY: modules
130 modules:
132 %$(EXESUF): %.o
133 $(call LINK,$(filter %.o %.a %.mo %.fa, $^))
135 %.a:
136 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"AR","$(TARGET_DIR)$@")
138 # Usage: $(call quiet-command,command and args,"NAME","args to print")
139 # This will run "command and args", and either:
140 # if V=1 just print the whole command and args
141 # otherwise print the 'quiet' output in the format " NAME args to print"
142 # NAME should be a short name of the command, 7 letters or fewer.
143 # If called with only a single argument, will print nothing in quiet mode.
144 quiet-command-run = $(if $(V),,$(if $2,printf " %-7s %s\n" $2 $3 && ))$1
145 quiet-@ = $(if $(V),,@)
146 quiet-command = $(quiet-@)$(call quiet-command-run,$1,$2,$3)
148 # cc-option
149 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
151 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
152 >/dev/null 2>&1 && echo OK), $2, $3)
153 cc-c-option = $(if $(shell $(CC) $1 $2 -c -o /dev/null -xc /dev/null \
154 >/dev/null 2>&1 && echo OK), $2, $3)
156 VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc Kconfig% %.json.in
157 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
159 # install-prog list, dir
160 define install-prog
161 $(INSTALL_DIR) "$2"
162 $(INSTALL_PROG) $1 "$2"
163 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
164 endef
166 # find-in-path
167 # Usage: $(call find-in-path, prog)
168 # Looks in the PATH if the argument contains no slash, else only considers one
169 # specific directory. Returns an # empty string if the program doesn't exist
170 # there.
171 find-in-path = $(if $(findstring /, $1), \
172 $(wildcard $1), \
173 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
175 # Logical functions (for operating on y/n values like CONFIG_FOO vars)
176 # Inputs to these must be either "y" (true) or "n" or "" (both false)
177 # Output is always either "y" or "n".
178 # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
179 # Logical NOT
180 lnot = $(if $(subst n,,$1),n,y)
181 # Logical AND
182 land = $(if $(findstring yy,$1$2),y,n)
183 # Logical OR
184 lor = $(if $(findstring y,$1$2),y,n)
185 # Logical XOR (note that this is the inverse of leqv)
186 lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
187 # Logical equivalence (note that leqv "","n" is true)
188 leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
189 # Logical if: like make's $(if) but with an leqv-like test
190 lif = $(if $(subst n,,$1),$2,$3)
192 # String testing functions: inputs to these can be any string;
193 # the output is always either "y" or "n". Leading and trailing whitespace
194 # is ignored when comparing strings.
195 # String equality
196 eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
197 # String inequality
198 ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
199 # Emptiness/non-emptiness tests:
200 isempty = $(if $1,n,y)
201 notempty = $(if $1,y,n)
203 # Generate files with tracetool
204 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
206 .PHONY: clean-timestamp
207 clean-timestamp:
208 rm -f *.timestamp
209 clean: clean-timestamp
211 # will delete the target of a rule if commands exit with a nonzero exit status
212 .DELETE_ON_ERROR:
214 # save-vars
215 # Usage: $(call save-vars, vars)
216 # Save each variable $v in $vars as save-vars-$v, save their object's
217 # variables, then clear $v. saved-vars-$v contains the variables that
218 # where saved for the objects, in order to speedup load-vars.
219 define save-vars
220 $(foreach v,$1,
221 $(eval save-vars-$v := $(value $v))
222 $(eval saved-vars-$v := $(foreach o,$($v), \
223 $(if $($o-cflags), $o-cflags $(eval save-vars-$o-cflags := $($o-cflags))$(eval $o-cflags := )) \
224 $(if $($o-libs), $o-libs $(eval save-vars-$o-libs := $($o-libs))$(eval $o-libs := )) \
225 $(if $($o-objs), $o-objs $(eval save-vars-$o-objs := $($o-objs))$(eval $o-objs := ))))
226 $(eval $v := ))
227 endef
229 # load-vars
230 # Usage: $(call load-vars, vars, add_var)
231 # Load the saved value for each variable in @vars, and the per object
232 # variables.
233 # Append @add_var's current value to the loaded value.
234 define load-vars
235 $(eval $2-new-value := $(value $2))
236 $(foreach v,$1,
237 $(eval $v := $(value save-vars-$v))
238 $(foreach o,$(saved-vars-$v),
239 $(eval $o := $(save-vars-$o)) $(eval save-vars-$o := ))
240 $(eval save-vars-$v := )
241 $(eval saved-vars-$v := ))
242 $(eval $2 := $(value $2) $($2-new-value))
243 endef
245 # fix-paths
246 # Usage: $(call fix-paths, obj_path, src_path, vars)
247 # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
248 # directories in @vars.
249 define fix-paths
250 $(foreach v,$3,
251 $(foreach o,$($v),
252 $(if $($o-libs),
253 $(eval $1$o-libs := $($o-libs)))
254 $(if $($o-cflags),
255 $(eval $1$o-cflags := $($o-cflags)))
256 $(if $($o-objs),
257 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
258 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
259 $(addprefix $2,$(filter %/,$($v)))))
260 endef
262 # unnest-var-recursive
263 # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
265 # Unnest @var by including subdir Makefile.objs, while protect others in @vars
266 # unchanged.
268 # @obj_prefix is the starting point of object path prefix.
270 define unnest-var-recursive
271 $(eval dirs := $(sort $(filter %/,$($3))))
272 $(eval $3 := $(filter-out %/,$($3)))
273 $(foreach d,$(dirs:%/=%),
274 $(call save-vars,$2)
275 $(eval obj := $(if $1,$1/)$d)
276 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
277 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
278 $(call load-vars,$2,$3)
279 $(call unnest-var-recursive,$1,$2,$3))
280 endef
282 # unnest-vars
283 # Usage: $(call unnest-vars, obj_prefix, vars)
285 # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
286 # ending '/'.
288 # @vars: the list of variable names to unnest.
290 # This macro will scan subdirectories's Makefile.objs, include them, to build
291 # up each variable listed in @vars.
293 # Per object and per module cflags and libs are saved with relative path fixed
294 # as well, those variables include -libs, -cflags and -objs. Items in -objs are
295 # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
297 # All nested variables postfixed by -m in names are treated as DSO variables,
298 # and will be built as modules, if enabled.
300 # A simple example of the unnest:
302 # obj_prefix = ..
303 # vars = hot cold
304 # hot = fire.o sun.o season/
305 # cold = snow.o water/ season/
307 # Unnest through a faked source directory structure:
309 # SRC_PATH
310 # ├── water
311 # │ └── Makefile.objs──────────────────┐
312 # │ │ hot += steam.o │
313 # │ │ cold += ice.mo │
314 # │ │ ice.mo-libs := -licemaker │
315 # │ │ ice.mo-objs := ice1.o ice2.o │
316 # │ └──────────────────────────────┘
317 # │
318 # └── season
319 # └── Makefile.objs──────┐
320 # │ hot += summer.o │
321 # │ cold += winter.o │
322 # └──────────────────┘
324 # In the end, the result will be:
326 # hot = ../fire.o ../sun.o ../season/summer.o
327 # cold = ../snow.o ../water/ice.mo ../season/winter.o
328 # ../water/ice.mo-libs = -licemaker
329 # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
331 # Note that 'hot' didn't include 'water/' in the input, so 'steam.o' is not
332 # included.
334 define unnest-vars
335 # In the case of target build (i.e. $1 == ..), fix path for top level
336 # Makefile.objs objects
337 $(if $1,$(call fix-paths,$1/,,$2))
339 # Descend and include every subdir Makefile.objs
340 $(foreach v, $2,
341 $(call unnest-var-recursive,$1,$2,$v)
342 # Pass the .mo-cflags and .mo-libs along to its member objects
343 $(foreach o, $(filter %.mo,$($v)),
344 $(foreach p,$($o-objs),
345 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
346 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
348 # For all %.mo objects that are directly added into -y, just expand them
349 $(foreach v,$(filter %-y,$2),
350 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
352 $(foreach v,$(filter %-m,$2),
353 # All .o found in *-m variables are single object modules, create .mo
354 # for them
355 $(foreach o,$(filter %.o,$($v)),
356 $(eval $(o:%.o=%.mo)-objs := $o))
357 # Now unify .o in -m variable to .mo
358 $(eval $v := $($v:%.o=%.mo))
359 $(eval modules-m += $($v))
361 # For module build, build shared libraries during "make modules"
362 # For non-module build, add -m to -y
363 $(if $(CONFIG_MODULES),
364 $(foreach o,$($v),
365 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
366 $(eval $o: $($o-objs)))
367 $(eval $(patsubst %-m,%-y,$v) += $($v))
368 $(eval modules: $($v:%.mo=%$(DSOSUF))),
369 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
371 # Post-process all the unnested vars
372 $(foreach v,$2,
373 $(foreach o, $(filter %.mo,$($v)),
374 # Find all the .mo objects in variables and add dependency rules
375 # according to .mo-objs. Report error if not set
376 $(if $($o-objs),
377 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs))))
378 $(shell mkdir -p ./ $(sort $(dir $($v))))
379 # Include all the .d files
380 $(eval -include $(patsubst %.o,%.d,$(patsubst %.mo,%.d,$(filter %.o,$($v)))))
381 $(eval $v := $(filter-out %/,$($v))))
382 endef
384 TEXI2MAN = $(call quiet-command, \
385 perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $(TEXI2PODFLAGS) $< $@.pod && \
386 $(POD2MAN) --section=$(subst .,,$(suffix $@)) --center=" " --release=" " $@.pod > $@, \
387 "GEN","$@")
389 %.1:
390 $(call TEXI2MAN)
391 %.7:
392 $(call TEXI2MAN)
393 %.8:
394 $(call TEXI2MAN)
396 # Support for building multiple output files by atomically executing
397 # a single rule which depends on several input files (so the rule
398 # will be executed exactly once, not once per output file, and
399 # not multiple times in parallel.) For more explanation see:
400 # https://www.cmcrossroads.com/article/atomic-rules-gnu-make
402 # Given a space-separated list of filenames, create the name of
403 # a 'sentinel' file to use to indicate that they have been built.
404 # We use fixed text on the end to avoid accidentally triggering
405 # automatic pattern rules, and . on the start to make the file
406 # not show up in ls output.
407 sentinel = .$(subst $(SPACE),_,$(subst /,_,$1)).sentinel.
409 # Define an atomic rule that builds multiple outputs from multiple inputs.
410 # To use:
411 # $(call atomic,out1 out2 ...,in1 in2 ...)
412 # <TAB>rule to do the operation
414 # Make 4.3 will have native support for this, and you would be able
415 # to instead write:
416 # out1 out2 ... &: in1 in2 ...
417 # <TAB>rule to do the operation
419 # The way this works is that it creates a make rule
420 # "out1 out2 ... : sentinel-file ; @:" which says that the sentinel
421 # depends on the dependencies, and the rule to do that is "do nothing".
422 # Then we have a rule
423 # "sentinel-file : in1 in2 ..."
424 # whose commands start with "touch sentinel-file" and then continue
425 # with the rule text provided by the user of this 'atomic' function.
426 # The foreach... is there to delete the sentinel file if any of the
427 # output files don't exist, so that we correctly rebuild in that situation.
428 atomic = $(eval $1: $(call sentinel,$1) ; @:) \
429 $(call sentinel,$1) : $2 ; @touch $$@ \
430 $(foreach t,$1,$(if $(wildcard $t),,$(shell rm -f $(call sentinel,$1))))
432 print-%:
433 @echo '$*=$($*)'