summaryrefslogtreecommitdiff
path: root/xen/Makefile
blob: c535a5cfd13b27ac95714b575f8856aaa269239f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# $(lastword,) for GNU Make older than 3.81
lastword = $(word $(words $(1)),$(1))
this-makefile := $(call lastword,$(MAKEFILE_LIST))

# This is the correct place to edit the build version.
# All other places this is stored (eg. compile.h) should be autogenerated.
export XEN_VERSION       = 4
export XEN_SUBVERSION    = 17
export XEN_EXTRAVERSION ?= .1$(XEN_VENDORVERSION)
export XEN_FULLVERSION   = $(XEN_VERSION).$(XEN_SUBVERSION)$(XEN_EXTRAVERSION)
-include xen-version

export XEN_WHOAMI	?= $(USER)
export XEN_DOMAIN	?= $(shell ([ -x /bin/dnsdomainname ] && /bin/dnsdomainname) || ([ -x /bin/domainname ] && /bin/domainname || echo [unknown]))
export XEN_BUILD_DATE	?= $(shell LC_ALL=C date)
export XEN_BUILD_TIME	?= $(shell LC_ALL=C date +%T)
export XEN_BUILD_HOST	?= $(shell hostname)

# Best effort attempt to find a python interpreter, defaulting to Python 3 if
# available.  Fall back to just `python` if `which` is nowhere to be found.
PYTHON_INTERPRETER	:= $(word 1,$(shell which python3 python python2 2>/dev/null) python)
export PYTHON		?= $(PYTHON_INTERPRETER)

export CHECKPOLICY	?= checkpolicy

$(if $(filter __%, $(MAKECMDGOALS)), \
    $(error targets prefixed with '__' are only for internal use))

# That's our default target when none is given on the command line
PHONY := __all
__all:

# Do not use make's built-in rules and variables
MAKEFLAGS += -rR

EFI_MOUNTPOINT ?= $(BOOT_DIR)/efi

ARCH=$(XEN_TARGET_ARCH)
SRCARCH=$(shell echo $(ARCH) | \
          sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
              -e s'/riscv.*/riscv/g')
export ARCH SRCARCH

# Allow someone to change their config file
export KCONFIG_CONFIG ?= .config

export TARGET := xen

.PHONY: dist
dist: install

ifneq ($(root-make-done),y)
# section to run before calling Rules.mk, but only once.

ifneq ($(origin crash_debug),undefined)
$(error "You must use e.g. 'make menuconfig' to enable/disable crash_debug now.")
endif
ifeq ($(origin debug),command line)
$(warning "You must use e.g. 'make menuconfig' to enable/disable debug now.")
endif
ifneq ($(origin frame_pointer),undefined)
$(error "You must use e.g. 'make menuconfig' to enable/disable frame_pointer now.")
endif
ifneq ($(origin kexec),undefined)
$(error "You must use e.g. 'make menuconfig' to enable/disable kexec now.")
endif
ifneq ($(origin lock_profile),undefined)
$(error "You must use e.g. 'make menuconfig' to enable/disable lock_profile now.")
endif
ifneq ($(origin perfc),undefined)
$(error "You must use e.g. 'make menuconfig' to enable/disable perfc now.")
endif
ifneq ($(origin verbose),undefined)
$(error "You must use e.g. 'make menuconfig' to enable/disable verbose now.")
endif

# Beautify output
# ---------------------------------------------------------------------------
#
# Normally, we echo the whole command before executing it. By making
# that echo $($(quiet)$(cmd)), we now have the possibility to set
# $(quiet) to choose other forms of output instead, e.g.
#
#         quiet_cmd_cc_o_c = Compiling $(RELDIR)/$@
#         cmd_cc_o_c       = $(CC) $(c_flags) -c -o $@ $<
#
# If $(quiet) is empty, the whole command will be printed.
# If it is set to "quiet_", only the short version will be printed.
# If it is set to "silent_", nothing will be printed at all, since
# the variable $(silent_cmd_cc_o_c) doesn't exist.
#
# A simple variant is to prefix commands with $(Q) - that's useful
# for commands that shall be hidden in non-verbose mode.
#
#	$(Q)ln $@ :<
#
# If KBUILD_VERBOSE equals 0 then the above command will be hidden.
# If KBUILD_VERBOSE equals 1 then the above command is displayed.
#
# To put more focus on warnings, be less verbose as default
# Use 'make V=1' to see the full commands

