xref: /linux/rust/Makefile (revision b2c261fa8629dff2bd1143fa790797a773ace102)
1# SPDX-License-Identifier: GPL-2.0
2
3# Where to place rustdoc generated documentation
4rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
5
6obj-$(CONFIG_RUST) += core.o compiler_builtins.o
7always-$(CONFIG_RUST) += exports_core_generated.h
8
9# Missing prototypes are expected in the helpers since these are exported
10# for Rust only, thus there is no header nor prototypes.
11obj-$(CONFIG_RUST) += helpers/helpers.o
12CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
13
14always-$(CONFIG_RUST) += libmacros.so
15no-clean-files += libmacros.so
16
17always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
18obj-$(CONFIG_RUST) += bindings.o kernel.o
19always-$(CONFIG_RUST) += exports_helpers_generated.h \
20    exports_bindings_generated.h exports_kernel_generated.h
21
22always-$(CONFIG_RUST) += uapi/uapi_generated.rs
23obj-$(CONFIG_RUST) += uapi.o
24
25ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
26obj-$(CONFIG_RUST) += build_error.o
27else
28always-$(CONFIG_RUST) += build_error.o
29endif
30
31obj-$(CONFIG_RUST) += exports.o
32
33always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
34always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
35
36obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
37obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
38
39# Avoids running `$(RUSTC)` for the sysroot when it may not be available.
40ifdef CONFIG_RUST
41
42# `$(rust_flags)` is passed in case the user added `--sysroot`.
43rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
44rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
45RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
46
47ifneq ($(quiet),)
48rust_test_quiet=-q
49rustdoc_test_quiet=--test-args -q
50rustdoc_test_kernel_quiet=>/dev/null
51endif
52
53core-cfgs = \
54    --cfg no_fp_fmt_parse
55
56quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
57      cmd_rustdoc = \
58	OBJTREE=$(abspath $(objtree)) \
59	$(RUSTDOC) $(filter-out $(skip_flags),$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
60		$(rustc_target_flags) -L$(objtree)/$(obj) \
61		-Zunstable-options --generate-link-to-definition \
62		--output $(rustdoc_output) \
63		--crate-name $(subst rustdoc-,,$@) \
64		$(if $(rustdoc_host),,--sysroot=/dev/null) \
65		@$(objtree)/include/generated/rustc_cfg $<
66
67# The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
68# can be used to specify a custom logo. However:
69#   - The given value is used as-is, thus it cannot be relative or a local file
70#     (unlike the non-custom case) since the generated docs have subfolders.
71#   - It requires adding it to every crate.
72#   - It requires changing `core` which comes from the sysroot.
73#
74# Using `-Zcrate-attr` would solve the last two points, but not the first.
75# The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
76# command-like flags to solve the issue. Meanwhile, we use the non-custom case
77# and then retouch the generated files.
78rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
79    rustdoc-kernel
80	$(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
81	$(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
82	$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
83		-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
84		-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
85		-e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
86		-e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
87	$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
88		echo ".logo-container > img { object-fit: contain; }" >> $$f; done
89
90rustdoc-macros: private rustdoc_host = yes
91rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
92    --extern proc_macro
93rustdoc-macros: $(src)/macros/lib.rs FORCE
94	+$(call if_changed,rustdoc)
95
96# Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
97# not be needed -- see https://github.com/rust-lang/rust/pull/128307.
98rustdoc-core: private skip_flags = -Wrustdoc::unescaped_backticks
99rustdoc-core: private rustc_target_flags = $(core-cfgs)
100rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
101	+$(call if_changed,rustdoc)
102
103rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
104	+$(call if_changed,rustdoc)
105
106rustdoc-kernel: private rustc_target_flags = \
107    --extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
108    --extern bindings --extern uapi
109rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
110    rustdoc-compiler_builtins $(obj)/libmacros.so \
111    $(obj)/bindings.o FORCE
112	+$(call if_changed,rustdoc)
113
114quiet_cmd_rustc_test_library = RUSTC TL $<
115      cmd_rustc_test_library = \
116	OBJTREE=$(abspath $(objtree)) \
117	$(RUSTC) $(rust_common_flags) \
118		@$(objtree)/include/generated/rustc_cfg $(rustc_target_flags) \
119		--crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
120		--out-dir $(objtree)/$(obj)/test --cfg testlib \
121		-L$(objtree)/$(obj)/test \
122		--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
123
124rusttestlib-build_error: $(src)/build_error.rs FORCE
125	+$(call if_changed,rustc_test_library)
126
127rusttestlib-macros: private rustc_target_flags = --extern proc_macro
128rusttestlib-macros: private rustc_test_library_proc = yes
129rusttestlib-macros: $(src)/macros/lib.rs FORCE
130	+$(call if_changed,rustc_test_library)
131
132rusttestlib-kernel: private rustc_target_flags = \
133    --extern build_error --extern macros \
134    --extern bindings --extern uapi
135rusttestlib-kernel: $(src)/kernel/lib.rs \
136    rusttestlib-bindings rusttestlib-uapi rusttestlib-build_error \
137    $(obj)/libmacros.so $(obj)/bindings.o FORCE
138	+$(call if_changed,rustc_test_library)
139
140rusttestlib-bindings: $(src)/bindings/lib.rs FORCE
141	+$(call if_changed,rustc_test_library)
142
143rusttestlib-uapi: $(src)/uapi/lib.rs FORCE
144	+$(call if_changed,rustc_test_library)
145
146quiet_cmd_rustdoc_test = RUSTDOC T $<
147      cmd_rustdoc_test = \
148	OBJTREE=$(abspath $(objtree)) \
149	$(RUSTDOC) --test $(rust_common_flags) \
150		@$(objtree)/include/generated/rustc_cfg \
151		$(rustc_target_flags) $(rustdoc_test_target_flags) \
152		$(rustdoc_test_quiet) \
153		-L$(objtree)/$(obj)/test --output $(rustdoc_output) \
154		--crate-name $(subst rusttest-,,$@) $<
155
156quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
157      cmd_rustdoc_test_kernel = \
158	rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
159	mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
160	OBJTREE=$(abspath $(objtree)) \
161	$(RUSTDOC) --test $(rust_flags) \
162		-L$(objtree)/$(obj) --extern kernel \
163		--extern build_error --extern macros \
164		--extern bindings --extern uapi \
165		--no-run --crate-name kernel -Zunstable-options \
166		--sysroot=/dev/null \
167		--test-builder $(objtree)/scripts/rustdoc_test_builder \
168		$< $(rustdoc_test_kernel_quiet); \
169	$(objtree)/scripts/rustdoc_test_gen
170
171%/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
172    $(src)/kernel/lib.rs $(obj)/kernel.o \
173    $(objtree)/scripts/rustdoc_test_builder \
174    $(objtree)/scripts/rustdoc_test_gen FORCE
175	+$(call if_changed,rustdoc_test_kernel)
176
177# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
178# so for the moment we skip `-Cpanic=abort`.
179quiet_cmd_rustc_test = RUSTC T  $<
180      cmd_rustc_test = \
181	OBJTREE=$(abspath $(objtree)) \
182	$(RUSTC) --test $(rust_common_flags) \
183		@$(objtree)/include/generated/rustc_cfg \
184		$(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
185		-L$(objtree)/$(obj)/test \
186		--crate-name $(subst rusttest-,,$@) $<; \
187	$(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
188		$(rustc_test_run_flags)
189
190rusttest: rusttest-macros rusttest-kernel
191
192rusttest-macros: private rustc_target_flags = --extern proc_macro \
193	--extern macros --extern kernel
194rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
195rusttest-macros: $(src)/macros/lib.rs \
196    rusttestlib-macros rusttestlib-kernel FORCE
197	+$(call if_changed,rustc_test)
198	+$(call if_changed,rustdoc_test)
199
200rusttest-kernel: private rustc_target_flags = \
201    --extern build_error --extern macros --extern bindings --extern uapi
202rusttest-kernel: $(src)/kernel/lib.rs rusttestlib-kernel \
203    rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
204    rusttestlib-uapi FORCE
205	+$(call if_changed,rustc_test)
206
207ifdef CONFIG_CC_IS_CLANG
208bindgen_c_flags = $(c_flags)
209else
210# bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
211# plugin backend and/or the Clang driver would be perfectly compatible with GCC.
212#
213# For the moment, here we are tweaking the flags on the fly. This is a hack,
214# and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
215# if we end up using one of those structs).
216bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
217	-mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
218	-mindirect-branch=thunk-extern -mindirect-branch-register \
219	-mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
220	-mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
221	-mno-pointers-to-nested-functions -mno-string \
222	-mno-strict-align -mstrict-align \
223	-fconserve-stack -falign-jumps=% -falign-loops=% \
224	-femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
225	-fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
226	-fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
227	-fzero-call-used-regs=% -fno-stack-clash-protection \
228	-fno-inline-functions-called-once -fsanitize=bounds-strict \
229	-fstrict-flex-arrays=% -fmin-function-alignment=% \
230	--param=% --param asan-%
231
232# Derived from `scripts/Makefile.clang`.
233BINDGEN_TARGET_x86	:= x86_64-linux-gnu
234BINDGEN_TARGET_arm64	:= aarch64-linux-gnu
235BINDGEN_TARGET		:= $(BINDGEN_TARGET_$(SRCARCH))
236
237# All warnings are inhibited since GCC builds are very experimental,
238# many GCC warnings are not supported by Clang, they may only appear in
239# some configurations, with new GCC versions, etc.
240bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
241
242# Auto variable zero-initialization requires an additional special option with
243# clang that is going to be removed sometime in the future (likely in
244# clang-18), so make sure to pass this option only if clang supports it
245# (libclang major version < 16).
246#
247# https://github.com/llvm/llvm-project/issues/44842
248# https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
249ifdef CONFIG_INIT_STACK_ALL_ZERO
250libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
251ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
252bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
253endif
254endif
255
256bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
257	$(bindgen_extra_c_flags)
258endif
259
260ifdef CONFIG_LTO
261bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
262else
263bindgen_c_flags_lto = $(bindgen_c_flags)
264endif
265
266bindgen_c_flags_final = $(bindgen_c_flags_lto) -D__BINDGEN__
267
268quiet_cmd_bindgen = BINDGEN $@
269      cmd_bindgen = \
270	$(BINDGEN) $< $(bindgen_target_flags) \
271		--use-core --with-derive-default --ctypes-prefix core::ffi --no-layout-tests \
272		--no-debug '.*' --enable-function-attribute-detection \
273		-o $@ -- $(bindgen_c_flags_final) -DMODULE \
274		$(bindgen_target_cflags) $(bindgen_target_extra)
275
276$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
277    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
278$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
279    sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
280$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
281    $(src)/bindgen_parameters FORCE
282	$(call if_changed_dep,bindgen)
283
284$(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
285    $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
286$(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
287    $(src)/bindgen_parameters FORCE
288	$(call if_changed_dep,bindgen)
289
290# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
291# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
292# given it is `libclang`; but for consistency, future Clang changes and/or
293# a potential future GCC backend for `bindgen`, we disable it too.
294$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
295    --blocklist-type '.*' --allowlist-var '' \
296    --allowlist-function 'rust_helper_.*'
297$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
298    -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
299$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
300    sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n    pub fn \1/g' $@
301$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
302	$(call if_changed_dep,bindgen)
303
304quiet_cmd_exports = EXPORTS $@
305      cmd_exports = \
306	$(NM) -p --defined-only $< \
307		| awk '$$2~/(T|R|D|B)/ && $$3!~/__cfi/ {printf "EXPORT_SYMBOL_RUST_GPL(%s);\n",$$3}' > $@
308
309$(obj)/exports_core_generated.h: $(obj)/core.o FORCE
310	$(call if_changed,exports)
311
312# Even though Rust kernel modules should never use the bindings directly,
313# symbols from the `bindings` crate and the C helpers need to be exported
314# because Rust generics and inlined functions may not get their code generated
315# in the crate where they are defined. Other helpers, called from non-inline
316# functions, may not be exported, in principle. However, in general, the Rust
317# compiler does not guarantee codegen will be performed for a non-inline
318# function either. Therefore, we export all symbols from helpers and bindings.
319# In the future, this may be revisited to reduce the number of exports after
320# the compiler is informed about the places codegen is required.
321$(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
322	$(call if_changed,exports)
323
324$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
325	$(call if_changed,exports)
326
327$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
328	$(call if_changed,exports)
329
330quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
331      cmd_rustc_procmacro = \
332	$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
333		-Clinker-flavor=gcc -Clinker=$(HOSTCC) \
334		-Clink-args='$(call escsq,$(KBUILD_HOSTLDFLAGS))' \
335		--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
336		--crate-type proc-macro \
337		--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<
338
339# Procedural macros can only be used with the `rustc` that compiled it.
340$(obj)/libmacros.so: $(src)/macros/lib.rs FORCE
341	+$(call if_changed_dep,rustc_procmacro)
342
343quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
344      cmd_rustc_library = \
345	OBJTREE=$(abspath $(objtree)) \
346	$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
347		$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
348		--emit=dep-info=$(depfile) --emit=obj=$@ \
349		--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
350		--crate-type rlib -L$(objtree)/$(obj) \
351		--crate-name $(patsubst %.o,%,$(notdir $@)) $< \
352		--sysroot=/dev/null \
353	$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
354	$(cmd_objtool)
355
356rust-analyzer:
357	$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
358		--cfgs='core=$(core-cfgs)' \
359		$(realpath $(srctree)) $(realpath $(objtree)) \
360		$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
361		$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
362
363redirect-intrinsics = \
364	__addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
365	__adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
366	__muloti4 __multi3 \
367	__udivmodti4 __udivti3 __umodti3
368
369ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
370	# These intrinsics are defined for ARM64 and RISCV64
371	redirect-intrinsics += \
372		__ashrti3 \
373		__ashlti3 __lshrti3
374endif
375
376define rule_rustc_library
377	$(call cmd_and_fixdep,rustc_library)
378	$(call cmd,gen_objtooldep)
379endef
380
381$(obj)/core.o: private skip_clippy = 1
382$(obj)/core.o: private skip_flags = -Wunreachable_pub
383$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
384$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
385$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
386    $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
387	+$(call if_changed_rule,rustc_library)
388ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
389$(obj)/core.o: scripts/target.json
390endif
391
392$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
393$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
394	+$(call if_changed_rule,rustc_library)
395
396$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
397	+$(call if_changed_rule,rustc_library)
398
399$(obj)/bindings.o: $(src)/bindings/lib.rs \
400    $(obj)/compiler_builtins.o \
401    $(obj)/bindings/bindings_generated.rs \
402    $(obj)/bindings/bindings_helpers_generated.rs FORCE
403	+$(call if_changed_rule,rustc_library)
404
405$(obj)/uapi.o: $(src)/uapi/lib.rs \
406    $(obj)/compiler_builtins.o \
407    $(obj)/uapi/uapi_generated.rs FORCE
408	+$(call if_changed_rule,rustc_library)
409
410$(obj)/kernel.o: private rustc_target_flags = \
411    --extern build_error --extern macros --extern bindings --extern uapi
412$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o \
413    $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
414	+$(call if_changed_rule,rustc_library)
415
416endif # CONFIG_RUST
417