18c2ecf20Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ciifneq ($(O),)
38c2ecf20Sopenharmony_ciifeq ($(origin O), command line)
48c2ecf20Sopenharmony_ci	dummy := $(if $(shell cd $(PWD); test -d $(O) || echo $(O)),$(error O=$(O) does not exist),)
58c2ecf20Sopenharmony_ci	ABSOLUTE_O := $(shell cd $(PWD); cd $(O) ; pwd)
68c2ecf20Sopenharmony_ci	OUTPUT := $(ABSOLUTE_O)/$(if $(subdir),$(subdir)/)
78c2ecf20Sopenharmony_ci	COMMAND_O := O=$(ABSOLUTE_O)
88c2ecf20Sopenharmony_ciifeq ($(objtree),)
98c2ecf20Sopenharmony_ci	objtree := $(O)
108c2ecf20Sopenharmony_ciendif
118c2ecf20Sopenharmony_ciendif
128c2ecf20Sopenharmony_ciendif
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci# check that the output directory actually exists
158c2ecf20Sopenharmony_ciifneq ($(OUTPUT),)
168c2ecf20Sopenharmony_ciOUTDIR := $(shell cd $(OUTPUT) && pwd)
178c2ecf20Sopenharmony_ci$(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist))
188c2ecf20Sopenharmony_ciendif
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#
218c2ecf20Sopenharmony_ci# Include saner warnings here, which can catch bugs:
228c2ecf20Sopenharmony_ci#
238c2ecf20Sopenharmony_ciEXTRA_WARNINGS := -Wbad-function-cast
248c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wdeclaration-after-statement
258c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wformat-security
268c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wformat-y2k
278c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Winit-self
288c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wmissing-declarations
298c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wmissing-prototypes
308c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wnested-externs
318c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wno-system-headers
328c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wold-style-definition
338c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wpacked
348c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wredundant-decls
358c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wstrict-prototypes
368c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wswitch-default
378c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wswitch-enum
388c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wundef
398c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wwrite-strings
408c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wformat
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci# Makefiles suck: This macro sets a default value of $(2) for the
438c2ecf20Sopenharmony_ci# variable named by $(1), unless the variable has been set by
448c2ecf20Sopenharmony_ci# environment or command line. This is necessary for CC and AR
458c2ecf20Sopenharmony_ci# because make sets default values, so the simpler ?= approach
468c2ecf20Sopenharmony_ci# won't work as expected.
478c2ecf20Sopenharmony_cidefine allow-override
488c2ecf20Sopenharmony_ci  $(if $(or $(findstring environment,$(origin $(1))),\
498c2ecf20Sopenharmony_ci            $(findstring command line,$(origin $(1)))),,\
508c2ecf20Sopenharmony_ci    $(eval $(1) = $(2)))
518c2ecf20Sopenharmony_ciendef
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ciifneq ($(LLVM),)
548c2ecf20Sopenharmony_ci$(call allow-override,CC,clang)
558c2ecf20Sopenharmony_ci$(call allow-override,AR,llvm-ar)
568c2ecf20Sopenharmony_ci$(call allow-override,LD,ld.lld)
578c2ecf20Sopenharmony_ci$(call allow-override,CXX,clang++)
588c2ecf20Sopenharmony_ci$(call allow-override,STRIP,llvm-strip)
598c2ecf20Sopenharmony_cielse
608c2ecf20Sopenharmony_ci# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
618c2ecf20Sopenharmony_ci$(call allow-override,CC,$(CROSS_COMPILE)gcc)
628c2ecf20Sopenharmony_ci$(call allow-override,AR,$(CROSS_COMPILE)ar)
638c2ecf20Sopenharmony_ci$(call allow-override,LD,$(CROSS_COMPILE)ld)
648c2ecf20Sopenharmony_ci$(call allow-override,CXX,$(CROSS_COMPILE)g++)
658c2ecf20Sopenharmony_ci$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
668c2ecf20Sopenharmony_ciendif
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciCC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciifneq ($(LLVM),)
718c2ecf20Sopenharmony_ciHOSTAR  ?= llvm-ar
728c2ecf20Sopenharmony_ciHOSTCC  ?= clang
738c2ecf20Sopenharmony_ciHOSTLD  ?= ld.lld
748c2ecf20Sopenharmony_cielse
758c2ecf20Sopenharmony_ciHOSTAR  ?= ar
768c2ecf20Sopenharmony_ciHOSTCC  ?= gcc
778c2ecf20Sopenharmony_ciHOSTLD  ?= ld
788c2ecf20Sopenharmony_ciendif
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ciifeq ($(CC_NO_CLANG), 1)
818c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wstrict-aliasing=3
828c2ecf20Sopenharmony_ciendif
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci# Hack to avoid type-punned warnings on old systems such as RHEL5:
858c2ecf20Sopenharmony_ci# We should be changing CFLAGS and checking gcc version, but this
868c2ecf20Sopenharmony_ci# will do for now and keep the above -Wstrict-aliasing=3 in place
878c2ecf20Sopenharmony_ci# in newer systems.
888c2ecf20Sopenharmony_ci# Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h
898c2ecf20Sopenharmony_ci#
908c2ecf20Sopenharmony_ci# See https://lkml.org/lkml/2006/11/28/253 and https://gcc.gnu.org/gcc-4.8/changes.html,
918c2ecf20Sopenharmony_ci# that takes into account Linus's comments (search for Wshadow) for the reasoning about
928c2ecf20Sopenharmony_ci# -Wshadow not being interesting before gcc 4.8.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciifneq ($(filter 3.%,$(MAKE_VERSION)),)  # make-3
958c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -fno-strict-aliasing
968c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wno-shadow
978c2ecf20Sopenharmony_cielse
988c2ecf20Sopenharmony_ciEXTRA_WARNINGS += -Wshadow
998c2ecf20Sopenharmony_ciendif
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciifneq ($(findstring $(MAKEFLAGS), w),w)
1028c2ecf20Sopenharmony_ciPRINT_DIR = --no-print-directory
1038c2ecf20Sopenharmony_cielse
1048c2ecf20Sopenharmony_ciNO_SUBDIR = :
1058c2ecf20Sopenharmony_ciendif
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
1088c2ecf20Sopenharmony_ci  silent=1
1098c2ecf20Sopenharmony_ciendif
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#
1128c2ecf20Sopenharmony_ci# Define a callable command for descending to a new directory
1138c2ecf20Sopenharmony_ci#
1148c2ecf20Sopenharmony_ci# Call by doing: $(call descend,directory[,target])
1158c2ecf20Sopenharmony_ci#
1168c2ecf20Sopenharmony_cidescend = \
1178c2ecf20Sopenharmony_ci	+mkdir -p $(OUTPUT)$(1) && \
1188c2ecf20Sopenharmony_ci	$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciQUIET_SUBDIR0  = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir
1218c2ecf20Sopenharmony_ciQUIET_SUBDIR1  =
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciifneq ($(silent),1)
1248c2ecf20Sopenharmony_ci  ifneq ($(V),1)
1258c2ecf20Sopenharmony_ci	QUIET_CC       = @echo '  CC       '$@;
1268c2ecf20Sopenharmony_ci	QUIET_CC_FPIC  = @echo '  CC FPIC  '$@;
1278c2ecf20Sopenharmony_ci	QUIET_CLANG    = @echo '  CLANG    '$@;
1288c2ecf20Sopenharmony_ci	QUIET_AR       = @echo '  AR       '$@;
1298c2ecf20Sopenharmony_ci	QUIET_LINK     = @echo '  LINK     '$@;
1308c2ecf20Sopenharmony_ci	QUIET_MKDIR    = @echo '  MKDIR    '$@;
1318c2ecf20Sopenharmony_ci	QUIET_GEN      = @echo '  GEN      '$@;
1328c2ecf20Sopenharmony_ci	QUIET_SUBDIR0  = +@subdir=
1338c2ecf20Sopenharmony_ci	QUIET_SUBDIR1  = ;$(NO_SUBDIR) \
1348c2ecf20Sopenharmony_ci			  echo '  SUBDIR   '$$subdir; \
1358c2ecf20Sopenharmony_ci			 $(MAKE) $(PRINT_DIR) -C $$subdir
1368c2ecf20Sopenharmony_ci	QUIET_FLEX     = @echo '  FLEX     '$@;
1378c2ecf20Sopenharmony_ci	QUIET_BISON    = @echo '  BISON    '$@;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	descend = \
1408c2ecf20Sopenharmony_ci		+@echo	       '  DESCEND  '$(1); \
1418c2ecf20Sopenharmony_ci		mkdir -p $(OUTPUT)$(1) && \
1428c2ecf20Sopenharmony_ci		$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	QUIET_CLEAN    = @printf '  CLEAN    %s\n' $1;
1458c2ecf20Sopenharmony_ci	QUIET_INSTALL  = @printf '  INSTALL  %s\n' $1;
1468c2ecf20Sopenharmony_ci	QUIET_UNINST   = @printf '  UNINST   %s\n' $1;
1478c2ecf20Sopenharmony_ci  endif
1488c2ecf20Sopenharmony_ciendif
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cipound := \#
151