ifeq ("$(origin V)", "command line")
    KBUILD_VERBOSE := $(V)
endif
ifndef KBUILD_VERBOSE
    KBUILD_VERBOSE := 0
endif

ifeq ($(KBUILD_VERBOSE),1)
    quiet :=
    Q :=
else
    quiet := quiet_
    Q := @
endif

# If the user is running make -s (silent mode), suppress echoing of
# commands

ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
    quiet := silent_
endif

export quiet Q KBUILD_VERBOSE

# $(realpath,) for GNU Make older than 3.81
realpath = $(wildcard $(foreach file,$(1),$(shell cd -P $(dir $(file)) && echo "$$PWD/$(notdir $(file))")))

ifeq ("$(origin O)", "command line")
    KBUILD_OUTPUT := $(O)
endif

ifneq ($(KBUILD_OUTPUT),)
# Make's built-in functions such as $(abspath ...), $(realpath ...) cannot
# expand a shell special character '~'. We use a somewhat tedious way here.
abs_objtree := $(shell mkdir -p $(KBUILD_OUTPUT) && cd $(KBUILD_OUTPUT) && pwd)
$(if $(abs_objtree),, \
     $(error failed to create output directory "$(KBUILD_OUTPUT)"))

# $(realpath ...) resolves symlinks
abs_objtree := $(call realpath,$(abs_objtree))
else
abs_objtree := $(CURDIR)
endif

ifeq ($(abs_objtree),$(CURDIR))
# Suppress "Entering directory ..." unless we are changing the work directory.
MAKEFLAGS += --no-print-directory
else
need-sub-make := 1
endif

abs_srctree := $(call realpath,$(dir $(this-makefile)))

ifneq ($(words $(subst :, ,$(abs_srctree))), 1)
$(error source directory cannot contain spaces or colons)
endif

ifneq ($(abs_srctree),$(abs_objtree))
# Look for make include files relative to root of kernel src
#
# This does not become effective immediately because MAKEFLAGS is re-parsed
# once after the Makefile is read. We need to invoke sub-make.
MAKEFLAGS += --include-dir=$(abs_srctree)
need-sub-make := 1
endif

export abs_srctree abs_objtree
export root-make-done := y

ifeq ($(need-sub-make),1)

PHONY += $(MAKECMDGOALS) __sub-make

$(filter-out $(this-makefile), $(MAKECMDGOALS)) __all: __sub-make
	@:

# Invoke a second make in the output directory, passing relevant variables
__sub-make:
	$(Q)$(MAKE) -C $(abs_objtree) -f $(abs_srctree)/Makefile $(MAKECMDGOALS)

endif # need-sub-make
endif # root-make-done

# We process the rest of the Makefile if this is the final invocation of make
ifeq ($(need-sub-make),)

# Do not print "Entering directory ...",
# but we want to display it when entering to the output directory
# so that IDEs/editors are able to understand relative filenames.
MAKEFLAGS += --no-print-directory

ifeq ($(abs_srctree),$(abs_objtree))
    # building in the source tree
    srctree := .
    building_out_of_srctree :=
else
    ifeq ($(abs_srctree)/,$(dir $(abs_objtree)))
        # building in a subdirectory of the source tree
        srctree := ..
    else
        srctree := $(abs_srctree)
    endif
    building_out_of_srctree := 1
endif

objtree := .
VPATH := $(srctree)

export building_out_of_srctree srctree objtree VPATH

export XEN_ROOT := $(abs_srctree)/..

# To make sure we do not include .config for any of the *config targets
# catch them early, and hand them over to tools/kconfig/Makefile

clean-targets := %clean
no-dot-config-targets := $(clean-targets) \
                         uninstall debug cloc \
                         cscope TAGS tags gtags \
                         xenversion

config-build    := n
need-config     := y

ifneq ($(filter $(no-dot-config-targets), $(MAKECMDGOALS)),)
    ifeq ($(filter-out $(no-dot-config-targets), $(MAKECMDGOALS)),)
        need-config := n
    endif
endif

ifneq ($(filter %config,$(MAKECMDGOALS)),)
    config-build := y
endif

include scripts/Kbuild.include

