Skip to content

Commit ecab411

Browse files
committed
kbuild: mark rustc (and others) invocations as recursive
`rustc` (like Cargo) may take advantage of the jobserver at any time (e.g. for backend parallelism, or eventually frontend too). In the kernel, we call `rustc` with `-Ccodegen-units=1` (and `-Zthreads` is 1 so far), so we do not expect parallelism. However, in the upcoming Rust 1.76.0, a warning is emitted by `rustc` [1] when it cannot connect to the jobserver it was passed (in many cases, but not all: compiling and `--print sysroot` do, but `--version` does not). And given GNU Make always passes the jobserver in the environment variable (even when a line is deemed non-recursive), `rustc` will end up complaining about it (in particular in Make 4.3 where there is only the simple pipe jobserver style). One solution is to remove the jobserver from `MAKEFLAGS`. However, we can mark the lines with calls to `rustc` (and Cargo) as recursive, which looks simpler. This is being documented as a recommendation in `rustc` [2] and allows us to be ready for the time we may use parallelism inside `rustc` (potentially now, if a user passes `-Zthreads`). Thus do so. Similarly, do the same for `rustdoc` and `cargo` calls. Finally, there is one case that the solution does not cover, which is the `$(shell ...)` call we have. Thus, for that one, set an empty `MAKEFLAGS` environment variable. Link: rust-lang/rust#120515 [1] Acked-by: Masahiro Yamada <[email protected]> Link: rust-lang/rust#121564 [2] Link: https://lore.kernel.org/r/[email protected] [ Reworded to add link to PR documenting the recommendation. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent e944171 commit ecab411

File tree

4 files changed

+31
-31
lines changed

4 files changed

+31
-31
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ prepare0: archprepare
11971197
# All the preparing..
11981198
prepare: prepare0
11991199
ifdef CONFIG_RUST
1200-
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
1200+
+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh
12011201
$(Q)$(MAKE) $(build)=rust
12021202
endif
12031203

@@ -1707,7 +1707,7 @@ $(DOC_TARGETS):
17071707
# "Is Rust available?" target
17081708
PHONY += rustavailable
17091709
rustavailable:
1710-
$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh && echo "Rust is available!"
1710+
+$(Q)$(CONFIG_SHELL) $(srctree)/scripts/rust_is_available.sh && echo "Rust is available!"
17111711

17121712
# Documentation target
17131713
#

rust/Makefile

+24-24
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o
4040
ifdef CONFIG_RUST
4141

4242
# `$(rust_flags)` is passed in case the user added `--sysroot`.
43-
rustc_sysroot := $(shell $(RUSTC) $(rust_flags) --print sysroot)
43+
rustc_sysroot := $(shell MAKEFLAGS= $(RUSTC) $(rust_flags) --print sysroot)
4444
rustc_host_target := $(shell $(RUSTC) --version --verbose | grep -F 'host: ' | cut -d' ' -f2)
4545
RUST_LIB_SRC ?= $(rustc_sysroot)/lib/rustlib/src/rust/library
4646

@@ -108,14 +108,14 @@ rustdoc-macros: private rustdoc_host = yes
108108
rustdoc-macros: private rustc_target_flags = --crate-type proc-macro \
109109
--extern proc_macro
110110
rustdoc-macros: $(src)/macros/lib.rs FORCE
111-
$(call if_changed,rustdoc)
111+
+$(call if_changed,rustdoc)
112112

113113
rustdoc-core: private rustc_target_flags = $(core-cfgs)
114114
rustdoc-core: $(RUST_LIB_SRC)/core/src/lib.rs FORCE
115-
$(call if_changed,rustdoc)
115+
+$(call if_changed,rustdoc)
116116

117117
rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
118-
$(call if_changed,rustdoc)
118+
+$(call if_changed,rustdoc)
119119

120120
# We need to allow `rustdoc::broken_intra_doc_links` because some
121121
# `no_global_oom_handling` functions refer to non-`no_global_oom_handling`
@@ -124,15 +124,15 @@ rustdoc-compiler_builtins: $(src)/compiler_builtins.rs rustdoc-core FORCE
124124
rustdoc-alloc: private rustc_target_flags = $(alloc-cfgs) \
125125
-Arustdoc::broken_intra_doc_links
126126
rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
127-
$(call if_changed,rustdoc)
127+
+$(call if_changed,rustdoc)
128128

