1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0 29e4a6648SBorislav Petkovifneq ($(O),) 3bf35182fSDavid Howellsifeq ($(origin O), command line) 4c2bd08baSMasahiro Yamada $(if $(shell cd $(PWD); test -d $(O) || echo $(O)),$(error O=$(O) does not exist),) 5be40920fSMasami Hiramatsu ABSOLUTE_O := $(shell cd $(PWD); cd $(O) ; pwd) 6bf35182fSDavid Howells OUTPUT := $(ABSOLUTE_O)/$(if $(subdir),$(subdir)/) 7c883122aSSteven Rostedt COMMAND_O := O=$(ABSOLUTE_O) 8bf35182fSDavid Howellsifeq ($(objtree),) 9bf35182fSDavid Howells objtree := $(O) 10bf35182fSDavid Howellsendif 1198d89bfdSBorislav Petkovendif 129e4a6648SBorislav Petkovendif 1398d89bfdSBorislav Petkov 1498d89bfdSBorislav Petkov# check that the output directory actually exists 159e4a6648SBorislav Petkovifneq ($(OUTPUT),) 1616f8259cSBjørn ForsmanOUTDIR := $(shell cd $(OUTPUT) && pwd) 1798d89bfdSBorislav Petkov$(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist)) 1898d89bfdSBorislav Petkovendif 1998d89bfdSBorislav Petkov 2098d89bfdSBorislav Petkov# 2198d89bfdSBorislav Petkov# Include saner warnings here, which can catch bugs: 2298d89bfdSBorislav Petkov# 23d8caf3ebSBorislav PetkovEXTRA_WARNINGS := -Wbad-function-cast 24d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wdeclaration-after-statement 25d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wformat-security 26d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wformat-y2k 27d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Winit-self 28d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wmissing-declarations 29d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wmissing-prototypes 30d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wnested-externs 31d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wno-system-headers 32d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wold-style-definition 33d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wpacked 34d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wredundant-decls 35d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wstrict-prototypes 36d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wswitch-default 37d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wswitch-enum 38d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wundef 39d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wwrite-strings 40d8caf3ebSBorislav PetkovEXTRA_WARNINGS += -Wformat 41d1d1a2cdSYury NorovEXTRA_WARNINGS += -Wno-type-limits 4298d89bfdSBorislav Petkov 437ed1c190SMartin Kelly# Makefiles suck: This macro sets a default value of $(2) for the 447ed1c190SMartin Kelly# variable named by $(1), unless the variable has been set by 457ed1c190SMartin Kelly# environment or command line. This is necessary for CC and AR 467ed1c190SMartin Kelly# because make sets default values, so the simpler ?= approach 477ed1c190SMartin Kelly# won't work as expected. 487ed1c190SMartin Kellydefine allow-override 497ed1c190SMartin Kelly $(if $(or $(findstring environment,$(origin $(1))),\ 507ed1c190SMartin Kelly $(findstring command line,$(origin $(1)))),,\ 517ed1c190SMartin Kelly $(eval $(1) = $(2))) 527ed1c190SMartin Kellyendef 537ed1c190SMartin Kelly 54f62700ceSYonghong Songifneq ($(LLVM),) 55e9c28192SNathan Chancellorifneq ($(filter %/,$(LLVM)),) 56e9c28192SNathan ChancellorLLVM_PREFIX := $(LLVM) 57e9c28192SNathan Chancellorelse ifneq ($(filter -%,$(LLVM)),) 58e9c28192SNathan ChancellorLLVM_SUFFIX := $(LLVM) 59e9c28192SNathan Chancellorendif 60e9c28192SNathan Chancellor 61e9c28192SNathan Chancellor$(call allow-override,CC,$(LLVM_PREFIX)clang$(LLVM_SUFFIX)) 62e9c28192SNathan Chancellor$(call allow-override,AR,$(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX)) 63e9c28192SNathan Chancellor$(call allow-override,LD,$(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX)) 64e9c28192SNathan Chancellor$(call allow-override,CXX,$(LLVM_PREFIX)clang++$(LLVM_SUFFIX)) 65e9c28192SNathan Chancellor$(call allow-override,STRIP,$(LLVM_PREFIX)llvm-strip$(LLVM_SUFFIX)) 66f62700ceSYonghong Songelse 677ed1c190SMartin Kelly# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. 687ed1c190SMartin Kelly$(call allow-override,CC,$(CROSS_COMPILE)gcc) 697ed1c190SMartin Kelly$(call allow-override,AR,$(CROSS_COMPILE)ar) 707ed1c190SMartin Kelly$(call allow-override,LD,$(CROSS_COMPILE)ld) 717ed1c190SMartin Kelly$(call allow-override,CXX,$(CROSS_COMPILE)g++) 727ed1c190SMartin Kelly$(call allow-override,STRIP,$(CROSS_COMPILE)strip) 73f62700ceSYonghong Songendif 74f62700ceSYonghong Song 75f62700ceSYonghong SongCC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?) 767ed1c190SMartin Kelly 77c8a950d0SJean-Philippe Bruckerifneq ($(LLVM),) 78e9c28192SNathan ChancellorHOSTAR ?= $(LLVM_PREFIX)llvm-ar$(LLVM_SUFFIX) 79e9c28192SNathan ChancellorHOSTCC ?= $(LLVM_PREFIX)clang$(LLVM_SUFFIX) 80e9c28192SNathan ChancellorHOSTLD ?= $(LLVM_PREFIX)ld.lld$(LLVM_SUFFIX) 81c8a950d0SJean-Philippe Bruckerelse 82c8a950d0SJean-Philippe BruckerHOSTAR ?= ar 83c8a950d0SJean-Philippe BruckerHOSTCC ?= gcc 84c8a950d0SJean-Philippe BruckerHOSTLD ?= ld 85c8a950d0SJean-Philippe Bruckerendif 86c8a950d0SJean-Philippe Brucker 87211a741cSSedat Dilek# Some tools require Clang, LLC and/or LLVM utils 88211a741cSSedat DilekCLANG ?= clang 89211a741cSSedat DilekLLC ?= llc 90211a741cSSedat DilekLLVM_CONFIG ?= llvm-config 91211a741cSSedat DilekLLVM_OBJCOPY ?= llvm-objcopy 92211a741cSSedat DilekLLVM_STRIP ?= llvm-strip 93211a741cSSedat Dilek 948a635c38STomas Glozar# Some tools require bpftool 95*814d051eSTomas GlozarSYSTEM_BPFTOOL ?= bpftool 968a635c38STomas Glozar 973866058eSDavid Carrillo-Cisnerosifeq ($(CC_NO_CLANG), 1) 98093b75efSArnaldo Carvalho de MeloEXTRA_WARNINGS += -Wstrict-aliasing=3 99cebdb737SJean-Philippe Brucker 100cebdb737SJean-Philippe Bruckerelse ifneq ($(CROSS_COMPILE),) 1017fd9fd46SAdrian Ratiu# Allow userspace to override CLANG_CROSS_FLAGS to specify their own 1027fd9fd46SAdrian Ratiu# sysroots and flags or to avoid the GCC call in pure Clang builds. 1037fd9fd46SAdrian Ratiuifeq ($(CLANG_CROSS_FLAGS),) 104cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%)) 105b7892f7dSJean-Philippe BruckerGCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc 2>/dev/null)) 106cebdb737SJean-Philippe Bruckerifneq ($(GCC_TOOLCHAIN_DIR),) 107cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE)) 108cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot) 109cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..) 110cebdb737SJean-Philippe Bruckerendif # GCC_TOOLCHAIN_DIR 1117fd9fd46SAdrian Ratiuendif # CLANG_CROSS_FLAGS 112cebdb737SJean-Philippe BruckerCFLAGS += $(CLANG_CROSS_FLAGS) 113cebdb737SJean-Philippe BruckerAFLAGS += $(CLANG_CROSS_FLAGS) 114b539a287SFlorent Revestelse 115b539a287SFlorent RevestCLANG_CROSS_FLAGS := 116cebdb737SJean-Philippe Bruckerendif # CROSS_COMPILE 117093b75efSArnaldo Carvalho de Melo 1183337e682SArnaldo Carvalho de Melo# Hack to avoid type-punned warnings on old systems such as RHEL5: 1193337e682SArnaldo Carvalho de Melo# We should be changing CFLAGS and checking gcc version, but this 1203337e682SArnaldo Carvalho de Melo# will do for now and keep the above -Wstrict-aliasing=3 in place 1213337e682SArnaldo Carvalho de Melo# in newer systems. 1223337e682SArnaldo Carvalho de Melo# Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h 12339e7317eSArnaldo Carvalho de Melo# 124c9491aadSKees Cook# See https://lore.kernel.org/lkml/[email protected]/ 125c9491aadSKees Cook# and https://gcc.gnu.org/gcc-4.8/changes.html, 12639e7317eSArnaldo Carvalho de Melo# that takes into account Linus's comments (search for Wshadow) for the reasoning about 12739e7317eSArnaldo Carvalho de Melo# -Wshadow not being interesting before gcc 4.8. 12839e7317eSArnaldo Carvalho de Melo 1293337e682SArnaldo Carvalho de Meloifneq ($(filter 3.%,$(MAKE_VERSION)),) # make-3 1303337e682SArnaldo Carvalho de MeloEXTRA_WARNINGS += -fno-strict-aliasing 13139e7317eSArnaldo Carvalho de MeloEXTRA_WARNINGS += -Wno-shadow 13239e7317eSArnaldo Carvalho de Meloelse 13339e7317eSArnaldo Carvalho de MeloEXTRA_WARNINGS += -Wshadow 1343337e682SArnaldo Carvalho de Meloendif 1353337e682SArnaldo Carvalho de Melo 13698d89bfdSBorislav Petkovifneq ($(findstring $(MAKEFLAGS), w),w) 13798d89bfdSBorislav PetkovPRINT_DIR = --no-print-directory 13898d89bfdSBorislav Petkovelse 13998d89bfdSBorislav PetkovNO_SUBDIR = : 14098d89bfdSBorislav Petkovendif 14198d89bfdSBorislav Petkov 142b5c532e9SJiri Olsa# Beautify output 143b5c532e9SJiri Olsa# --------------------------------------------------------------------------- 144b5c532e9SJiri Olsa# 145b5c532e9SJiri Olsa# Most of build commands in Kbuild start with "cmd_". You can optionally define 146b5c532e9SJiri Olsa# "quiet_cmd_*". If defined, the short log is printed. Otherwise, no log from 147b5c532e9SJiri Olsa# that command is printed by default. 148b5c532e9SJiri Olsa# 149b5c532e9SJiri Olsa# e.g.) 150b5c532e9SJiri Olsa# quiet_cmd_depmod = DEPMOD $(MODLIB) 151e572d088SJosh Poimboeuf# cmd_depmod = $(srctree)/scripts/depmod.sh $(DEPMOD) $(KERNELRELEASE) 152e572d088SJosh Poimboeuf# 153e572d088SJosh Poimboeuf# A simple variant is to prefix commands with $(Q) - that's useful 154ca9dfc6cSDavid Howells# for commands that shall be hidden in non-verbose mode. 155ca9dfc6cSDavid Howells# 156ca9dfc6cSDavid Howells# $(Q)$(MAKE) $(build)=scripts/basic 157ca9dfc6cSDavid Howells# 158ca9dfc6cSDavid Howells# To put more focus on warnings, be less verbose as default 159ca9dfc6cSDavid Howells# Use 'make V=1' to see the full commands 160bf35182fSDavid Howells 1612b73f65dSDavid Howellsifeq ($(V),1) 162ca9dfc6cSDavid Howells quiet = 163bf35182fSDavid Howells Q = 16498d89bfdSBorislav Petkovelse 16598d89bfdSBorislav Petkov quiet = quiet_ 166e572d088SJosh Poimboeuf Q = @ 1675125bc22SRobert Richterendif 16865fb0992SIngo Molnar 169bdebbacdSJiri Olsa# If the user is running make -s (silent mode), suppress echoing of commands 17047c09d6aSSong Liu# make-4.0 (and later) keep single letter options in the 1st word of MAKEFLAGS. 17165fb0992SIngo Molnarifeq ($(filter 3.%,$(MAKE_VERSION)),) 17265fb0992SIngo Molnarshort-opts := $(firstword -$(MAKEFLAGS)) 17365fb0992SIngo Molnarelse 17465fb0992SIngo Molnarshort-opts := $(filter-out --%,$(MAKEFLAGS)) 17598d89bfdSBorislav Petkovendif 17665fb0992SIngo Molnar 17765fb0992SIngo Molnarifneq ($(findstring s,$(short-opts)),) 17898d89bfdSBorislav Petkov silent=1 17965fb0992SIngo Molnar quiet=silent_ 18065fb0992SIngo Molnarendif 181c6de37ddSKees Cook 182bf35182fSDavid Howellsexport quiet Q 183bf35182fSDavid Howells 18465fb0992SIngo Molnar# 185bf35182fSDavid Howells# Define a callable command for descending to a new directory 1862b73f65dSDavid Howells# 187b7248defSJiri Olsa# Call by doing: $(call descend,directory[,target]) 188b7248defSJiri Olsa# 189b7248defSJiri Olsadescend = \ 190d3244248SQuentin Monnet +mkdir -p $(OUTPUT)$(1) && \ 19198d89bfdSBorislav Petkov $(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2) 19298d89bfdSBorislav Petkov 1939564a8cfSRasmus VillemoesQUIET_SUBDIR0 = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir 1949564a8cfSRasmus VillemoesQUIET_SUBDIR1 = 195 196ifneq ($(silent),1) 197 ifneq ($(V),1) 198 QUIET_CC = @echo ' CC '$@; 199 QUIET_CC_FPIC = @echo ' CC FPIC '$@; 200 QUIET_CLANG = @echo ' CLANG '$@; 201 QUIET_AR = @echo ' AR '$@; 202 QUIET_LINK = @echo ' LINK '$@; 203 QUIET_MKDIR = @echo ' MKDIR '$@; 204 QUIET_GEN = @echo ' GEN '$@; 205 QUIET_SUBDIR0 = +@subdir= 206 QUIET_SUBDIR1 = ;$(NO_SUBDIR) \ 207 echo ' SUBDIR '$$subdir; \ 208 $(MAKE) $(PRINT_DIR) -C $$subdir 209 QUIET_FLEX = @echo ' FLEX '$@; 210 QUIET_BISON = @echo ' BISON '$@; 211 QUIET_GENSKEL = @echo ' GENSKEL '$@; 212 213 descend = \ 214 +@echo ' DESCEND '$(1); \ 215 mkdir -p $(OUTPUT)$(1) && \ 216 $(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2) 217 218 QUIET_CLEAN = @printf ' CLEAN %s\n' $1; 219 QUIET_INSTALL = @printf ' INSTALL %s\n' $1; 220 QUIET_UNINST = @printf ' UNINST %s\n' $1; 221 endif 222endif 223 224pound := \# 225