xref: /openbmc/qemu/Makefile (revision 02f5360d80a7a45190e31b8255b60edaf101211c)
1 # Makefile for QEMU.
2 
3 ifneq ($(words $(subst :, ,$(CURDIR))), 1)
4   $(error main directory cannot contain spaces nor colons)
5 endif
6 
7 # Always point to the root of the build tree (needs GNU make).
8 BUILD_DIR=$(CURDIR)
9 
10 # Before including a proper config-host.mak, assume we are in the source tree
11 SRC_PATH=.
12 
13 # Don't use implicit rules or variables
14 # we have explicit rules for everything
15 MAKEFLAGS += -rR
16 
17 SHELL = bash -o pipefail
18 
19 # Usage: $(call quiet-command,command and args,"NAME","args to print")
20 # This will run "command and args", and either:
21 #  if V=1 just print the whole command and args
22 #  otherwise print the 'quiet' output in the format "  NAME     args to print"
23 # NAME should be a short name of the command, 7 letters or fewer.
24 # If called with only a single argument, will print nothing in quiet mode.
25 quiet-command-run = $(if $(V),,$(if $2,printf "  %-7s %s\n" $2 $3 && ))$1
26 quiet-@ = $(if $(V),,@)
27 quiet-command = $(quiet-@)$(call quiet-command-run,$1,$2,$3)
28 
29 UNCHECKED_GOALS := TAGS gtags cscope ctags dist \
30     help check-help print-% \
31     docker docker-% lcitool-refresh vm-help vm-test vm-build-%
32 
33 all:
34 .PHONY: all clean distclean recurse-all dist msi FORCE
35 
36 # Don't try to regenerate Makefile or configure
37 # We don't generate any of them
38 Makefile: ;
39 configure: ;
40 
41 # All following code might depend on configuration variables
42 ifneq ($(wildcard config-host.mak),)
43 include config-host.mak
44 
45 include Makefile.prereqs
46 Makefile.prereqs: config-host.mak
47 
48 # 0. ensure the build tree is okay
49 
50 # Check that we're not trying to do an out-of-tree build from
51 # a tree that's been used for an in-tree build.
52 ifneq ($(realpath $(SRC_PATH)),$(realpath .))
53 ifneq ($(wildcard $(SRC_PATH)/config-host.mak),)
54 $(error This is an out of tree build but your source tree ($(SRC_PATH)) \
55 seems to have been used for an in-tree build. You can fix this by running \
56 "$(MAKE) distclean && rm -rf *-linux-user *-softmmu" in your source tree)
57 endif
58 endif
59 
60 # force a rerun of configure if config-host.mak is too old or corrupted
61 ifeq ($(MESON),)
62 .PHONY: config-host.mak
63 x := $(shell rm -rf meson-private meson-info meson-logs)
64 endif
65 ifeq ($(NINJA),)
66 .PHONY: config-host.mak
67 x := $(shell rm -rf meson-private meson-info meson-logs)
68 else
69 export NINJA
70 endif
71 ifeq ($(wildcard build.ninja),)
72 .PHONY: config-host.mak
73 x := $(shell rm -rf meson-private meson-info meson-logs)
74 endif
75 ifeq ($(origin prefix),file)
76 .PHONY: config-host.mak
77 x := $(shell rm -rf meson-private meson-info meson-logs)
78 endif
79 
80 # 1. ensure config-host.mak is up-to-date
81 config-host.mak: $(SRC_PATH)/configure $(SRC_PATH)/scripts/meson-buildoptions.sh \
82 		$(SRC_PATH)/pythondeps.toml $(SRC_PATH)/VERSION
83 	@echo config-host.mak is out-of-date, running configure
84 	@if test -f meson-private/coredata.dat; then \
85 	  ./config.status --skip-meson; \
86 	else \
87 	  ./config.status; \
88 	fi
89 
90 # 2. meson.stamp exists if meson has run at least once (so ninja reconfigure
91 # works), but otherwise never needs to be updated
92 
93 meson-private/coredata.dat: meson.stamp
94 meson.stamp: config-host.mak
95 	@touch meson.stamp
96 
97 # 3. ensure meson-generated build files are up-to-date
98 
99 ifneq ($(NINJA),)
100 Makefile.ninja: build.ninja
101 	$(quiet-@){ \
102 	  echo 'ninja-targets = \'; \
103 	  $(NINJA) -t targets all | sed 's/:.*//; $$!s/$$/ \\/'; \
104 	  echo 'build-files = \'; \
105 	  $(NINJA) -t query build.ninja | sed -n '1,/^  input:/d; /^  outputs:/q; s/$$/ \\/p'; \
106 	} > $@.tmp && mv $@.tmp $@
107 -include Makefile.ninja
108 endif
109 
110 ifneq ($(MESON),)
111 # The path to meson always points to pyvenv/bin/meson, but the absolute
112 # paths could change.  In that case, force a regeneration of build.ninja.
113 # Note that this invocation of $(NINJA), just like when Make rebuilds
114 # Makefiles, does not include -n.
115 build.ninja: build.ninja.stamp
116 $(build-files):
117 build.ninja.stamp: meson.stamp $(build-files)
118 	@if test "$$(cat build.ninja.stamp)" = "$(MESON)" && test -n "$(NINJA)"; then \
119 	  $(NINJA) build.ninja; \
120 	else \
121 	  echo "$(MESON) setup --reconfigure $(SRC_PATH)"; \
122 	  $(MESON) setup --reconfigure $(SRC_PATH); \
123 	fi && echo "$(MESON)" > $@
124 
125 Makefile.mtest: build.ninja scripts/mtest2make.py
126 	$(MESON) introspect --targets --tests --benchmarks | $(PYTHON) scripts/mtest2make.py > $@
127 -include Makefile.mtest
128 
129 .PHONY: update-buildoptions
130 all update-buildoptions: $(SRC_PATH)/scripts/meson-buildoptions.sh
131 $(SRC_PATH)/scripts/meson-buildoptions.sh: $(SRC_PATH)/meson_options.txt
132 	$(MESON) introspect --buildoptions $(SRC_PATH)/meson.build | $(PYTHON) \
133 	  scripts/meson-buildoptions.py > $@.tmp && mv $@.tmp $@
134 endif
135 
136 # 4. Rules to bridge to other makefiles
137 
138 ifneq ($(NINJA),)
139 # Filter out long options to avoid flags like --no-print-directory which
140 # may result in false positive match for MAKE.n
141 MAKE.n = $(findstring n,$(firstword $(filter-out --%,$(MAKEFLAGS))))
142 MAKE.k = $(findstring k,$(firstword $(filter-out --%,$(MAKEFLAGS))))
143 MAKE.q = $(findstring q,$(firstword $(filter-out --%,$(MAKEFLAGS))))
144 MAKE.nq = $(if $(word 2, $(MAKE.n) $(MAKE.q)),nq)
145 NINJAFLAGS = \
146         $(if $V,-v) \
147         $(if $(MAKE.n), -n) \
148         $(if $(MAKE.k), -k0) \
149         $(filter-out -j, \
150           $(or $(filter -l% -j%, $(MAKEFLAGS)), \
151                $(if $(filter --jobserver-auth=%, $(MAKEFLAGS)),, -j1))) \
152         -d keepdepfile
153 ninja-cmd-goals = $(or $(MAKECMDGOALS), all)
154 ninja-cmd-goals += $(foreach g, $(MAKECMDGOALS), $(.ninja-goals.$g))
155 
156 makefile-targets := build.ninja ctags TAGS cscope dist clean
157 # "ninja -t targets" also lists all prerequisites.  If build system
158 # files are marked as PHONY, however, Make will always try to execute
159 # "ninja build.ninja".
160 ninja-targets := $(filter-out $(build-files) $(makefile-targets), $(ninja-targets))
161 .PHONY: $(ninja-targets) run-ninja
162 $(ninja-targets): run-ninja
163 
164 # Use "| cat" to give Ninja a more "make-y" output.  Use "+" to bypass the
165 # --output-sync line.
166 run-ninja: config-host.mak
167 ifneq ($(filter $(ninja-targets), $(ninja-cmd-goals)),)
168 	+$(if $(MAKE.nq),@:,$(quiet-@)$(NINJA) $(NINJAFLAGS) \
169 	   $(sort $(filter $(ninja-targets), $(ninja-cmd-goals))) | cat)
170 endif
171 endif
172 
173 else # config-host.mak does not exist
174 ifneq ($(filter-out $(UNCHECKED_GOALS),$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
175 $(error Please call configure before running make)
176 endif
177 endif # config-host.mak does not exist
178 
179 SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory --quiet)
180 
181 include $(SRC_PATH)/tests/Makefile.include
182 
183 all: recurse-all
184 
185 SUBDIR_RULES=$(foreach t, all clean distclean, $(addsuffix /$(t), $(SUBDIRS)))
186 .PHONY: $(SUBDIR_RULES)
187 $(SUBDIR_RULES):
188 	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $(dir $@) V="$(V)" TARGET_DIR="$(dir $@)" $(notdir $@),)
189 
190 ifneq ($(filter contrib/plugins, $(SUBDIRS)),)
191 .PHONY: plugins
192 plugins: contrib/plugins/all
193 endif
194 
195 .PHONY: recurse-all recurse-clean
196 recurse-all: $(addsuffix /all, $(SUBDIRS))
197 recurse-clean: $(addsuffix /clean, $(SUBDIRS))
198 recurse-distclean: $(addsuffix /distclean, $(SUBDIRS))
199 
200 ######################################################################
201 
202 clean: recurse-clean
203 	-$(quiet-@)test -f build.ninja && $(NINJA) $(NINJAFLAGS) -t clean || :
204 	-$(quiet-@)test -f build.ninja && $(NINJA) $(NINJAFLAGS) clean-ctlist || :
205 	find . \( -name '*.so' -o -name '*.dll' -o \
206 		  -name '*.[oda]' -o -name '*.gcno' \) -type f \
207 		! -path ./roms/edk2/ArmPkg/Library/GccLto/liblto-aarch64.a \
208 		! -path ./roms/edk2/ArmPkg/Library/GccLto/liblto-arm.a \
209 		-exec rm {} +
210 	rm -f TAGS cscope.* *~ */*~
211 	@$(MAKE) -Ctests/qemu-iotests clean
212 
213 VERSION = $(shell cat $(SRC_PATH)/VERSION)
214 
215 dist: qemu-$(VERSION).tar.bz2
216 
217 qemu-%.tar.bz2:
218 	$(SRC_PATH)/scripts/make-release "$(SRC_PATH)" "$(patsubst qemu-%.tar.bz2,%,$@)"
219 
220 distclean: clean recurse-distclean
221 	-$(quiet-@)test -f build.ninja && $(NINJA) $(NINJAFLAGS) -t clean -g || :
222 	rm -f config-host.mak Makefile.prereqs
223 	rm -f tests/tcg/*/config-target.mak tests/tcg/config-host.mak
224 	rm -f config.status
225 	rm -f roms/seabios/config.mak
226 	rm -f qemu-plugins-ld.symbols qemu-plugins-ld64.symbols
227 	rm -f *-config-target.h *-config-devices.mak *-config-devices.h
228 	rm -rf meson-private meson-logs meson-info compile_commands.json
229 	rm -f Makefile.ninja Makefile.mtest build.ninja.stamp meson.stamp
230 	rm -f config.log
231 	rm -f linux-headers/asm
232 	rm -Rf .sdk qemu-bundle
233 
234 find-src-path = find "$(SRC_PATH)" -path "$(SRC_PATH)/meson" -prune -o \
235 	-type l -prune -o \( -name "*.[chsS]" -o -name "*.[ch].inc" \)
236 
237 .PHONY: ctags
238 ctags:
239 	$(call quiet-command, 			\
240 		rm -f "$(SRC_PATH)/"tags, 	\
241 		"CTAGS", "Remove old tags")
242 	$(call quiet-command, \
243 		$(find-src-path) -exec ctags 		\
244 		-f "$(SRC_PATH)/"tags --append {} +,	\
245 		"CTAGS", "Re-index $(SRC_PATH)")
246 
247 .PHONY: gtags
248 gtags:
249 	$(call quiet-command, 			\
250 		rm -f "$(SRC_PATH)/"GTAGS; 	\
251 		rm -f "$(SRC_PATH)/"GRTAGS; 	\
252 		rm -f "$(SRC_PATH)/"GPATH, 	\
253 		"GTAGS", "Remove old $@ files")
254 	$(call quiet-command, 				\
255 	        (cd $(SRC_PATH) && 			\
256 		 $(find-src-path) -print | gtags -f -),	\
257 		"GTAGS", "Re-index $(SRC_PATH)")
258 
259 .PHONY: TAGS
260 TAGS:
261 	$(call quiet-command, 			\
262 		rm -f "$(SRC_PATH)/"TAGS,	\
263 		"TAGS", "Remove old $@")
264 	$(call quiet-command, 				\
265 		$(find-src-path) -exec etags 		\
266 		-f "$(SRC_PATH)/"TAGS --append {} +, 	\
267 		"TAGS", "Re-index $(SRC_PATH)")
268 
269 .PHONY: cscope
270 cscope:
271 	$(call quiet-command,			\
272 		rm -f "$(SRC_PATH)/"cscope.* ,	\
273 		"cscope", "Remove old $@ files")
274 	$(call quiet-command, 					\
275 		($(find-src-path) -print | sed -e 's,^\./,,'    \
276 		> "$(SRC_PATH)/cscope.files"), 			\
277 		"cscope", "Create file list")
278 	$(call quiet-command, 				\
279 		cscope -b -i"$(SRC_PATH)/cscope.files" 	\
280 		-f"$(SRC_PATH)"/cscope.out, 		\
281 		"cscope", "Re-index $(SRC_PATH)")
282 
283 # Needed by "meson install"
284 export DESTDIR
285 
286 include $(SRC_PATH)/tests/lcitool/Makefile.include
287 include $(SRC_PATH)/tests/docker/Makefile.include
288 include $(SRC_PATH)/tests/vm/Makefile.include
289 
290 print-help-run = printf "  %-30s - %s\\n" "$1" "$2"
291 print-help = @$(call print-help-run,$1,$2)
292 
293 .PHONY: update-linux-vdso
294 update-linux-vdso:
295 	@for m in $(SRC_PATH)/linux-user/*/Makefile.vdso; do \
296 	  $(MAKE) $(SUBDIR_MAKEFLAGS) -C $$(dirname $$m) -f Makefile.vdso \
297 		SRC_PATH=$(SRC_PATH) BUILD_DIR=$(BUILD_DIR); \
298 	done
299 
300 .PHONY: help
301 help:
302 	@echo  'Generic targets:'
303 	$(call print-help,all,Build all)
304 	$(call print-help,dir/file.o,Build specified target only)
305 	$(call print-help,install,Install QEMU, documentation and tools)
306 	$(call print-help,ctags/gtags/TAGS,Generate tags file for editors)
307 	$(call print-help,cscope,Generate cscope index)
308 	$(call print-help,sparse,Run sparse on the QEMU source)
309 	@echo  ''
310 ifneq ($(filter contrib/plugins, $(SUBDIRS)),)
311 	@echo  'Plugin targets:'
312 	$(call print-help,plugins,Build the example TCG plugins)
313 	@echo  ''
314 endif
315 	@echo  'Cleaning targets:'
316 	$(call print-help,clean,Remove most generated files but keep the config)
317 	$(call print-help,distclean,Remove all generated files)
318 	$(call print-help,dist,Build a distributable tarball)
319 	@echo  ''
320 	@echo  'Linux-user targets:'
321 	$(call print-help,update-linux-vdso,Build linux-user vdso images)
322 	@echo  ''
323 	@echo  'Test targets:'
324 	$(call print-help,check,Run all tests (check-help for details))
325 	$(call print-help,bench,Run all benchmarks)
326 	$(call print-help,lcitool-help,Help about targets for managing build environment manifests)
327 	$(call print-help,docker-help,Help about targets running tests inside containers)
328 	$(call print-help,vm-help,Help about targets running tests inside VM)
329 	@echo  ''
330 	@echo  'Documentation targets:'
331 	$(call print-help,html man,Build documentation in specified format)
332 	@echo  ''
333 ifneq ($(filter msi, $(ninja-targets)),)
334 	@echo  'Windows targets:'
335 	$(call print-help,installer,Build NSIS-based installer for QEMU)
336 	$(call print-help,msi,Build MSI-based installer for qemu-ga)
337 	@echo  ''
338 endif
339 	$(call print-help,$(MAKE) [targets],(quiet build, default))
340 	$(call print-help,$(MAKE) V=1 [targets],(verbose build))
341 
342 # will delete the target of a rule if commands exit with a nonzero exit status
343 .DELETE_ON_ERROR:
344 
345 print-%:
346 	@echo '$*=$($*)'
347