rules.mak: introduce DSO rules
[qemu-kvm.git] / rules.mak
blob0abf3d1f36f16be4547dd3d6cc9a3622e0115c62
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 maybe-add = $(filter-out $1, $2) $1
26 extract-libs = $(strip $(sort $(foreach o,$1,$($o-libs)) \
27 $(foreach o,$(call expand-objs,$1),$($o-libs))))
28 expand-objs = $(strip $(sort $(filter %.o,$1)) \
29 $(foreach o,$(filter %.mo,$1),$($o-objs)) \
30 $(filter-out %.o %.mo,$1))
32 %.o: %.c
33 $(call quiet-command,$(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) $($@-cflags) -c -o $@ $<," CC $(TARGET_DIR)$@")
34 %.o: %.rc
35 $(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
37 # If we have a CXX we might have some C++ objects, in which case we
38 # must link with the C++ compiler, not the plain C compiler.
39 LINKPROG = $(or $(CXX),$(CC))
41 ifeq ($(LIBTOOL),)
42 LINK = $(call quiet-command,$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
43 $(call expand-objs,$1) $(version-obj-y) \
44 $(call extract-libs,$1) $(LIBS)," LINK $(TARGET_DIR)$@")
45 else
46 LIBTOOL += $(if $(V),,--quiet)
47 %.lo: %.c
48 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC $(CC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," lt CC $@")
49 %.lo: %.rc
50 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=RC $(WINDRES) -I. -o $@ $<,"lt RC $(TARGET_DIR)$@")
51 %.lo: %.dtrace
52 $(call quiet-command,$(LIBTOOL) --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN $(TARGET_DIR)$@")
54 LINK = $(call quiet-command,\
55 $(if $(filter %.lo %.la,$1),$(LIBTOOL) --mode=link --tag=CC \
56 )$(LINKPROG) $(QEMU_CFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ \
57 $(call expand-objs,$1) \
58 $(if $(filter %.lo %.la,$1),$(version-lobj-y),$(version-obj-y)) \
59 $(if $(filter %.lo %.la,$1),$(LIBTOOLFLAGS)) \
60 $(call extract-libs,$1) $(LIBS),$(if $(filter %.lo %.la,$1),"lt LINK ", " LINK ")"$(TARGET_DIR)$@")
61 endif
63 %.asm: %.S
64 $(call quiet-command,$(CPP) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -o $@ $<," CPP $(TARGET_DIR)$@")
66 %.o: %.asm
67 $(call quiet-command,$(AS) $(ASFLAGS) -o $@ $<," AS $(TARGET_DIR)$@")
69 %.o: %.cc
70 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," CXX $(TARGET_DIR)$@")
72 %.o: %.cpp
73 $(call quiet-command,$(CXX) $(QEMU_INCLUDES) $(QEMU_CXXFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," CXX $(TARGET_DIR)$@")
75 %.o: %.m
76 $(call quiet-command,$(OBJCC) $(QEMU_INCLUDES) $(QEMU_CFLAGS) $(QEMU_DGFLAGS) $(CFLAGS) -c -o $@ $<," OBJC $(TARGET_DIR)$@")
78 %.o: %.dtrace
79 $(call quiet-command,dtrace -o $@ -G -s $<, " GEN $(TARGET_DIR)$@")
81 DSO_CFLAGS := -fPIC -DBUILD_DSO
82 %$(DSOSUF): LDFLAGS += $(LDFLAGS_SHARED)
83 %$(DSOSUF): %.mo libqemustub.a
84 $(call LINK,$^)
86 .PHONY: modules
87 modules:
89 %$(EXESUF): %.o
90 $(call LINK,$^)
92 %.a:
93 $(call quiet-command,rm -f $@ && $(AR) rcs $@ $^," AR $(TARGET_DIR)$@")
95 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
97 # cc-option
98 # Usage: CFLAGS+=$(call cc-option, -falign-functions=0, -malign-functions=0)
100 cc-option = $(if $(shell $(CC) $1 $2 -S -o /dev/null -xc /dev/null \
101 >/dev/null 2>&1 && echo OK), $2, $3)
103 VPATH_SUFFIXES = %.c %.h %.S %.cc %.cpp %.m %.mak %.texi %.sh %.rc
104 set-vpath = $(if $1,$(foreach PATTERN,$(VPATH_SUFFIXES),$(eval vpath $(PATTERN) $1)))
106 # find-in-path
107 # Usage: $(call find-in-path, prog)
108 # Looks in the PATH if the argument contains no slash, else only considers one
109 # specific directory. Returns an # empty string if the program doesn't exist
110 # there.
111 find-in-path = $(if $(find-string /, $1), \
112 $(wildcard $1), \
113 $(wildcard $(patsubst %, %/$1, $(subst :, ,$(PATH)))))
115 # Logical functions (for operating on y/n values like CONFIG_FOO vars)
116 # Inputs to these must be either "y" (true) or "n" or "" (both false)
117 # Output is always either "y" or "n".
118 # Usage: $(call land,$(CONFIG_FOO),$(CONFIG_BAR))
119 # Logical NOT
120 lnot = $(if $(subst n,,$1),n,y)
121 # Logical AND
122 land = $(if $(findstring yy,$1$2),y,n)
123 # Logical OR
124 lor = $(if $(findstring y,$1$2),y,n)
125 # Logical XOR (note that this is the inverse of leqv)
126 lxor = $(if $(filter $(call lnot,$1),$(call lnot,$2)),n,y)
127 # Logical equivalence (note that leqv "","n" is true)
128 leqv = $(if $(filter $(call lnot,$1),$(call lnot,$2)),y,n)
129 # Logical if: like make's $(if) but with an leqv-like test
130 lif = $(if $(subst n,,$1),$2,$3)
132 # String testing functions: inputs to these can be any string;
133 # the output is always either "y" or "n". Leading and trailing whitespace
134 # is ignored when comparing strings.
135 # String equality
136 eq = $(if $(subst $2,,$1)$(subst $1,,$2),n,y)
137 # String inequality
138 ne = $(if $(subst $2,,$1)$(subst $1,,$2),y,n)
139 # Emptiness/non-emptiness tests:
140 isempty = $(if $1,n,y)
141 notempty = $(if $1,y,n)
143 # Generate files with tracetool
144 TRACETOOL=$(PYTHON) $(SRC_PATH)/scripts/tracetool.py
146 # Generate timestamp files for .h include files
148 config-%.h: config-%.h-timestamp
149 @cmp $< $@ >/dev/null 2>&1 || cp $< $@
151 config-%.h-timestamp: config-%.mak
152 $(call quiet-command, sh $(SRC_PATH)/scripts/create_config < $< > $@, " GEN $(TARGET_DIR)config-$*.h")
154 .PHONY: clean-timestamp
155 clean-timestamp:
156 rm -f *.timestamp
157 clean: clean-timestamp
159 # will delete the target of a rule if commands exit with a nonzero exit status
160 .DELETE_ON_ERROR:
162 # magic to descend into other directories
164 define push-var
165 $(eval save-$2-$1 = $(value $1))
166 $(eval $1 :=)
167 endef
169 define pop-var
170 $(eval subdir-$2-$1 := $(if $(filter $2,$(save-$2-$1)),$(addprefix $2,$($1))))
171 $(eval $1 = $(value save-$2-$1) $$(subdir-$2-$1))
172 $(eval save-$2-$1 :=)
173 endef
175 define fix-obj-vars
176 $(foreach v,$($1), \
177 $(if $($v-cflags), \
178 $(eval $2$v-cflags := $($v-cflags)) \
179 $(eval $v-cflags := )) \
180 $(if $($v-libs), \
181 $(eval $2$v-libs := $($v-libs)) \
182 $(eval $v-libs := )) \
183 $(if $($v-objs), \
184 $(eval $2$v-objs := $(addprefix $2,$($v-objs))) \
185 $(eval $v-objs := )))
186 endef
188 define unnest-dir
189 $(foreach var,$(nested-vars),$(call push-var,$(var),$1/))
190 $(eval obj-parent-$1 := $(obj))
191 $(eval obj := $(if $(obj),$(obj)/$1,$1))
192 $(eval include $(SRC_PATH)/$1/Makefile.objs)
193 $(foreach v,$(nested-vars),$(call fix-obj-vars,$v,$(if $(obj),$(obj)/)))
194 $(eval obj := $(obj-parent-$1))
195 $(eval obj-parent-$1 := )
196 $(foreach var,$(nested-vars),$(call pop-var,$(var),$1/))
197 endef
199 define unnest-vars-1
200 $(eval nested-dirs := $(filter-out \
201 $(old-nested-dirs), \
202 $(sort $(foreach var,$(nested-vars), $(filter %/, $($(var)))))))
203 $(if $(nested-dirs),
204 $(foreach dir,$(nested-dirs),$(call unnest-dir,$(patsubst %/,%,$(dir))))
205 $(eval old-nested-dirs := $(old-nested-dirs) $(nested-dirs))
206 $(call unnest-vars-1))
207 endef
209 define process-modules
210 $(foreach o,$(filter %.o,$($1)),
211 $(eval $(patsubst %.o,%.mo,$o): $o) \
212 $(eval $(patsubst %.o,%.mo,$o)-objs := $o))
213 $(foreach o,$(filter-out $(modules-m), $(patsubst %.o,%.mo,$($1))), \
214 $(eval $o: $($o-objs))
215 $(eval modules-objs-m += $($o-objs))
216 $(eval modules-m += $o)
217 $(eval $o:; $$(call quiet-command,touch $$@," GEN $$(TARGET_DIR)$$@"))
218 $(if $(CONFIG_MODULES),$(eval modules: $(patsubst %.mo,%$(DSOSUF),$o)))) \
219 $(eval modules-objs-m := $(sort $(modules-objs-m)))
220 $(foreach o,$(modules-objs-m), \
221 $(if $(CONFIG_MODULES),$(eval $o-cflags := $(call maybe-add, $(DSO_CFLAGS), $($o-cflags)))))
222 $(eval $(patsubst %-m,%-$(call lnot,$(CONFIG_MODULES)),$1) += $($1))
223 endef
225 define unnest-vars
226 $(eval obj := $1)
227 $(eval nested-vars := $2)
228 $(eval old-nested-dirs := )
229 $(call unnest-vars-1)
230 $(if $1,$(foreach v,$(nested-vars),$(eval \
231 $v := $(addprefix $1/,$($v)))))
232 $(foreach var,$(nested-vars),$(eval $(var) := $(filter-out %/, $($(var)))))
233 $(shell mkdir -p $(sort $(foreach var,$(nested-vars),$(dir $($(var))))))
234 $(foreach var,$(nested-vars), $(eval \
235 -include $(addsuffix *.d, $(sort $(dir $($(var)))))))
236 $(foreach v,$(filter %-m,$(nested-vars)), \
237 $(call process-modules,$v))
238 endef