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