blob: 784f46d41959b635bb1581f8bbfeee8fa47d3024 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002# ==========================================================================
3# Building
4# ==========================================================================
5
6src := $(obj)
7
Paul Smith4f193362006-03-05 17:14:10 -05008PHONY := __build
Linus Torvalds1da177e2005-04-16 15:20:36 -07009__build:
10
Sam Ravnborgd72e5ed2007-05-28 22:47:48 +020011# Init all relevant variables used in kbuild files so
12# 1) they have correct type
13# 2) they do not inherit any value from the environment
14obj-y :=
15obj-m :=
16lib-y :=
17lib-m :=
Masahiro Yamada5f2fb522020-02-02 01:49:24 +090018always-y :=
19always-m :=
Sam Ravnborgd72e5ed2007-05-28 22:47:48 +020020targets :=
21subdir-y :=
22subdir-m :=
23EXTRA_AFLAGS :=
24EXTRA_CFLAGS :=
25EXTRA_CPPFLAGS :=
26EXTRA_LDFLAGS :=
Sam Ravnborgf77bf012007-10-15 22:25:06 +020027asflags-y :=
28ccflags-y :=
29cppflags-y :=
30ldflags-y :=
Sam Ravnborgd72e5ed2007-05-28 22:47:48 +020031
Sam Ravnborg720097d2009-04-19 11:04:26 +020032subdir-asflags-y :=
33subdir-ccflags-y :=
34
Robert P. J. Day3156fd02008-02-18 04:48:20 -050035# Read auto.conf if it exists, otherwise ignore
Roman Zippelc955cca2006-06-08 22:12:39 -070036-include include/config/auto.conf
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090038include $(srctree)/scripts/Kbuild.include
Masahiro Yamada57fd2512021-02-28 15:10:27 +090039include $(srctree)/scripts/Makefile.compiler
Sam Ravnborg20a468b2006-01-22 13:34:15 +010040
Sam Ravnborg2a691472005-07-25 20:26:04 +000041# The filename Kbuild has precedence over Makefile
Sam Ravnborgdb8c1a72005-07-27 22:11:01 +020042kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src))
Masahiro Yamada5c816642022-02-11 14:14:11 +090043include $(or $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Makefile)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090045include $(srctree)/scripts/Makefile.lib
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Masahiro Yamada85569d12020-07-29 12:15:37 +090047# Do not include hostprogs rules unless needed.
48# $(sort ...) is used here to remove duplicated words and excessive spaces.
49hostprogs := $(sort $(hostprogs))
Masahiro Yamada42640b132020-07-29 12:15:36 +090050ifneq ($(hostprogs),)
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090051include $(srctree)/scripts/Makefile.host
Linus Torvalds1da177e2005-04-16 15:20:36 -070052endif
53
Masahiro Yamada7f3a59d2020-04-29 12:45:14 +090054# Do not include userprogs rules unless needed.
Masahiro Yamada85569d12020-07-29 12:15:37 +090055# $(sort ...) is used here to remove duplicated words and excessive spaces.
Masahiro Yamada7f3a59d2020-04-29 12:45:14 +090056userprogs := $(sort $(userprogs))
57ifneq ($(userprogs),)
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090058include $(srctree)/scripts/Makefile.userprogs
Masahiro Yamada7f3a59d2020-04-29 12:45:14 +090059endif
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061ifndef obj
62$(warning kbuild: Makefile.build is included improperly)
63endif
64
Masahiro Yamada394053f2019-08-15 00:19:18 +090065ifeq ($(need-modorder),)
Masahiro Yamadac07d8d472019-08-08 20:21:11 +090066ifneq ($(obj-m),)
67$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
68$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
69endif
70endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072# ===========================================================================
73
Masahiro Yamada454753d2020-05-22 11:00:02 +090074# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +090075subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
Masahiro Yamadab2c88552020-06-01 14:57:00 +090076subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
Masahiro Yamada6ba3bcb2020-05-22 11:00:00 +090077
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090078targets-for-builtin := $(extra-y)
79
Masahiro Yamadaa4954fd2014-09-09 19:26:21 +090080ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090081targets-for-builtin += $(obj)/lib.a
Linus Torvalds1da177e2005-04-16 15:20:36 -070082endif
83
Masahiro Yamada56d58932019-12-19 01:04:28 +090084ifdef need-builtin
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090085targets-for-builtin += $(obj)/built-in.a
Linus Torvalds1da177e2005-04-16 15:20:36 -070086endif
87
Masahiro Yamadafc93a4c2022-04-07 00:30:22 +090088targets-for-modules := $(foreach x, o mod $(if $(CONFIG_TRIM_UNUSED_KSYMS), usyms), \
Masahiro Yamada9413e762022-04-07 00:30:20 +090089 $(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090090
Masahiro Yamadab480fec2020-05-22 10:59:59 +090091ifdef need-modorder
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090092targets-for-modules += $(obj)/modules.order
Masahiro Yamada1d8001e2019-02-14 12:05:15 +090093endif
Tejun Heo551559e2007-12-07 21:04:30 +090094
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090095targets += $(targets-for-builtin) $(targets-for-modules)
Masahiro Yamada9f69a492019-07-17 15:18:00 +090096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097# Linus' kernel sanity checking tool
Masahiro Yamada7d0ea252018-10-10 00:05:39 +090098ifeq ($(KBUILD_CHECKSRC),1)
99 quiet_cmd_checksrc = CHECK $<
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900100 cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
Masahiro Yamada7d0ea252018-10-10 00:05:39 +0900101else ifeq ($(KBUILD_CHECKSRC),2)
102 quiet_cmd_force_checksrc = CHECK $<
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900103 cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104endif
105
Masahiro Yamadae27128d2019-09-01 01:25:55 +0900106ifneq ($(KBUILD_EXTRA_WARN),)
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900107 cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $<
Matthew Wilcox3a025e12017-11-20 10:40:40 -0800108endif
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110# Compile C sources (.c)
111# ---------------------------------------------------------------------------
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
Sami Tolvanendc5723b2020-12-11 10:46:19 -0800114 cmd_cc_s_c = $(CC) $(filter-out $(DEBUG_CFLAGS) $(CC_FLAGS_LTO), $(c_flags)) -fverbose-asm -S -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Sam Ravnborg767e5812007-05-06 09:23:45 +0200116$(obj)/%.s: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 $(call if_changed_dep,cc_s_c)
118
Masahiro Yamada23d43842016-04-08 11:24:47 +0900119quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
120cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Sam Ravnborg767e5812007-05-06 09:23:45 +0200122$(obj)/%.i: $(src)/%.c FORCE
Masahiro Yamada23d43842016-04-08 11:24:47 +0900123 $(call if_changed_dep,cpp_i_c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900125genksyms = scripts/genksyms/genksyms \
126 $(if $(1), -T $(2)) \
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900127 $(if $(KBUILD_PRESERVE), -p) \
128 -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
129
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100130# These mirror gensymtypes_S and co below, keep them in synch.
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900131cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
Sam Ravnborg37a8d9f2008-12-19 21:38:09 +0100132
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200133quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
Masahiro Yamadadc6dc3e2022-04-05 20:33:50 +0900134 cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200135
Sam Ravnborg767e5812007-05-06 09:23:45 +0200136$(obj)/%.symtypes : $(src)/%.c FORCE
Andreas Gruenbacher64e6c1e2008-12-01 14:21:01 -0800137 $(call cmd,cc_symtypes_c)
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200138
Vinícius Tinti433db3e2017-04-24 13:04:58 -0700139# LLVM assembly
140# Generate .ll files from .c
141quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
142 cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -o $@ $<
143
144$(obj)/%.ll: $(src)/%.c FORCE
145 $(call if_changed_dep,cc_ll_c)
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147# C (.c) files
148# The C file is compiled and updated dependency information is generated.
149# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
150
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900151is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
152
153# When a module consists of a single object, there is no reason to keep LLVM IR.
154# Make $(LD) covert LLVM IR to ELF here.
155ifdef CONFIG_LTO_CLANG
156cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
157endif
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900160 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
161 $(cmd_ld_single_m) \
162 $(cmd_objtool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900164ifdef CONFIG_MODVERSIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165# When module versioning is enabled the following steps are executed:
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900166# o compile a <file>.o from <file>.c
167# o if <file>.o doesn't contain a __ksymtab version, i.e. does
168# not export symbols, it's done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169# o otherwise, we calculate symbol versions using the good old
Masahiro Yamada7b453712022-05-13 20:39:22 +0900170# genksyms on the preprocessed source and dump them into the .cmd file.
171# o modpost will extract versions from that file and create *.c files that will
172# be compiled and linked to the kernel and/or modules.
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100173
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900174gen_symversions = \
Sami Tolvanen38e89182020-12-11 10:46:20 -0800175 if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then \
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900176 $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
Masahiro Yamada78e9e562022-05-05 16:22:35 +0900177 >> $(dot-target).cmd; \
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900178 fi
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900179
180cmd_gen_symversions_c = $(call gen_symversions,c)
181
Sami Tolvanen38e89182020-12-11 10:46:20 -0800182endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Sami Tolvanen3b15cdc12020-12-11 10:46:18 -0800184ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
Vasily Gorbik07d04082018-08-06 15:17:44 +0200185# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
Steven Rostedt72441cb2010-10-13 17:12:30 -0400186ifdef BUILD_C_RECORDMCOUNT
Steven Rostedt85356f82011-04-12 18:59:10 -0400187ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
188 RECORDMCOUNT_FLAGS = -w
189endif
Steven Rostedtd7b4d6d2010-10-18 14:42:00 -0400190# Due to recursion, we must skip empty.o.
191# The empty.o file is created in the make process in order to determine
Cao jin312a3d02017-08-02 10:31:06 +0800192# the target endianness and word size. It is made before all other C
193# files, including recordmcount.
Wu Zhangjin45677452010-10-28 00:24:34 +0800194sub_cmd_record_mcount = \
195 if [ $(@) != "scripts/mod/empty.o" ]; then \
Steven Rostedt85356f82011-04-12 18:59:10 -0400196 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
Wu Zhangjin45677452010-10-28 00:24:34 +0800197 fi;
Michal Marekd6971822011-05-17 15:36:46 +0200198recordmcount_source := $(srctree)/scripts/recordmcount.c \
199 $(srctree)/scripts/recordmcount.h
Steven Rostedt72441cb2010-10-13 17:12:30 -0400200else
Masahiro Yamada4317ee32018-11-30 10:05:24 +0900201sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
Wu Zhangjine6299d22009-11-20 20:34:31 +0800202 "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
Steven Rostedtb3acf292008-10-29 15:30:26 -0400203 "$(if $(CONFIG_64BIT),64,32)" \
Joel Stanley5a4630a2018-09-17 17:07:54 +0930204 "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
Masahiro Yamadad503ac52018-08-24 08:20:39 +0900205 "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
Shaohua Li18c167f2009-01-12 10:00:51 +0800206 "$(if $(part-of-module),1,0)" "$(@)";
Michal Marekd6971822011-05-17 15:36:46 +0200207recordmcount_source := $(srctree)/scripts/recordmcount.pl
Cao jin312a3d02017-08-02 10:31:06 +0800208endif # BUILD_C_RECORDMCOUNT
Joe Lawrence1a49b2f2019-03-26 10:50:28 -0400209cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
210 $(sub_cmd_record_mcount))
Sami Tolvanen3b15cdc12020-12-11 10:46:18 -0800211endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
Steven Rostedt8da38212008-08-14 15:45:07 -0400212
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600213# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
214# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
215# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
Masahiro Yamada918a6b72021-08-31 16:39:55 +0900216
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900217is-standard-object = $(if $(filter-out y%, $(OBJECT_FILES_NON_STANDARD_$(basetarget).o)$(OBJECT_FILES_NON_STANDARD)n),y)
Masahiro Yamada918a6b72021-08-31 16:39:55 +0900218
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900219$(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600220
Masahiro Yamadabbda5ec2018-11-30 10:05:26 +0900221ifdef CONFIG_TRIM_UNUSED_KSYMS
222cmd_gen_ksymdeps = \
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900223 $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd
Masahiro Yamadabbda5ec2018-11-30 10:05:26 +0900224endif
225
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900226cmd_check_local_export = $(srctree)/scripts/check-local-export $@
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228define rule_cc_o_c
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900229 $(call cmd_and_fixdep,cc_o_c)
230 $(call cmd,gen_ksymdeps)
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900231 $(call cmd,check_local_export)
Luc Van Oostenryck0c33f122020-06-22 17:45:12 +0200232 $(call cmd,checksrc)
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900233 $(call cmd,checkdoc)
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900234 $(call cmd,gen_objtooldep)
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900235 $(call cmd,gen_symversions_c)
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900236 $(call cmd,record_mcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237endef
238
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600239define rule_as_o_S
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900240 $(call cmd_and_fixdep,as_o_S)
241 $(call cmd,gen_ksymdeps)
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900242 $(call cmd,check_local_export)
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900243 $(call cmd,gen_objtooldep)
Masahiro Yamada7cfa2fc2022-04-05 20:33:58 +0900244 $(call cmd,gen_symversions_S)
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600245endef
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247# Built-in and composite module parts
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900248$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 $(call if_changed_rule,cc_o_c)
Luc Van Oostenryck0c33f122020-06-22 17:45:12 +0200250 $(call cmd,force_checksrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Masahiro Yamadac6031b12022-05-27 19:01:53 +0900252# To make this rule robust against "Argument list too long" error,
253# ensure to add $(obj)/ prefix by a shell command.
Kevin Locke7bf179d2022-06-06 20:42:54 -0600254cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
255 $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
Masahiro Yamada9f69a492019-07-17 15:18:00 +0900256
Masahiro Yamadafc93a4c2022-04-07 00:30:22 +0900257$(obj)/%.mod: FORCE
Masahiro Yamada9f69a492019-07-17 15:18:00 +0900258 $(call if_changed,mod)
259
Masahiro Yamada9413e762022-04-07 00:30:20 +0900260# List module undefined symbols
261cmd_undefined_syms = $(NM) $< | sed -n 's/^ *U //p' > $@
262
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900263$(obj)/%.usyms: $(obj)/%.o FORCE
Masahiro Yamada9413e762022-04-07 00:30:20 +0900264 $(call if_changed,undefined_syms)
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266quiet_cmd_cc_lst_c = MKLST $@
267 cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
268 $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
269 System.map $(OBJDUMP) > $@
270
Sam Ravnborg767e5812007-05-06 09:23:45 +0200271$(obj)/%.lst: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 $(call if_changed_dep,cc_lst_c)
273
274# Compile assembler sources (.S)
275# ---------------------------------------------------------------------------
276
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100277# .S file exports must have their C prototypes defined in asm/asm-prototypes.h
278# or a file that it includes, in order to get versioned symbols. We build a
279# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
280# the .S file (with trailing ';'), and run genksyms on that, to extract vers.
281#
282# This is convoluted. The .S file must first be preprocessed to run guards and
283# expand names, then the resulting exports must be constructed into plain
284# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
285# to make the genksyms input.
286#
287# These mirror gensymtypes_c and co above, keep them in synch.
288cmd_gensymtypes_S = \
Masahiro Yamadab79c6aa2019-01-17 19:02:44 +0900289 { echo "\#include <linux/kernel.h>" ; \
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100290 echo "\#include <asm/asm-prototypes.h>" ; \
291 $(CPP) $(a_flags) $< | \
Nicholas Piggincc6acc12016-11-09 15:34:05 +1100292 grep "\<___EXPORT_SYMBOL\>" | \
Masahiro Yamadab79c6aa2019-01-17 19:02:44 +0900293 sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900294 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100295
296quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
Masahiro Yamadadc6dc3e2022-04-05 20:33:50 +0900297 cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100298
299$(obj)/%.symtypes : $(src)/%.S FORCE
300 $(call cmd,cc_symtypes_S)
301
302
Masahiro Yamadae0f41e52016-04-08 11:24:48 +0900303quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
304cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Sam Ravnborg767e5812007-05-06 09:23:45 +0200306$(obj)/%.s: $(src)/%.S FORCE
Masahiro Yamadae0f41e52016-04-08 11:24:48 +0900307 $(call if_changed_dep,cpp_s_S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309quiet_cmd_as_o_S = AS $(quiet_modtag) $@
Masahiro Yamada92594d52021-08-31 16:39:54 +0900310 cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100311
Masahiro Yamada2ff2b7e2019-08-19 14:54:20 +0900312ifdef CONFIG_ASM_MODVERSIONS
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100313
314# versioning matches the C process described above, with difference that
315# we parse asm-prototypes.h C header to get function definitions.
316
Masahiro Yamada7cfa2fc2022-04-05 20:33:58 +0900317cmd_gen_symversions_S = $(call gen_symversions,S)
318
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100319endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900321$(obj)/%.o: $(src)/%.S FORCE
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600322 $(call if_changed_rule,as_o_S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900324targets += $(filter-out $(subdir-builtin), $(real-obj-y))
325targets += $(filter-out $(subdir-modorder), $(real-obj-m))
Masahiro Yamadabcf0c662021-05-03 03:09:56 +0900326targets += $(real-dtb-y) $(lib-y) $(always-y) $(MAKECMDGOALS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328# Linker scripts preprocessor (.lds.S -> .lds)
329# ---------------------------------------------------------------------------
330quiet_cmd_cpp_lds_S = LDS $@
Linus Torvalds5cb05122017-11-02 14:10:37 -0700331 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
Tim Abbott42f29a22009-09-20 18:14:12 -0400332 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Sam Ravnborg767e5812007-05-06 09:23:45 +0200334$(obj)/%.lds: $(src)/%.lds.S FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 $(call if_changed_dep,cpp_lds_S)
336
David Howells4520c6a2012-09-21 23:31:13 +0100337# ASN.1 grammar
338# ---------------------------------------------------------------------------
Masahiro Yamada49d50892019-07-21 01:27:38 +0900339quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
David Howells4520c6a2012-09-21 23:31:13 +0100340 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
Masahiro Yamada49d50892019-07-21 01:27:38 +0900341 $(basename $@).c $(basename $@).h
David Howells4520c6a2012-09-21 23:31:13 +0100342
Masahiro Yamada4fa8bc92018-03-23 22:04:37 +0900343$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
David Howells4520c6a2012-09-21 23:31:13 +0100344 $(call cmd,asn1_compiler)
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346# Build the compiled-in targets
347# ---------------------------------------------------------------------------
348
349# To build objects in subdirs, we need to descend into the directories
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +0900350$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
Masahiro Yamada454753d2020-05-22 11:00:02 +0900351$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353#
Masahiro Yamadadee94952019-01-17 09:10:03 +0900354# Rule to compile a set of .o files into one .a file (without symbol table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355#
Masahiro Yamadacd968b92022-05-27 19:01:52 +0900356# To make this rule robust against "Argument list too long" error,
357# remove $(obj)/ prefix, and restore it by a shell command.
Stephen Rothwella5967db2016-08-24 22:29:19 +1000358
Masahiro Yamada5e18f022018-03-19 20:26:12 +0900359quiet_cmd_ar_builtin = AR $@
Masahiro Yamadacd968b92022-05-27 19:01:52 +0900360 cmd_ar_builtin = rm -f $@; \
Masahiro Yamada5439d4d2022-06-14 14:51:49 +0900361 $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
362 $(AR) cDPrST $@
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Masahiro Yamadae578edc2020-05-22 10:59:58 +0900364$(obj)/built-in.a: $(real-obj-y) FORCE
Masahiro Yamada7375cbc2022-05-13 20:39:23 +0900365 $(call if_changed,ar_builtin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#
Tejun Heo551559e2007-12-07 21:04:30 +0900368# Rule to create modules.order file
369#
370# Create commands to either record .ko file or cat modules.order from
371# a subdirectory
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900372# Add $(obj-m) as the prerequisite to avoid updating the timestamp of
373# modules.order unless contained modules are updated.
374
375cmd_modules_order = { $(foreach m, $(real-prereqs), \
376 $(if $(filter %/modules.order, $m), cat $m, echo $(patsubst %.o,%.ko,$m));) :; } \
Masahiro Yamadae0e1b1e2019-07-17 15:17:51 +0900377 | $(AWK) '!x[$$0]++' - > $@
Tejun Heo551559e2007-12-07 21:04:30 +0900378
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900379$(obj)/modules.order: $(obj-m) FORCE
380 $(call if_changed,modules_order)
381
Tejun Heo551559e2007-12-07 21:04:30 +0900382#
Masahiro Yamadadee94952019-01-17 09:10:03 +0900383# Rule to compile a set of .o files into one .a file (with symbol table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#
Sami Tolvanen38e89182020-12-11 10:46:20 -0800385
Masahiro Yamadae578edc2020-05-22 10:59:58 +0900386$(obj)/lib.a: $(lib-y) FORCE
Masahiro Yamada7375cbc2022-05-13 20:39:23 +0900387 $(call if_changed,ar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900389quiet_cmd_ld_multi_m = LD [M] $@
390 cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$(patsubst %.o,%.mod,$@) $(cmd_objtool)
391
392define rule_ld_multi_m
393 $(call cmd_and_savecmd,ld_multi_m)
394 $(call cmd,gen_objtooldep)
395endef
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900397$(multi-obj-m): objtool-enabled := $(delay-objtool)
398$(multi-obj-m): part-of-module := y
Masahiro Yamadafeb7d792022-04-07 00:30:23 +0900399$(multi-obj-m): %.o: %.mod FORCE
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900400 $(call if_changed_rule,ld_multi_m)
Masahiro Yamadaa6601e02021-03-06 15:11:20 +0900401$(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Masahiro Yamada591f6682017-11-13 19:29:36 +0900403targets := $(filter-out $(PHONY), $(targets))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900405# Add intermediate targets:
406# When building objects with specific suffix patterns, add intermediate
407# targets that the final targets are derived from.
408intermediate_targets = $(foreach sfx, $(2), \
409 $(patsubst %$(strip $(1)),%$(sfx), \
410 $(filter %$(strip $(1)), $(targets))))
Masahiro Yamada54a702f2018-03-23 22:04:39 +0900411# %.asn1.o <- %.asn1.[ch] <- %.asn1
Masahiro Yamadaa7f92412018-03-23 22:04:34 +0900412# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900413# %.lex.o <- %.lex.c <- %.l
414# %.tab.o <- %.tab.[ch] <- %.y
Masahiro Yamada54a702f2018-03-23 22:04:39 +0900415targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
416 $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
Masahiro Yamadaa7f92412018-03-23 22:04:34 +0900417 $(call intermediate_targets, .lex.o, .lex.c) \
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900418 $(call intermediate_targets, .tab.o, .tab.c .tab.h)
419
Masahiro Yamada394053f2019-08-15 00:19:18 +0900420# Build
421# ---------------------------------------------------------------------------
422
423ifdef single-build
424
Masahiro Yamada20312622019-10-16 14:15:46 +0900425KBUILD_SINGLE_TARGETS := $(filter $(obj)/%, $(KBUILD_SINGLE_TARGETS))
426
Masahiro Yamada394053f2019-08-15 00:19:18 +0900427curdir-single := $(sort $(foreach x, $(KBUILD_SINGLE_TARGETS), \
428 $(if $(filter $(x) $(basename $(x)).o, $(targets)), $(x))))
429
430# Handle single targets without any rule: show "Nothing to be done for ..." or
431# "No rule to make target ..." depending on whether the target exists.
432unknown-single := $(filter-out $(addsuffix /%, $(subdir-ym)), \
Masahiro Yamada20312622019-10-16 14:15:46 +0900433 $(filter-out $(curdir-single), $(KBUILD_SINGLE_TARGETS)))
Masahiro Yamada394053f2019-08-15 00:19:18 +0900434
Masahiro Yamada2dffd232019-10-16 14:15:47 +0900435single-subdirs := $(foreach d, $(subdir-ym), \
436 $(if $(filter $(d)/%, $(KBUILD_SINGLE_TARGETS)), $(d)))
437
438__build: $(curdir-single) $(single-subdirs)
Masahiro Yamada394053f2019-08-15 00:19:18 +0900439ifneq ($(unknown-single),)
440 $(Q)$(MAKE) -f /dev/null $(unknown-single)
441endif
442 @:
443
444ifeq ($(curdir-single),)
445# Nothing to do in this directory. Do not include any .*.cmd file for speed-up
446targets :=
447else
448targets += $(curdir-single)
449endif
450
451else
452
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +0900453__build: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
454 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900455 $(subdir-ym) $(always-y)
Masahiro Yamada394053f2019-08-15 00:19:18 +0900456 @:
457
458endif
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460# Descending
461# ---------------------------------------------------------------------------
462
Paul Smith4f193362006-03-05 17:14:10 -0500463PHONY += $(subdir-ym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464$(subdir-ym):
Masahiro Yamadad9f78ed2019-08-06 19:03:21 +0900465 $(Q)$(MAKE) $(build)=$@ \
Masahiro Yamada394053f2019-08-15 00:19:18 +0900466 $(if $(filter $@/, $(KBUILD_SINGLE_TARGETS)),single-build=) \
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +0900467 need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
Masahiro Yamada454753d2020-05-22 11:00:02 +0900468 need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470# Add FORCE to the prequisites of a target to force it to be always rebuilt.
471# ---------------------------------------------------------------------------
472
Paul Smith4f193362006-03-05 17:14:10 -0500473PHONY += FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475FORCE:
476
477# Read all saved command lines and dependencies for the $(targets) we
478# may be building above, using $(if_changed{,_dep}). As an
479# optimization, we don't need to read them if the target does not
480# exist, we will rebuild anyway in that case.
481
Masahiro Yamada392885e2018-11-30 10:05:22 +0900482existing-targets := $(wildcard $(sort $(targets)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Masahiro Yamada392885e2018-11-30 10:05:22 +0900484-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
Paul Smith4f193362006-03-05 17:14:10 -0500485
Masahiro Yamada8a787562017-11-13 19:29:37 +0900486# Create directories for object files if they do not exist
Masahiro Yamadacc8a51c2020-07-01 12:29:51 +0900487obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
Masahiro Yamada392885e2018-11-30 10:05:22 +0900488# If targets exist, their directories apparently exist. Skip mkdir.
489existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
490obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
Masahiro Yamadac4da7ed2017-11-13 19:29:38 +0900491ifneq ($(obj-dirs),)
Masahiro Yamada8a787562017-11-13 19:29:37 +0900492$(shell mkdir -p $(obj-dirs))
493endif
494
Paul Smith4f193362006-03-05 17:14:10 -0500495.PHONY: $(PHONY)