1# SPDX-License-Identifier: GPL-2.0
2ifneq ($(O),)
3ifeq ($(origin O), command line)
4	dummy := $(if $(shell cd $(PWD); test -d $(O) || echo $(O)),$(error O=$(O) does not exist),)
5	ABSOLUTE_O := $(shell cd $(PWD); cd $(O) ; pwd)
6	OUTPUT := $(ABSOLUTE_O)/$(if $(subdir),$(subdir)/)
7	COMMAND_O := O=$(ABSOLUTE_O)
8ifeq ($(objtree),)
9	objtree := $(O)
10endif
11endif
12endif
13
14# check that the output directory actually exists
15ifneq ($(OUTPUT),)
16OUTDIR := $(shell cd $(OUTPUT) && pwd)
17$(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist))
18endif
19
20#
21# Include saner warnings here, which can catch bugs:
22#
23EXTRA_WARNINGS := -Wbad-function-cast
24EXTRA_WARNINGS += -Wdeclaration-after-statement
25EXTRA_WARNINGS += -Wformat-security
26EXTRA_WARNINGS += -Wformat-y2k
27EXTRA_WARNINGS += -Winit-self
28EXTRA_WARNINGS += -Wmissing-declarations
29EXTRA_WARNINGS += -Wmissing-prototypes
30EXTRA_WARNINGS += -Wnested-externs
31EXTRA_WARNINGS += -Wno-system-headers
32EXTRA_WARNINGS += -Wold-style-definition
33EXTRA_WARNINGS += -Wpacked
34EXTRA_WARNINGS += -Wredundant-decls
35EXTRA_WARNINGS += -Wstrict-prototypes
36EXTRA_WARNINGS += -Wswitch-default
37EXTRA_WARNINGS += -Wswitch-enum
38EXTRA_WARNINGS += -Wundef
39EXTRA_WARNINGS += -Wwrite-strings
40EXTRA_WARNINGS += -Wformat
41EXTRA_WARNINGS += -Wno-type-limits
42
43# Makefiles suck: This macro sets a default value of $(2) for the
44# variable named by $(1), unless the variable has been set by
45# environment or command line. This is necessary for CC and AR
46# because make sets default values, so the simpler ?= approach
47# won't work as expected.
48define allow-override
49  $(if $(or $(findstring environment,$(origin $(1))),\
50            $(findstring command line,$(origin $(1)))),,\
51    $(eval $(1) = $(2)))
52endef
53
54ifneq ($(LLVM),)
55ifneq ($(filter %/,$(LLVM)),)
56LLVM_PREFIX := $(LLVM)
57else ifneq ($(filter -%,$(LLVM)),)
58LLVM_SUFFIX := $(LLVM)
59endif
60
61$(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX))
62$(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX))
63$(call allow-override,LD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX))
64$(call allow-override,CXX,$(LLVM_PREFIX)clang++$(LLVM_SUFFIX))
65$(call allow-override,STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX))
66else
67# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
68$(call allow-override,CC,$(CROSS_COMPILE)gcc)
69$(call allow-override,AR,$(CROSS_COMPILE)ar)
70$(call allow-override,LD,$(CROSS_COMPILE)ld)
71$(call allow-override,CXX,$(CROSS_COMPILE)g++)
72$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
73endif
74
75CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
76
77ifneq ($(LLVM),)
78HOSTAR  ?= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)
79HOSTCC  ?= $(LLVM_PREFIX)clang$(LLVM_SUFFIX)
80HOSTLD  ?= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)
81else
82HOSTAR  ?= ar
83HOSTCC  ?= gcc
84HOSTLD  ?= ld
85endif
86
87# Some tools require Clang, LLC and/or LLVM utils
88CLANG		?= clang
89LLC		?= llc
90LLVM_CONFIG	?= llvm-config
91LLVM_OBJCOPY	?= llvm-objcopy
92LLVM_STRIP	?= llvm-strip
93
94ifeq ($(CC_NO_CLANG), 1)
95EXTRA_WARNINGS += -Wstrict-aliasing=3
96
97else ifneq ($(CROSS_COMPILE),)
98# Allow userspace to override CLANG_CROSS_FLAGS to specify their own
99# sysroots and flags or to avoid the GCC call in pure Clang builds.
100ifeq ($(CLANG_CROSS_FLAGS),)
101CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
102GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc 2>/dev/null))
103ifneq ($(GCC_TOOLCHAIN_DIR),)
104CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
105CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)
106CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
107endif # GCC_TOOLCHAIN_DIR
108endif # CLANG_CROSS_FLAGS
109CFLAGS += $(CLANG_CROSS_FLAGS)
110AFLAGS += $(CLANG_CROSS_FLAGS)
111else
112CLANG_CROSS_FLAGS :=
113endif # CROSS_COMPILE
114
115# Hack to avoid type-punned warnings on old systems such as RHEL5:
116# We should be changing CFLAGS and checking gcc version, but this
117# will do for now and keep the above -Wstrict-aliasing=3 in place
118# in newer systems.
119# Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h
120#
121# See https://lore.kernel.org/lkml/9a8748490611281710g78402fbeh8ff7fcc162dbcbca@mail.gmail.com/
122# and https://gcc.gnu.org/gcc-4.8/changes.html,
123# that takes into account Linus's comments (search for Wshadow) for the reasoning about
124# -Wshadow not being interesting before gcc 4.8.
125
126ifneq ($(filter 3.%,$(MAKE_VERSION)),)  # make-3
127EXTRA_WARNINGS += -fno-strict-aliasing
128EXTRA_WARNINGS += -Wno-shadow
129else
130EXTRA_WARNINGS += -Wshadow
131endif
132
133ifneq ($(findstring $(MAKEFLAGS), w),w)
134PRINT_DIR = --no-print-directory
135else
136NO_SUBDIR = :
137endif
138
139ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
140  silent=1
141endif
142
143#
144# Define a callable command for descending to a new directory
145#
146# Call by doing: $(call descend,directory[,target])
147#
148descend = \
149	+mkdir -p $(OUTPUT)$(1) && \
150	$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
151
152QUIET_SUBDIR0  = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir
153QUIET_SUBDIR1  =
154
155ifneq ($(silent),1)
156  ifneq ($(V),1)
157	QUIET_CC       = @echo '  CC      '$@;
158	QUIET_CC_FPIC  = @echo '  CC FPIC '$@;
159	QUIET_CLANG    = @echo '  CLANG   '$@;
160	QUIET_AR       = @echo '  AR      '$@;
161	QUIET_LINK     = @echo '  LINK    '$@;
162	QUIET_MKDIR    = @echo '  MKDIR   '$@;
163	QUIET_GEN      = @echo '  GEN     '$@;
164	QUIET_SUBDIR0  = +@subdir=
165	QUIET_SUBDIR1  = ;$(NO_SUBDIR) \
166			  echo '  SUBDIR  '$$subdir; \
167			 $(MAKE) $(PRINT_DIR) -C $$subdir
168	QUIET_FLEX     = @echo '  FLEX    '$@;
169	QUIET_BISON    = @echo '  BISON   '$@;
170	QUIET_GENSKEL  = @echo '  GENSKEL '$@;
171
172	descend = \
173		+@echo	       '  DESCEND '$(1); \
174		mkdir -p $(OUTPUT)$(1) && \
175		$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
176
177	QUIET_CLEAN    = @printf '  CLEAN   %s\n' $1;
178	QUIET_INSTALL  = @printf '  INSTALL %s\n' $1;
179	QUIET_UNINST   = @printf '  UNINST  %s\n' $1;
180  endif
181endif
182
183pound := \#
184