129129
rustdoc-kernel: private rustc_target_flags = --extern alloc \
130130
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
131131
--extern bindings --extern uapi
132132
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
133133
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
134134
$(obj)/bindings.o FORCE
135-
$(call if_changed,rustdoc)
135+
+$(call if_changed,rustdoc)
136136

137137
quiet_cmd_rustc_test_library = RUSTC TL $<
138138
cmd_rustc_test_library = \
@@ -146,18 +146,18 @@ quiet_cmd_rustc_test_library = RUSTC TL $<
146146
--crate-name $(subst rusttest-,,$(subst rusttestlib-,,$@)) $<
147147

148148
rusttestlib-build_error: $(src)/build_error.rs rusttest-prepare FORCE
149-
$(call if_changed,rustc_test_library)
149+
+$(call if_changed,rustc_test_library)
150150

151151
rusttestlib-macros: private rustc_target_flags = --extern proc_macro
152152
rusttestlib-macros: private rustc_test_library_proc = yes
153153
rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
154-
$(call if_changed,rustc_test_library)
154+
+$(call if_changed,rustc_test_library)
155155

156156
rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
157-
$(call if_changed,rustc_test_library)
157+
+$(call if_changed,rustc_test_library)
158158

159159
rusttestlib-uapi: $(src)/uapi/lib.rs rusttest-prepare FORCE
160-
$(call if_changed,rustc_test_library)
160+
+$(call if_changed,rustc_test_library)
161161

162162
quiet_cmd_rustdoc_test = RUSTDOC T $<
163163
cmd_rustdoc_test = \
@@ -189,7 +189,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
189189
$(src)/kernel/lib.rs $(obj)/kernel.o \
190190
$(objtree)/scripts/rustdoc_test_builder \
191191
$(objtree)/scripts/rustdoc_test_gen FORCE
192-
$(call if_changed,rustdoc_test_kernel)
192+
+$(call if_changed,rustdoc_test_kernel)
193193

194194
# We cannot use `-Zpanic-abort-tests` because some tests are dynamic,
195195
# so for the moment we skip `-Cpanic=abort`.
@@ -254,21 +254,21 @@ quiet_cmd_rustsysroot = RUSTSYSROOT
254254
$(objtree)/$(obj)/test/sysroot/lib/rustlib/$(rustc_host_target)/lib
255255

256256
rusttest-prepare: FORCE
257-
$(call if_changed,rustsysroot)
257+
+$(call if_changed,rustsysroot)
258258

259259
rusttest-macros: private rustc_target_flags = --extern proc_macro
260260
rusttest-macros: private rustdoc_test_target_flags = --crate-type proc-macro
261261
rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
262-
$(call if_changed,rustc_test)
263-
$(call if_changed,rustdoc_test)
262+
+$(call if_changed,rustc_test)
263+
+$(call if_changed,rustdoc_test)
264264

265265
rusttest-kernel: private rustc_target_flags = --extern alloc \
266266
--extern build_error --extern macros --extern bindings --extern uapi
267267
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
268268
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings \
269269
rusttestlib-uapi FORCE
270-
$(call if_changed,rustc_test)
271-
$(call if_changed,rustc_test_library)
270+
+$(call if_changed,rustc_test)
271+
+$(call if_changed,rustc_test_library)
272272

273273
ifdef CONFIG_CC_IS_CLANG
274274
bindgen_c_flags = $(c_flags)
@@ -396,7 +396,7 @@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
396396
# Therefore, to get `libmacros.so` automatically recompiled when the compiler
397397
# version changes, we add `core.o` as a dependency (even if it is not needed).
398398
$(obj)/libmacros.so: $(src)/macros/lib.rs $(obj)/core.o FORCE
399-
$(call if_changed_dep,rustc_procmacro)
399+
+$(call if_changed_dep,rustc_procmacro)
400400

