blob: 76323201232a5402b8918fb4cbc27de20701a766 [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
Masahiro Yamadacc306ab2022-09-06 15:13:06 +09008PHONY := $(obj)/
9$(obj)/:
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
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 :=
Miguel Ojeda2f7ab122021-07-03 16:42:57 +020029rustflags-y :=
Sam Ravnborgf77bf012007-10-15 22:25:06 +020030cppflags-y :=
31ldflags-y :=
Sam Ravnborgd72e5ed2007-05-28 22:47:48 +020032
Sam Ravnborg720097d2009-04-19 11:04:26 +020033subdir-asflags-y :=
34subdir-ccflags-y :=
35
Robert P. J. Day3156fd02008-02-18 04:48:20 -050036# Read auto.conf if it exists, otherwise ignore
Roman Zippelc955cca2006-06-08 22:12:39 -070037-include include/config/auto.conf
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090039include $(srctree)/scripts/Kbuild.include
Masahiro Yamada57fd2512021-02-28 15:10:27 +090040include $(srctree)/scripts/Makefile.compiler
Masahiro Yamadaa2430b22022-11-19 04:15:50 +090041include $(kbuild-file)
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090042include $(srctree)/scripts/Makefile.lib
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Masahiro Yamada85569d12020-07-29 12:15:37 +090044# Do not include hostprogs rules unless needed.
45# $(sort ...) is used here to remove duplicated words and excessive spaces.
46hostprogs := $(sort $(hostprogs))
Masahiro Yamada42640b132020-07-29 12:15:36 +090047ifneq ($(hostprogs),)
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090048include $(srctree)/scripts/Makefile.host
Linus Torvalds1da177e2005-04-16 15:20:36 -070049endif
50
Masahiro Yamada7f3a59d2020-04-29 12:45:14 +090051# Do not include userprogs rules unless needed.
Masahiro Yamada85569d12020-07-29 12:15:37 +090052# $(sort ...) is used here to remove duplicated words and excessive spaces.
Masahiro Yamada7f3a59d2020-04-29 12:45:14 +090053userprogs := $(sort $(userprogs))
54ifneq ($(userprogs),)
Masahiro Yamada3204a7f2021-02-28 15:10:26 +090055include $(srctree)/scripts/Makefile.userprogs
Masahiro Yamada7f3a59d2020-04-29 12:45:14 +090056endif
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058ifndef obj
59$(warning kbuild: Makefile.build is included improperly)
60endif
61
Masahiro Yamada394053f2019-08-15 00:19:18 +090062ifeq ($(need-modorder),)
Masahiro Yamadac07d8d472019-08-08 20:21:11 +090063ifneq ($(obj-m),)
64$(warning $(patsubst %.o,'%.ko',$(obj-m)) will not be built even though obj-m is specified.)
65$(warning You cannot use subdir-y/m to visit a module Makefile. Use obj-y/m instead.)
66endif
67endif
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069# ===========================================================================
70
Masahiro Yamada454753d2020-05-22 11:00:02 +090071# subdir-builtin and subdir-modorder may contain duplications. Use $(sort ...)
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +090072subdir-builtin := $(sort $(filter %/built-in.a, $(real-obj-y)))
Masahiro Yamadab2c88552020-06-01 14:57:00 +090073subdir-modorder := $(sort $(filter %/modules.order, $(obj-m)))
Masahiro Yamada6ba3bcb2020-05-22 11:00:00 +090074
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090075targets-for-builtin := $(extra-y)
76
Masahiro Yamadaa4954fd2014-09-09 19:26:21 +090077ifneq ($(strip $(lib-y) $(lib-m) $(lib-)),)
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090078targets-for-builtin += $(obj)/lib.a
Linus Torvalds1da177e2005-04-16 15:20:36 -070079endif
80
Masahiro Yamada56d58932019-12-19 01:04:28 +090081ifdef need-builtin
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090082targets-for-builtin += $(obj)/built-in.a
Linus Torvalds1da177e2005-04-16 15:20:36 -070083endif
84
Masahiro Yamadafc93a4c2022-04-07 00:30:22 +090085targets-for-modules := $(foreach x, o mod $(if $(CONFIG_TRIM_UNUSED_KSYMS), usyms), \
Masahiro Yamada9413e762022-04-07 00:30:20 +090086 $(patsubst %.o, %.$x, $(filter %.o, $(obj-m))))
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090087
Masahiro Yamadab480fec2020-05-22 10:59:59 +090088ifdef need-modorder
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090089targets-for-modules += $(obj)/modules.order
Masahiro Yamada1d8001e2019-02-14 12:05:15 +090090endif
Tejun Heo551559e2007-12-07 21:04:30 +090091
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +090092targets += $(targets-for-builtin) $(targets-for-modules)
Masahiro Yamada9f69a492019-07-17 15:18:00 +090093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094# Linus' kernel sanity checking tool
Masahiro Yamada7d0ea252018-10-10 00:05:39 +090095ifeq ($(KBUILD_CHECKSRC),1)
96 quiet_cmd_checksrc = CHECK $<
Masahiro Yamadae5d28912018-11-30 10:05:28 +090097 cmd_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
Masahiro Yamada7d0ea252018-10-10 00:05:39 +090098else ifeq ($(KBUILD_CHECKSRC),2)
99 quiet_cmd_force_checksrc = CHECK $<
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900100 cmd_force_checksrc = $(CHECK) $(CHECKFLAGS) $(c_flags) $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101endif
102
Masahiro Yamadae27128d2019-09-01 01:25:55 +0900103ifneq ($(KBUILD_EXTRA_WARN),)
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900104 cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $<
Matthew Wilcox3a025e12017-11-20 10:40:40 -0800105endif
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107# Compile C sources (.c)
108# ---------------------------------------------------------------------------
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
Sami Tolvanendc5723b2020-12-11 10:46:19 -0800111 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 -0700112
Sam Ravnborg767e5812007-05-06 09:23:45 +0200113$(obj)/%.s: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 $(call if_changed_dep,cc_s_c)
115
Masahiro Yamada23d43842016-04-08 11:24:47 +0900116quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
117cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Sam Ravnborg767e5812007-05-06 09:23:45 +0200119$(obj)/%.i: $(src)/%.c FORCE
Masahiro Yamada23d43842016-04-08 11:24:47 +0900120 $(call if_changed_dep,cpp_i_c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900122genksyms = scripts/genksyms/genksyms \
123 $(if $(1), -T $(2)) \
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900124 $(if $(KBUILD_PRESERVE), -p) \
125 -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
126
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100127# These mirror gensymtypes_S and co below, keep them in synch.
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900128cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
Sam Ravnborg37a8d9f2008-12-19 21:38:09 +0100129
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200130quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
Masahiro Yamadadc6dc3e2022-04-05 20:33:50 +0900131 cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200132
Sam Ravnborg767e5812007-05-06 09:23:45 +0200133$(obj)/%.symtypes : $(src)/%.c FORCE
Andreas Gruenbacher64e6c1e2008-12-01 14:21:01 -0800134 $(call cmd,cc_symtypes_c)
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200135
Vinícius Tinti433db3e2017-04-24 13:04:58 -0700136# LLVM assembly
137# Generate .ll files from .c
138quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
Nick Desaulniersc67a85b2022-10-14 09:53:02 -0700139 cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $<
Vinícius Tinti433db3e2017-04-24 13:04:58 -0700140
141$(obj)/%.ll: $(src)/%.c FORCE
142 $(call if_changed_dep,cc_ll_c)
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144# C (.c) files
145# The C file is compiled and updated dependency information is generated.
146# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
147
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900148is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
149
150# When a module consists of a single object, there is no reason to keep LLVM IR.
151# Make $(LD) covert LLVM IR to ELF here.
152ifdef CONFIG_LTO_CLANG
153cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
154endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900157 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
158 $(cmd_ld_single_m) \
159 $(cmd_objtool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900161ifdef CONFIG_MODVERSIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162# When module versioning is enabled the following steps are executed:
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900163# o compile a <file>.o from <file>.c
164# o if <file>.o doesn't contain a __ksymtab version, i.e. does
165# not export symbols, it's done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166# o otherwise, we calculate symbol versions using the good old
Masahiro Yamada7b453712022-05-13 20:39:22 +0900167# genksyms on the preprocessed source and dump them into the .cmd file.
168# o modpost will extract versions from that file and create *.c files that will
169# be compiled and linked to the kernel and/or modules.
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100170
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900171gen_symversions = \
Sami Tolvanen38e89182020-12-11 10:46:20 -0800172 if $(NM) $@ 2>/dev/null | grep -q __ksymtab; then \
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900173 $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
Masahiro Yamada78e9e562022-05-05 16:22:35 +0900174 >> $(dot-target).cmd; \
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900175 fi
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900176
177cmd_gen_symversions_c = $(call gen_symversions,c)
178
Sami Tolvanen38e89182020-12-11 10:46:20 -0800179endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Sami Tolvanen3b15cdc12020-12-11 10:46:18 -0800181ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
Vasily Gorbik07d04082018-08-06 15:17:44 +0200182# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
Steven Rostedt72441cb2010-10-13 17:12:30 -0400183ifdef BUILD_C_RECORDMCOUNT
Steven Rostedt85356f82011-04-12 18:59:10 -0400184ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
185 RECORDMCOUNT_FLAGS = -w
186endif
Steven Rostedtd7b4d6d2010-10-18 14:42:00 -0400187# Due to recursion, we must skip empty.o.
188# The empty.o file is created in the make process in order to determine
Cao jin312a3d02017-08-02 10:31:06 +0800189# the target endianness and word size. It is made before all other C
190# files, including recordmcount.
Wu Zhangjin45677452010-10-28 00:24:34 +0800191sub_cmd_record_mcount = \
192 if [ $(@) != "scripts/mod/empty.o" ]; then \
Steven Rostedt85356f82011-04-12 18:59:10 -0400193 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
Wu Zhangjin45677452010-10-28 00:24:34 +0800194 fi;
Michal Marekd6971822011-05-17 15:36:46 +0200195recordmcount_source := $(srctree)/scripts/recordmcount.c \
196 $(srctree)/scripts/recordmcount.h
Steven Rostedt72441cb2010-10-13 17:12:30 -0400197else
Masahiro Yamada4317ee32018-11-30 10:05:24 +0900198sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
Wu Zhangjine6299d22009-11-20 20:34:31 +0800199 "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
Steven Rostedtb3acf292008-10-29 15:30:26 -0400200 "$(if $(CONFIG_64BIT),64,32)" \
Joel Stanley5a4630a2018-09-17 17:07:54 +0930201 "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
Masahiro Yamadad503ac52018-08-24 08:20:39 +0900202 "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
Shaohua Li18c167f2009-01-12 10:00:51 +0800203 "$(if $(part-of-module),1,0)" "$(@)";
Michal Marekd6971822011-05-17 15:36:46 +0200204recordmcount_source := $(srctree)/scripts/recordmcount.pl
Cao jin312a3d02017-08-02 10:31:06 +0800205endif # BUILD_C_RECORDMCOUNT
Joe Lawrence1a49b2f2019-03-26 10:50:28 -0400206cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
207 $(sub_cmd_record_mcount))
Sami Tolvanen3b15cdc12020-12-11 10:46:18 -0800208endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
Steven Rostedt8da38212008-08-14 15:45:07 -0400209
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600210# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
211# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
212# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
Masahiro Yamada918a6b72021-08-31 16:39:55 +0900213
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900214is-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 +0900215
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900216$(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600217
Masahiro Yamadabbda5ec2018-11-30 10:05:26 +0900218ifdef CONFIG_TRIM_UNUSED_KSYMS
219cmd_gen_ksymdeps = \
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900220 $(CONFIG_SHELL) $(srctree)/scripts/gen_ksymdeps.sh $@ >> $(dot-target).cmd
Masahiro Yamadabbda5ec2018-11-30 10:05:26 +0900221endif
222
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900223cmd_check_local_export = $(srctree)/scripts/check-local-export $@
224
Masahiro Yamada598afa02022-11-19 04:15:51 +0900225ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
226cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
227endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229define rule_cc_o_c
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900230 $(call cmd_and_fixdep,cc_o_c)
231 $(call cmd,gen_ksymdeps)
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900232 $(call cmd,check_local_export)
Luc Van Oostenryck0c33f122020-06-22 17:45:12 +0200233 $(call cmd,checksrc)
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900234 $(call cmd,checkdoc)
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900235 $(call cmd,gen_objtooldep)
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900236 $(call cmd,gen_symversions_c)
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900237 $(call cmd,record_mcount)
Masahiro Yamada598afa02022-11-19 04:15:51 +0900238 $(call cmd,warn_shared_object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239endef
240
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600241define rule_as_o_S
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900242 $(call cmd_and_fixdep,as_o_S)
243 $(call cmd,gen_ksymdeps)
Masahiro Yamada31cb50b2022-05-27 19:01:51 +0900244 $(call cmd,check_local_export)
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900245 $(call cmd,gen_objtooldep)
Masahiro Yamada7cfa2fc2022-04-05 20:33:58 +0900246 $(call cmd,gen_symversions_S)
Masahiro Yamada598afa02022-11-19 04:15:51 +0900247 $(call cmd,warn_shared_object)
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600248endef
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250# Built-in and composite module parts
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900251$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 $(call if_changed_rule,cc_o_c)
Luc Van Oostenryck0c33f122020-06-22 17:45:12 +0200253 $(call cmd,force_checksrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Masahiro Yamadac6031b12022-05-27 19:01:53 +0900255# To make this rule robust against "Argument list too long" error,
256# ensure to add $(obj)/ prefix by a shell command.
Kevin Locke7bf179d2022-06-06 20:42:54 -0600257cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
258 $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
Masahiro Yamada9f69a492019-07-17 15:18:00 +0900259
Masahiro Yamadafc93a4c2022-04-07 00:30:22 +0900260$(obj)/%.mod: FORCE
Masahiro Yamada9f69a492019-07-17 15:18:00 +0900261 $(call if_changed,mod)
262
Masahiro Yamada9413e762022-04-07 00:30:20 +0900263# List module undefined symbols
264cmd_undefined_syms = $(NM) $< | sed -n 's/^ *U //p' > $@
265
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900266$(obj)/%.usyms: $(obj)/%.o FORCE
Masahiro Yamada9413e762022-04-07 00:30:20 +0900267 $(call if_changed,undefined_syms)
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269quiet_cmd_cc_lst_c = MKLST $@
270 cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
271 $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
272 System.map $(OBJDUMP) > $@
273
Sam Ravnborg767e5812007-05-06 09:23:45 +0200274$(obj)/%.lst: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 $(call if_changed_dep,cc_lst_c)
276
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200277# Compile Rust sources (.rs)
278# ---------------------------------------------------------------------------
279
280rust_allowed_features := core_ffi_c
281
282rust_common_cmd = \
283 RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
284 -Zallow-features=$(rust_allowed_features) \
285 -Zcrate-attr=no_std \
286 -Zcrate-attr='feature($(rust_allowed_features))' \
287 --extern alloc --extern kernel \
Masahiro Yamada295d8392023-01-07 18:18:15 +0900288 --crate-type rlib -L $(objtree)/rust/ \
289 --crate-name $(basename $(notdir $@)) \
290 --emit=dep-info=$(depfile)
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200291
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200292# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
293# will be used. We explicitly request `-Ccodegen-units=1` in any case, and
294# the compiler shows a warning if it is not 1. However, if we ever stop
295# requesting it explicitly and we start using some other `--emit` that does not
296# imply it (and for which codegen is performed), then we would be out of sync,
297# i.e. the outputs we would get for the different single targets (e.g. `.ll`)
298# would not match each other.
299
300quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
Masahiro Yamada21852422023-01-07 18:18:17 +0900301 cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $<
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200302
303$(obj)/%.o: $(src)/%.rs FORCE
304 $(call if_changed_dep,rustc_o_rs)
305
306quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
307 cmd_rustc_rsi_rs = \
Masahiro Yamada295d8392023-01-07 18:18:15 +0900308 $(rust_common_cmd) -Zunpretty=expanded $< >$@; \
Masahiro Yamada21852422023-01-07 18:18:17 +0900309 command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200310
311$(obj)/%.rsi: $(src)/%.rs FORCE
312 $(call if_changed_dep,rustc_rsi_rs)
313
314quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
Masahiro Yamada21852422023-01-07 18:18:17 +0900315 cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200316
317$(obj)/%.s: $(src)/%.rs FORCE
318 $(call if_changed_dep,rustc_s_rs)
319
320quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
Masahiro Yamada21852422023-01-07 18:18:17 +0900321 cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200322
323$(obj)/%.ll: $(src)/%.rs FORCE
324 $(call if_changed_dep,rustc_ll_rs)
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326# Compile assembler sources (.S)
327# ---------------------------------------------------------------------------
328
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100329# .S file exports must have their C prototypes defined in asm/asm-prototypes.h
330# or a file that it includes, in order to get versioned symbols. We build a
331# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
332# the .S file (with trailing ';'), and run genksyms on that, to extract vers.
333#
334# This is convoluted. The .S file must first be preprocessed to run guards and
335# expand names, then the resulting exports must be constructed into plain
336# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
337# to make the genksyms input.
338#
339# These mirror gensymtypes_c and co above, keep them in synch.
340cmd_gensymtypes_S = \
Masahiro Yamadab79c6aa2019-01-17 19:02:44 +0900341 { echo "\#include <linux/kernel.h>" ; \
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100342 echo "\#include <asm/asm-prototypes.h>" ; \
343 $(CPP) $(a_flags) $< | \
Nicholas Piggincc6acc12016-11-09 15:34:05 +1100344 grep "\<___EXPORT_SYMBOL\>" | \
Masahiro Yamadab79c6aa2019-01-17 19:02:44 +0900345 sed 's/.*___EXPORT_SYMBOL[[:space:]]*\([a-zA-Z0-9_]*\)[[:space:]]*,.*/EXPORT_SYMBOL(\1);/' ; } | \
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900346 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100347
348quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
Masahiro Yamadadc6dc3e2022-04-05 20:33:50 +0900349 cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100350
351$(obj)/%.symtypes : $(src)/%.S FORCE
352 $(call cmd,cc_symtypes_S)
353
354
Masahiro Yamadae0f41e52016-04-08 11:24:48 +0900355quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
356cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Sam Ravnborg767e5812007-05-06 09:23:45 +0200358$(obj)/%.s: $(src)/%.S FORCE
Masahiro Yamadae0f41e52016-04-08 11:24:48 +0900359 $(call if_changed_dep,cpp_s_S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361quiet_cmd_as_o_S = AS $(quiet_modtag) $@
Masahiro Yamada92594d52021-08-31 16:39:54 +0900362 cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100363
Masahiro Yamada2ff2b7e2019-08-19 14:54:20 +0900364ifdef CONFIG_ASM_MODVERSIONS
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100365
366# versioning matches the C process described above, with difference that
367# we parse asm-prototypes.h C header to get function definitions.
368
Masahiro Yamada7cfa2fc2022-04-05 20:33:58 +0900369cmd_gen_symversions_S = $(call gen_symversions,S)
370
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100371endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900373$(obj)/%.o: $(src)/%.S FORCE
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600374 $(call if_changed_rule,as_o_S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900376targets += $(filter-out $(subdir-builtin), $(real-obj-y))
377targets += $(filter-out $(subdir-modorder), $(real-obj-m))
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900378targets += $(real-dtb-y) $(lib-y) $(always-y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380# Linker scripts preprocessor (.lds.S -> .lds)
381# ---------------------------------------------------------------------------
382quiet_cmd_cpp_lds_S = LDS $@
Linus Torvalds5cb05122017-11-02 14:10:37 -0700383 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
Tim Abbott42f29a22009-09-20 18:14:12 -0400384 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Sam Ravnborg767e5812007-05-06 09:23:45 +0200386$(obj)/%.lds: $(src)/%.lds.S FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 $(call if_changed_dep,cpp_lds_S)
388
David Howells4520c6a2012-09-21 23:31:13 +0100389# ASN.1 grammar
390# ---------------------------------------------------------------------------
Masahiro Yamada49d50892019-07-21 01:27:38 +0900391quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
David Howells4520c6a2012-09-21 23:31:13 +0100392 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
Masahiro Yamada49d50892019-07-21 01:27:38 +0900393 $(basename $@).c $(basename $@).h
David Howells4520c6a2012-09-21 23:31:13 +0100394
Masahiro Yamada4fa8bc92018-03-23 22:04:37 +0900395$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
David Howells4520c6a2012-09-21 23:31:13 +0100396 $(call cmd,asn1_compiler)
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398# Build the compiled-in targets
399# ---------------------------------------------------------------------------
400
401# To build objects in subdirs, we need to descend into the directories
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +0900402$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
Masahiro Yamada454753d2020-05-22 11:00:02 +0900403$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405#
Masahiro Yamadadee94952019-01-17 09:10:03 +0900406# Rule to compile a set of .o files into one .a file (without symbol table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#
Masahiro Yamadacd968b92022-05-27 19:01:52 +0900408# To make this rule robust against "Argument list too long" error,
409# remove $(obj)/ prefix, and restore it by a shell command.
Stephen Rothwella5967db2016-08-24 22:29:19 +1000410
Masahiro Yamada5e18f022018-03-19 20:26:12 +0900411quiet_cmd_ar_builtin = AR $@
Masahiro Yamadacd968b92022-05-27 19:01:52 +0900412 cmd_ar_builtin = rm -f $@; \
Masahiro Yamada5439d4d2022-06-14 14:51:49 +0900413 $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
414 $(AR) cDPrST $@
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Masahiro Yamadae578edc2020-05-22 10:59:58 +0900416$(obj)/built-in.a: $(real-obj-y) FORCE
Masahiro Yamada7375cbc2022-05-13 20:39:23 +0900417 $(call if_changed,ar_builtin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#
Tejun Heo551559e2007-12-07 21:04:30 +0900420# Rule to create modules.order file
421#
422# Create commands to either record .ko file or cat modules.order from
423# a subdirectory
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900424# Add $(obj-m) as the prerequisite to avoid updating the timestamp of
425# modules.order unless contained modules are updated.
426
427cmd_modules_order = { $(foreach m, $(real-prereqs), \
Masahiro Yamadaf65a4862022-12-11 22:04:07 +0900428 $(if $(filter %/modules.order, $m), cat $m, echo $m);) :; } \
Masahiro Yamadad724b572022-08-20 18:15:30 +0900429 > $@
Tejun Heo551559e2007-12-07 21:04:30 +0900430
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900431$(obj)/modules.order: $(obj-m) FORCE
432 $(call if_changed,modules_order)
433
Tejun Heo551559e2007-12-07 21:04:30 +0900434#
Masahiro Yamadadee94952019-01-17 09:10:03 +0900435# Rule to compile a set of .o files into one .a file (with symbol table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436#
Sami Tolvanen38e89182020-12-11 10:46:20 -0800437
Masahiro Yamadae578edc2020-05-22 10:59:58 +0900438$(obj)/lib.a: $(lib-y) FORCE
Masahiro Yamada7375cbc2022-05-13 20:39:23 +0900439 $(call if_changed,ar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900441quiet_cmd_ld_multi_m = LD [M] $@
442 cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$(patsubst %.o,%.mod,$@) $(cmd_objtool)
443
444define rule_ld_multi_m
445 $(call cmd_and_savecmd,ld_multi_m)
446 $(call cmd,gen_objtooldep)
447endef
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900449$(multi-obj-m): objtool-enabled := $(delay-objtool)
450$(multi-obj-m): part-of-module := y
Masahiro Yamadafeb7d792022-04-07 00:30:23 +0900451$(multi-obj-m): %.o: %.mod FORCE
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900452 $(call if_changed_rule,ld_multi_m)
Masahiro Yamadaa6601e02021-03-06 15:11:20 +0900453$(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900455# Add intermediate targets:
456# When building objects with specific suffix patterns, add intermediate
457# targets that the final targets are derived from.
458intermediate_targets = $(foreach sfx, $(2), \
459 $(patsubst %$(strip $(1)),%$(sfx), \
460 $(filter %$(strip $(1)), $(targets))))
Masahiro Yamada54a702f2018-03-23 22:04:39 +0900461# %.asn1.o <- %.asn1.[ch] <- %.asn1
Masahiro Yamadaa7f92412018-03-23 22:04:34 +0900462# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
Andrew Davisdcad2402022-11-14 14:59:39 -0600463# %.dtbo.o <- %.dtbo.S <- %.dtbo <- %.dtso
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900464# %.lex.o <- %.lex.c <- %.l
465# %.tab.o <- %.tab.[ch] <- %.y
Masahiro Yamada54a702f2018-03-23 22:04:39 +0900466targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
467 $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
Andrew Davisdcad2402022-11-14 14:59:39 -0600468 $(call intermediate_targets, .dtbo.o, .dtbo.S .dtbo) \
Masahiro Yamadaa7f92412018-03-23 22:04:34 +0900469 $(call intermediate_targets, .lex.o, .lex.c) \
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900470 $(call intermediate_targets, .tab.o, .tab.c .tab.h)
471
Masahiro Yamada394053f2019-08-15 00:19:18 +0900472# Build
473# ---------------------------------------------------------------------------
474
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900475$(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +0900476 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900477 $(subdir-ym) $(always-y)
Masahiro Yamada394053f2019-08-15 00:19:18 +0900478 @:
479
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900480# Single targets
481# ---------------------------------------------------------------------------
482
483single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d))
484single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS))
485
486$(single-subdir-goals): $(single-subdirs)
487 @:
Masahiro Yamada394053f2019-08-15 00:19:18 +0900488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489# Descending
490# ---------------------------------------------------------------------------
491
Paul Smith4f193362006-03-05 17:14:10 -0500492PHONY += $(subdir-ym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493$(subdir-ym):
Masahiro Yamadad9f78ed2019-08-06 19:03:21 +0900494 $(Q)$(MAKE) $(build)=$@ \
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +0900495 need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900496 need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \
497 $(filter $@/%, $(single-subdir-goals))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499# Add FORCE to the prequisites of a target to force it to be always rebuilt.
500# ---------------------------------------------------------------------------
501
Paul Smith4f193362006-03-05 17:14:10 -0500502PHONY += FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504FORCE:
505
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900506targets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS))
507targets := $(filter-out $(PHONY), $(targets))
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509# Read all saved command lines and dependencies for the $(targets) we
510# may be building above, using $(if_changed{,_dep}). As an
511# optimization, we don't need to read them if the target does not
512# exist, we will rebuild anyway in that case.
513
Masahiro Yamada392885e2018-11-30 10:05:22 +0900514existing-targets := $(wildcard $(sort $(targets)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Masahiro Yamada392885e2018-11-30 10:05:22 +0900516-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
Paul Smith4f193362006-03-05 17:14:10 -0500517
Masahiro Yamada8a787562017-11-13 19:29:37 +0900518# Create directories for object files if they do not exist
Masahiro Yamadacc8a51c2020-07-01 12:29:51 +0900519obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
Masahiro Yamada392885e2018-11-30 10:05:22 +0900520# If targets exist, their directories apparently exist. Skip mkdir.
521existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
522obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
Masahiro Yamadac4da7ed2017-11-13 19:29:38 +0900523ifneq ($(obj-dirs),)
Masahiro Yamada8a787562017-11-13 19:29:37 +0900524$(shell mkdir -p $(obj-dirs))
525endif
526
Paul Smith4f193362006-03-05 17:14:10 -0500527.PHONY: $(PHONY)