slirp: Fix non blocking connect for w32
[qemu/ar7.git] / rules.mak
blob8821389d79460e8f9082c2a5a3ff5cb00c427872
2 # Don't use implicit rules or variables
3 # we have explicit rules for everything
4 MAKEFLAGS += -rR
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:
16 # Flags for C++ compilation
17 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))
19 # Flags for dependency generation
20 QEMU_DGFLAGS += -MMD -MP -MT $@ -MF $(*D)/$(*F).d
22 # Same as -I$(SRC_PATH) -I., but for the nested source/object directories
23 QEMU_INCLUDES += -I$(<D) -I$(@D)
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}')
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.
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.
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.
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)
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))
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)$@")
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))
65 ifeq ($(LIBTOOL),)
66 LINK = $(call quiet-command, $(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
67 $(call process-archive-undefs, $1) \
68 $(version-obj-y) $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@")
69 else
70 LIBTOOL += $(if $(V),,--quiet)
71 %.lo: %.c
72 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($*.o-cflags) -c -o $@ $<," lt CC $@")
73 %.lo: %.rc
74 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=RC $(WINDRES) -I. -o $@ $<,"lt RC $(TARGET_DIR)$@")
75 %.lo: %.dtrace
76 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@")
78 LINK = $(call quiet-command,\
79 $(if $(filter %.lo %.la,$1),$(LIBTOOL) --mode=link --tag=CC \
80 )$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
81 $(call process-archive-undefs, $1)\
82 $(if $(filter %.lo %.la,$1),$(version-lobj-y),$(version-obj-y)) \
83 $(if $(filter %.lo %.la,$1),$(LIBTOOLFLAGS)) \
84 $(call extract-libs,$(1:.lo=.o)) $(LIBS),$(if $(filter %.lo %.la,$1),"lt LINK ", " LINK ")"$(TARGET_DIR)$@")
85 endif
87 %.asm: %.S
88 $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@")
90 %.o: %.asm
91 $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@")
93 %.o: %.cc
94 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
96 %.o: %.cpp
97 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
99 %.o: %.m
100 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
102 %.o: %.dtrace
103 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
105 ifdef CONFIG_WIN32
106 DSO_OBJ_CFLAGS := -DBUILD_DSO
107 else
108 DSO_OBJ_CFLAGS := -fPIC -DBUILD_DSO
109 endif
110 module-common.o: CFLAGS += $(DSO_OBJ_CFLAGS)
111 %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
112 %$(DSOSUF): %.mo
113 $(call LINK,$^)
114 @# Copy to build root so modules can be loaded when program started without install
115 $(if $(findstring /,$@),$(call quiet-command,cp $@ $(subst /,-,$@), " CP $(subst /,-,$@)"))
118 LD_REL := $(CC) -nostdlib -Wl,-r
120 %.mo:
121 $(call quiet-command,$(LD_REL) -o $@ $^," LD -r $(TARGET_DIR)$@")
123 .PHONY: modules
124 modules:
126 %$(EXESUF): %.o
127 $(call LINK,$^)
129 %.a:
130 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
132 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
134 # cc-option
135 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
137 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
138 >/dev/null 2>&1 && echo OK), $2, $3)
140 VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
141 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
143 # install-prog list, dir
144 define install-prog
145 $(INSTALL_DIR) "$2"
146 $(INSTALL_PROG) $1 "$2"
147 $(if $(STRIP),$(STRIP) $(foreach T,$1,"$2/$(notdir $T)"),)
148 endef
150 # find-in-path
151 # Usage: $(call find-in-path, prog)
152 # Looks in the PATH if the argument contains no slash, else only considers one
153 # specific directory. Returns an # empty string if the program doesn't exist
154 # there.
155 find-in-path = $(if $(find-string /, $1), \
156 $(wildcard $1), \
157 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
159 # Logical functions (for operating on y/n values like CONFIG_FOO vars)
160 # Inputs to these must be either "y" (true) or "n" or "" (both false)
161 # Output is always either "y" or "n".
162 # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
163 # Logical NOT
164 lnot = $(if $(subst n,,$1),n,y)
165 # Logical AND
166 land = $(if $(findstring yy,$1$2),y,n)
167 # Logical OR
168 lor = $(if $(findstring y,$1$2),y,n)
169 # Logical XOR (note that this is the inverse of leqv)
170 lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
171 # Logical equivalence (note that leqv "","n" is true)
172 leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
173 # Logical if: like make's $(if) but with an leqv-like test
174 lif = $(if $(subst n,,$1),$2,$3)
176 # String testing functions: inputs to these can be any string;
177 # the output is always either "y" or "n". Leading and trailing whitespace
178 # is ignored when comparing strings.
179 # String equality
180 eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
181 # String inequality
182 ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
183 # Emptiness/non-emptiness tests:
184 isempty = $(if $1,n,y)
185 notempty = $(if $1,y,n)
187 # Generate files with tracetool
188 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
190 # Generate timestamp files for .h include files
192 config-%.h: config-%.h-timestamp
193 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
195 config-%.h-timestamp: config-%.mak
196 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
198 .PHONY: clean-timestamp
199 clean-timestamp:
200 rm -f *.timestamp
201 clean: clean-timestamp
203 # will delete the target of a rule if commands exit with a nonzero exit status
204 .DELETE_ON_ERROR:
206 # save-vars
207 # Usage: $(call save-vars, vars)
208 # Save each variable $v in $vars as save-vars-$v, save their object's
209 # variables, then clear $v.
210 define save-vars
211 $(foreach v,$1,
212 $(eval save-vars-$v := $(value $v))
213 $(foreach o,$($v),
214 $(foreach k,cflags libs objs,
215 $(if $($o-$k),
216 $(eval save-vars-$o-$k := $($o-$k))
217 $(eval $o-$k := ))))
218 $(eval $v := ))
219 endef
221 # load-vars
222 # Usage: $(call load-vars, vars, add_var)
223 # Load the saved value for each variable in @vars, and the per object
224 # variables.
225 # Append @add_var's current value to the loaded value.
226 define load-vars
227 $(eval $2-new-value := $(value $2))
228 $(foreach v,$1,
229 $(eval $v := $(value save-vars-$v))
230 $(foreach o,$($v),
231 $(foreach k,cflags libs objs,
232 $(if $(save-vars-$o-$k),
233 $(eval $o-$k := $(save-vars-$o-$k))
234 $(eval save-vars-$o-$k := ))))
235 $(eval save-vars-$v := ))
236 $(eval $2 := $(value $2) $($2-new-value))
237 endef
239 # fix-paths
240 # Usage: $(call fix-paths, obj_path, src_path, vars)
241 # Add prefix @obj_path to all objects in @vars, and add prefix @src_path to all
242 # directories in @vars.
243 define fix-paths
244 $(foreach v,$3,
245 $(foreach o,$($v),
246 $(if $($o-libs),
247 $(eval $1$o-libs := $($o-libs)))
248 $(if $($o-cflags),
249 $(eval $1$o-cflags := $($o-cflags)))
250 $(if $($o-objs),
251 $(eval $1$o-objs := $(addprefix $1,$($o-objs)))))
252 $(eval $v := $(addprefix $1,$(filter-out %/,$($v))) \
253 $(addprefix $2,$(filter %/,$($v)))))
254 endef
256 # unnest-var-recursive
257 # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
259 # Unnest @var by including subdir Makefile.objs, while protect others in @vars
260 # unchanged.
262 # @obj_prefix is the starting point of object path prefix.
264 define unnest-var-recursive
265 $(eval dirs := $(sort $(filter %/,$($3))))
266 $(eval $3 := $(filter-out %/,$($3)))
267 $(foreach d,$(dirs:%/=%),
268 $(call save-vars,$2)
269 $(eval obj := $(if $1,$1/)$d)
270 $(eval -include $(SRC_PATH)/$d/Makefile.objs)
271 $(call fix-paths,$(if $1,$1/)$d/,$d/,$2)
272 $(call load-vars,$2,$3)
273 $(call unnest-var-recursive,$1,$2,$3))
274 endef
276 # unnest-vars
277 # Usage: $(call unnest-vars, obj_prefix, vars)
279 # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
280 # ending '/'.
282 # @vars: the list of variable names to unnest.
284 # This macro will scan subdirectories's Makefile.objs, include them, to build
285 # up each variable listed in @vars.
287 # Per object and per module cflags and libs are saved with relative path fixed
288 # as well, those variables include -libs, -cflags and -objs. Items in -objs are
289 # also fixed to relative path against SRC_PATH plus the prefix @obj_prefix.
291 # All nested variables postfixed by -m in names are treated as DSO variables,
292 # and will be built as modules, if enabled.
294 # A simple example of the unnest:
296 # obj_prefix = ..
297 # vars = hot cold
298 # hot = fire.o sun.o season/
299 # cold = snow.o water/ season/
301 # Unnest through a faked source directory structure:
303 # SRC_PATH
304 # ├── water
305 # │ └── Makefile.objs──────────────────┐
306 # │ │ hot += steam.o │
307 # │ │ cold += ice.mo │
308 # │ │ ice.mo-libs := -licemaker │
309 # │ │ ice.mo-objs := ice1.o ice2.o │
310 # │ └──────────────────────────────┘
311 # │
312 # └── season
313 # └── Makefile.objs──────┐
314 # │ hot += summer.o │
315 # │ cold += winter.o │
316 # └──────────────────┘
318 # In the end, the result will be:
320 # hot = ../fire.o ../sun.o ../season/summer.o
321 # cold = ../snow.o ../water/ice.mo ../season/winter.o
322 # ../water/ice.mo-libs = -licemaker
323 # ../water/ice.mo-objs = ../water/ice1.o ../water/ice2.o
325 # Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
326 # included.
328 define unnest-vars
329 # In the case of target build (i.e. $1 == ..), fix path for top level
330 # Makefile.objs objects
331 $(if $1,$(call fix-paths,$1/,,$2))
333 # Descend and include every subdir Makefile.objs
334 $(foreach v, $2,
335 $(call unnest-var-recursive,$1,$2,$v)
336 # Pass the .mo-cflags and .mo-libs along to its member objects
337 $(foreach o, $(filter %.mo,$($v)),
338 $(foreach p,$($o-objs),
339 $(if $($o-cflags), $(eval $p-cflags += $($o-cflags)))
340 $(if $($o-libs), $(eval $p-libs += $($o-libs))))))
342 # For all %.mo objects that are directly added into -y, just expand them
343 $(foreach v,$(filter %-y,$2),
344 $(eval $v := $(foreach o,$($v),$(if $($o-objs),$($o-objs),$o))))
346 $(foreach v,$(filter %-m,$2),
347 # All .o found in *-m variables are single object modules, create .mo
348 # for them
349 $(foreach o,$(filter %.o,$($v)),
350 $(eval $(o:%.o=%.mo)-objs := $o))
351 # Now unify .o in -m variable to .mo
352 $(eval $v := $($v:%.o=%.mo))
353 $(eval modules-m += $($v))
355 # For module build, build shared libraries during "make modules"
356 # For non-module build, add -m to -y
357 $(if $(CONFIG_MODULES),
358 $(foreach o,$($v),
359 $(eval $($o-objs): CFLAGS += $(DSO_OBJ_CFLAGS))
360 $(eval $o: $($o-objs)))
361 $(eval $(patsubst %-m,%-y,$v) += $($v))
362 $(eval modules: $($v:%.mo=%$(DSOSUF))),
363 $(eval $(patsubst %-m,%-y,$v) += $(call expand-objs, $($v)))))
365 # Post-process all the unnested vars
366 $(foreach v,$2,
367 $(foreach o, $(filter %.mo,$($v)),
368 # Find all the .mo objects in variables and add dependency rules
369 # according to .mo-objs. Report error if not set
370 $(if $($o-objs),
371 $(eval $(o:%.mo=%$(DSOSUF)): module-common.o $($o-objs)),
372 $(error $o added in $v but $o-objs is not set)))
373 $(shell mkdir -p ./ $(sort $(dir $($v))))
374 # Include all the .d files
375 $(eval -include $(addsuffix *.d, $(sort $(dir $($v)))))
376 $(eval $v := $(filter-out %/,$($v))))
377 endef