# Don't break if the build process wasn't called from the top level
# we need XEN_TARGET_ARCH to generate the proper config
include $(XEN_ROOT)/Config.mk

# Set ARCH/SUBARCH appropriately.
export TARGET_SUBARCH  := $(XEN_TARGET_ARCH)
export TARGET_ARCH     := $(shell echo $(XEN_TARGET_ARCH) | \
                            sed -e 's/x86.*/x86/' -e s'/arm\(32\|64\)/arm/g' \
                                -e s'/riscv.*/riscv/g')

export CONFIG_SHELL := $(SHELL)
export CC CXX LD NM OBJCOPY OBJDUMP ADDR2LINE
export YACC = $(if $(BISON),$(BISON),bison)
export LEX = $(if $(FLEX),$(FLEX),flex)

# Default file for 'make defconfig'.
export KBUILD_DEFCONFIG := $(ARCH)_defconfig

# Copy CFLAGS generated by "Config.mk" so they can be reused later without
# reparsing Config.mk by e.g. arch/x86/boot/.
export XEN_TREEWIDE_CFLAGS := $(CFLAGS)

# CLANG_FLAGS needs to be calculated before calling Kconfig
ifneq ($(shell $(CC) --version 2>&1 | head -n 1 | grep clang),)
CLANG_FLAGS :=

ifeq ($(TARGET_ARCH),x86)
# The tests to select whether the integrated assembler is usable need to happen
# before testing any assembler features, or else the result of the tests would
# be stale if the integrated assembler is not used.

# Older clang's built-in assembler doesn't understand .skip with labels:
# https://bugs.llvm.org/show_bug.cgi?id=27369
t1 = $(call as-insn,$(CC),".L0: .L1: .skip (.L1 - .L0)",,-no-integrated-as)

# Check whether clang asm()-s support .include.
t2 = $(call as-insn,$(CC) -I$(srctree)/arch/x86/include,".include \"asm/asm-defns.h\"",,-no-integrated-as)

# Check whether clang keeps .macro-s between asm()-s:
# https://bugs.llvm.org/show_bug.cgi?id=36110
t3 = $(call as-insn,$(CC),".macro FOO;.endm"$(close); asm volatile $(open)".macro FOO;.endm",-no-integrated-as)

CLANG_FLAGS += $(call or,$(t1),$(t2),$(t3))
endif

CLANG_FLAGS += -Werror=unknown-warning-option
CFLAGS += $(CLANG_FLAGS)
export CLANG_FLAGS
endif

export XEN_HAS_CHECKPOLICY := $(call success,$(CHECKPOLICY) -h 2>&1 | grep -q xen)

# ===========================================================================
# Rules shared between *config targets and build targets

PHONY += tools_fixdep
tools_fixdep:
	$(Q)$(MAKE) $(build)=tools tools/fixdep

PHONY += outputmakefile
# Before starting out-of-tree build, make sure the source tree is clean.
# outputmakefile generates a Makefile in the output directory, if using a
# separate output directory. This allows convenient use of make in the
# output directory.
# At the same time when output Makefile generated, generate .gitignore to
# ignore whole output directory

quiet_cmd_makefile = GEN     Makefile
cmd_makefile = { \
    echo "\# Automatically generated by $(srctree)/Makefile: don't edit"; \
    echo "include $(srctree)/Makefile"; \
    } > Makefile

outputmakefile:
	$(Q)ln -fsn $(srctree) source
ifdef building_out_of_srctree
	$(Q)if [ -f $(srctree)/.config -o \
		 -d $(srctree)/include/config -o \
		 -d $(srctree)/include/generated ]; then \
		echo >&2 "***"; \
		echo >&2 "*** The source tree is not clean, please run 'make$(if $(findstring command line, $(origin XEN_TARGET_ARCH)), XEN_TARGET_ARCH=$(XEN_TARGET_ARCH)) distclean'"; \
		echo >&2 "*** in $(abs_srctree)";\
		echo >&2 "***"; \
		false; \
	fi
	$(call cmd,makefile)
	$(Q)test -e .gitignore || \
	{ echo "# this is build directory, ignore it"; echo "*"; } > .gitignore
endif

ifeq ($(config-build),y)
# ===========================================================================
# *config targets only - make sure prerequisites are updated, and descend
# in tools/kconfig to make the *config target

