1# This is the correct place to edit the build version.
2# All other places this is stored (eg. compile.h) should be autogenerated.
3export XEN_VERSION       = 4
4export XEN_SUBVERSION    = 14
5export XEN_EXTRAVERSION ?= .1$(XEN_VENDORVERSION)
6export XEN_FULLVERSION   = $(XEN_VERSION).$(XEN_SUBVERSION)$(XEN_EXTRAVERSION)
7-include xen-version
8
9export XEN_WHOAMI	?= $(USER)
10export XEN_DOMAIN	?= $(shell ([ -x /bin/dnsdomainname ] && /bin/dnsdomainname) || ([ -x /bin/domainname ] && /bin/domainname || echo [unknown]))
11export XEN_BUILD_DATE	?= $(shell LC_ALL=C date)
12export XEN_BUILD_TIME	?= $(shell LC_ALL=C date +%T)
13export XEN_BUILD_HOST	?= $(shell hostname)
14
15# Best effort attempt to find a python interpreter, defaulting to Python 3 if
16# available.  Fall back to just `python` if `which` is nowhere to be found.
17PYTHON_INTERPRETER	:= $(word 1,$(shell which python3 python python2 2>/dev/null) python)
18export PYTHON		?= $(PYTHON_INTERPRETER)
19
20export BASEDIR := $(CURDIR)
21export XEN_ROOT := $(BASEDIR)/..
22
23# Do not use make's built-in rules and variables
24MAKEFLAGS += -rR
25
26EFI_MOUNTPOINT ?= $(BOOT_DIR)/efi
27
28ARCH=$(XEN_TARGET_ARCH)
29SRCARCH=$(shell echo $(ARCH) | sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g')
30
31# Don't break if the build process wasn't called from the top level
32# we need XEN_TARGET_ARCH to generate the proper config
33include $(XEN_ROOT)/Config.mk
34
35# Set ARCH/SUBARCH appropriately.
36export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
37export TARGET_ARCH     := $(shell echo $(XEN_TARGET_ARCH) | \
38                            sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g')
39
40# Allow someone to change their config file
41export KCONFIG_CONFIG ?= .config
42
43export CC CXX LD
44
45.PHONY: default
46default: build
47
48.PHONY: dist
49dist: install
50
51include scripts/Kbuild.include
52
53ifneq ($(root-make-done),y)
54# section to run before calling Rules.mk, but only once.
55
56# Beautify output
57# ---------------------------------------------------------------------------
58#
59# Normally, we echo the whole command before executing it. By making
60# that echo $($(quiet)$(cmd)), we now have the possibility to set
61# $(quiet) to choose other forms of output instead, e.g.
62#
63#         quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
64#         cmd_cc_o_c       = $(CC) $(c_flags) -c -o $@ $<
65#
66# If $(quiet) is empty, the whole command will be printed.
67# If it is set to "quiet_", only the short version will be printed.
68# If it is set to "silent_", nothing will be printed at all, since
69# the variable $(silent_cmd_cc_o_c) doesn't exist.
70#
71# A simple variant is to prefix commands with $(Q) - that's useful
72# for commands that shall be hidden in non-verbose mode.
73#
74#	$(Q)ln $@ :<
75#
76# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
77# If KBUILD_VERBOSE equals 1 then the above command is displayed.
78#
79# To put more focus on warnings, be less verbose as default
80# Use 'make V=1' to see the full commands
81
82ifeq ("$(origin V)", "command line")
83    KBUILD_VERBOSE := $(V)
84endif
85ifndef KBUILD_VERBOSE
86    KBUILD_VERBOSE := 0
87endif
88
89ifeq ($(KBUILD_VERBOSE),1)
90    quiet :=
91    Q :=
92else
93    quiet := quiet_
94    Q := @
95endif
96
97# If the user is running make -s (silent mode), suppress echoing of
98# commands
99
100ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
101    quiet := silent_
102endif
103
104export quiet Q KBUILD_VERBOSE
105
106# To make sure we do not include .config for any of the *config targets
107# catch them early, and hand them over to tools/kconfig/Makefile
108
109clean-targets := %clean
110no-dot-config-targets := $(clean-targets) \
111                         uninstall debug cloc \
112                         cscope TAGS tags MAP gtags \
113                         xenversion
114
115config-build    := n
116need-config     := y
117
118ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
119    ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
120        need-config := n
121    endif
122endif
123
124ifneq ($(filter %config,$(MAKECMDGOALS)),)
125    config-build := y
126endif
127
128# CLANG_FLAGS needs to be calculated before calling Kconfig
129ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
130CLANG_FLAGS :=
131
132ifeq ($(TARGET_ARCH),x86)
133# The tests to select whether the integrated assembler is usable need to happen
134# before testing any assembler features, or else the result of the tests would
135# be stale if the integrated assembler is not used.
136
137# Older clang's built-in assembler doesn't understand .skip with labels:
138# https://bugs.llvm.org/show_bug.cgi?id=27369
139t1 = $(call as-insn,$(CC),".L0: .L1: .skip (.L1 - .L0)",,-no-integrated-as)
140
141# Check whether clang asm()-s support .include.
142t2 = $(call as-insn,$(CC) -I$(BASEDIR)/include,".include \"asm-x86/indirect_thunk_asm.h\"",,-no-integrated-as)
143
144# Check whether clang keeps .macro-s between asm()-s:
145# https://bugs.llvm.org/show_bug.cgi?id=36110
146t3 = $(call as-insn,$(CC),".macro FOO;.endm"$(close); asm volatile $(open)".macro FOO;.endm",-no-integrated-as)
147
148CLANG_FLAGS += $(call or,$(t1),$(t2),$(t3))
149endif
150
151CLANG_FLAGS += -Werror=unknown-warning-option
152CFLAGS += $(CLANG_FLAGS)
153export CLANG_FLAGS
154endif
155
156export root-make-done := y
157endif # root-make-done
158
159# Shorthand for kconfig
160kconfig = -f $(BASEDIR)/tools/kconfig/Makefile.kconfig ARCH=$(ARCH) SRCARCH=$(SRCARCH) HOSTCC="$(HOSTCC)" HOSTCXX="$(HOSTCXX)"
161
162ifeq ($(config-build),y)
163# ===========================================================================
164# *config targets only - make sure prerequisites are updated, and descend
165# in tools/kconfig to make the *config target
166
167config: FORCE
168	$(MAKE) $(kconfig) $@
169
170# Config.mk tries to include .config file, don't try to remake it
171%/.config: ;
172
173%config: FORCE
174	$(MAKE) $(kconfig) $@
175
176else # !config-build
177
178ifeq ($(need-config),y)
179-include include/config/auto.conf
180# Read in dependencies to all Kconfig* files, make sure to run syncconfig if
181# changes are detected.
182-include include/config/auto.conf.cmd
183
184# Allow people to just run `make` as before and not force them to configure
185$(KCONFIG_CONFIG):
186	$(MAKE) $(kconfig) defconfig
187
188# The actual configuration files used during the build are stored in
189# include/generated/ and include/config/. Update them if .config is newer than
190# include/config/auto.conf (which mirrors .config).
191#
192# This exploits the 'multi-target pattern rule' trick.
193# The syncconfig should be executed only once to make all the targets.
194include/config/%.conf include/config/%.conf.cmd: $(KCONFIG_CONFIG)
195	$(MAKE) $(kconfig) syncconfig
196
197ifeq ($(CONFIG_DEBUG),y)
198CFLAGS += -O1
199else
200CFLAGS += -O2
201endif
202
203ifeq ($(CONFIG_FRAME_POINTER),y)
204CFLAGS += -fno-omit-frame-pointer
205else
206CFLAGS += -fomit-frame-pointer
207endif
208
209CFLAGS += -nostdinc -fno-builtin -fno-common
210CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith
211$(call cc-option-add,CFLAGS,CC,-Wvla)
212CFLAGS += -pipe -D__XEN__ -include $(BASEDIR)/include/xen/config.h
213CFLAGS-$(CONFIG_DEBUG_INFO) += -g
214
215ifneq ($(CONFIG_CC_IS_CLANG),y)
216# Clang doesn't understand this command line argument, and doesn't appear to
217# have a suitable alternative.  The resulting compiled binary does function,
218# but has an excessively large symbol table.
219CFLAGS += -Wa,--strip-local-absolute
220endif
221
222AFLAGS += -D__ASSEMBLY__
223
224CFLAGS += $(CFLAGS-y)
225# allow extra CFLAGS externally via EXTRA_CFLAGS_XEN_CORE
226CFLAGS += $(EXTRA_CFLAGS_XEN_CORE)
227
228# Most CFLAGS are safe for assembly files:
229#  -std=gnu{89,99} gets confused by #-prefixed end-of-line comments
230#  -flto makes no sense and annoys clang
231AFLAGS += $(filter-out -std=gnu% -flto,$(CFLAGS)) $(AFLAGS-y)
232
233# LDFLAGS are only passed directly to $(LD)
234LDFLAGS += $(LDFLAGS_DIRECT) $(LDFLAGS-y)
235
236ifeq ($(CONFIG_UBSAN),y)
237CFLAGS_UBSAN := -fsanitize=undefined
238else
239CFLAGS_UBSAN :=
240endif
241
242ifeq ($(CONFIG_LTO),y)
243CFLAGS += -flto
244LDFLAGS-$(CONFIG_CC_IS_CLANG) += -plugin LLVMgold.so
245endif
246
247include $(BASEDIR)/arch/$(TARGET_ARCH)/arch.mk
248
249# define new variables to avoid the ones defined in Config.mk
250export XEN_CFLAGS := $(CFLAGS)
251export XEN_AFLAGS := $(AFLAGS)
252export XEN_LDFLAGS := $(LDFLAGS)
253export CFLAGS_UBSAN
254
255endif # need-config
256
257.PHONY: build install uninstall clean distclean MAP
258build install uninstall debug clean distclean MAP::
259ifneq ($(XEN_TARGET_ARCH),x86_32)
260	$(MAKE) -f Rules.mk _$@
261else
262	echo "*** Xen x86/32 target no longer supported!"
263endif
264
265.PHONY: _build
266_build: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX)
267
268.PHONY: _install
269_install: D=$(DESTDIR)
270_install: T=$(notdir $(TARGET))
271_install: Z=$(CONFIG_XEN_INSTALL_SUFFIX)
272_install: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX)
273	[ -d $(D)$(BOOT_DIR) ] || $(INSTALL_DIR) $(D)$(BOOT_DIR)
274	$(INSTALL_DATA) $(TARGET)$(Z) $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION)$(Z)
275	ln -f -s $(T)-$(XEN_FULLVERSION)$(Z) $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION)$(Z)
276	ln -f -s $(T)-$(XEN_FULLVERSION)$(Z) $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION)$(Z)
277	ln -f -s $(T)-$(XEN_FULLVERSION)$(Z) $(D)$(BOOT_DIR)/$(T)$(Z)
278	[ -d "$(D)$(DEBUG_DIR)" ] || $(INSTALL_DIR) $(D)$(DEBUG_DIR)
279	$(INSTALL_DATA) $(TARGET)-syms $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION)
280	$(INSTALL_DATA) $(TARGET)-syms.map $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION).map
281	$(INSTALL_DATA) $(KCONFIG_CONFIG) $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION).config
282	if [ -r $(TARGET).efi -a -n '$(EFI_DIR)' ]; then \
283		[ -d $(D)$(EFI_DIR) ] || $(INSTALL_DIR) $(D)$(EFI_DIR); \
284		$(INSTALL_DATA) $(TARGET).efi $(D)$(EFI_DIR)/$(T)-$(XEN_FULLVERSION).efi; \
285		if [ -e $(TARGET).efi.map ]; then \
286			$(INSTALL_DATA) $(TARGET).efi.map $(D)$(DEBUG_DIR)/$(T)-$(XEN_FULLVERSION).efi.map; \
287		fi; \
288		ln -sf $(T)-$(XEN_FULLVERSION).efi $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION).efi; \
289		ln -sf $(T)-$(XEN_FULLVERSION).efi $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).efi; \
290		ln -sf $(T)-$(XEN_FULLVERSION).efi $(D)$(EFI_DIR)/$(T).efi; \
291		if [ -n '$(EFI_MOUNTPOINT)' -a -n '$(EFI_VENDOR)' ]; then \
292			$(INSTALL_DATA) $(TARGET).efi $(D)$(EFI_MOUNTPOINT)/efi/$(EFI_VENDOR)/$(T)-$(XEN_FULLVERSION).efi; \
293		elif [ "$(D)" = "$(patsubst $(shell cd $(XEN_ROOT) && pwd)/%,%,$(D))" ]; then \
294			echo 'EFI installation only partially done (EFI_VENDOR not set)' >&2; \
295		fi; \
296	fi
297
298.PHONY: tests
299tests:
300	$(MAKE) -f $(BASEDIR)/Rules.mk -C test tests
301.PHONY: install-tests
302install-tests:
303	$(MAKE) -f $(BASEDIR)/Rules.mk -C test install
304
305.PHONY: _uninstall
306_uninstall: D=$(DESTDIR)
307_uninstall: T=$(notdir $(TARGET))
308_uninstall: Z=$(CONFIG_XEN_INSTALL_SUFFIX)
309_uninstall:
310	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION).config
311	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION)$(Z)
312	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION)$(Z)
313	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION)$(Z)
314	rm -f $(D)$(BOOT_DIR)/$(T)$(Z)
315	rm -f $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION)
316	rm -f $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION).map
317	rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_FULLVERSION).efi
318	rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION).efi
319	rm -f $(D)$(DEBUG_DIR)/$(T)-$(XEN_FULLVERSION).efi.map
320	rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).efi
321	rm -f $(D)$(EFI_DIR)/$(T).efi
322	rm -f $(D)$(EFI_MOUNTPOINT)/efi/$(EFI_VENDOR)/$(T)-$(XEN_FULLVERSION).efi
323
324.PHONY: _debug
325_debug:
326	$(OBJDUMP) -D -S $(TARGET)-syms > $(TARGET).s
327
328.PHONY: _clean
329_clean: delete-unfresh-files
330	$(MAKE) -C tools clean
331	$(MAKE) $(clean) include
332	$(MAKE) $(clean) common
333	$(MAKE) $(clean) drivers
334	$(MAKE) $(clean) xsm
335	$(MAKE) $(clean) crypto
336	$(MAKE) $(clean) arch/arm
337	$(MAKE) $(clean) arch/x86
338	$(MAKE) $(clean) test
339	$(MAKE) -f $(BASEDIR)/tools/kconfig/Makefile.kconfig ARCH=$(ARCH) SRCARCH=$(SRCARCH) clean
340	find . \( -name "*.o" -o -name ".*.d" -o -name ".*.d2" \
341		-o -name "*.gcno" -o -name ".*.cmd" \) -exec rm -f {} \;
342	rm -f include/asm $(TARGET) $(TARGET).gz $(TARGET).efi $(TARGET).efi.map $(TARGET)-syms $(TARGET)-syms.map *~ core
343	rm -f include/asm-*/asm-offsets.h
344	rm -f .banner
345
346.PHONY: _distclean
347_distclean: clean
348	rm -f tags TAGS cscope.files cscope.in.out cscope.out cscope.po.out GTAGS GPATH GRTAGS GSYMS .config
349
350$(TARGET).gz: $(TARGET)
351	gzip -n -f -9 < $< > $@.new
352	mv $@.new $@
353
354$(TARGET): delete-unfresh-files
355	$(MAKE) -C tools
356	$(MAKE) -f $(BASEDIR)/Rules.mk include/xen/compile.h
357	[ -e include/asm ] || ln -sf asm-$(TARGET_ARCH) include/asm
358	[ -e arch/$(TARGET_ARCH)/efi ] && for f in boot.c runtime.c compat.c efi.h;\
359		do test -r arch/$(TARGET_ARCH)/efi/$$f || \
360		   ln -nsf ../../../common/efi/$$f arch/$(TARGET_ARCH)/efi/; \
361		done; \
362		true
363	$(MAKE) -f $(BASEDIR)/Rules.mk -C include
364	$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) asm-offsets.s
365	$(MAKE) -f $(BASEDIR)/Rules.mk include/asm-$(TARGET_ARCH)/asm-offsets.h
366	$(MAKE) -f $(BASEDIR)/Rules.mk -C arch/$(TARGET_ARCH) $@
367
368# drivers/char/console.o contains static banner/compile info. Blow it away.
369# Don't refresh these files during e.g., 'sudo make install'
370.PHONY: delete-unfresh-files
371delete-unfresh-files:
372	@if [ ! -r include/xen/compile.h -o -O include/xen/compile.h ]; then \
373		rm -f include/xen/compile.h; \
374	fi
375
376.banner: Makefile
377	@if which figlet >/dev/null 2>&1 ; then \
378		echo " Xen $(XEN_FULLVERSION)" | figlet -f tools/xen.flf > $@.tmp; \
379	else \
380		echo " Xen $(XEN_FULLVERSION)" > $@.tmp; \
381	fi
382	@mv -f $@.tmp $@
383
384# compile.h contains dynamic build info. Rebuilt on every 'make' invocation.
385include/xen/compile.h: include/xen/compile.h.in .banner
386	@sed -e 's/@@date@@/$(XEN_BUILD_DATE)/g' \
387	    -e 's/@@time@@/$(XEN_BUILD_TIME)/g' \
388	    -e 's/@@whoami@@/$(XEN_WHOAMI)/g' \
389	    -e 's/@@domain@@/$(XEN_DOMAIN)/g' \
390	    -e 's/@@hostname@@/$(XEN_BUILD_HOST)/g' \
391	    -e 's!@@compiler@@!$(shell $(CC) $(CFLAGS) --version 2>&1 | head -1)!g' \
392	    -e 's/@@version@@/$(XEN_VERSION)/g' \
393	    -e 's/@@subversion@@/$(XEN_SUBVERSION)/g' \
394	    -e 's/@@extraversion@@/$(XEN_EXTRAVERSION)/g' \
395	    -e 's!@@changeset@@!$(shell tools/scmversion $(XEN_ROOT) || echo "unavailable")!g' \
396	    < include/xen/compile.h.in > $@.new
397	@cat .banner
398	@sed -rf tools/process-banner.sed < .banner >> $@.new
399	@mv -f $@.new $@
400
401include/asm-$(TARGET_ARCH)/asm-offsets.h: arch/$(TARGET_ARCH)/asm-offsets.s
402	@(set -e; \
403	  echo "/*"; \
404	  echo " * DO NOT MODIFY."; \
405	  echo " *"; \
406	  echo " * This file was auto-generated from $<"; \
407	  echo " *"; \
408	  echo " */"; \
409	  echo ""; \
410	  echo "#ifndef __ASM_OFFSETS_H__"; \
411	  echo "#define __ASM_OFFSETS_H__"; \
412	  echo ""; \
413	  sed -rne "/^[^#].*==>/{s:.*==>(.*)<==.*:\1:; s: [\$$#]: :; p;}"; \
414	  echo ""; \
415	  echo "#endif") <$< >$@
416
417SUBDIRS = xsm arch/$(TARGET_ARCH) common drivers test
418define all_sources
419    ( find include/asm-$(TARGET_ARCH) -name '*.h' -print; \
420      find include -name 'asm-*' -prune -o -name '*.h' -print; \
421      find $(SUBDIRS) -name '*.[chS]' -print )
422endef
423
424define set_exuberant_flags
425    exuberant_flags=`$1 --version 2>/dev/null | (grep -iq exuberant && \
426	echo "-I __initdata,__exitdata,__acquires,__releases \
427	    -I EXPORT_SYMBOL \
428	    --extra=+f --c-kinds=+px") || true`
429endef
430
431.PHONY: xenversion
432xenversion:
433	@echo $(XEN_FULLVERSION)
434
435.PHONY: TAGS
436TAGS:
437	set -e; rm -f TAGS; \
438	$(call set_exuberant_flags,etags); \
439	$(all_sources) | xargs etags $$exuberant_flags -a
440
441.PHONY: tags
442tags:
443	set -e; rm -f tags; \
444	$(call set_exuberant_flags,ctags); \
445	$(all_sources) | xargs ctags $$exuberant_flags -a
446
447.PHONY: gtags
448gtags:
449	set -e; rm -f GTAGS GSYMS GPATH GRTAGS
450	$(all_sources) | gtags -f -
451
452.PHONY: cscope
453cscope:
454	$(all_sources) > cscope.files
455	cscope -k -b -q
456
457.PHONY: _MAP
458_MAP:
459	$(NM) -n $(TARGET)-syms | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' > System.map
460
461%.o %.i %.s: %.c FORCE
462	$(MAKE) -f $(BASEDIR)/Rules.mk -C $(*D) $(@F)
463
464%.o %.s: %.S FORCE
465	$(MAKE) -f $(BASEDIR)/Rules.mk -C $(*D) $(@F)
466
467%/: FORCE
468	$(MAKE) -f $(BASEDIR)/Rules.mk -C $* built_in.o built_in_bin.o
469
470build-intermediate = $(eval $(call build-intermediate-closure,$(1)))
471define build-intermediate-closure
472$(1): FORCE
473	$(MAKE) -f $(BASEDIR)/Rules.mk -C $$(@D) $$(@F)
474endef
475
476$(foreach base,arch/x86/mm/guest_walk_% \
477               arch/x86/mm/hap/guest_walk_%level \
478               arch/x86/mm/shadow/guest_%, \
479    $(foreach ext,o i s,$(call build-intermediate,$(base).$(ext))))
480
481.PHONY: cloc
482cloc:
483	$(eval tmpfile := $(shell mktemp))
484	$(foreach f, $(shell find $(BASEDIR) -name *.o.d), \
485		$(eval path := $(dir $(f))) \
486		$(eval names := $(shell grep -o "[a-zA-Z0-9_/-]*\.[cS]" $(f))) \
487		$(foreach sf, $(names), \
488			$(shell if test -f $(path)/$(sf) ; then echo $(path)/$(sf) >> $(tmpfile); fi;)))
489	cloc --list-file=$(tmpfile)
490	rm $(tmpfile)
491
492endif #config-build
493
494PHONY += FORCE
495FORCE:
496
497# Declare the contents of the PHONY variable as phony.  We keep that
498# information in a variable so we can use it in if_changed and friends.
499.PHONY: $(PHONY)
500