xref: /linux/tools/scripts/Makefile.include (revision 7fd9fd46a459272e641be78c1cc36baab1921fa1)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
29e4a6648SBorislav Petkovifneq ($(O),)
3bf35182fSDavid Howellsifeq ($(origin O), command line)
4be40920fSMasami Hiramatsu	dummy := $(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),)
55f62700ceSYonghong Song$(call allow-override,CC,clang)
56f62700ceSYonghong Song$(call allow-override,AR,llvm-ar)
57f62700ceSYonghong Song$(call allow-override,LD,ld.lld)
58f62700ceSYonghong Song$(call allow-override,CXX,clang++)
59f62700ceSYonghong Song$(call allow-override,STRIP,llvm-strip)
60f62700ceSYonghong Songelse
617ed1c190SMartin Kelly# Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix.
627ed1c190SMartin Kelly$(call allow-override,CC,$(CROSS_COMPILE)gcc)
637ed1c190SMartin Kelly$(call allow-override,AR,$(CROSS_COMPILE)ar)
647ed1c190SMartin Kelly$(call allow-override,LD,$(CROSS_COMPILE)ld)
657ed1c190SMartin Kelly$(call allow-override,CXX,$(CROSS_COMPILE)g++)
667ed1c190SMartin Kelly$(call allow-override,STRIP,$(CROSS_COMPILE)strip)
67f62700ceSYonghong Songendif
68f62700ceSYonghong Song
69f62700ceSYonghong SongCC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?)
707ed1c190SMartin Kelly
71c8a950d0SJean-Philippe Bruckerifneq ($(LLVM),)
72c8a950d0SJean-Philippe BruckerHOSTAR  ?= llvm-ar
73c8a950d0SJean-Philippe BruckerHOSTCC  ?= clang
74c8a950d0SJean-Philippe BruckerHOSTLD  ?= ld.lld
75c8a950d0SJean-Philippe Bruckerelse
76c8a950d0SJean-Philippe BruckerHOSTAR  ?= ar
77c8a950d0SJean-Philippe BruckerHOSTCC  ?= gcc
78c8a950d0SJean-Philippe BruckerHOSTLD  ?= ld
79c8a950d0SJean-Philippe Bruckerendif
80c8a950d0SJean-Philippe Brucker
81211a741cSSedat Dilek# Some tools require Clang, LLC and/or LLVM utils
82211a741cSSedat DilekCLANG		?= clang
83211a741cSSedat DilekLLC		?= llc
84211a741cSSedat DilekLLVM_CONFIG	?= llvm-config
85211a741cSSedat DilekLLVM_OBJCOPY	?= llvm-objcopy
86211a741cSSedat DilekLLVM_STRIP	?= llvm-strip
87211a741cSSedat Dilek
883866058eSDavid Carrillo-Cisnerosifeq ($(CC_NO_CLANG), 1)
89093b75efSArnaldo Carvalho de MeloEXTRA_WARNINGS += -Wstrict-aliasing=3
90cebdb737SJean-Philippe Brucker
91cebdb737SJean-Philippe Bruckerelse ifneq ($(CROSS_COMPILE),)
92*7fd9fd46SAdrian Ratiu# Allow userspace to override CLANG_CROSS_FLAGS to specify their own
93*7fd9fd46SAdrian Ratiu# sysroots and flags or to avoid the GCC call in pure Clang builds.
94*7fd9fd46SAdrian Ratiuifeq ($(CLANG_CROSS_FLAGS),)
95cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%))
96b7892f7dSJean-Philippe BruckerGCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc 2>/dev/null))
97cebdb737SJean-Philippe Bruckerifneq ($(GCC_TOOLCHAIN_DIR),)
98cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE))
99cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot)
100cebdb737SJean-Philippe BruckerCLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..)
101cebdb737SJean-Philippe Bruckerendif # GCC_TOOLCHAIN_DIR
102*7fd9fd46SAdrian Ratiuendif # CLANG_CROSS_FLAGS
103cebdb737SJean-Philippe BruckerCFLAGS += $(CLANG_CROSS_FLAGS)
104cebdb737SJean-Philippe BruckerAFLAGS += $(CLANG_CROSS_FLAGS)
105cebdb737SJean-Philippe Bruckerendif # CROSS_COMPILE
106093b75efSArnaldo Carvalho de Melo
1073337e682SArnaldo Carvalho de Melo# Hack to avoid type-punned warnings on old systems such as RHEL5:
1083337e682SArnaldo Carvalho de Melo# We should be changing CFLAGS and checking gcc version, but this
1093337e682SArnaldo Carvalho de Melo# will do for now and keep the above -Wstrict-aliasing=3 in place
1103337e682SArnaldo Carvalho de Melo# in newer systems.
1113337e682SArnaldo Carvalho de Melo# Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h
11239e7317eSArnaldo Carvalho de Melo#
113c9491aadSKees Cook# See https://lore.kernel.org/lkml/9a8748490611281710g78402fbeh8ff7fcc162dbcbca@mail.gmail.com/
114c9491aadSKees Cook# and https://gcc.gnu.org/gcc-4.8/changes.html,
11539e7317eSArnaldo Carvalho de Melo# that takes into account Linus's comments (search for Wshadow) for the reasoning about
11639e7317eSArnaldo Carvalho de Melo# -Wshadow not being interesting before gcc 4.8.
11739e7317eSArnaldo Carvalho de Melo
1183337e682SArnaldo Carvalho de Meloifneq ($(filter 3.%,$(MAKE_VERSION)),)  # make-3
1193337e682SArnaldo Carvalho de MeloEXTRA_WARNINGS += -fno-strict-aliasing
12039e7317eSArnaldo Carvalho de MeloEXTRA_WARNINGS += -Wno-shadow
12139e7317eSArnaldo Carvalho de Meloelse
12239e7317eSArnaldo Carvalho de MeloEXTRA_WARNINGS += -Wshadow
1233337e682SArnaldo Carvalho de Meloendif
1243337e682SArnaldo Carvalho de Melo
12598d89bfdSBorislav Petkovifneq ($(findstring $(MAKEFLAGS), w),w)
12698d89bfdSBorislav PetkovPRINT_DIR = --no-print-directory
12798d89bfdSBorislav Petkovelse
12898d89bfdSBorislav PetkovNO_SUBDIR = :
12998d89bfdSBorislav Petkovendif
13098d89bfdSBorislav Petkov
1316f0fa58eSMasahiro Yamadaifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),)
132e572d088SJosh Poimboeuf  silent=1
133e572d088SJosh Poimboeufendif
134e572d088SJosh Poimboeuf
135ca9dfc6cSDavid Howells#
136ca9dfc6cSDavid Howells# Define a callable command for descending to a new directory
137ca9dfc6cSDavid Howells#
138ca9dfc6cSDavid Howells# Call by doing: $(call descend,directory[,target])
139ca9dfc6cSDavid Howells#
140ca9dfc6cSDavid Howellsdescend = \
141bf35182fSDavid Howells	+mkdir -p $(OUTPUT)$(1) && \
1422b73f65dSDavid Howells	$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
143ca9dfc6cSDavid Howells
144bf35182fSDavid HowellsQUIET_SUBDIR0  = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir
14598d89bfdSBorislav PetkovQUIET_SUBDIR1  =
14698d89bfdSBorislav Petkov
147e572d088SJosh Poimboeufifneq ($(silent),1)
1485125bc22SRobert Richter  ifneq ($(V),1)
14965fb0992SIngo Molnar	QUIET_CC       = @echo '  CC      '$@;
150bdebbacdSJiri Olsa	QUIET_CC_FPIC  = @echo '  CC FPIC '$@;
15147c09d6aSSong Liu	QUIET_CLANG    = @echo '  CLANG   '$@;
15265fb0992SIngo Molnar	QUIET_AR       = @echo '  AR      '$@;
15365fb0992SIngo Molnar	QUIET_LINK     = @echo '  LINK    '$@;
15465fb0992SIngo Molnar	QUIET_MKDIR    = @echo '  MKDIR   '$@;
15565fb0992SIngo Molnar	QUIET_GEN      = @echo '  GEN     '$@;
15698d89bfdSBorislav Petkov	QUIET_SUBDIR0  = +@subdir=
15765fb0992SIngo Molnar	QUIET_SUBDIR1  = ;$(NO_SUBDIR) \
15865fb0992SIngo Molnar			  echo '  SUBDIR  '$$subdir; \
15998d89bfdSBorislav Petkov			 $(MAKE) $(PRINT_DIR) -C $$subdir
16065fb0992SIngo Molnar	QUIET_FLEX     = @echo '  FLEX    '$@;
16165fb0992SIngo Molnar	QUIET_BISON    = @echo '  BISON   '$@;
162c6de37ddSKees Cook	QUIET_GENSKEL  = @echo '  GENSKEL '$@;
163bf35182fSDavid Howells
164bf35182fSDavid Howells	descend = \
16565fb0992SIngo Molnar		+@echo	       '  DESCEND '$(1); \
166bf35182fSDavid Howells		mkdir -p $(OUTPUT)$(1) && \
1672b73f65dSDavid Howells		$(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2)
168b7248defSJiri Olsa
169b7248defSJiri Olsa	QUIET_CLEAN    = @printf '  CLEAN   %s\n' $1;
170b7248defSJiri Olsa	QUIET_INSTALL  = @printf '  INSTALL %s\n' $1;
171d3244248SQuentin Monnet	QUIET_UNINST   = @printf '  UNINST  %s\n' $1;
17298d89bfdSBorislav Petkov  endif
17398d89bfdSBorislav Petkovendif
1749564a8cfSRasmus Villemoes
1759564a8cfSRasmus Villemoespound := \#
176