| 1 | # SPDX-License-Identifier: GPL-2.0 |
| 2 | ifneq ($(O),) |
| 3 | ifeq ($(origin O), command line) |
| 4 | $(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) |
| 8 | ifeq ($(objtree),) |
| 9 | objtree := $(O) |
| 10 | endif |
| 11 | endif |
| 12 | endif |
| 13 | |
| 14 | # check that the output directory actually exists |
| 15 | ifneq ($(OUTPUT),) |
| 16 | OUTDIR := $(shell cd $(OUTPUT) && pwd) |
| 17 | $(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist)) |
| 18 | endif |
| 19 | |
| 20 | # |
| 21 | # Include saner warnings here, which can catch bugs: |
| 22 | # |
| 23 | EXTRA_WARNINGS := -Wbad-function-cast |
| 24 | EXTRA_WARNINGS += -Wdeclaration-after-statement |
| 25 | EXTRA_WARNINGS += -Wformat-security |
| 26 | EXTRA_WARNINGS += -Wformat-y2k |
| 27 | EXTRA_WARNINGS += -Winit-self |
| 28 | EXTRA_WARNINGS += -Wmissing-declarations |
| 29 | EXTRA_WARNINGS += -Wmissing-prototypes |
| 30 | EXTRA_WARNINGS += -Wnested-externs |
| 31 | EXTRA_WARNINGS += -Wno-system-headers |
| 32 | EXTRA_WARNINGS += -Wold-style-definition |
| 33 | EXTRA_WARNINGS += -Wpacked |
| 34 | EXTRA_WARNINGS += -Wredundant-decls |
| 35 | EXTRA_WARNINGS += -Wstrict-prototypes |
| 36 | EXTRA_WARNINGS += -Wswitch-default |
| 37 | EXTRA_WARNINGS += -Wswitch-enum |
| 38 | EXTRA_WARNINGS += -Wundef |
| 39 | EXTRA_WARNINGS += -Wwrite-strings |
| 40 | EXTRA_WARNINGS += -Wformat |
| 41 | EXTRA_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. |
| 48 | define allow-override |
| 49 | $(if $(or $(findstring environment,$(origin $(1))),\ |
| 50 | $(findstring command line,$(origin $(1)))),,\ |
| 51 | $(eval $(1) = $(2))) |
| 52 | endef |
| 53 | |
| 54 | ifneq ($(LLVM),) |
| 55 | ifneq ($(filter %/,$(LLVM)),) |
| 56 | LLVM_PREFIX := $(LLVM) |
| 57 | else ifneq ($(filter -%,$(LLVM)),) |
| 58 | LLVM_SUFFIX := $(LLVM) |
| 59 | else ifneq ($(LLVM),1) |
| 60 | $(error Invalid value for LLVM, see Documentation/kbuild/llvm.rst) |
| 61 | endif |
| 62 | |
| 63 | $(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) |
| 64 | $(call allow-override,CLANG,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) |
| 65 | $(call allow-override,HOSTCC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) |
| 66 | $(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)) |
| 67 | $(call allow-override,HOSTAR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)) |
| 68 | $(call allow-override,LD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)) |
| 69 | $(call allow-override,HOSTLD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)) |
| 70 | $(call allow-override,CXX,$(LLVM_PREFIX)clang++$(LLVM_SUFFIX)) |
| 71 | $(call allow-override,STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)) |
| 72 | $(call allow-override,LLVM_STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)) |
| 73 | $(call allow-override,LLC,$(LLVM_PREFIX)llc$(LLVM_SUFFIX)) |
| 74 | $(call allow-override,LLVM_CONFIG,$(LLVM_PREFIX)llvm-config$(LLVM_SUFFIX)) |
| 75 | $(call allow-override,LLVM_OBJCOPY,$(LLVM_PREFIX)llvm-objcopy$(LLVM_SUFFIX)) |
| 76 | $(call allow-override,READELF,$(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)) |
| 77 | $(call allow-override,HOSTREADELF,$(LLVM_PREFIX)llvm-readelf$(LLVM_SUFFIX)) |
| 78 | else |
| 79 | # Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. |
| 80 | $(call allow-override,CC,$(CROSS_COMPILE)gcc) |
| 81 | $(call allow-override,AR,$(CROSS_COMPILE)ar) |
| 82 | $(call allow-override,LD,$(CROSS_COMPILE)ld) |
| 83 | $(call allow-override,CXX,$(CROSS_COMPILE)g++) |
| 84 | $(call allow-override,STRIP,$(CROSS_COMPILE)strip) |
| 85 | $(call allow-override,READELF,$(CROSS_COMPILE)readelf) |
| 86 | |
| 87 | # Host versions aren't prefixed |
| 88 | $(call allow-override,HOSTAR,ar) |
| 89 | $(call allow-override,HOSTCC,gcc) |
| 90 | $(call allow-override,HOSTLD,ld) |
| 91 | $(call allow-override,HOSTREADELF,readelf) |
| 92 | |
| 93 | # Some tools still require Clang, LLC and/or LLVM utils |
| 94 | $(call allow-override,CLANG,clang) |
| 95 | $(call allow-override,LLC,llc) |
| 96 | $(call allow-override,LLVM_CONFIG,llvm-config) |
| 97 | $(call allow-override,LLVM_OBJCOPY,llvm-objcopy) |
| 98 | $(call allow-override,LLVM_STRIP,llvm-strip) |
| 99 | endif |
| 100 | |
| 101 | # The CC_NO_CLANG probe also runs at parse time for targets that never |
| 102 | # compile, so guard it with 'command -v': when the compiler is not yet |
| 103 | # installed, e.g. the first install-build-deps run, it would otherwise |
| 104 | # spew "gcc: not found". Only the first word of CC is consulted, as CC |
| 105 | # may carry arguments, e.g. 'ccache gcc'. A missing compiler leaves |
| 106 | # CC_NO_CLANG at 1, as if the probe had failed. |
| 107 | CC_NO_CLANG := $(shell if command -v $(firstword $(CC)) >/dev/null 2>&1; then $(CC) -dM -E -x c /dev/null; fi | grep -Fq "__clang__"; echo $$?) |
| 108 | |
| 109 | # Some tools require bpftool |
| 110 | SYSTEM_BPFTOOL ?= bpftool |
| 111 | |
| 112 | RUSTC ?= rustc |
| 113 | |
| 114 | ifeq ($(CC_NO_CLANG), 1) |
| 115 | EXTRA_WARNINGS += -Wstrict-aliasing=3 |
| 116 | |
| 117 | else ifneq ($(CROSS_COMPILE),) |
| 118 | # Allow userspace to override CLANG_CROSS_FLAGS to specify their own |
| 119 | # sysroots and flags or to avoid the GCC call in pure Clang builds. |
| 120 | ifeq ($(CLANG_CROSS_FLAGS),) |
| 121 | CLANG_TARGET := $(notdir $(CROSS_COMPILE:%-=%)) |
| 122 | CLANG_TARGET := $(subst s390-linux,s390x-linux,$(CLANG_TARGET)) |
| 123 | CLANG_CROSS_FLAGS := --target=$(CLANG_TARGET) |
| 124 | GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc 2>/dev/null)) |
| 125 | ifneq ($(GCC_TOOLCHAIN_DIR),) |
| 126 | CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE)) |
| 127 | CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot) |
| 128 | CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..) |
| 129 | endif # GCC_TOOLCHAIN_DIR |
| 130 | endif # CLANG_CROSS_FLAGS |
| 131 | CFLAGS += $(CLANG_CROSS_FLAGS) |
| 132 | AFLAGS += $(CLANG_CROSS_FLAGS) |
| 133 | else |
| 134 | CLANG_CROSS_FLAGS := |
| 135 | endif # CROSS_COMPILE |
| 136 | |
| 137 | # Hack to avoid type-punned warnings on old systems such as RHEL5: |
| 138 | # We should be changing CFLAGS and checking gcc version, but this |
| 139 | # will do for now and keep the above -Wstrict-aliasing=3 in place |
| 140 | # in newer systems. |
| 141 | # Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h |
| 142 | # |
| 143 | # See https://lore.kernel.org/lkml/[email protected]/ |
| 144 | # and https://gcc.gnu.org/gcc-4.8/changes.html, |
| 145 | # that takes into account Linus's comments (search for Wshadow) for the reasoning about |
| 146 | # -Wshadow not being interesting before gcc 4.8. |
| 147 | |
| 148 | ifneq ($(filter 3.%,$(MAKE_VERSION)),) # make-3 |
| 149 | EXTRA_WARNINGS += -fno-strict-aliasing |
| 150 | EXTRA_WARNINGS += -Wno-shadow |
| 151 | else |
| 152 | EXTRA_WARNINGS += -Wshadow |
| 153 | endif |
| 154 | |
| 155 | ifneq ($(findstring $(MAKEFLAGS), w),w) |
| 156 | PRINT_DIR = --no-print-directory |
| 157 | else |
| 158 | NO_SUBDIR = : |
| 159 | endif |
| 160 | |
| 161 | # Beautify output |
| 162 | # --------------------------------------------------------------------------- |
| 163 | # |
| 164 | # Most of build commands in Kbuild start with "cmd_". You can optionally define |
| 165 | # "quiet_cmd_*". If defined, the short log is printed. Otherwise, no log from |
| 166 | # that command is printed by default. |
| 167 | # |
| 168 | # e.g.) |
| 169 | # quiet_cmd_depmod = DEPMOD $(MODLIB) |
| 170 | # cmd_depmod = $(srctree)/scripts/depmod.sh $(DEPMOD) $(KERNELRELEASE) |
| 171 | # |
| 172 | # A simple variant is to prefix commands with $(Q) - that's useful |
| 173 | # for commands that shall be hidden in non-verbose mode. |
| 174 | # |
| 175 | # $(Q)$(MAKE) $(build)=scripts/basic |
| 176 | # |
| 177 | # To put more focus on warnings, be less verbose as default |
| 178 | # Use 'make V=1' to see the full commands |
| 179 | |
| 180 | ifeq ($(V),1) |
| 181 | quiet = |
| 182 | Q = |
| 183 | else |
| 184 | quiet = quiet_ |
| 185 | Q = @ |
| 186 | endif |
| 187 | |
| 188 | # If the user is running make -s (silent mode), suppress echoing of commands |
| 189 | # make-4.0 (and later) keep single letter options in the 1st word of MAKEFLAGS. |
| 190 | ifeq ($(filter 3.%,$(MAKE_VERSION)),) |
| 191 | short-opts := $(firstword -$(MAKEFLAGS)) |
| 192 | else |
| 193 | short-opts := $(filter-out --%,$(MAKEFLAGS)) |
| 194 | endif |
| 195 | |
| 196 | ifneq ($(findstring s,$(short-opts)),) |
| 197 | silent=1 |
| 198 | quiet=silent_ |
| 199 | endif |
| 200 | |
| 201 | export quiet Q |
| 202 | |
| 203 | # |
| 204 | # Define a callable command for descending to a new directory |
| 205 | # |
| 206 | # Call by doing: $(call descend,directory[,target]) |
| 207 | # |
| 208 | descend = \ |
| 209 | +mkdir -p $(OUTPUT)$(1) && \ |
| 210 | $(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2) |
| 211 | |
| 212 | QUIET_SUBDIR0 = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir |
| 213 | QUIET_SUBDIR1 = |
| 214 | |
| 215 | ifneq ($(silent),1) |
| 216 | ifneq ($(V),1) |
| 217 | QUIET_CC = @echo ' CC '$@; |
| 218 | QUIET_CC_FPIC = @echo ' CC FPIC '$@; |
| 219 | QUIET_CLANG = @echo ' CLANG '$@; |
| 220 | QUIET_AR = @echo ' AR '$@; |
| 221 | QUIET_LINK = @echo ' LINK '$@; |
| 222 | QUIET_MKDIR = @echo ' MKDIR '$@; |
| 223 | QUIET_GEN = @echo ' GEN '$@; |
| 224 | QUIET_SUBDIR0 = +@subdir= |
| 225 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) \ |
| 226 | echo ' SUBDIR '$$subdir; \ |
| 227 | $(MAKE) $(PRINT_DIR) -C $$subdir |
| 228 | QUIET_FLEX = @echo ' FLEX '$@; |
| 229 | QUIET_BISON = @echo ' BISON '$@; |
| 230 | QUIET_GENSKEL = @echo ' GENSKEL '$@; |
| 231 | |
| 232 | descend = \ |
| 233 | +@echo ' DESCEND '$(1); \ |
| 234 | mkdir -p $(OUTPUT)$(1) && \ |
| 235 | $(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2) |
| 236 | |
| 237 | QUIET_CLEAN = @printf ' CLEAN %s\n' $1; |
| 238 | QUIET_INSTALL = @printf ' INSTALL %s\n' $1; |
| 239 | QUIET_UNINST = @printf ' UNINST %s\n' $1; |
| 240 | endif |
| 241 | endif |
| 242 | |
| 243 | pound := \# |