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