Debian Codegraph @

rust/Makefile

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