Debian Codegraph @

rust/Makefile

raw
1 # SPDX-License-Identifier: GPL-2.0
2
3 # Where to place rustdoc generated documentation
4 rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
5
6 obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o
7 always-$(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.
11 obj-$(CONFIG_RUST) += helpers/helpers.o
12 CFLAGS_REMOVE_helpers/helpers.o = -Wmissing-prototypes -Wmissing-declarations
13
14 always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
15 obj-$(CONFIG_RUST) += bindings.o pin_init.o kernel.o
16 always-$(CONFIG_RUST) += exports_helpers_generated.h \
17 exports_bindings_generated.h exports_kernel_generated.h
18
19 always-$(CONFIG_RUST) += uapi/uapi_generated.rs
20 obj-$(CONFIG_RUST) += uapi.o
21
22 ifdef CONFIG_RUST_BUILD_ASSERT_ALLOW
23 obj-$(CONFIG_RUST) += build_error.o
24 else
25 always-$(CONFIG_RUST) += build_error.o
26 endif
27
28 obj-$(CONFIG_RUST) += exports.o
29
30 always-$(CONFIG_RUST) += libproc_macro2.rlib libquote.rlib libsyn.rlib
31
32 always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.rs
33 always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c
34
35 obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o
36 obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
37
38 always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/generated_arch_static_branch_asm.rs
39 ifndef CONFIG_UML
40 always-$(subst y,$(CONFIG_RUST),$(CONFIG_BUG)) += kernel/generated_arch_warn_asm.rs kernel/generated_arch_reachable_asm.rs
41 endif
42
43 # Avoids running `$(RUSTC)` when it may not be available.
44 ifdef CONFIG_RUST
45
46 libmacros_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name macros --crate-type proc-macro - </dev/null)
47 libmacros_extension := $(patsubst libmacros.%,%,$(libmacros_name))
48
49 libpin_init_internal_name := $(shell MAKEFLAGS= $(RUSTC) --print file-names --crate-name pin_init_internal --crate-type proc-macro - </dev/null)
50 libpin_init_internal_extension := $(patsubst libpin_init_internal.%,%,$(libpin_init_internal_name))
51
52 always-$(CONFIG_RUST) += $(libmacros_name) $(libpin_init_internal_name)
53
54 # `$(rust_flags)` is passed in case the user added `--sysroot`.
55 rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
56 rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
57 RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
58
59 ifneq ($(quiet),)
60 rust_test_quiet=-q
61 rustdoc_test_quiet=--test-args -q
62 rustdoc_test_kernel_quiet=>/dev/null
63 endif
64
65 cfgs-to-flags = $(patsubst %,--cfg='%',$1)
66
67 core-cfgs := \
68 no_fp_fmt_parse
69
70 core-edition := $(if $(call rustc-min-version,108700),2024,2021)
71
72 core-skip_flags := \
73 --edition=2021 \
74 -Wunreachable_pub \
75 -Wrustdoc::unescaped_backticks
76
77 core-flags := \
78 --edition=$(core-edition) \
79 $(call cfgs-to-flags,$(core-cfgs))
80
81 proc_macro2-cfgs := \
82 feature="proc-macro" \
83 wrap_proc_macro \
84 $(if $(call rustc-min-version,108800),proc_macro_span_file proc_macro_span_location)
85
86 # Stable since Rust 1.79.0: `feature(proc_macro_byte_character,proc_macro_c_str_literals)`.
87 proc_macro2-flags := \
88 --cap-lints=allow \
89 -Zcrate-attr='feature(proc_macro_byte_character,proc_macro_c_str_literals)' \
90 $(call cfgs-to-flags,$(proc_macro2-cfgs))
91
92 quote-cfgs := \
93 feature="proc-macro"
94
95 quote-skip_flags := \
96 --edition=2021
97
98 quote-flags := \
99 --edition=2018 \
100 --cap-lints=allow \
101 --extern proc_macro2 \
102 $(call cfgs-to-flags,$(quote-cfgs))
103
104 # `extra-traits`, `fold` and `visit` may be enabled if needed.
105 syn-cfgs := \
106 feature="clone-impls" \
107 feature="derive" \
108 feature="full" \
109 feature="parsing" \
110 feature="printing" \
111 feature="proc-macro" \
112 feature="visit-mut"
113
114 syn-flags := \
115 --cap-lints=allow \
116 --extern proc_macro2 \
117 --extern quote \
118 $(call cfgs-to-flags,$(syn-cfgs))
119
120 pin_init_internal-cfgs := \
121 kernel
122
123 pin_init_internal-flags := \
124 --extern proc_macro2 \
125 --extern quote \
126 --extern syn \
127 $(call cfgs-to-flags,$(pin_init_internal-cfgs))
128
129 pin_init-cfgs := \
130 kernel
131
132 pin_init-flags := \
133 --extern pin_init_internal \
134 --extern macros \
135 $(call cfgs-to-flags,$(pin_init-cfgs))
136
137 # `rustdoc` did not save the target modifiers, thus workaround for
138 # the time being (https://github.com/rust-lang/rust/issues/144521).
139 rustdoc_modifiers_workaround := $(if $(call rustc-min-version,108800),-Cunsafe-allow-abi-mismatch=fixed-x18)
140
141 # Similarly, for doctests (https://github.com/rust-lang/rust/issues/146465).
142 doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rustc-min-version,109100),$(comma)sanitizer)
143
144 # `rustc` recognizes `--remap-path-prefix` since 1.26.0, but `rustdoc` only
145 # since Rust 1.81.0. Moreover, `rustdoc` ICEs on out-of-tree builds since Rust
146 # 1.82.0 (https://github.com/rust-lang/rust/issues/138520). Thus workaround both
147 # issues skipping the flag. The former also applies to `RUSTDOC TK`.
148 quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
149 cmd_rustdoc = \
150 OBJTREE=$(abspath $(objtree)) \
151 $(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=% --remap-path-scope=%, \
152 $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
153 $(rustc_target_flags) -L$(objtree)/$(obj) \
154 -Zunstable-options --generate-link-to-definition \
155 --output $(rustdoc_output) \
156 --crate-name $(subst rustdoc-,,$@) \
157 $(rustdoc_modifiers_workaround) \
158 $(if $(rustdoc_host),,--sysroot=/dev/null) \
159 @$(objtree)/include/generated/rustc_cfg $<
160
161 # The `html_logo_url` and `html_favicon_url` forms of the `doc` attribute
162 # can be used to specify a custom logo. However:
163 # - The given value is used as-is, thus it cannot be relative or a local file
164 # (unlike the non-custom case) since the generated docs have subfolders.
165 # - It requires adding it to every crate.
166 # - It requires changing `core` which comes from the sysroot.
167 #
168 # Using `-Zcrate-attr` would solve the last two points, but not the first.
169 # The https://github.com/rust-lang/rfcs/pull/3226 RFC suggests two new
170 # command-like flags to solve the issue. Meanwhile, we use the non-custom case
171 # and then retouch the generated files.
172 rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
173 rustdoc-kernel rustdoc-pin_init
174 $(Q)grep -Ehro '<a href="srctree/([^"]+)"' $(rustdoc_output) | \
175 cut -d'"' -f2 | cut -d/ -f2- | while read f; do \
176 if [ ! -e "$(srctree)/$$f" ]; then \
177 echo "warning: srctree/ link to $$f does not exist"; \
178 fi \
179 done
180 $(Q)cp $(srctree)/Documentation/images/logo.svg $(rustdoc_output)/static.files/
181 $(Q)cp $(srctree)/Documentation/images/COPYING-logo $(rustdoc_output)/static.files/
182 $(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
183 -e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
184 -e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
185 -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
186 -e 's:<a href="srctree/([^"]+)">:<a href="$(realpath $(srctree))/\1">:g'
187 $(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
188 echo ".logo-container > img { object-fit: contain; }" >> $$f; done
189
190 rustdoc-proc_macro2: private rustdoc_host = yes
191 rustdoc-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
192 rustdoc-proc_macro2: $(src)/proc-macro2/lib.rs rustdoc-clean FORCE
193 +$(call if_changed,rustdoc)
194
195 rustdoc-quote: private rustdoc_host = yes
196 rustdoc-quote: private rustc_target_flags = $(quote-flags)
197 rustdoc-quote: private skip_flags = $(quote-skip_flags)
198 rustdoc-quote: $(src)/quote/lib.rs rustdoc-clean rustdoc-proc_macro2 FORCE
199 +$(call if_changed,rustdoc)
200
201 rustdoc-syn: private rustdoc_host = yes
202 rustdoc-syn: private rustc_target_flags = $(syn-flags)
203 rustdoc-syn: $(src)/syn/lib.rs rustdoc-clean rustdoc-quote FORCE
204 +$(call if_changed,rustdoc)
205
206 rustdoc-macros: private rustdoc_host = yes
207 rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
208 --extern proc_macro --extern proc_macro2 --extern quote --extern syn
209 rustdoc-macros: $(src)/macros/lib.rs rustdoc-clean rustdoc-proc_macro2 \
210 rustdoc-quote rustdoc-syn FORCE
211 +$(call if_changed,rustdoc)
212
213 # Starting with Rust 1.82.0, skipping `-Wrustdoc::unescaped_backticks` should
214 # not be needed -- see https://github.com/rust-lang/rust/pull/128307.
215 rustdoc-core: private skip_flags = $(core-skip_flags)
216 rustdoc-core: private rustc_target_flags = $(core-flags)
217 rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs rustdoc-clean FORCE
218 +$(call if_changed,rustdoc)
219
220 # Even if `rustdoc` targets are not kernel objects, they should still be
221 # treated as such so that we pass the same flags. Otherwise, for instance,
222 # `rustdoc` will complain about missing sanitizer flags causing an ABI mismatch.
223 rustdoc-compiler_builtins: private is-kernel-object := y
224 rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
225 +$(call if_changed,rustdoc)
226
227 rustdoc-ffi: private is-kernel-object := y
228 rustdoc-ffi: $(src)/ffi.rs rustdoc-core FORCE
229 +$(call if_changed,rustdoc)
230
231 rustdoc-pin_init_internal: private rustdoc_host = yes
232 rustdoc-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
233 --extern proc_macro --crate-type proc-macro
234 rustdoc-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
235 rustdoc-clean rustdoc-proc_macro2 rustdoc-quote rustdoc-syn FORCE
236 +$(call if_changed,rustdoc)
237
238 rustdoc-pin_init: private rustdoc_host = yes
239 rustdoc-pin_init: private rustc_target_flags = $(pin_init-flags) \
240 --extern alloc --cfg feature=\"alloc\"
241 rustdoc-pin_init: $(src)/pin-init/src/lib.rs rustdoc-pin_init_internal \
242 rustdoc-macros FORCE
243 +$(call if_changed,rustdoc)
244
245 rustdoc-kernel: private is-kernel-object := y
246 rustdoc-kernel: private rustc_target_flags = --extern ffi --extern pin_init \
247 --extern build_error --extern macros \
248 --extern bindings --extern uapi
249 rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-ffi rustdoc-macros \
250 rustdoc-pin_init rustdoc-compiler_builtins $(obj)/$(libmacros_name) \
251 $(obj)/bindings.o FORCE
252 +$(call if_changed,rustdoc)
253
254 rustdoc-clean: FORCE
255 $(Q)rm -rf $(rustdoc_output)
256
257 quiet_cmd_rustc_test_library = $(RUSTC_OR_CLIPPY_QUIET) TL $<
258 cmd_rustc_test_library = \
259 OBJTREE=$(abspath $(objtree)) \
260 $(RUSTC_OR_CLIPPY) $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
261 @$(objtree)/include/generated/rustc_cfg \
262 --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
263 --out-dir $(objtree)/$(obj)/test --cfg testlib \
264 -L$(objtree)/$(obj)/test \
265 --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
266
267 rusttestlib-build_error: $(src)/build_error.rs FORCE
268 +$(call if_changed,rustc_test_library)
269
270 rusttestlib-ffi: $(src)/ffi.rs FORCE
271 +$(call if_changed,rustc_test_library)
272
273 rusttestlib-proc_macro2: private rustc_target_flags = $(proc_macro2-flags)
274 rusttestlib-proc_macro2: $(src)/proc-macro2/lib.rs FORCE
275 +$(call if_changed,rustc_test_library)
276
277 rusttestlib-quote: private skip_flags = $(quote-skip_flags)
278 rusttestlib-quote: private rustc_target_flags = $(quote-flags)
279 rusttestlib-quote: $(src)/quote/lib.rs rusttestlib-proc_macro2 FORCE
280 +$(call if_changed,rustc_test_library)
281
282 rusttestlib-syn: private rustc_target_flags = $(syn-flags)
283 rusttestlib-syn: $(src)/syn/lib.rs rusttestlib-quote FORCE
284 +$(call if_changed,rustc_test_library)
285
286 rusttestlib-macros: private rustc_target_flags = --extern proc_macro \
287 --extern proc_macro2 --extern quote --extern syn
288 rusttestlib-macros: private rustc_test_library_proc = yes
289 rusttestlib-macros: $(src)/macros/lib.rs \
290 rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
291 +$(call if_changed,rustc_test_library)
292
293 rusttestlib-pin_init_internal: private rustc_target_flags = $(pin_init_internal-flags) \
294 --extern proc_macro
295 rusttestlib-pin_init_internal: private rustc_test_library_proc = yes
296 rusttestlib-pin_init_internal: $(src)/pin-init/internal/src/lib.rs \
297 rusttestlib-proc_macro2 rusttestlib-quote rusttestlib-syn FORCE
298 +$(call if_changed,rustc_test_library)
299
300 rusttestlib-pin_init: private rustc_target_flags = $(pin_init-flags)
301 rusttestlib-pin_init: $(src)/pin-init/src/lib.rs rusttestlib-macros \
302 rusttestlib-pin_init_internal $(obj)/$(libpin_init_internal_name) FORCE
303 +$(call if_changed,rustc_test_library)
304
305 rusttestlib-kernel: private rustc_target_flags = --extern ffi \
306 --extern build_error --extern macros --extern pin_init \
307 --extern bindings --extern uapi
308 rusttestlib-kernel: $(src)/kernel/lib.rs rusttestlib-bindings rusttestlib-uapi \
309 rusttestlib-build_error rusttestlib-pin_init $(obj)/$(libmacros_name) \
310 $(obj)/bindings.o FORCE
311 +$(call if_changed,rustc_test_library)
312
313 rusttestlib-bindings: private rustc_target_flags = --extern ffi --extern pin_init
314 rusttestlib-bindings: $(src)/bindings/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
315 +$(call if_changed,rustc_test_library)
316
317 rusttestlib-uapi: private rustc_target_flags = --extern ffi --extern pin_init
318 rusttestlib-uapi: $(src)/uapi/lib.rs rusttestlib-ffi rusttestlib-pin_init FORCE
319 +$(call if_changed,rustc_test_library)
320
321 quiet_cmd_rustdoc_test = RUSTDOC T $<
322 cmd_rustdoc_test = \
323 RUST_MODFILE=test.rs \
324 OBJTREE=$(abspath $(objtree)) \
325 $(RUSTDOC) --test $(rust_common_flags) \
326 -Zcrate-attr='feature(used_with_arg)' \
327 @$(objtree)/include/generated/rustc_cfg \
328 $(rustc_target_flags) $(rustdoc_test_target_flags) \
329 $(rustdoc_test_quiet) \
330 -L$(objtree)/$(obj)/test --output $(rustdoc_output) \
331 --crate-name $(subst rusttest-,,$@) $<
332
333 quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
334 cmd_rustdoc_test_kernel = \
335 rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
336 mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
337 OBJTREE=$(abspath $(objtree)) \
338 $(RUSTDOC) --test $(filter-out --remap-path-prefix=% --remap-path-scope=%,$(rust_flags)) \
339 -L$(objtree)/$(obj) --extern ffi --extern pin_init \
340 --extern kernel --extern build_error --extern macros \
341 --extern bindings --extern uapi \
342 --no-run --crate-name kernel -Zunstable-options \
343 --sysroot=/dev/null \
344 $(doctests_modifiers_workaround) \
345 --test-builder $(objtree)/scripts/rustdoc_test_builder \
346 $< $(rustdoc_test_kernel_quiet); \
347 $(objtree)/scripts/rustdoc_test_gen
348
349 %/doctests_kernel_generated.rs %/doctests_kernel_generated_kunit.c: \
350 $(src)/kernel/lib.rs $(obj)/kernel.o \
351 $(objtree)/scripts/rustdoc_test_builder \
352 $(objtree)/scripts/rustdoc_test_gen FORCE
353 +$(call if_changed,rustdoc_test_kernel)
354
355 # We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
356 # so for the moment we skip `-Cpanic=abort`.
357 quiet_cmd_rustc_test = $(RUSTC_OR_CLIPPY_QUIET) T $<
358 cmd_rustc_test = \
359 OBJTREE=$(abspath $(objtree)) \
360 $(RUSTC_OR_CLIPPY) --test $(rust_common_flags) \
361 @$(objtree)/include/generated/rustc_cfg \
362 $(rustc_target_flags) --out-dir $(objtree)/$(obj)/test \
363 -L$(objtree)/$(obj)/test \
364 --crate-name $(subst rusttest-,,$@) $<; \
365 $(objtree)/$(obj)/test/$(subst rusttest-,,$@) $(rust_test_quiet) \
366 $(rustc_test_run_flags)
367
368 rusttest: rusttest-macros
369
370 rusttest-macros: private rustc_target_flags = --extern proc_macro \
371 --extern macros --extern kernel --extern pin_init \
372 --extern proc_macro2 --extern quote --extern syn
373 rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
374 rusttest-macros: $(src)/macros/lib.rs \
375 rusttestlib-macros rusttestlib-kernel rusttestlib-pin_init FORCE
376 +$(call if_changed,rustc_test)
377 +$(call if_changed,rustdoc_test)
378
379 ifdef CONFIG_CC_IS_CLANG
380 bindgen_c_flags = $(c_flags)
381 else
382 # bindgen relies on libclang to parse C. Ideally, bindgen would support a GCC
383 # plugin backend and/or the Clang driver would be perfectly compatible with GCC.
384 #
385 # For the moment, here we are tweaking the flags on the fly. This is a hack,
386 # and some kernel configurations may not work (e.g. `GCC_PLUGIN_RANDSTRUCT`
387 # if we end up using one of those structs).
388 bindgen_skip_c_flags := -mno-fp-ret-in-387 -mpreferred-stack-boundary=% \
389 -mskip-rax-setup -mgeneral-regs-only -msign-return-address=% \
390 -mindirect-branch=thunk-extern -mindirect-branch-register \
391 -mfunction-return=thunk-extern -mrecord-mcount -mabi=lp64 \
392 -mindirect-branch-cs-prefix -mstack-protector-guard% -mtraceback=no \
393 -mno-pointers-to-nested-functions -mno-string \
394 -mno-strict-align -mstrict-align -mdirect-extern-access \
395 -mexplicit-relocs -mno-check-zero-division \
396 -fconserve-stack -falign-jumps=% -falign-loops=% \
397 -femit-struct-debug-baseonly -fno-ipa-cp-clone -fno-ipa-sra \
398 -fno-partial-inlining -fplugin-arg-arm_ssp_per_task_plugin-% \
399 -fno-reorder-blocks -fno-allow-store-data-races -fasan-shadow-offset=% \
400 -fzero-call-used-regs=% -fno-stack-clash-protection \
401 -fno-inline-functions-called-once -fsanitize=bounds-strict \
402 -fstrict-flex-arrays=% -fmin-function-alignment=% \
403 -fzero-init-padding-bits=% -mno-fdpic \
404 -fdiagnostics-show-context -fdiagnostics-show-context=% \
405 --param=% --param asan-% -fno-isolate-erroneous-paths-dereference
406
407 # Derived from `scripts/Makefile.clang`.
408 BINDGEN_TARGET_x86 := x86_64-linux-gnu
409 BINDGEN_TARGET_arm64 := aarch64-linux-gnu
410 BINDGEN_TARGET_arm := arm-linux-gnueabi
411 BINDGEN_TARGET_loongarch := loongarch64-linux-gnusf
412 BINDGEN_TARGET_um := $(BINDGEN_TARGET_$(SUBARCH))
413 BINDGEN_TARGET := $(BINDGEN_TARGET_$(SRCARCH))
414
415 # All warnings are inhibited since GCC builds are very experimental,
416 # many GCC warnings are not supported by Clang, they may only appear in
417 # some configurations, with new GCC versions, etc.
418 bindgen_extra_c_flags = -w --target=$(BINDGEN_TARGET)
419
420 # Auto variable zero-initialization requires an additional special option with
421 # clang that is going to be removed sometime in the future (likely in
422 # clang-18), so make sure to pass this option only if clang supports it
423 # (libclang major version < 16).
424 #
425 # https://github.com/llvm/llvm-project/issues/44842
426 # https://github.com/llvm/llvm-project/blob/llvmorg-16.0.0-rc2/clang/docs/ReleaseNotes.rst#deprecated-compiler-flags
427 ifdef CONFIG_INIT_STACK_ALL_ZERO
428 libclang_maj_ver=$(shell $(BINDGEN) $(srctree)/scripts/rust_is_available_bindgen_libclang.h 2>&1 | sed -ne 's/.*clang version \([0-9]*\).*/\1/p')
429 ifeq ($(shell expr $(libclang_maj_ver) \< 16), 1)
430 bindgen_extra_c_flags += -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang
431 endif
432 endif
433
434 bindgen_c_flags = $(filter-out $(bindgen_skip_c_flags), $(c_flags)) \
435 $(bindgen_extra_c_flags)
436 endif
437
438 ifdef CONFIG_LTO
439 bindgen_c_flags_lto = $(filter-out $(CC_FLAGS_LTO), $(bindgen_c_flags))
440 else
441 bindgen_c_flags_lto = $(bindgen_c_flags)
442 endif
443
444 # `-fno-builtin` is passed to avoid `bindgen` from using `clang` builtin
445 # prototypes for functions like `memcpy` -- if this flag is not passed,
446 # `bindgen`-generated prototypes use `c_ulong` or `c_uint` depending on
447 # architecture instead of generating `usize`.
448 bindgen_c_flags_final = $(bindgen_c_flags_lto) -fno-builtin -D__BINDGEN__
449
450 # Each `bindgen` release may upgrade the list of Rust target versions. By
451 # default, the highest stable release in their list is used. Thus we need to set
452 # a `--rust-target` to avoid future `bindgen` releases emitting code that
453 # `rustc` may not understand. On top of that, `bindgen` does not support passing
454 # an unknown Rust target version.
455 #
456 # Therefore, the Rust target for `bindgen` can be only as high as the minimum
457 # Rust version the kernel supports and only as high as the greatest stable Rust
458 # target supported by the minimum `bindgen` version the kernel supports (that
459 # is, if we do not test the actual `rustc`/`bindgen` versions running).
460 #
461 # Starting with `bindgen` 0.71.0, we will be able to set any future Rust version
462 # instead, i.e. we will be able to set here our minimum supported Rust version.
463 quiet_cmd_bindgen = BINDGEN $@
464 cmd_bindgen = \
465 $(BINDGEN) $< $(bindgen_target_flags) --rust-target 1.68 \
466 --use-core --with-derive-default --ctypes-prefix ffi --no-layout-tests \
467 --no-debug '.*' --enable-function-attribute-detection \
468 -o $@ -- $(bindgen_c_flags_final) -DMODULE \
469 $(bindgen_target_cflags) $(bindgen_target_extra)
470
471 $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
472 $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
473 $(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \
474 sed -Ei 's/pub const RUST_CONST_HELPER_([a-zA-Z0-9_]*)/pub const \1/g' $@
475 $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
476 $(src)/bindgen_parameters FORCE
477 $(call if_changed_dep,bindgen)
478
479 $(obj)/uapi/uapi_generated.rs: private bindgen_target_flags = \
480 $(shell grep -Ev '^#|^$$' $(src)/bindgen_parameters)
481 $(obj)/uapi/uapi_generated.rs: $(src)/uapi/uapi_helper.h \
482 $(src)/bindgen_parameters FORCE
483 $(call if_changed_dep,bindgen)
484
485 # See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
486 # with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
487 # given it is `libclang`; but for consistency, future Clang changes and/or
488 # a potential future GCC backend for `bindgen`, we disable it too.
489 $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
490 --blocklist-type '.*' --allowlist-var '' \
491 --allowlist-function 'rust_helper_.*'
492 $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
493 -I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
494 $(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
495 sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
496 $(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers/helpers.c FORCE
497 $(call if_changed_dep,bindgen)
498
499 rust_exports = $(NM) -p --defined-only $(1) | awk '$$2~/(T|R|D|B)/ && $$3!~/__(pfx|cfi|odr_asan)/ { printf $(2),$$3 }'
500
501 quiet_cmd_exports = EXPORTS $@
502 cmd_exports = \
503 $(call rust_exports,$<,"EXPORT_SYMBOL_RUST_GPL(%s);\n") > $@
504
505 $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
506 $(call if_changed,exports)
507
508 # Even though Rust kernel modules should never use the bindings directly,
509 # symbols from the `bindings` crate and the C helpers need to be exported
510 # because Rust generics and inlined functions may not get their code generated
511 # in the crate where they are defined. Other helpers, called from non-inline
512 # functions, may not be exported, in principle. However, in general, the Rust
513 # compiler does not guarantee codegen will be performed for a non-inline
514 # function either. Therefore, we export all symbols from helpers and bindings.
515 # In the future, this may be revisited to reduce the number of exports after
516 # the compiler is informed about the places codegen is required.
517 $(obj)/exports_helpers_generated.h: $(obj)/helpers/helpers.o FORCE
518 $(call if_changed,exports)
519
520 $(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
521 $(call if_changed,exports)
522
523 $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
524 $(call if_changed,exports)
525
526 quiet_cmd_rustc_procmacrolibrary = $(RUSTC_OR_CLIPPY_QUIET) PL $@
527 cmd_rustc_procmacrolibrary = \
528 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
529 $(filter-out $(skip_flags),$(rust_common_flags) $(rustc_target_flags)) \
530 --emit=dep-info=$(depfile) --emit=link=$@ --crate-type rlib -O \
531 --out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
532 --crate-name $(patsubst lib%.rlib,%,$(notdir $@)) $<
533
534 $(obj)/libproc_macro2.rlib: private skip_clippy = 1
535 $(obj)/libproc_macro2.rlib: private rustc_target_flags = $(proc_macro2-flags)
536 $(obj)/libproc_macro2.rlib: $(src)/proc-macro2/lib.rs FORCE
537 +$(call if_changed_dep,rustc_procmacrolibrary)
538
539 $(obj)/libquote.rlib: private skip_clippy = 1
540 $(obj)/libquote.rlib: private skip_flags = $(quote-skip_flags)
541 $(obj)/libquote.rlib: private rustc_target_flags = $(quote-flags)
542 $(obj)/libquote.rlib: $(src)/quote/lib.rs $(obj)/libproc_macro2.rlib FORCE
543 +$(call if_changed_dep,rustc_procmacrolibrary)
544
545 $(obj)/libsyn.rlib: private skip_clippy = 1
546 $(obj)/libsyn.rlib: private rustc_target_flags = $(syn-flags)
547 $(obj)/libsyn.rlib: $(src)/syn/lib.rs $(obj)/libquote.rlib FORCE
548 +$(call if_changed_dep,rustc_procmacrolibrary)
549
550 quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
551 cmd_rustc_procmacro = \
552 $(RUSTC_OR_CLIPPY) $(rust_common_flags) $(rustc_target_flags) \
553 -Clinker-flavor=gcc -Clinker=$(HOSTCC) \
554 -Clink-args='$(call escsq,$(KBUILD_PROCMACROLDFLAGS))' \
555 --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
556 --crate-type proc-macro -L$(objtree)/$(obj) \
557 --crate-name $(patsubst lib%.$(libmacros_extension),%,$(notdir $@)) \
558 @$(objtree)/include/generated/rustc_cfg $<
559
560 # Procedural macros can only be used with the `rustc` that compiled it.
561 $(obj)/$(libmacros_name): private rustc_target_flags = \
562 --extern proc_macro2 --extern quote --extern syn
563 $(obj)/$(libmacros_name): $(src)/macros/lib.rs $(obj)/libproc_macro2.rlib \
564 $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
565 +$(call if_changed_dep,rustc_procmacro)
566
567 $(obj)/$(libpin_init_internal_name): private rustc_target_flags = $(pin_init_internal-flags)
568 $(obj)/$(libpin_init_internal_name): $(src)/pin-init/internal/src/lib.rs \
569 $(obj)/libproc_macro2.rlib $(obj)/libquote.rlib $(obj)/libsyn.rlib FORCE
570 +$(call if_changed_dep,rustc_procmacro)
571
572 # `rustc` requires `-Zunstable-options` to use custom target specifications
573 # since Rust 1.95.0 (https://github.com/rust-lang/rust/pull/151534).
574 quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
575 cmd_rustc_library = \
576 OBJTREE=$(abspath $(objtree)) \
577 $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
578 $(filter-out $(skip_flags),$(rust_flags)) $(rustc_target_flags) \
579 --emit=dep-info=$(depfile) --emit=obj=$@ \
580 --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
581 --crate-type rlib -L$(objtree)/$(obj) \
582 --crate-name $(patsubst %.o,%,$(notdir $@)) $< \
583 --sysroot=/dev/null \
584 -Zunstable-options \
585 $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) \
586 $(cmd_objtool)
587
588 rust-analyzer:
589 $(Q)MAKEFLAGS= $(srctree)/scripts/generate_rust_analyzer.py \
590 --cfgs='core=$(core-cfgs)' $(core-edition) \
591 --cfgs='proc_macro2=$(proc_macro2-cfgs)' \
592 --cfgs='quote=$(quote-cfgs)' \
593 --cfgs='syn=$(syn-cfgs)' \
594 $(realpath $(srctree)) $(realpath $(objtree)) \
595 $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \
596 > rust-project.json
597
598 redirect-intrinsics = \
599 __addsf3 __eqsf2 __extendsfdf2 __gesf2 __lesf2 __ltsf2 __mulsf3 __nesf2 __truncdfsf2 __unordsf2 \
600 __adddf3 __eqdf2 __ledf2 __ltdf2 __muldf3 __unorddf2 \
601 __muloti4 __multi3 \
602 __udivmodti4 __udivti3 __umodti3
603
604 ifdef CONFIG_ARM
605 # Add eabi initrinsics for ARM 32-bit
606 redirect-intrinsics += \
607 __aeabi_fadd __aeabi_fmul __aeabi_fcmpeq __aeabi_fcmple __aeabi_fcmplt __aeabi_fcmpun \
608 __aeabi_dadd __aeabi_dmul __aeabi_dcmple __aeabi_dcmplt __aeabi_dcmpun \
609 __aeabi_uldivmod
610 endif
611 ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
612 # These intrinsics are defined for ARM64 and RISCV64
613 redirect-intrinsics += \
614 __ashrti3 \
615 __ashlti3 __lshrti3
616 endif
617
618 ifdef CONFIG_MODVERSIONS
619 cmd_gendwarfksyms = $(if $(skip_gendwarfksyms),, \
620 $(call rust_exports,$@,"%s\n") | \
621 scripts/gendwarfksyms/gendwarfksyms \
622 $(if $(KBUILD_GENDWARFKSYMS_STABLE), --stable) \
623 $(if $(KBUILD_SYMTYPES), --symtypes $(@:.o=.symtypes),) \
624 $@ >> $(dot-target).cmd)
625 endif
626
627 define rule_rustc_library
628 $(call cmd_and_fixdep,rustc_library)
629 $(call cmd,gen_objtooldep)
630 $(call cmd,gendwarfksyms)
631 endef
632
633 define rule_rust_cc_library
634 $(call if_changed_rule,cc_o_c)
635 $(call cmd,force_checksrc)
636 $(call cmd,gendwarfksyms)
637 endef
638
639 # helpers.o uses the same export mechanism as Rust libraries, so ensure symbol
640 # versions are calculated for the helpers too.
641 $(obj)/helpers/helpers.o: $(src)/helpers/helpers.c $(recordmcount_source) FORCE
642 +$(call if_changed_rule,rust_cc_library)
643
644 # Disable symbol versioning for exports.o to avoid conflicts with the actual
645 # symbol versions generated from Rust objects.
646 $(obj)/exports.o: private skip_gendwarfksyms = 1
647
648 $(obj)/core.o: private skip_clippy = 1
649 $(obj)/core.o: private skip_flags = $(core-skip_flags)
650 $(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
651 $(obj)/core.o: private rustc_target_flags = $(core-flags)
652 $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs \
653 $(wildcard $(objtree)/include/config/RUSTC_VERSION_TEXT) FORCE
654 +$(call if_changed_rule,rustc_library)
655 ifneq ($(or $(CONFIG_X86_64),$(CONFIG_X86_32)),)
656 $(obj)/core.o: scripts/target.json
657 endif
658 KCOV_INSTRUMENT_core.o := n
659
660 $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1
661 $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
662 $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
663 +$(call if_changed_rule,rustc_library)
664
665 $(obj)/pin_init.o: private skip_gendwarfksyms = 1
666 $(obj)/pin_init.o: private rustc_target_flags = $(pin_init-flags)
667 $(obj)/pin_init.o: $(src)/pin-init/src/lib.rs $(obj)/compiler_builtins.o \
668 $(obj)/$(libpin_init_internal_name) $(obj)/$(libmacros_name) FORCE
669 +$(call if_changed_rule,rustc_library)
670
671 # Even if normally `build_error` is not a kernel object, it should still be
672 # treated as such so that we pass the same flags. Otherwise, for instance,
673 # `rustc` will complain about missing sanitizer flags causing an ABI mismatch.
674 $(obj)/build_error.o: private is-kernel-object := y
675 $(obj)/build_error.o: private skip_gendwarfksyms = 1
676 $(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
677 +$(call if_changed_rule,rustc_library)
678
679 $(obj)/ffi.o: private skip_gendwarfksyms = 1
680 $(obj)/ffi.o: $(src)/ffi.rs $(obj)/compiler_builtins.o FORCE
681 +$(call if_changed_rule,rustc_library)
682
683 $(obj)/bindings.o: private rustc_target_flags = --extern ffi --extern pin_init
684 $(obj)/bindings.o: $(src)/bindings/lib.rs \
685 $(obj)/ffi.o \
686 $(obj)/pin_init.o \
687 $(obj)/bindings/bindings_generated.rs \
688 $(obj)/bindings/bindings_helpers_generated.rs FORCE
689 +$(call if_changed_rule,rustc_library)
690
691 $(obj)/uapi.o: private rustc_target_flags = --extern ffi --extern pin_init
692 $(obj)/uapi.o: private skip_gendwarfksyms = 1
693 $(obj)/uapi.o: $(src)/uapi/lib.rs \
694 $(obj)/ffi.o \
695 $(obj)/pin_init.o \
696 $(obj)/uapi/uapi_generated.rs FORCE
697 +$(call if_changed_rule,rustc_library)
698
699 $(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
700 --extern build_error --extern macros --extern bindings --extern uapi
701 $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/build_error.o $(obj)/pin_init.o \
702 $(obj)/$(libmacros_name) $(obj)/bindings.o $(obj)/uapi.o FORCE
703 +$(call if_changed_rule,rustc_library)
704
705 ifdef CONFIG_JUMP_LABEL
706 $(obj)/kernel.o: $(obj)/kernel/generated_arch_static_branch_asm.rs
707 endif
708 ifndef CONFIG_UML
709 ifdef CONFIG_BUG
710 $(obj)/kernel.o: $(obj)/kernel/generated_arch_warn_asm.rs $(obj)/kernel/generated_arch_reachable_asm.rs
711 endif
712 endif
713
714 endif # CONFIG_RUST