162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ciifneq ($(O),)
362306a36Sopenharmony_ciifeq ($(origin O), command line)
462306a36Sopenharmony_ci	dummy := $(if $(shell cd $(PWD); test -d $(O) || echo $(O)),$(error O=$(O) does not exist),)
562306a36Sopenharmony_ci	ABSOLUTE_O := $(shell cd $(PWD); cd $(O) ; pwd)
662306a36Sopenharmony_ci	OUTPUT := $(ABSOLUTE_O)/$(if $(subdir),$(subdir)/)
762306a36Sopenharmony_ci	COMMAND_O := O=$(ABSOLUTE_O)
862306a36Sopenharmony_ciifeq ($(objtree),)
962306a36Sopenharmony_ci	objtree := $(O)
1062306a36Sopenharmony_ciendif
1162306a36Sopenharmony_ciendif
1262306a36Sopenharmony_ciendif
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci# check that the output directory actually exists
1562306a36Sopenharmony_ciifneq ($(OUTPUT),)
1662306a36Sopenharmony_ciOUTDIR := $(shell cd $(OUTPUT) && pwd)
1762306a36Sopenharmony_ci$(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist))
1862306a36Sopenharmony_ciendif
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#
2162306a36Sopenharmony_ci# Include saner warnings here, which can catch bugs:
2262306a36Sopenharmony_ci#
2362306a36Sopenharmony_ciEXTRA_WARNINGS := -Wbad-function-cast
2462306a36Sopenharmony_ciEXTRA_WARNINGS += -Wdeclaration-after-statement
2562306a36Sopenharmony_ciEXTRA_WARNINGS += -Wformat-security
2662306a36Sopenharmony_ciEXTRA_WARNINGS += -Wformat-y2k
2762306a36Sopenharmony_ciEXTRA_WARNINGS += -Winit-self
2862306a36Sopenharmony_ciEXTRA_WARNINGS += -Wmissing-declarations
2962306a36Sopenharmony_ciEXTRA_WARNINGS += -Wmissing-prototypes
3062306a36Sopenharmony_ciEXTRA_WARNINGS += -Wnested-externs
3162306a36Sopenharmony_ciEXTRA_WARNINGS += -Wno-system-headers
3262306a36Sopenharmony_ciEXTRA_WARNINGS += -Wold-style-definition
3362306a36Sopenharmony_ciEXTRA_WARNINGS += -Wpacked
3462306a36Sopenharmony_ciEXTRA_WARNINGS += -Wredundant-decls
3562306a36Sopenharmony_ciEXTRA_WARNINGS += -Wstrict-prototypes
3662306a36Sopenharmony_ciEXTRA_WARNINGS += -Wswitch-default
3762306a36Sopenharmony_ciEXTRA_WARNINGS += -Wswitch-enum
3862306a36Sopenharmony_ciEXTRA_WARNINGS += -Wundef
3962306a36Sopenharmony_ciEXTRA_WARNINGS += -Wwrite-strings
4062306a36Sopenharmony_ciEXTRA_WARNINGS += -Wformat
4162306a36Sopenharmony_ciEXTRA_WARNINGS += -Wno-type-limits
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci# Makefiles suck: This macro sets a default value of $(2) for the
4462306a36Sopenharmony_ci# variable named by $(1), unless the variable has been set by
4562306a36Sopenharmony_ci# environment or command line. This is necessary for CC and AR
4662306a36Sopenharmony_ci# because make sets default values, so the simpler ?= approach
4762306a36Sopenharmony_ci# won't work as expected.
4862306a36Sopenharmony_cidefine allow-override
4962306a36Sopenharmony_ci  $(if $(or $(findstring environment,$(origin $(1))),\
5062306a36Sopenharmony_ci            $(findstring command line,$(origin $(1)))),,\
5162306a36Sopenharmony_ci    $(eval $(1) = $(2)))
5262306a36Sopenharmony_ciendef
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ciifneq ($(LLVM),)
5562306a36Sopenharmony_ciifneq ($(filter %/,$(LLVM)),)
5662306a36Sopenharmony_ciLLVM_PREFIX := $(LLVM)
5762306a36Sopenharmony_cielse ifneq ($(filter -%,$(LLVM)),)
5862306a36Sopenharmony_ciLLVM_SUFFIX := $(LLVM)
5962306a36Sopenharmony_ciendif
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci$(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX))
6262306a36Sopenharmony_ci$(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX))
6362306a36Sopenharmony_ci$(call allow-override,LD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX))
6462306a36Sopenharmony_ci$(call allow-override,CXX,$(LLVM_PREFIX)clang++$(LLVM_SUFFIX))
6562306a36Sopenharmony_ci$(call allow-override,STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX))
6662306a36Sopenharmony_cielse
6762306a36Sopenharmony_ci# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
6862306a36Sopenharmony_ci$(call allow-override,CC,$(CROSS_COMPILE)gcc)
6962306a36Sopenharmony_ci$(call allow-override,AR,$(CROSS_COMPILE)ar)
7062306a36Sopenharmony_ci$(call allow-override,LD,$(CROSS_COMPILE)ld)
7162306a36Sopenharmony_ci$(call allow-override,CXX,$(CROSS_COMPILE)g++)
7262306a36Sopenharmony_ci$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
7362306a36Sopenharmony_ciendif
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciCC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciifneq ($(LLVM),)
7862306a36Sopenharmony_ciHOSTAR  ?= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
7962306a36Sopenharmony_ciHOSTCC  ?= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
8062306a36Sopenharmony_ciHOSTLD  ?= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
8162306a36Sopenharmony_cielse
8262306a36Sopenharmony_ciHOSTAR  ?= ar
8362306a36Sopenharmony_ciHOSTCC  ?= gcc
8462306a36Sopenharmony_ciHOSTLD  ?= ld
8562306a36Sopenharmony_ciendif
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci# Some tools require Clang, LLC and/or LLVM utils
8862306a36Sopenharmony_ciCLANG		?= clang
8962306a36Sopenharmony_ciLLC		?= llc
9062306a36Sopenharmony_ciLLVM_CONFIG	?= llvm-config
9162306a36Sopenharmony_ciLLVM_OBJCOPY	?= llvm-objcopy
9262306a36Sopenharmony_ciLLVM_STRIP	?= llvm-strip
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciifeq ($(CC_NO_CLANG), 1)
9562306a36Sopenharmony_ciEXTRA_WARNINGS += -Wstrict-aliasing=3
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cielse ifneq ($(CROSS_COMPILE),)
9862306a36Sopenharmony_ci# Allow userspace to override CLANG_CROSS_FLAGS to specify their own
9962306a36Sopenharmony_ci# sysroots and flags or to avoid the GCC call in pure Clang builds.
10062306a36Sopenharmony_ciifeq ($(CLANG_CROSS_FLAGS),)
10162306a36Sopenharmony_ciCLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
10262306a36Sopenharmony_ciGCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc 2>/dev/null))
10362306a36Sopenharmony_ciifneq ($(GCC_TOOLCHAIN_DIR),)
10462306a36Sopenharmony_ciCLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
10562306a36Sopenharmony_ciCLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)
10662306a36Sopenharmony_ciCLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
10762306a36Sopenharmony_ciendif # GCC_TOOLCHAIN_DIR
10862306a36Sopenharmony_ciendif # CLANG_CROSS_FLAGS
10962306a36Sopenharmony_ciCFLAGS += $(CLANG_CROSS_FLAGS)
11062306a36Sopenharmony_ciAFLAGS += $(CLANG_CROSS_FLAGS)
11162306a36Sopenharmony_cielse
11262306a36Sopenharmony_ciCLANG_CROSS_FLAGS :=
11362306a36Sopenharmony_ciendif # CROSS_COMPILE
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci# Hack to avoid type-punned warnings on old systems such as RHEL5:
11662306a36Sopenharmony_ci# We should be changing CFLAGS and checking gcc version, but this
11762306a36Sopenharmony_ci# will do for now and keep the above -Wstrict-aliasing=3 in place
11862306a36Sopenharmony_ci# in newer systems.
11962306a36Sopenharmony_ci# Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h
12062306a36Sopenharmony_ci#
12162306a36Sopenharmony_ci# See https://lore.kernel.org/lkml/9a8748490611281710g78402fbeh8ff7fcc162dbcbca@mail.gmail.com/
12262306a36Sopenharmony_ci# and https://gcc.gnu.org/gcc-4.8/changes.html,
12362306a36Sopenharmony_ci# that takes into account Linus's comments (search for Wshadow) for the reasoning about
12462306a36Sopenharmony_ci# -Wshadow not being interesting before gcc 4.8.
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ciifneq ($(filter 3.%,$(MAKE_VERSION)),)  # make-3
12762306a36Sopenharmony_ciEXTRA_WARNINGS += -fno-strict-aliasing
12862306a36Sopenharmony_ciEXTRA_WARNINGS += -Wno-shadow
12962306a36Sopenharmony_cielse
13062306a36Sopenharmony_ciEXTRA_WARNINGS += -Wshadow
13162306a36Sopenharmony_ciendif
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ciifneq ($(findstring $(MAKEFLAGS), w),w)
13462306a36Sopenharmony_ciPRINT_DIR = --no-print-directory
13562306a36Sopenharmony_cielse
13662306a36Sopenharmony_ciNO_SUBDIR = :
13762306a36Sopenharmony_ciendif
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
14062306a36Sopenharmony_ci  silent=1
14162306a36Sopenharmony_ciendif
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci#
14462306a36Sopenharmony_ci# Define a callable command for descending to a new directory
14562306a36Sopenharmony_ci#
14662306a36Sopenharmony_ci# Call by doing: $(call descend,directory[,target])
14762306a36Sopenharmony_ci#
14862306a36Sopenharmony_cidescend = \
14962306a36Sopenharmony_ci	+mkdir -p $(OUTPUT)$(1) && \
15062306a36Sopenharmony_ci	$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ciQUIET_SUBDIR0  = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir
15362306a36Sopenharmony_ciQUIET_SUBDIR1  =
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ciifneq ($(silent),1)
15662306a36Sopenharmony_ci  ifneq ($(V),1)
15762306a36Sopenharmony_ci	QUIET_CC       = @echo '  CC      '$@;
15862306a36Sopenharmony_ci	QUIET_CC_FPIC  = @echo '  CC FPIC '$@;
15962306a36Sopenharmony_ci	QUIET_CLANG    = @echo '  CLANG   '$@;
16062306a36Sopenharmony_ci	QUIET_AR       = @echo '  AR      '$@;
16162306a36Sopenharmony_ci	QUIET_LINK     = @echo '  LINK    '$@;
16262306a36Sopenharmony_ci	QUIET_MKDIR    = @echo '  MKDIR   '$@;
16362306a36Sopenharmony_ci	QUIET_GEN      = @echo '  GEN     '$@;
16462306a36Sopenharmony_ci	QUIET_SUBDIR0  = +@subdir=
16562306a36Sopenharmony_ci	QUIET_SUBDIR1  = ;$(NO_SUBDIR) \
16662306a36Sopenharmony_ci			  echo '  SUBDIR  '$$subdir; \
16762306a36Sopenharmony_ci			 $(MAKE) $(PRINT_DIR) -C $$subdir
16862306a36Sopenharmony_ci	QUIET_FLEX     = @echo '  FLEX    '$@;
16962306a36Sopenharmony_ci	QUIET_BISON    = @echo '  BISON   '$@;
17062306a36Sopenharmony_ci	QUIET_GENSKEL  = @echo '  GENSKEL '$@;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	descend = \
17362306a36Sopenharmony_ci		+@echo	       '  DESCEND '$(1); \
17462306a36Sopenharmony_ci		mkdir -p $(OUTPUT)$(1) && \
17562306a36Sopenharmony_ci		$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	QUIET_CLEAN    = @printf '  CLEAN   %s\n' $1;
17862306a36Sopenharmony_ci	QUIET_INSTALL  = @printf '  INSTALL %s\n' $1;
17962306a36Sopenharmony_ci	QUIET_UNINST   = @printf '  UNINST  %s\n' $1;
18062306a36Sopenharmony_ci  endif
18162306a36Sopenharmony_ciendif
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cipound := \#
184