Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | # SPDX-License-Identifier: GPL-2.0 |
Borislav Petkov | 9e4a664 | 2013-02-20 16:32:29 +0100 | [diff] [blame] | 2 | ifneq ($(O),) |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 3 | ifeq ($(origin O), command line) |
Masami Hiramatsu | be40920 | 2020-03-07 03:32:58 +0900 | [diff] [blame] | 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) |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 6 | OUTPUT := $(ABSOLUTE_O)/$(if $(subdir),$(subdir)/) |
Steven Rostedt | c883122 | 2012-08-13 10:23:02 -0400 | [diff] [blame] | 7 | COMMAND_O := O=$(ABSOLUTE_O) |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 8 | ifeq ($(objtree),) |
| 9 | objtree := $(O) |
| 10 | endif |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 11 | endif |
Borislav Petkov | 9e4a664 | 2013-02-20 16:32:29 +0100 | [diff] [blame] | 12 | endif |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 13 | |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 14 | # check that the output directory actually exists |
Borislav Petkov | 9e4a664 | 2013-02-20 16:32:29 +0100 | [diff] [blame] | 15 | ifneq ($(OUTPUT),) |
Bjørn Forsman | 16f8259 | 2017-11-05 10:44:16 +0100 | [diff] [blame] | 16 | OUTDIR := $(shell cd $(OUTPUT) && pwd) |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 17 | $(if $(OUTDIR),, $(error output directory "$(OUTPUT)" does not exist)) |
| 18 | endif |
| 19 | |
| 20 | # |
| 21 | # Include saner warnings here, which can catch bugs: |
| 22 | # |
Borislav Petkov | d8caf3e | 2012-04-11 18:36:15 +0200 | [diff] [blame] | 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 |
Borislav Petkov | d8caf3e | 2012-04-11 18:36:15 +0200 | [diff] [blame] | 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 |
Yury Norov | d1d1a2cd | 2021-05-06 18:02:42 -0700 | [diff] [blame] | 41 | EXTRA_WARNINGS += -Wno-type-limits |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 42 | |
Martin Kelly | 7ed1c19 | 2018-02-21 14:45:12 -0800 | [diff] [blame] | 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 | |
Yonghong Song | f62700c | 2021-04-13 08:34:19 -0700 | [diff] [blame] | 54 | ifneq ($(LLVM),) |
| 55 | $(call allow-override,CC,clang) |
| 56 | $(call allow-override,AR,llvm-ar) |
| 57 | $(call allow-override,LD,ld.lld) |
| 58 | $(call allow-override,CXX,clang++) |
| 59 | $(call allow-override,STRIP,llvm-strip) |
| 60 | else |
Martin Kelly | 7ed1c19 | 2018-02-21 14:45:12 -0800 | [diff] [blame] | 61 | # Allow setting various cross-compile vars or setting CROSS_COMPILE as a prefix. |
| 62 | $(call allow-override,CC,$(CROSS_COMPILE)gcc) |
| 63 | $(call allow-override,AR,$(CROSS_COMPILE)ar) |
| 64 | $(call allow-override,LD,$(CROSS_COMPILE)ld) |
| 65 | $(call allow-override,CXX,$(CROSS_COMPILE)g++) |
| 66 | $(call allow-override,STRIP,$(CROSS_COMPILE)strip) |
Yonghong Song | f62700c | 2021-04-13 08:34:19 -0700 | [diff] [blame] | 67 | endif |
| 68 | |
| 69 | CC_NO_CLANG := $(shell $(CC) -dM -E -x c /dev/null | grep -Fq "__clang__"; echo $$?) |
Martin Kelly | 7ed1c19 | 2018-02-21 14:45:12 -0800 | [diff] [blame] | 70 | |
Jean-Philippe Brucker | c8a950d | 2020-11-10 17:43:05 +0100 | [diff] [blame] | 71 | ifneq ($(LLVM),) |
| 72 | HOSTAR ?= llvm-ar |
| 73 | HOSTCC ?= clang |
| 74 | HOSTLD ?= ld.lld |
| 75 | else |
| 76 | HOSTAR ?= ar |
| 77 | HOSTCC ?= gcc |
| 78 | HOSTLD ?= ld |
| 79 | endif |
| 80 | |
Sedat Dilek | 211a741 | 2021-01-28 02:50:58 +0100 | [diff] [blame] | 81 | # Some tools require Clang, LLC and/or LLVM utils |
| 82 | CLANG ?= clang |
| 83 | LLC ?= llc |
| 84 | LLVM_CONFIG ?= llvm-config |
| 85 | LLVM_OBJCOPY ?= llvm-objcopy |
| 86 | LLVM_STRIP ?= llvm-strip |
| 87 | |
David Carrillo-Cisneros | 3866058 | 2017-08-27 00:54:40 -0700 | [diff] [blame] | 88 | ifeq ($(CC_NO_CLANG), 1) |
Arnaldo Carvalho de Melo | 093b75e | 2017-02-14 10:34:35 -0300 | [diff] [blame] | 89 | EXTRA_WARNINGS += -Wstrict-aliasing=3 |
Jean-Philippe Brucker | cebdb73 | 2021-12-16 16:38:38 +0000 | [diff] [blame] | 90 | |
| 91 | else ifneq ($(CROSS_COMPILE),) |
| 92 | CLANG_CROSS_FLAGS := --target=$(notdir $(CROSS_COMPILE:%-=%)) |
| 93 | GCC_TOOLCHAIN_DIR := $(dir $(shell which $(CROSS_COMPILE)gcc)) |
| 94 | ifneq ($(GCC_TOOLCHAIN_DIR),) |
| 95 | CLANG_CROSS_FLAGS += --prefix=$(GCC_TOOLCHAIN_DIR)$(notdir $(CROSS_COMPILE)) |
| 96 | CLANG_CROSS_FLAGS += --sysroot=$(shell $(CROSS_COMPILE)gcc -print-sysroot) |
| 97 | CLANG_CROSS_FLAGS += --gcc-toolchain=$(realpath $(GCC_TOOLCHAIN_DIR)/..) |
| 98 | endif # GCC_TOOLCHAIN_DIR |
| 99 | CFLAGS += $(CLANG_CROSS_FLAGS) |
| 100 | AFLAGS += $(CLANG_CROSS_FLAGS) |
| 101 | endif # CROSS_COMPILE |
Arnaldo Carvalho de Melo | 093b75e | 2017-02-14 10:34:35 -0300 | [diff] [blame] | 102 | |
Arnaldo Carvalho de Melo | 3337e68 | 2017-02-22 16:54:53 -0300 | [diff] [blame] | 103 | # Hack to avoid type-punned warnings on old systems such as RHEL5: |
| 104 | # We should be changing CFLAGS and checking gcc version, but this |
| 105 | # will do for now and keep the above -Wstrict-aliasing=3 in place |
| 106 | # in newer systems. |
| 107 | # Needed for the __raw_cmpxchg in tools/arch/x86/include/asm/cmpxchg.h |
Arnaldo Carvalho de Melo | 39e7317 | 2019-07-19 15:34:30 -0300 | [diff] [blame] | 108 | # |
Kees Cook | c9491aa | 2021-02-10 15:40:05 -0800 | [diff] [blame] | 109 | # See https://lore.kernel.org/lkml/9a8748490611281710g78402fbeh8ff7fcc162dbcbca@mail.gmail.com/ |
| 110 | # and https://gcc.gnu.org/gcc-4.8/changes.html, |
Arnaldo Carvalho de Melo | 39e7317 | 2019-07-19 15:34:30 -0300 | [diff] [blame] | 111 | # that takes into account Linus's comments (search for Wshadow) for the reasoning about |
| 112 | # -Wshadow not being interesting before gcc 4.8. |
| 113 | |
Arnaldo Carvalho de Melo | 3337e68 | 2017-02-22 16:54:53 -0300 | [diff] [blame] | 114 | ifneq ($(filter 3.%,$(MAKE_VERSION)),) # make-3 |
| 115 | EXTRA_WARNINGS += -fno-strict-aliasing |
Arnaldo Carvalho de Melo | 39e7317 | 2019-07-19 15:34:30 -0300 | [diff] [blame] | 116 | EXTRA_WARNINGS += -Wno-shadow |
| 117 | else |
| 118 | EXTRA_WARNINGS += -Wshadow |
Arnaldo Carvalho de Melo | 3337e68 | 2017-02-22 16:54:53 -0300 | [diff] [blame] | 119 | endif |
| 120 | |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 121 | ifneq ($(findstring $(MAKEFLAGS), w),w) |
| 122 | PRINT_DIR = --no-print-directory |
| 123 | else |
| 124 | NO_SUBDIR = : |
| 125 | endif |
| 126 | |
Masahiro Yamada | 6f0fa58 | 2017-05-19 20:42:30 +0900 | [diff] [blame] | 127 | ifneq ($(findstring s,$(filter-out --%,$(MAKEFLAGS))),) |
Josh Poimboeuf | e572d08 | 2017-01-18 22:16:55 -0600 | [diff] [blame] | 128 | silent=1 |
| 129 | endif |
Josh Poimboeuf | e572d08 | 2017-01-18 22:16:55 -0600 | [diff] [blame] | 130 | |
David Howells | ca9dfc6 | 2012-11-05 15:15:24 +0000 | [diff] [blame] | 131 | # |
| 132 | # Define a callable command for descending to a new directory |
| 133 | # |
| 134 | # Call by doing: $(call descend,directory[,target]) |
| 135 | # |
| 136 | descend = \ |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 137 | +mkdir -p $(OUTPUT)$(1) && \ |
David Howells | 2b73f65 | 2012-11-13 14:14:38 -0300 | [diff] [blame] | 138 | $(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2) |
David Howells | ca9dfc6 | 2012-11-05 15:15:24 +0000 | [diff] [blame] | 139 | |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 140 | QUIET_SUBDIR0 = +$(MAKE) $(COMMAND_O) -C # space to separate -C and subdir |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 141 | QUIET_SUBDIR1 = |
| 142 | |
Josh Poimboeuf | e572d08 | 2017-01-18 22:16:55 -0600 | [diff] [blame] | 143 | ifneq ($(silent),1) |
Ingo Molnar | 65fb099 | 2013-10-09 11:49:27 +0200 | [diff] [blame] | 144 | ifneq ($(V),1) |
Kees Cook | c6de37d | 2021-04-21 11:58:48 -0700 | [diff] [blame] | 145 | QUIET_CC = @echo ' CC '$@; |
| 146 | QUIET_CC_FPIC = @echo ' CC FPIC '$@; |
| 147 | QUIET_CLANG = @echo ' CLANG '$@; |
| 148 | QUIET_AR = @echo ' AR '$@; |
| 149 | QUIET_LINK = @echo ' LINK '$@; |
| 150 | QUIET_MKDIR = @echo ' MKDIR '$@; |
| 151 | QUIET_GEN = @echo ' GEN '$@; |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 152 | QUIET_SUBDIR0 = +@subdir= |
Ingo Molnar | 65fb099 | 2013-10-09 11:49:27 +0200 | [diff] [blame] | 153 | QUIET_SUBDIR1 = ;$(NO_SUBDIR) \ |
Kees Cook | c6de37d | 2021-04-21 11:58:48 -0700 | [diff] [blame] | 154 | echo ' SUBDIR '$$subdir; \ |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 155 | $(MAKE) $(PRINT_DIR) -C $$subdir |
Kees Cook | c6de37d | 2021-04-21 11:58:48 -0700 | [diff] [blame] | 156 | QUIET_FLEX = @echo ' FLEX '$@; |
| 157 | QUIET_BISON = @echo ' BISON '$@; |
| 158 | QUIET_GENSKEL = @echo ' GENSKEL '$@; |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 159 | |
| 160 | descend = \ |
Kees Cook | c6de37d | 2021-04-21 11:58:48 -0700 | [diff] [blame] | 161 | +@echo ' DESCEND '$(1); \ |
David Howells | bf35182 | 2012-11-05 21:02:08 +0000 | [diff] [blame] | 162 | mkdir -p $(OUTPUT)$(1) && \ |
David Howells | 2b73f65 | 2012-11-13 14:14:38 -0300 | [diff] [blame] | 163 | $(MAKE) $(COMMAND_O) subdir=$(if $(subdir),$(subdir)/$(1),$(1)) $(PRINT_DIR) -C $(1) $(2) |
Jiri Olsa | b7248de | 2013-12-19 14:42:00 +0100 | [diff] [blame] | 164 | |
Kees Cook | c6de37d | 2021-04-21 11:58:48 -0700 | [diff] [blame] | 165 | QUIET_CLEAN = @printf ' CLEAN %s\n' $1; |
| 166 | QUIET_INSTALL = @printf ' INSTALL %s\n' $1; |
| 167 | QUIET_UNINST = @printf ' UNINST %s\n' $1; |
Ingo Molnar | 65fb099 | 2013-10-09 11:49:27 +0200 | [diff] [blame] | 168 | endif |
Borislav Petkov | 98d89bf | 2012-04-11 18:36:14 +0200 | [diff] [blame] | 169 | endif |
Rasmus Villemoes | 9564a8c | 2018-04-08 23:35:28 +0200 | [diff] [blame] | 170 | |
| 171 | pound := \# |