# Create a file for KCONFIG_ALLCONFIG which depends on the environment.
# This will be use by kconfig targets allyesconfig/allmodconfig/allnoconfig/randconfig
filechk_kconfig_allconfig = \
    $(if $(findstring n,$(XEN_HAS_CHECKPOLICY)), echo 'CONFIG_XSM_FLASK_POLICY=n';) \
    $(if $(KCONFIG_ALLCONFIG), cat $(KCONFIG_ALLCONFIG);) \
    :

.allconfig.tmp: FORCE
	set -e; { $(call filechk_kconfig_allconfig); } > $@

config: tools_fixdep outputmakefile FORCE
	$(Q)$(MAKE) $(build)=tools/kconfig $@

# Config.mk tries to include .config file, don't try to remake it
%/.config: ;

%config: .allconfig.tmp tools_fixdep outputmakefile FORCE
	$(Q)$(MAKE) $(build)=tools/kconfig KCONFIG_ALLCONFIG=$< $@

else # !config-build

ifeq ($(need-config),y)
-include include/config/auto.conf
# Read in dependencies to all Kconfig* files, make sure to run syncconfig if
# changes are detected.
-include include/config/auto.conf.cmd

# Allow people to just run `make` as before and not force them to configure
# Only run defconfig if $(KCONFIG_CONFIG) is missing
$(KCONFIG_CONFIG): tools_fixdep
	$(if $(wildcard $@), , $(Q)$(MAKE) $(build)=tools/kconfig defconfig)

# The actual configuration files used during the build are stored in
# include/generated/ and include/config/. Update them if .config is newer than
# include/config/auto.conf (which mirrors .config).
#
# This exploits the 'multi-target pattern rule' trick.
# The syncconfig should be executed only once to make all the targets.
include/config/%.conf include/config/%.conf.cmd: $(KCONFIG_CONFIG)
	$(Q)$(MAKE) $(build)=tools/kconfig syncconfig

ifeq ($(CONFIG_DEBUG),y)
CFLAGS += -O1
else
CFLAGS += -O2
endif

ifeq ($(CONFIG_FRAME_POINTER),y)
CFLAGS += -fno-omit-frame-pointer
else
CFLAGS += -fomit-frame-pointer
endif

CFLAGS-$(CONFIG_CC_SPLIT_SECTIONS) += -ffunction-sections -fdata-sections

CFLAGS += -nostdinc -fno-builtin -fno-common
CFLAGS += -Werror -Wredundant-decls -Wno-pointer-arith
$(call cc-option-add,CFLAGS,CC,-Wvla)
CFLAGS += -pipe -D__XEN__ -include $(srctree)/include/xen/config.h
CFLAGS-$(CONFIG_DEBUG_INFO) += -g

ifneq ($(CONFIG_CC_IS_CLANG),y)
# Clang doesn't understand this command line argument, and doesn't appear to
# have a suitable alternative.  The resulting compiled binary does function,
# but has an excessively large symbol table.
CFLAGS += -Wa,--strip-local-absolute
endif

AFLAGS += -D__ASSEMBLY__

$(call cc-option-add,AFLAGS,CC,-Wa$(comma)--noexecstack)

LDFLAGS-$(call ld-option,--warn-rwx-segments) += --no-warn-rwx-segments

CFLAGS += $(CFLAGS-y)
# allow extra CFLAGS externally via EXTRA_CFLAGS_XEN_CORE
CFLAGS += $(EXTRA_CFLAGS_XEN_CORE)

# Most CFLAGS are safe for assembly files:
#  -std=gnu{89,99} gets confused by #-prefixed end-of-line comments
#  -flto makes no sense and annoys clang
AFLAGS += $(filter-out -std=gnu% -flto,$(CFLAGS)) $(AFLAGS-y)

# LDFLAGS are only passed directly to $(LD)
LDFLAGS += $(LDFLAGS_DIRECT) $(LDFLAGS-y)

ifeq ($(CONFIG_UBSAN),y)
CFLAGS_UBSAN := -fsanitize=undefined
else
CFLAGS_UBSAN :=
endif

ifeq ($(CONFIG_LTO),y)
CFLAGS += -flto
LDFLAGS-$(CONFIG_CC_IS_CLANG) += -plugin LLVMgold.so
endif

