@@ -17,18 +17,27 @@ endif
17
17
18
18
obj-$(CONFIG_RUST) += exports.o
19
19
20
- RUSTDOC = rustdoc
20
+ ifeq ($(quiet ) ,silent_)
21
+ cargo_quiet =-q
22
+ rust_test_quiet =-q
23
+ rustdoc_test_quiet =--test-args -q
24
+ else ifeq ($(quiet),quiet_)
25
+ rust_test_quiet =-q
26
+ rustdoc_test_quiet =--test-args -q
27
+ else
28
+ cargo_quiet =--verbose
29
+ endif
21
30
22
- quiet_cmd_rustdoc = RUSTDOC $(if $(filter --test, $( rustdoc_target_flags ) ) ,T, ) $( if $( rustdoc_host ) ,H, ) $<
31
+ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host ) ,H, ) $<
23
32
cmd_rustdoc = \
24
33
RUST_BINDINGS_FILE=$(abspath $(objtree ) /rust/bindings_generated.rs) \
25
34
$(RUSTDOC ) $(if $(rustdoc_host ) ,,$(rustc_cross_flags ) ) \
26
- $(filter-out -- emit=% , $(rustc_flags ) ) $( rustc_target_flags ) $( rustdoc_target_flags ) \
27
- -L $( objtree ) /rust/ $( if $( filter --test, $( rustdoc_target_flags ) ) ,test/) \
28
- --output $(objtree ) /rust/doc --crate-name $(subst rusttest-,, $( subst rustdoc-,,$@ ) ) \
29
- -Fmissing-docs @$(objtree ) /include/generated/rustc_cfg $<
35
+ $(filter-out -Cpanic=abort, $( filter-out -- emit=% , $(rustc_flags ) ) ) \
36
+ $( rustc_target_flags ) -L $( objtree ) /rust \
37
+ --output $(objtree ) /rust/doc --crate-name $(subst rustdoc-,,$@ ) \
38
+ @$(objtree ) /include/generated/rustc_cfg $<
30
39
31
- rustdoc : rustdoc-macros rustdoc-compiler_builtins rustdoc-kernel
40
+ rustdoc : rustdoc-macros rustdoc-compiler_builtins rustdoc-alloc rustdoc- kernel
32
41
33
42
rustdoc-macros : private rustdoc_host = yes
34
43
rustdoc-macros : private rustc_target_flags = --crate-type proc-macro \
@@ -39,6 +48,15 @@ rustdoc-macros: $(srctree)/rust/macros/lib.rs FORCE
39
48
rustdoc-compiler_builtins : $(srctree ) /rust/compiler_builtins.rs FORCE
40
49
$(call if_changed,rustdoc)
41
50
51
+ # We need to allow `broken_intra_doc_links` because some
52
+ # `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
53
+ # functions. Ideally `rustdoc` would have a way to distinguish broken links
54
+ # due to things that are "configured out" vs. entirely non-existing ones.
55
+ rustdoc-alloc : private rustc_target_flags = \
56
+ -Abroken_intra_doc_links
57
+ rustdoc-alloc : $(srctree ) /rust/alloc/lib.rs FORCE
58
+ $(call if_changed,rustdoc)
59
+
42
60
rustdoc-kernel : private rustc_target_flags = --extern alloc \
43
61
--extern build_error \
44
62
--extern macros=$(objtree)/rust/libmacros.so
@@ -49,50 +67,108 @@ rustdoc-kernel: $(srctree)/rust/kernel/lib.rs rustdoc-macros \
49
67
quiet_cmd_rustc_test_library = RUSTC TL $<
50
68
cmd_rustc_test_library = \
51
69
RUST_BINDINGS_FILE=$(abspath $(objtree ) /rust/bindings_generated.rs) \
52
- $(RUSTC ) $(filter-out -Cpanic=abort, $(filter-out --emit=% , $(rustc_flags ) ) ) \
70
+ $(RUSTC ) $(filter-out --sysroot= % , $( filter-out - Cpanic=abort, $(filter-out --emit=% , $(rustc_flags ) ) ) ) \
53
71
$(rustc_target_flags ) --crate-type $(if $(rustc_test_library_proc ) ,proc-macro,rlib) \
54
72
--out-dir $(objtree ) /rust/test/ --cfg testlib \
73
+ --sysroot $(objtree ) /rust/test/sysroot \
55
74
-L $(objtree ) /rust/test/ --crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@ ) ) $<
56
75
57
- rusttestlib-build_error : $(srctree ) /rust/build_error.rs FORCE
76
+ rusttestlib-build_error : $(srctree ) /rust/build_error.rs rusttest-prepare FORCE
58
77
$(call if_changed,rustc_test_library)
59
78
60
79
rusttestlib-macros : private rustc_target_flags = --extern proc_macro
61
80
rusttestlib-macros : private rustc_test_library_proc = yes
62
- rusttestlib-macros : $(srctree ) /rust/macros/lib.rs FORCE
81
+ rusttestlib-macros : $(srctree ) /rust/macros/lib.rs rusttest-prepare FORCE
63
82
$(call if_changed,rustc_test_library)
64
83
84
+ quiet_cmd_rustdoc_test = RUSTDOC T $<
85
+ cmd_rustdoc_test = \
86
+ RUST_BINDINGS_FILE=$(abspath $(objtree ) /rust/bindings_generated.rs) \
87
+ $(RUSTDOC ) --test $(filter-out --sysroot=% , $(filter-out -Cpanic=abort, $(filter-out --emit=% , $(rustc_flags ) ) ) ) \
88
+ $(rustc_target_flags ) $(rustdoc_test_target_flags ) \
89
+ --sysroot $(objtree ) /rust/test/sysroot $(rustdoc_test_quiet ) \
90
+ -L $(objtree ) /rust/test \
91
+ --output $(objtree ) /rust/doc --crate-name $(subst rusttest-,,$@ ) \
92
+ @$(objtree ) /include/generated/rustc_cfg $<
93
+
65
94
# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
66
95
# so for the moment we skip `-Cpanic=abort`.
67
96
quiet_cmd_rustc_test = RUSTC T $<
68
97
cmd_rustc_test = \
69
98
RUST_BINDINGS_FILE=$(abspath $(objtree ) /rust/bindings_generated.rs) \
70
- $(RUSTC ) --test $(filter-out -Cpanic=abort, $(filter-out --emit=% , $(rustc_flags ) ) ) \
99
+ $(RUSTC ) --test $(filter-out --sysroot= % , $( filter-out - Cpanic=abort, $(filter-out --emit=% , $(rustc_flags ) ) ) ) \
71
100
$(rustc_target_flags ) --out-dir $(objtree ) /rust/test \
101
+ --sysroot $(objtree ) /rust/test/sysroot \
72
102
-L $(objtree ) /rust/test/ --crate-name $(subst rusttest-,,$@ ) $< ; \
73
- $(objtree ) /rust/test/$(subst rusttest-,,$@ ) $(rustc_test_run_flags )
103
+ $(objtree ) /rust/test/$(subst rusttest-,,$@ ) $(rust_test_quiet ) \
104
+ $(rustc_test_run_flags )
74
105
75
106
rusttest : rusttest-macros rusttest-kernel
76
107
108
+ # This prepares a custom sysroot with our custom `alloc` instead of
109
+ # the standard one.
110
+ #
111
+ # This requires several hacks:
112
+ # - Unlike `core` and `alloc`, `std` depends on more than a dozen crates,
113
+ # including third-party crates that need to be downloaded, plus custom
114
+ # `build.rs` steps. Thus hardcoding things here is not maintainable.
115
+ # - `cargo` knows how to build the standard library, but it is an unstable
116
+ # feature so far (`-Zbuild-std`).
117
+ # - `cargo` only considers the use case of building the standard library
118
+ # to use it in a given package. Thus we need to create a dummy package
119
+ # and pick the generated libraries from there.
120
+ # - Since we only keep a subset of upstream `alloc` in-tree, we need
121
+ # to recreate it on the fly by putting our sources on top.
122
+ # - The usual ways of modifying the dependency graph in `cargo` do not seem
123
+ # to apply for the `-Zbuild-std` steps, thus we have to mislead it
124
+ # by modifying the sources in the sysroot.
125
+ # - To avoid messing with the user's Rust installation, we create a clone
126
+ # of the sysroot. However, `cargo` ignores `RUSTFLAGS` in the `-Zbuild-std`
127
+ # steps, thus we use a wrapper binary passed via `RUSTC` to pass the flag.
128
+ #
129
+ # In the future, we hope to avoid the whole ordeal by either:
130
+ # - Making the `test` crate not depend on `std` (either improving upstream
131
+ # or having our own custom crate).
132
+ # - Making the tests run in kernel space (requires the previous point).
133
+ # - Making `std` and friends be more like a "normal" crate, so that
134
+ # `-Zbuild-std` and related hacks are not needed.
135
+ quiet_cmd_rustsysroot = RUSTSYSROOT
136
+ cmd_rustsysroot = \
137
+ rm -rf $(objtree ) /rust/test; \
138
+ mkdir -p $(objtree ) /rust/test; \
139
+ cp -a $(rustc_sysroot ) $(objtree ) /rust/test/sysroot; \
140
+ cp -r $(srctree ) /rust/alloc/* \
141
+ $(objtree ) /rust/test/sysroot/lib/rustlib/src/rust/library/alloc/src; \
142
+ echo '\# !/bin/sh' > $(objtree)/rust/test/rustc_sysroot; \
143
+ echo "$(RUSTC) --sysroot=$(abspath $(objtree)/rust/test/sysroot) \"\$$@\"" \
144
+ >> $(objtree)/rust/test/rustc_sysroot; \
145
+ chmod u+x $(objtree)/rust/test/rustc_sysroot; \
146
+ $(CARGO) -q new $(objtree)/rust/test/dummy; \
147
+ RUSTC=$(objtree)/rust/test/rustc_sysroot $(CARGO) $(cargo_quiet) \
148
+ test -Zbuild-std --target $(rustc_host_target) \
149
+ --manifest-path $(objtree)/rust/test/dummy/Cargo.toml; \
150
+ rm $(objtree)/rust/test/sysroot/lib/rustlib/$(rustc_host_target)/lib/*; \
151
+ cp $(objtree)/rust/test/dummy/target/$(rustc_host_target)/debug/deps/* \
152
+ $(objtree)/rust/test/sysroot/lib/rustlib/$(rustc_host_target)/lib
153
+
154
+ rusttest-prepare : FORCE
155
+ $(call if_changed,rustsysroot)
156
+
77
157
rusttest-macros : private rustc_target_flags = --extern proc_macro
78
- rusttest-macros : private rustdoc_host = yes
79
- rusttest-macros : private rustdoc_target_flags = --test --crate-type proc-macro
80
- rusttest-macros : $(srctree ) /rust/macros/lib.rs FORCE
158
+ rusttest-macros : private rustdoc_test_target_flags = --crate-type proc-macro
159
+ rusttest-macros : $(srctree ) /rust/macros/lib.rs rusttest-prepare FORCE
81
160
$(call if_changed,rustc_test)
82
- $(call if_changed,rustdoc )
161
+ $(call if_changed,rustdoc_test )
83
162
84
163
rusttest-kernel : private rustc_target_flags = --extern alloc \
85
- --extern build_error \
86
- --extern macros=$(objtree)/rust/test/libmacros.so
164
+ --extern build_error --extern macros
87
165
rusttest-kernel : private rustc_test_run_flags = \
88
166
--skip bindgen_test_layout_
89
- rusttest-kernel : private rustdoc_host = yes
90
- rusttest-kernel : private rustdoc_target_flags = --test
91
- rusttest-kernel : $(srctree ) /rust/kernel/lib.rs rusttestlib-build_error \
92
- rusttestlib-macros FORCE
167
+ rusttest-kernel : $(srctree ) /rust/kernel/lib.rs rusttest-prepare \
168
+ rusttestlib-build_error rusttestlib-macros FORCE
93
169
$(call if_changed,rustc_test)
94
170
$(call if_changed,rustc_test_library)
95
- $(call if_changed,rustdoc )
171
+ $(call if_changed,rustdoc_test )
96
172
97
173
ifdef CONFIG_CC_IS_CLANG
98
174
bindgen_c_flags = $(c_flags )
@@ -188,6 +264,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
188
264
189
265
# `$(rustc_flags)` is passed in case the user added `--sysroot`.
190
266
rustc_sysroot = $(shell $(RUSTC ) $(rustc_flags ) --print sysroot)
267
+ rustc_host_target = $(shell $(RUSTC ) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
191
268
RUST_LIB_SRC ?= $(rustc_sysroot ) /lib/rustlib/src/rust/library
192
269
193
270
rust-analyzer :
@@ -198,6 +275,11 @@ $(objtree)/rust/compiler_builtins.o: $(srctree)/rust/compiler_builtins.rs \
198
275
$(objtree)/rust/core.o FORCE
199
276
$(call if_changed_dep,rustc_library)
200
277
278
+ $(objtree ) /rust/alloc.o : private skip_clippy = 1
279
+ $(objtree ) /rust/alloc.o : $(srctree ) /rust/alloc/lib.rs \
280
+ $(objtree)/rust/compiler_builtins.o FORCE
281
+ $(call if_changed_dep,rustc_library)
282
+
201
283
$(objtree ) /rust/build_error.o : $(srctree ) /rust/build_error.rs \
202
284
$(objtree)/rust/compiler_builtins.o FORCE
203
285
$(call if_changed_dep,rustc_library)
@@ -216,8 +298,3 @@ $(objtree)/rust/kernel.o: $(srctree)/rust/kernel/lib.rs $(objtree)/rust/alloc.o
216
298
$(objtree ) /rust/core.o : private skip_clippy = 1
217
299
$(objtree ) /rust/core.o : $$(RUST_LIB_SRC ) /core/src/lib.rs FORCE
218
300
$(call if_changed_dep,rustc_library)
219
-
220
- $(objtree ) /rust/alloc.o : private skip_clippy = 1
221
- $(objtree ) /rust/alloc.o : $$(RUST_LIB_SRC ) /alloc/src/lib.rs \
222
- $(objtree)/rust/compiler_builtins.o FORCE
223
- $(call if_changed_dep,rustc_library)
0 commit comments