qemu-char: avoid leaking unused fds in tcp_get_msgfds()
[qemu/ar7.git] / rules.mak
blob945484ecb4e168913cef16afb0e104ea6a1e3688
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 -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 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))
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)$@")
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))
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)$@")
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
59 %.asm: %.S
60 $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@")
62 %.o: %.asm
63 $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@")
65 %.o: %.cc
66 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
68 %.o: %.cpp
69 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CXX $(TARGET_DIR)$@")
71 %.o: %.m
72 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
74 %.o: %.dtrace
75 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
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 /,-,$@)"))
84 .PHONY: modules
85 modules:
87 %$(EXESUF): %.o
88 $(call LINK,$^)
90 %.a:
91 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
93 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
95 # cc-option
96 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
98 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
99 >/dev/null 2>&1 && echo OK), $2, $3)
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)))
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)))))
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)
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)
141 # Generate files with tracetool
142 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
144 # Generate timestamp files for .h include files
146 config-%.h: config-%.h-timestamp
147 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
149 config-%.h-timestamp: config-%.mak
150 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
152 .PHONY: clean-timestamp
153 clean-timestamp:
154 rm -f *.timestamp
155 clean: clean-timestamp
157 # will delete the target of a rule if commands exit with a nonzero exit status
158 .DELETE_ON_ERROR:
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
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
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
210 # unnest-var-recursive
211 # Usage: $(call unnest-var-recursive, obj_prefix, vars, var)
213 # Unnest @var by including subdir Makefile.objs, while protect others in @vars
214 # unchanged.
216 # @obj_prefix is the starting point of object path prefix.
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
230 # unnest-vars
231 # Usage: $(call unnest-vars, obj_prefix, vars)
233 # @obj_prefix: object path prefix, can be empty, or '..', etc. Don't include
234 # ending '/'.
236 # @vars: the list of variable names to unnest.
238 # This macro will scan subdirectories's Makefile.objs, include them, to build
239 # up each variable listed in @vars.
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.
245 # All nested variables postfixed by -m in names are treated as DSO variables,
246 # and will be built as modules, if enabled.
248 # A simple example of the unnest:
250 # obj_prefix = ..
251 # vars = hot cold
252 # hot = fire.o sun.o season/
253 # cold = snow.o water/ season/
255 # Unnest through a faked source directory structure:
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 # └──────────────────┘
272 # In the end, the result will be:
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
279 # Note that 'hot' didn't include 'season/' in the input, so 'summer.o' is not
280 # included.
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))
287 # Descend and include every subdir Makefile.objs
288 $(foreach v, $2, $(call unnest-var-recursive,$1,$2,$v))
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))
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)))))
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