ifdef building_out_of_srctree
    CFLAGS += -I$(objtree)/include
    CFLAGS += -I$(objtree)/arch/$(TARGET_ARCH)/include
endif
CFLAGS += -I$(srctree)/include
CFLAGS += -I$(srctree)/arch/$(TARGET_ARCH)/include

# Note that link order matters!
ALL_OBJS-y                := common/built_in.o
ALL_OBJS-y                += drivers/built_in.o
ALL_OBJS-y                += lib/built_in.o
ALL_OBJS-y                += xsm/built_in.o
ALL_OBJS-y                += arch/$(TARGET_ARCH)/built_in.o
ALL_OBJS-$(CONFIG_CRYPTO) += crypto/built_in.o

ALL_LIBS-y                := lib/lib.a

include $(srctree)/arch/$(TARGET_ARCH)/arch.mk

# define new variables to avoid the ones defined in Config.mk
export XEN_CFLAGS := $(CFLAGS)
export XEN_AFLAGS := $(AFLAGS)
export XEN_LDFLAGS := $(LDFLAGS)
export CFLAGS_UBSAN

endif # need-config

__all: build

main-targets := build install uninstall clean distclean MAP cppcheck cppcheck-html
.PHONY: $(main-targets)
ifneq ($(XEN_TARGET_ARCH),x86_32)
$(main-targets): %: _% ;
else
$(main-targets):
	echo "*** Xen x86/32 target no longer supported!"
endif

.PHONY: _build
_build: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX)

# Strip
#
# INSTALL_EFI_STRIP, if defined, will cause xen.efi to be stripped before it
# is installed. If INSTALL_EFI_STRIP is '1', then the default option(s) below
# will be used. Otherwise, INSTALL_EFI_STRIP value will be used as the
# option(s) to the strip command.
ifdef INSTALL_EFI_STRIP

ifeq ($(INSTALL_EFI_STRIP),1)
efi-strip-opt := --strip-debug --keep-file-symbols
else
efi-strip-opt := $(INSTALL_EFI_STRIP)
endif

endif

.PHONY: _install
_install: D=$(DESTDIR)
_install: T=$(notdir $(TARGET))
_install: Z=$(CONFIG_XEN_INSTALL_SUFFIX)
_install: $(TARGET)$(CONFIG_XEN_INSTALL_SUFFIX)
	[ -d $(D)$(BOOT_DIR) ] || $(INSTALL_DIR) $(D)$(BOOT_DIR)
	$(INSTALL_DATA) $(TARGET)$(Z) $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION)$(Z)
	ln -f -s $(T)-$(XEN_FULLVERSION)$(Z) $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION)$(Z)
	ln -f -s $(T)-$(XEN_FULLVERSION)$(Z) $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION)$(Z)
	ln -f -s $(T)-$(XEN_FULLVERSION)$(Z) $(D)$(BOOT_DIR)/$(T)$(Z)
	[ -d "$(D)$(DEBUG_DIR)" ] || $(INSTALL_DIR) $(D)$(DEBUG_DIR)
	$(INSTALL_DATA) $(TARGET)-syms $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION)
	$(INSTALL_DATA) $(TARGET)-syms.map $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION).map
	$(INSTALL_DATA) $(KCONFIG_CONFIG) $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION).config
	if [ -r $(TARGET).efi -a -n '$(EFI_DIR)' ]; then \
		[ -d $(D)$(EFI_DIR) ] || $(INSTALL_DIR) $(D)$(EFI_DIR); \
		$(INSTALL_DATA) $(TARGET).efi $(D)$(EFI_DIR)/$(T)-$(XEN_FULLVERSION).efi; \
		if [ -e $(TARGET).efi.map ]; then \
			$(INSTALL_DATA) $(TARGET).efi.map $(D)$(DEBUG_DIR)/$(T)-$(XEN_FULLVERSION).efi.map; \
		fi; \
		ln -sf $(T)-$(XEN_FULLVERSION).efi $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION).efi; \
		ln -sf $(T)-$(XEN_FULLVERSION).efi $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).efi; \
		ln -sf $(T)-$(XEN_FULLVERSION).efi $(D)$(EFI_DIR)/$(T).efi; \
		if [ -n '$(EFI_MOUNTPOINT)' -a -n '$(EFI_VENDOR)' ]; then \
			$(if $(efi-strip-opt), \
			     $(STRIP) $(efi-strip-opt) -p -o $(TARGET).efi.stripped $(TARGET).efi && \
			     $(INSTALL_DATA) $(TARGET).efi.stripped $(D)$(EFI_MOUNTPOINT)/efi/$(EFI_VENDOR)/$(T)-$(XEN_FULLVERSION).efi ||) \
			$(INSTALL_DATA) $(TARGET).efi $(D)$(EFI_MOUNTPOINT)/efi/$(EFI_VENDOR)/$(T)-$(XEN_FULLVERSION).efi; \
		elif [ "$(D)" = "$(patsubst $(shell cd $(XEN_ROOT) && pwd)/%,%,$(D))" ]; then \
			echo 'EFI installation only partially done (EFI_VENDOR not set)' >&2; \
		fi; \
	fi