401401
quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L $@
402402
cmd_rustc_library = \
@@ -435,36 +435,36 @@ $(obj)/core.o: private skip_flags = -Dunreachable_pub
435435
$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
436436
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)
437437
$(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs scripts/target.json FORCE
438-
$(call if_changed_dep,rustc_library)
438+
+$(call if_changed_dep,rustc_library)
439439

440440
$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'
441441
$(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE
442-
$(call if_changed_dep,rustc_library)
442+
+$(call if_changed_dep,rustc_library)
443443

444444
$(obj)/alloc.o: private skip_clippy = 1
445445
$(obj)/alloc.o: private skip_flags = -Dunreachable_pub
446446
$(obj)/alloc.o: private rustc_target_flags = $(alloc-cfgs)
447447
$(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
448-
$(call if_changed_dep,rustc_library)
448+
+$(call if_changed_dep,rustc_library)
449449

450450
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
451-
$(call if_changed_dep,rustc_library)
451+
+$(call if_changed_dep,rustc_library)
452452

453453
$(obj)/bindings.o: $(src)/bindings/lib.rs \
454454
$(obj)/compiler_builtins.o \
455455
$(obj)/bindings/bindings_generated.rs \
456456
$(obj)/bindings/bindings_helpers_generated.rs FORCE
457-
$(call if_changed_dep,rustc_library)
457+
+$(call if_changed_dep,rustc_library)
458458

459459
$(obj)/uapi.o: $(src)/uapi/lib.rs \
460460
$(obj)/compiler_builtins.o \
461461
$(obj)/uapi/uapi_generated.rs FORCE
462-
$(call if_changed_dep,rustc_library)
462+
+$(call if_changed_dep,rustc_library)
463463

464464
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
465465
--extern build_error --extern macros --extern bindings --extern uapi
466466
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
467467
$(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE
468-
$(call if_changed_dep,rustc_library)
468+
+$(call if_changed_dep,rustc_library)
469469

470470
endif # CONFIG_RUST

scripts/Makefile.build

+4-4
Original file line numberDiff line numberDiff line change
@@ -290,27 +290,27 @@ quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
290290
cmd_rustc_o_rs = $(rust_common_cmd) --emit=obj=$@ $<
291291

292292
$(obj)/%.o: $(src)/%.rs FORCE
293-
$(call if_changed_dep,rustc_o_rs)
293+
+$(call if_changed_dep,rustc_o_rs)
294294

295295
quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
296296
cmd_rustc_rsi_rs = \
297297
$(rust_common_cmd) -Zunpretty=expanded $< >$@; \
298298
command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@
299299

300300
$(obj)/%.rsi: $(src)/%.rs FORCE
301-
$(call if_changed_dep,rustc_rsi_rs)
301+
+$(call if_changed_dep,rustc_rsi_rs)
302302

303303
quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
304304
cmd_rustc_s_rs = $(rust_common_cmd) --emit=asm=$@ $<
305305

306306
$(obj)/%.s: $(src)/%.rs FORCE
307-
$(call if_changed_dep,rustc_s_rs)
307+
+$(call if_changed_dep,rustc_s_rs)
308308

309309
quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
310310
cmd_rustc_ll_rs = $(rust_common_cmd) --emit=llvm-ir=$@ $<
311311

312312
$(obj)/%.ll: $(src)/%.rs FORCE
313-
$(call if_changed_dep,rustc_ll_rs)
313+
+$(call if_changed_dep,rustc_ll_rs)
314314

315315
# Compile assembler sources (.S)
316316
# ---------------------------------------------------------------------------

scripts/Makefile.host

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ quiet_cmd_host-rust = HOSTRUSTC $@
156156
cmd_host-rust = \
157157
$(HOSTRUSTC) $(hostrust_flags) --emit=link=$@ $<
158158
$(host-rust): $(obj)/%: $(src)/%.rs FORCE
159-
$(call if_changed_dep,host-rust)
159+
+$(call if_changed_dep,host-rust)
160160

161161
targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
162162
$(host-cxxmulti) $(host-cxxobjs) $(host-rust)

0 commit comments

Comments
 (0)