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