.PHONY: tests
tests:
	$(Q)$(MAKE) $(build)=test
.PHONY: install-tests
install-tests:
	$(Q)$(MAKE) $(build)=test install

.PHONY: _uninstall
_uninstall: D=$(DESTDIR)
_uninstall: T=$(notdir $(TARGET))
_uninstall: Z=$(CONFIG_XEN_INSTALL_SUFFIX)
_uninstall:
	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION).config
	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_FULLVERSION)$(Z)
	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION)$(Z)
	rm -f $(D)$(BOOT_DIR)/$(T)-$(XEN_VERSION)$(Z)
	rm -f $(D)$(BOOT_DIR)/$(T)$(Z)
	rm -f $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION)
	rm -f $(D)$(DEBUG_DIR)/$(T)-syms-$(XEN_FULLVERSION).map
	rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_FULLVERSION).efi
	rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).$(XEN_SUBVERSION).efi
	rm -f $(D)$(DEBUG_DIR)/$(T)-$(XEN_FULLVERSION).efi.map
	rm -f $(D)$(EFI_DIR)/$(T)-$(XEN_VERSION).efi
	rm -f $(D)$(EFI_DIR)/$(T).efi
	if [ -n '$(EFI_MOUNTPOINT)' -a -n '$(EFI_VENDOR)' ]; then \
		rm -f $(D)$(EFI_MOUNTPOINT)/efi/$(EFI_VENDOR)/$(T)-$(XEN_FULLVERSION).efi; \
	fi

.PHONY: _debug
_debug:
	$(OBJDUMP) -D -S $(TARGET)-syms > $(TARGET).s

