blob: 9a3063735e711b6d21f46b98ac1e704c6318ba48 [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 Yamada5e9e95cc92023-06-12 00:50:57 +090085targets-for-modules := $(foreach x, o mod, \
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),)
Johannes Bergdd203fe2023-06-09 10:46:42 +0200104 cmd_checkdoc = $(srctree)/scripts/kernel-doc -none $(KDOCFLAGS) \
105 $(if $(findstring 2, $(KBUILD_EXTRA_WARN)), -Wall) \
106 $<
Matthew Wilcox3a025e12017-11-20 10:40:40 -0800107endif
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109# Compile C sources (.c)
110# ---------------------------------------------------------------------------
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112quiet_cmd_cc_s_c = CC $(quiet_modtag) $@
Sami Tolvanendc5723b2020-12-11 10:46:19 -0800113 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 -0700114
Sam Ravnborg767e5812007-05-06 09:23:45 +0200115$(obj)/%.s: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 $(call if_changed_dep,cc_s_c)
117
Masahiro Yamada23d43842016-04-08 11:24:47 +0900118quiet_cmd_cpp_i_c = CPP $(quiet_modtag) $@
119cmd_cpp_i_c = $(CPP) $(c_flags) -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Sam Ravnborg767e5812007-05-06 09:23:45 +0200121$(obj)/%.i: $(src)/%.c FORCE
Masahiro Yamada23d43842016-04-08 11:24:47 +0900122 $(call if_changed_dep,cpp_i_c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900124genksyms = scripts/genksyms/genksyms \
125 $(if $(1), -T $(2)) \
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900126 $(if $(KBUILD_PRESERVE), -p) \
127 -r $(or $(wildcard $(2:.symtypes=.symref)), /dev/null)
128
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100129# These mirror gensymtypes_S and co below, keep them in synch.
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900130cmd_gensymtypes_c = $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms)
Sam Ravnborg37a8d9f2008-12-19 21:38:09 +0100131
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200132quiet_cmd_cc_symtypes_c = SYM $(quiet_modtag) $@
Masahiro Yamadadc6dc3e2022-04-05 20:33:50 +0900133 cmd_cc_symtypes_c = $(call cmd_gensymtypes_c,true,$@) >/dev/null
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200134
Sam Ravnborg767e5812007-05-06 09:23:45 +0200135$(obj)/%.symtypes : $(src)/%.c FORCE
Andreas Gruenbacher64e6c1e2008-12-01 14:21:01 -0800136 $(call cmd,cc_symtypes_c)
Andreas Gruenbacher15fde672006-05-09 20:37:30 +0200137
Vinícius Tinti433db3e2017-04-24 13:04:58 -0700138# LLVM assembly
139# Generate .ll files from .c
140quiet_cmd_cc_ll_c = CC $(quiet_modtag) $@
Nick Desaulniersc67a85b2022-10-14 09:53:02 -0700141 cmd_cc_ll_c = $(CC) $(c_flags) -emit-llvm -S -fno-discard-value-names -o $@ $<
Vinícius Tinti433db3e2017-04-24 13:04:58 -0700142
143$(obj)/%.ll: $(src)/%.c FORCE
144 $(call if_changed_dep,cc_ll_c)
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146# C (.c) files
147# The C file is compiled and updated dependency information is generated.
148# (See cmd_cc_o_c + relevant part of rule_cc_o_c)
149
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900150is-single-obj-m = $(and $(part-of-module),$(filter $@, $(obj-m)),y)
151
152# When a module consists of a single object, there is no reason to keep LLVM IR.
153# Make $(LD) covert LLVM IR to ELF here.
154ifdef CONFIG_LTO_CLANG
155cmd_ld_single_m = $(if $(is-single-obj-m), ; $(LD) $(ld_flags) -r -o $(tmp-target) $@; mv $(tmp-target) $@)
156endif
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158quiet_cmd_cc_o_c = CC $(quiet_modtag) $@
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900159 cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $< \
160 $(cmd_ld_single_m) \
161 $(cmd_objtool)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900163ifdef CONFIG_MODVERSIONS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164# When module versioning is enabled the following steps are executed:
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900165# o compile a <file>.o from <file>.c
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +0900166# o if <file>.o doesn't contain a __export_symbol_*, i.e. does
Masahiro Yamadaee3e46b2018-11-30 10:05:25 +0900167# not export symbols, it's done.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168# o otherwise, we calculate symbol versions using the good old
Masahiro Yamada7b453712022-05-13 20:39:22 +0900169# genksyms on the preprocessed source and dump them into the .cmd file.
170# o modpost will extract versions from that file and create *.c files that will
171# be compiled and linked to the kernel and/or modules.
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100172
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900173gen_symversions = \
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +0900174 if $(NM) $@ 2>/dev/null | grep -q ' __export_symbol_'; then \
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900175 $(call cmd_gensymtypes_$(1),$(KBUILD_SYMTYPES),$(@:.o=.symtypes)) \
Masahiro Yamada78e9e562022-05-05 16:22:35 +0900176 >> $(dot-target).cmd; \
Masahiro Yamadae5d28912018-11-30 10:05:28 +0900177 fi
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900178
179cmd_gen_symversions_c = $(call gen_symversions,c)
180
Sami Tolvanen38e89182020-12-11 10:46:20 -0800181endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Sami Tolvanen3b15cdc12020-12-11 10:46:18 -0800183ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
Vasily Gorbik07d04082018-08-06 15:17:44 +0200184# compiler will not generate __mcount_loc use recordmcount or recordmcount.pl
Steven Rostedt72441cb2010-10-13 17:12:30 -0400185ifdef BUILD_C_RECORDMCOUNT
Steven Rostedt85356f82011-04-12 18:59:10 -0400186ifeq ("$(origin RECORDMCOUNT_WARN)", "command line")
187 RECORDMCOUNT_FLAGS = -w
188endif
Steven Rostedtd7b4d6d2010-10-18 14:42:00 -0400189# Due to recursion, we must skip empty.o.
190# The empty.o file is created in the make process in order to determine
Cao jin312a3d02017-08-02 10:31:06 +0800191# the target endianness and word size. It is made before all other C
192# files, including recordmcount.
Wu Zhangjin45677452010-10-28 00:24:34 +0800193sub_cmd_record_mcount = \
194 if [ $(@) != "scripts/mod/empty.o" ]; then \
Steven Rostedt85356f82011-04-12 18:59:10 -0400195 $(objtree)/scripts/recordmcount $(RECORDMCOUNT_FLAGS) "$(@)"; \
Wu Zhangjin45677452010-10-28 00:24:34 +0800196 fi;
Michal Marekd6971822011-05-17 15:36:46 +0200197recordmcount_source := $(srctree)/scripts/recordmcount.c \
198 $(srctree)/scripts/recordmcount.h
Steven Rostedt72441cb2010-10-13 17:12:30 -0400199else
Masahiro Yamada4317ee32018-11-30 10:05:24 +0900200sub_cmd_record_mcount = perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
Wu Zhangjine6299d22009-11-20 20:34:31 +0800201 "$(if $(CONFIG_CPU_BIG_ENDIAN),big,little)" \
Steven Rostedtb3acf292008-10-29 15:30:26 -0400202 "$(if $(CONFIG_64BIT),64,32)" \
Joel Stanley5a4630a2018-09-17 17:07:54 +0930203 "$(OBJDUMP)" "$(OBJCOPY)" "$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS)" \
Masahiro Yamadad503ac52018-08-24 08:20:39 +0900204 "$(LD) $(KBUILD_LDFLAGS)" "$(NM)" "$(RM)" "$(MV)" \
Shaohua Li18c167f2009-01-12 10:00:51 +0800205 "$(if $(part-of-module),1,0)" "$(@)";
Michal Marekd6971822011-05-17 15:36:46 +0200206recordmcount_source := $(srctree)/scripts/recordmcount.pl
Cao jin312a3d02017-08-02 10:31:06 +0800207endif # BUILD_C_RECORDMCOUNT
Joe Lawrence1a49b2f2019-03-26 10:50:28 -0400208cmd_record_mcount = $(if $(findstring $(strip $(CC_FLAGS_FTRACE)),$(_c_flags)), \
209 $(sub_cmd_record_mcount))
Sami Tolvanen3b15cdc12020-12-11 10:46:18 -0800210endif # CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT
Steven Rostedt8da38212008-08-14 15:45:07 -0400211
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600212# 'OBJECT_FILES_NON_STANDARD := y': skip objtool checking for a directory
213# 'OBJECT_FILES_NON_STANDARD_foo.o := 'y': skip objtool checking for a file
214# 'OBJECT_FILES_NON_STANDARD_foo.o := 'n': override directory skip for a file
Masahiro Yamada918a6b72021-08-31 16:39:55 +0900215
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900216is-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 +0900217
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900218$(obj)/%.o: objtool-enabled = $(if $(is-standard-object),$(if $(delay-objtool),$(is-single-obj-m),y))
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600219
Masahiro Yamada598afa02022-11-19 04:15:51 +0900220ifneq ($(findstring 1, $(KBUILD_EXTRA_WARN)),)
221cmd_warn_shared_object = $(if $(word 2, $(modname-multi)),$(warning $(kbuild-file): $*.o is added to multiple modules: $(modname-multi)))
222endif
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224define rule_cc_o_c
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900225 $(call cmd_and_fixdep,cc_o_c)
Luc Van Oostenryck0c33f122020-06-22 17:45:12 +0200226 $(call cmd,checksrc)
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900227 $(call cmd,checkdoc)
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900228 $(call cmd,gen_objtooldep)
Masahiro Yamada8017ce52022-04-05 20:33:57 +0900229 $(call cmd,gen_symversions_c)
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900230 $(call cmd,record_mcount)
Masahiro Yamada598afa02022-11-19 04:15:51 +0900231 $(call cmd,warn_shared_object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232endef
233
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600234define rule_as_o_S
Masahiro Yamada3a2429e2018-11-30 10:05:27 +0900235 $(call cmd_and_fixdep,as_o_S)
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900236 $(call cmd,gen_objtooldep)
Masahiro Yamada7cfa2fc2022-04-05 20:33:58 +0900237 $(call cmd,gen_symversions_S)
Masahiro Yamada598afa02022-11-19 04:15:51 +0900238 $(call cmd,warn_shared_object)
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600239endef
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241# Built-in and composite module parts
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900242$(obj)/%.o: $(src)/%.c $(recordmcount_source) FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 $(call if_changed_rule,cc_o_c)
Luc Van Oostenryck0c33f122020-06-22 17:45:12 +0200244 $(call cmd,force_checksrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Masahiro Yamadac6031b12022-05-27 19:01:53 +0900246# To make this rule robust against "Argument list too long" error,
247# ensure to add $(obj)/ prefix by a shell command.
Kevin Locke7bf179d2022-06-06 20:42:54 -0600248cmd_mod = printf '%s\n' $(call real-search, $*.o, .o, -objs -y -m) | \
249 $(AWK) '!x[$$0]++ { print("$(obj)/"$$0) }' > $@
Masahiro Yamada9f69a492019-07-17 15:18:00 +0900250
Masahiro Yamadafc93a4c2022-04-07 00:30:22 +0900251$(obj)/%.mod: FORCE
Masahiro Yamada9f69a492019-07-17 15:18:00 +0900252 $(call if_changed,mod)
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254quiet_cmd_cc_lst_c = MKLST $@
255 cmd_cc_lst_c = $(CC) $(c_flags) -g -c -o $*.o $< && \
256 $(CONFIG_SHELL) $(srctree)/scripts/makelst $*.o \
257 System.map $(OBJDUMP) > $@
258
Sam Ravnborg767e5812007-05-06 09:23:45 +0200259$(obj)/%.lst: $(src)/%.c FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 $(call if_changed_dep,cc_lst_c)
261
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200262# Compile Rust sources (.rs)
263# ---------------------------------------------------------------------------
264
Alice Ryhl7324b882023-08-28 10:48:04 +0000265rust_allowed_features := new_uninit,offset_of
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200266
Miguel Ojedadf01b7c2023-07-23 16:21:28 +0200267# `--out-dir` is required to avoid temporaries being created by `rustc` in the
268# current working directory, which may be not accessible in the out-of-tree
269# modules case.
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200270rust_common_cmd = \
271 RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \
272 -Zallow-features=$(rust_allowed_features) \
273 -Zcrate-attr=no_std \
274 -Zcrate-attr='feature($(rust_allowed_features))' \
275 --extern alloc --extern kernel \
Masahiro Yamada295d8392023-01-07 18:18:15 +0900276 --crate-type rlib -L $(objtree)/rust/ \
277 --crate-name $(basename $(notdir $@)) \
Miguel Ojedadf01b7c2023-07-23 16:21:28 +0200278 --out-dir $(dir $@) --emit=dep-info=$(depfile)
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200279
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200280# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
281# will be used. We explicitly request `-Ccodegen-units=1` in any case, and
282# the compiler shows a warning if it is not 1. However, if we ever stop
283# requesting it explicitly and we start using some other `--emit` that does not
284# imply it (and for which codegen is performed), then we would be out of sync,
285# i.e. the outputs we would get for the different single targets (e.g. `.ll`)
286# would not match each other.
287
288quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
Masahiro Yamada21852422023-01-07 18:18:17 +0900289 cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $<
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200290
291$(obj)/%.o: $(src)/%.rs FORCE
292 $(call if_changed_dep,rustc_o_rs)
293
294quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
295 cmd_rustc_rsi_rs = \
Masahiro Yamada295d8392023-01-07 18:18:15 +0900296 $(rust_common_cmd) -Zunpretty=expanded $< >$@; \
Masahiro Yamada21852422023-01-07 18:18:17 +0900297 command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200298
299$(obj)/%.rsi: $(src)/%.rs FORCE
300 $(call if_changed_dep,rustc_rsi_rs)
301
302quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
Masahiro Yamada21852422023-01-07 18:18:17 +0900303 cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200304
305$(obj)/%.s: $(src)/%.rs FORCE
306 $(call if_changed_dep,rustc_s_rs)
307
308quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
Masahiro Yamada21852422023-01-07 18:18:17 +0900309 cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
Miguel Ojeda2f7ab122021-07-03 16:42:57 +0200310
311$(obj)/%.ll: $(src)/%.rs FORCE
312 $(call if_changed_dep,rustc_ll_rs)
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314# Compile assembler sources (.S)
315# ---------------------------------------------------------------------------
316
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100317# .S file exports must have their C prototypes defined in asm/asm-prototypes.h
318# or a file that it includes, in order to get versioned symbols. We build a
319# dummy C file that includes asm-prototypes and the EXPORT_SYMBOL lines from
320# the .S file (with trailing ';'), and run genksyms on that, to extract vers.
321#
322# This is convoluted. The .S file must first be preprocessed to run guards and
323# expand names, then the resulting exports must be constructed into plain
324# EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preprocessed
325# to make the genksyms input.
326#
327# These mirror gensymtypes_c and co above, keep them in synch.
328cmd_gensymtypes_S = \
Masahiro Yamadab79c6aa2019-01-17 19:02:44 +0900329 { echo "\#include <linux/kernel.h>" ; \
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100330 echo "\#include <asm/asm-prototypes.h>" ; \
Masahiro Yamadaddb5cdb2023-06-12 00:50:52 +0900331 $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' ; } | \
Masahiro Yamadaf43e31d52022-04-05 20:33:49 +0900332 $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms)
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100333
334quiet_cmd_cc_symtypes_S = SYM $(quiet_modtag) $@
Masahiro Yamadadc6dc3e2022-04-05 20:33:50 +0900335 cmd_cc_symtypes_S = $(call cmd_gensymtypes_S,true,$@) >/dev/null
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100336
337$(obj)/%.symtypes : $(src)/%.S FORCE
338 $(call cmd,cc_symtypes_S)
339
340
Masahiro Yamadae0f41e52016-04-08 11:24:48 +0900341quiet_cmd_cpp_s_S = CPP $(quiet_modtag) $@
342cmd_cpp_s_S = $(CPP) $(a_flags) -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Sam Ravnborg767e5812007-05-06 09:23:45 +0200344$(obj)/%.s: $(src)/%.S FORCE
Masahiro Yamadae0f41e52016-04-08 11:24:48 +0900345 $(call if_changed_dep,cpp_s_S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347quiet_cmd_as_o_S = AS $(quiet_modtag) $@
Masahiro Yamada92594d52021-08-31 16:39:54 +0900348 cmd_as_o_S = $(CC) $(a_flags) -c -o $@ $< $(cmd_objtool)
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100349
Masahiro Yamada2ff2b7e2019-08-19 14:54:20 +0900350ifdef CONFIG_ASM_MODVERSIONS
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100351
352# versioning matches the C process described above, with difference that
353# we parse asm-prototypes.h C header to get function definitions.
354
Masahiro Yamada7cfa2fc2022-04-05 20:33:58 +0900355cmd_gen_symversions_S = $(call gen_symversions,S)
356
Nicholas Piggin4efca4e2016-11-01 12:46:19 +1100357endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Masahiro Yamadaef62588c2021-08-31 16:39:56 +0900359$(obj)/%.o: $(src)/%.S FORCE
Josh Poimboeufb9ab5eb2016-02-28 22:22:42 -0600360 $(call if_changed_rule,as_o_S)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900362targets += $(filter-out $(subdir-builtin), $(real-obj-y))
363targets += $(filter-out $(subdir-modorder), $(real-obj-m))
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900364targets += $(real-dtb-y) $(lib-y) $(always-y)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366# Linker scripts preprocessor (.lds.S -> .lds)
367# ---------------------------------------------------------------------------
368quiet_cmd_cpp_lds_S = LDS $@
Linus Torvalds5cb05122017-11-02 14:10:37 -0700369 cmd_cpp_lds_S = $(CPP) $(cpp_flags) -P -U$(ARCH) \
Tim Abbott42f29a22009-09-20 18:14:12 -0400370 -D__ASSEMBLY__ -DLINKER_SCRIPT -o $@ $<
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Sam Ravnborg767e5812007-05-06 09:23:45 +0200372$(obj)/%.lds: $(src)/%.lds.S FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 $(call if_changed_dep,cpp_lds_S)
374
David Howells4520c6a2012-09-21 23:31:13 +0100375# ASN.1 grammar
376# ---------------------------------------------------------------------------
Masahiro Yamada49d50892019-07-21 01:27:38 +0900377quiet_cmd_asn1_compiler = ASN.1 $(basename $@).[ch]
David Howells4520c6a2012-09-21 23:31:13 +0100378 cmd_asn1_compiler = $(objtree)/scripts/asn1_compiler $< \
Masahiro Yamada49d50892019-07-21 01:27:38 +0900379 $(basename $@).c $(basename $@).h
David Howells4520c6a2012-09-21 23:31:13 +0100380
Masahiro Yamada4fa8bc92018-03-23 22:04:37 +0900381$(obj)/%.asn1.c $(obj)/%.asn1.h: $(src)/%.asn1 $(objtree)/scripts/asn1_compiler
David Howells4520c6a2012-09-21 23:31:13 +0100382 $(call cmd,asn1_compiler)
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384# Build the compiled-in targets
385# ---------------------------------------------------------------------------
386
387# To build objects in subdirs, we need to descend into the directories
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +0900388$(subdir-builtin): $(obj)/%/built-in.a: $(obj)/% ;
Masahiro Yamada454753d2020-05-22 11:00:02 +0900389$(subdir-modorder): $(obj)/%/modules.order: $(obj)/% ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391#
Masahiro Yamadadee94952019-01-17 09:10:03 +0900392# Rule to compile a set of .o files into one .a file (without symbol table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#
Masahiro Yamadacd968b92022-05-27 19:01:52 +0900394# To make this rule robust against "Argument list too long" error,
395# remove $(obj)/ prefix, and restore it by a shell command.
Stephen Rothwella5967db2016-08-24 22:29:19 +1000396
Masahiro Yamada5e18f022018-03-19 20:26:12 +0900397quiet_cmd_ar_builtin = AR $@
Masahiro Yamadacd968b92022-05-27 19:01:52 +0900398 cmd_ar_builtin = rm -f $@; \
Masahiro Yamada5439d4d2022-06-14 14:51:49 +0900399 $(if $(real-prereqs), printf "$(obj)/%s " $(patsubst $(obj)/%,%,$(real-prereqs)) | xargs) \
400 $(AR) cDPrST $@
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Masahiro Yamadae578edc2020-05-22 10:59:58 +0900402$(obj)/built-in.a: $(real-obj-y) FORCE
Masahiro Yamada7375cbc2022-05-13 20:39:23 +0900403 $(call if_changed,ar_builtin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405#
Tejun Heo551559e2007-12-07 21:04:30 +0900406# Rule to create modules.order file
407#
408# Create commands to either record .ko file or cat modules.order from
409# a subdirectory
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900410# Add $(obj-m) as the prerequisite to avoid updating the timestamp of
411# modules.order unless contained modules are updated.
412
413cmd_modules_order = { $(foreach m, $(real-prereqs), \
Masahiro Yamadaf65a4862022-12-11 22:04:07 +0900414 $(if $(filter %/modules.order, $m), cat $m, echo $m);) :; } \
Masahiro Yamadad724b572022-08-20 18:15:30 +0900415 > $@
Tejun Heo551559e2007-12-07 21:04:30 +0900416
Masahiro Yamadab2c88552020-06-01 14:57:00 +0900417$(obj)/modules.order: $(obj-m) FORCE
418 $(call if_changed,modules_order)
419
Tejun Heo551559e2007-12-07 21:04:30 +0900420#
Masahiro Yamadadee94952019-01-17 09:10:03 +0900421# Rule to compile a set of .o files into one .a file (with symbol table)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422#
Sami Tolvanen38e89182020-12-11 10:46:20 -0800423
Masahiro Yamadae578edc2020-05-22 10:59:58 +0900424$(obj)/lib.a: $(lib-y) FORCE
Masahiro Yamada7375cbc2022-05-13 20:39:23 +0900425 $(call if_changed,ar)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900427quiet_cmd_ld_multi_m = LD [M] $@
Masahiro Yamada1b1595c2023-10-23 01:30:14 +0900428 cmd_ld_multi_m = $(LD) $(ld_flags) -r -o $@ @$< $(cmd_objtool)
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900429
430define rule_ld_multi_m
431 $(call cmd_and_savecmd,ld_multi_m)
432 $(call cmd,gen_objtooldep)
433endef
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Masahiro Yamadac25e1c52022-05-27 19:01:49 +0900435$(multi-obj-m): objtool-enabled := $(delay-objtool)
436$(multi-obj-m): part-of-module := y
Masahiro Yamadafeb7d792022-04-07 00:30:23 +0900437$(multi-obj-m): %.o: %.mod FORCE
Masahiro Yamadaf6b66ca2022-05-27 19:01:55 +0900438 $(call if_changed_rule,ld_multi_m)
Masahiro Yamadaa6601e02021-03-06 15:11:20 +0900439$(call multi_depend, $(multi-obj-m), .o, -objs -y -m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900441# Add intermediate targets:
442# When building objects with specific suffix patterns, add intermediate
443# targets that the final targets are derived from.
444intermediate_targets = $(foreach sfx, $(2), \
445 $(patsubst %$(strip $(1)),%$(sfx), \
446 $(filter %$(strip $(1)), $(targets))))
Masahiro Yamada54a702f2018-03-23 22:04:39 +0900447# %.asn1.o <- %.asn1.[ch] <- %.asn1
Masahiro Yamadaa7f92412018-03-23 22:04:34 +0900448# %.dtb.o <- %.dtb.S <- %.dtb <- %.dts
Andrew Davisdcad2402022-11-14 14:59:39 -0600449# %.dtbo.o <- %.dtbo.S <- %.dtbo <- %.dtso
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900450# %.lex.o <- %.lex.c <- %.l
451# %.tab.o <- %.tab.[ch] <- %.y
Masahiro Yamada54a702f2018-03-23 22:04:39 +0900452targets += $(call intermediate_targets, .asn1.o, .asn1.c .asn1.h) \
453 $(call intermediate_targets, .dtb.o, .dtb.S .dtb) \
Andrew Davisdcad2402022-11-14 14:59:39 -0600454 $(call intermediate_targets, .dtbo.o, .dtbo.S .dtbo) \
Masahiro Yamadaa7f92412018-03-23 22:04:34 +0900455 $(call intermediate_targets, .lex.o, .lex.c) \
Masahiro Yamadab23d1a22018-03-23 22:04:33 +0900456 $(call intermediate_targets, .tab.o, .tab.c .tab.h)
457
Masahiro Yamada394053f2019-08-15 00:19:18 +0900458# Build
459# ---------------------------------------------------------------------------
460
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900461$(obj)/: $(if $(KBUILD_BUILTIN), $(targets-for-builtin)) \
Masahiro Yamadaf3908ab2020-06-01 14:56:56 +0900462 $(if $(KBUILD_MODULES), $(targets-for-modules)) \
Masahiro Yamada5f2fb522020-02-02 01:49:24 +0900463 $(subdir-ym) $(always-y)
Masahiro Yamada394053f2019-08-15 00:19:18 +0900464 @:
465
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900466# Single targets
467# ---------------------------------------------------------------------------
468
469single-subdirs := $(foreach d, $(subdir-ym), $(if $(filter $d/%, $(MAKECMDGOALS)), $d))
470single-subdir-goals := $(filter $(addsuffix /%, $(single-subdirs)), $(MAKECMDGOALS))
471
472$(single-subdir-goals): $(single-subdirs)
473 @:
Masahiro Yamada394053f2019-08-15 00:19:18 +0900474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475# Descending
476# ---------------------------------------------------------------------------
477
Paul Smith4f193362006-03-05 17:14:10 -0500478PHONY += $(subdir-ym)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479$(subdir-ym):
Masahiro Yamadad9f78ed2019-08-06 19:03:21 +0900480 $(Q)$(MAKE) $(build)=$@ \
Masahiro Yamadaaaa385b2020-05-22 11:00:01 +0900481 need-builtin=$(if $(filter $@/built-in.a, $(subdir-builtin)),1) \
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900482 need-modorder=$(if $(filter $@/modules.order, $(subdir-modorder)),1) \
483 $(filter $@/%, $(single-subdir-goals))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485# Add FORCE to the prequisites of a target to force it to be always rebuilt.
486# ---------------------------------------------------------------------------
487
Paul Smith4f193362006-03-05 17:14:10 -0500488PHONY += FORCE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490FORCE:
491
Masahiro Yamadacc306ab2022-09-06 15:13:06 +0900492targets += $(filter-out $(single-subdir-goals), $(MAKECMDGOALS))
493targets := $(filter-out $(PHONY), $(targets))
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495# Read all saved command lines and dependencies for the $(targets) we
496# may be building above, using $(if_changed{,_dep}). As an
497# optimization, we don't need to read them if the target does not
498# exist, we will rebuild anyway in that case.
499
Masahiro Yamada392885e2018-11-30 10:05:22 +0900500existing-targets := $(wildcard $(sort $(targets)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Masahiro Yamada392885e2018-11-30 10:05:22 +0900502-include $(foreach f,$(existing-targets),$(dir $(f)).$(notdir $(f)).cmd)
Paul Smith4f193362006-03-05 17:14:10 -0500503
Masahiro Yamada8a787562017-11-13 19:29:37 +0900504# Create directories for object files if they do not exist
Masahiro Yamadacc8a51c2020-07-01 12:29:51 +0900505obj-dirs := $(sort $(patsubst %/,%, $(dir $(targets))))
Masahiro Yamada392885e2018-11-30 10:05:22 +0900506# If targets exist, their directories apparently exist. Skip mkdir.
507existing-dirs := $(sort $(patsubst %/,%, $(dir $(existing-targets))))
508obj-dirs := $(strip $(filter-out $(existing-dirs), $(obj-dirs)))
Masahiro Yamadac4da7ed2017-11-13 19:29:38 +0900509ifneq ($(obj-dirs),)
Masahiro Yamada8a787562017-11-13 19:29:37 +0900510$(shell mkdir -p $(obj-dirs))
511endif
512
Paul Smith4f193362006-03-05 17:14:10 -0500513.PHONY: $(PHONY)