.PHONY: _clean
_clean:
	$(Q)$(MAKE) $(clean)=tools
	$(Q)$(MAKE) $(clean)=include
	$(Q)$(MAKE) $(clean)=common
	$(Q)$(MAKE) $(clean)=drivers
	$(Q)$(MAKE) $(clean)=lib
	$(Q)$(MAKE) $(clean)=xsm
	$(Q)$(MAKE) $(clean)=crypto
	$(Q)$(MAKE) $(clean)=arch/arm
	$(Q)$(MAKE) $(clean)=arch/riscv
	$(Q)$(MAKE) $(clean)=arch/x86
	$(Q)$(MAKE) $(clean)=test
	$(Q)$(MAKE) $(clean)=tools/kconfig
	find . \( -name "*.o" -o -name ".*.d" -o -name ".*.d2" \
		-o -name ".*.o.tmp" -o -name "*~" -o -name "core" \
		-o -name '*.lex.c' -o -name '*.tab.[ch]' -o -name '*.c.cppcheck' \
		-o -name "*.gcno" -o -name ".*.cmd" -o -name "lib.a" \) -exec rm -f {} \;
	rm -f include/asm $(TARGET) $(TARGET).gz $(TARGET)-syms $(TARGET)-syms.map
	rm -f $(TARGET).efi $(TARGET).efi.map $(TARGET).efi.stripped
	rm -f asm-offsets.s arch/*/include/asm/asm-offsets.h
	rm -f .banner .allconfig.tmp include/xen/compile.h
	rm -f cppcheck-misra.* xen-cppcheck.xml

.PHONY: _distclean
_distclean: clean
	rm -f tags TAGS cscope.files cscope.in.out cscope.out cscope.po.out GTAGS GPATH GRTAGS GSYMS .config source
	rm -rf $(CPPCHECK_HTMLREPORT_OUTDIR)

$(TARGET).gz: $(TARGET)
	gzip -n -f -9 < $< > $@.new
	mv $@.new $@

$(TARGET): outputmakefile FORCE
	$(Q)$(MAKE) $(build)=tools
	$(Q)$(MAKE) $(build)=. include/xen/compile.h
	$(Q)$(MAKE) $(build)=include all
	$(Q)$(MAKE) $(build)=arch/$(TARGET_ARCH) include
	$(Q)$(MAKE) $(build)=. arch/$(TARGET_ARCH)/include/asm/asm-offsets.h
	$(Q)$(MAKE) $(build)=. MKRELOC=$(MKRELOC) 'ALL_OBJS=$(ALL_OBJS-y)' 'ALL_LIBS=$(ALL_LIBS-y)' $@

SUBDIRS = xsm arch/$(TARGET_ARCH) common drivers lib test
define all_sources
    ( find include -type f -name '*.h' -print; \
      find $(SUBDIRS) -type f -name '*.[chS]' -print )
endef

define set_exuberant_flags
    exuberant_flags=`$1 --version 2>/dev/null | (grep -iq exuberant && \
	echo "-I __initdata,__exitdata,__acquires,__releases \
	    -I EXPORT_SYMBOL \
	    --extra=+f --c-kinds=+px") || true` 
endef

.PHONY: xenversion
xenversion:
	@echo $(XEN_FULLVERSION)

.PHONY: TAGS
TAGS:
	set -e; rm -f TAGS; \
	$(call set_exuberant_flags,etags); \
	$(all_sources) | xargs etags $$exuberant_flags -a

.PHONY: tags
tags:
	set -e; rm -f tags; \
	$(call set_exuberant_flags,ctags); \
	$(all_sources) | xargs ctags $$exuberant_flags -a

.PHONY: gtags
gtags:
	set -e; rm -f GTAGS GSYMS GPATH GRTAGS
	$(all_sources) | gtags -f -

.PHONY: cscope
cscope:
	$(all_sources) > cscope.files
	cscope -k -b -q

.PHONY: _MAP
_MAP: $(TARGET)
	$(NM) -n $(TARGET)-syms | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aUw] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)' > System.map

%.o %.i %.s: %.c tools_fixdep FORCE
	$(Q)$(MAKE) $(build)=$(*D) $(*D)/$(@F)

%.o %.s: %.S tools_fixdep FORCE
	$(Q)$(MAKE) $(build)=$(*D) $(*D)/$(@F)

%/: tools_fixdep FORCE
	$(Q)$(MAKE) $(build)=$* need-builtin=1

.PHONY: cloc
cloc:
	find . -name tools -prune -o -name '*.o.cmd' -print | while read f; do \
	    for sf in $$(grep -o "[a-zA-Z0-9_/-]*\.[cS]" $$f); do \
		test -f "$$sf" && echo "$$sf"; \
	    done; \
	done | cloc --list-file=-

# What cppcheck command to use.
# To get proper results, it is recommended to build cppcheck manually from the
# latest source and use CPPCHECK to give the full path to the built version.
CPPCHECK ?= cppcheck

# What cppcheck-htmlreport to use.
# If you give the full path to a self compiled cppcheck, this should be set
# to the full path to cppcheck-html in the htmlreport directory of cppcheck.
# On recent distribution, this is available in the standard path.
CPPCHECK_HTMLREPORT ?= cppcheck-htmlreport

# By default we generate the report in cppcheck-htmlreport directory in the
# build directory. This can be changed by giving a directory in this variable.
CPPCHECK_HTMLREPORT_OUTDIR ?= cppcheck-htmlreport

# By default we do not check misra rules, to enable pass "CPPCHECK_MISRA=y" to
# make command line.
CPPCHECK_MISRA ?= n

# Compile flags to pass to cppcheck:
# - include directories and defines Xen Makefile is passing (from CFLAGS)
# - include config.h as this is passed directly to the compiler.
# - define CPPCHECK as we use to disable or enable some specific part of the
#   code to solve some cppcheck issues.
# - explicitely enable some cppcheck checks as we do not want to use "all"
#   which includes unusedFunction which gives wrong positives as we check file
#   per file.
#
# Compiler defines are in compiler-def.h which is included in config.h
#
CPPCHECKFLAGS := -DCPPCHECK --max-ctu-depth=10 \
                 --enable=style,information,missingInclude \
                 --include=$(srctree)/include/xen/config.h \
                 -I $(srctree)/xsm/flask/include \
                 -I $(srctree)/include/xen/libfdt \
                 $(filter -D% -I%,$(CFLAGS))

# We need to find all C files (as we are not checking assembly files) so
# we find all generated .o files which have a .c corresponding file.
CPPCHECKFILES := $(wildcard $(patsubst $(objtree)/%.o,$(srctree)/%.c, \
                 $(filter-out $(objtree)/tools/%, \
                 $(shell find $(objtree) -name "*.o"))))

# Headers and files required to run cppcheck on a file
CPPCHECKDEPS := $(objtree)/include/generated/autoconf.h \
                $(objtree)/include/generated/compiler-def.h

ifeq ($(CPPCHECK_MISRA),y)
    CPPCHECKFLAGS += --addon=cppcheck-misra.json
    CPPCHECKDEPS += cppcheck-misra.json
endif

quiet_cmd_cppcheck_xml = CPPCHECK $(patsubst $(srctree)/%,%,$<)
cmd_cppcheck_xml = $(CPPCHECK) -v -q --xml $(CPPCHECKFLAGS) \
                   --output-file=$@ $<

quiet_cmd_merge_cppcheck_reports = CPPCHECK-MERGE $@
cmd_merge_cppcheck_reports = $(PYTHON) $(srctree)/tools/merge_cppcheck_reports.py $^ $@

quiet_cmd_cppcheck_html = CPPCHECK-HTML $<
cmd_cppcheck_html = $(CPPCHECK_HTMLREPORT) --file=$< --source-dir=$(srctree) \
                    --report-dir=$(CPPCHECK_HTMLREPORT_OUTDIR) --title=Xen

PHONY += _cppcheck _cppcheck-html cppcheck-version

_cppcheck-html: xen-cppcheck.xml
	$(call if_changed,cppcheck_html)

_cppcheck: xen-cppcheck.xml

xen-cppcheck.xml: $(patsubst $(srctree)/%.c,$(objtree)/%.c.cppcheck,$(CPPCHECKFILES))
ifeq ($(CPPCHECKFILES),)
	$(error Please build Xen before running cppcheck)
endif
	$(call if_changed,merge_cppcheck_reports)

$(objtree)/%.c.cppcheck: $(srctree)/%.c $(CPPCHECKDEPS) | cppcheck-version
	$(call if_changed,cppcheck_xml)

cppcheck-version:
	$(Q)if ! which $(CPPCHECK) > /dev/null 2>&1; then \
		echo "Cannot find cppcheck executable: $(CPPCHECK)"; \
		exit 1; \
	fi
	$(Q)if [ "$$($(CPPCHECK) --version | awk '{print ($$2 < 2.7)}')" -eq 1 ]; then \
		echo "Please upgrade your cppcheck to version 2.7 or greater"; \
		exit 1; \
	fi

# List of Misra rules to respect is written inside a doc.
# In order to have some helpful text in the cppcheck output, generate a text
# file containing the rules identifier, classification and text from the Xen
# documentation file. Also generate a json file with the right arguments for
# cppcheck in json format including the list of rules to ignore.
#
# convert_misra_doc.py, producing both targets at the same time, should be
# executed only once. Utilize a pattern rule to achieve this effect, with the
# stem kind of arbitrarily chosen to be "cppcheck".
.PRECIOUS: %-misra.json
%-misra.txt %-misra.json: $(XEN_ROOT)/docs/misra/rules.rst $(srctree)/tools/convert_misra_doc.py
	$(Q)$(PYTHON) $(srctree)/tools/convert_misra_doc.py -i $< -o $*-misra.txt -j $*-misra.json

# Put this in generated headers this way it is cleaned by include/Makefile
$(objtree)/include/generated/compiler-def.h:
	$(Q)$(CC) -dM -E -o $@ - < /dev/null

endif #config-build
endif # need-sub-make

PHONY += FORCE
FORCE:

# Declare the contents of the PHONY variable as phony.  We keep that
# information in a variable so we can use it in if_changed and friends.
.PHONY: $(PHONY)