Skip to content

Commit e64af88

Browse files
masahir0ySasha Levin
authored and
Sasha Levin
committed
kbuild: specify output names separately for each emission type from rustc
[ Upstream commit 295d839 ] In Kbuild, two different rules must not write to the same file, but it happens when compiling rust source files. For example, set CONFIG_SAMPLE_RUST_MINIMAL=m and run the following: $ make -j$(nproc) samples/rust/rust_minimal.o samples/rust/rust_minimal.rsi \ samples/rust/rust_minimal.s samples/rust/rust_minimal.ll [snip] RUSTC [M] samples/rust/rust_minimal.o RUSTC [M] samples/rust/rust_minimal.rsi RUSTC [M] samples/rust/rust_minimal.s RUSTC [M] samples/rust/rust_minimal.ll mv: cannot stat 'samples/rust/rust_minimal.d': No such file or directory make[3]: *** [scripts/Makefile.build:334: samples/rust/rust_minimal.ll] Error 1 make[3]: *** Waiting for unfinished jobs.... mv: cannot stat 'samples/rust/rust_minimal.d': No such file or directory make[3]: *** [scripts/Makefile.build:309: samples/rust/rust_minimal.o] Error 1 mv: cannot stat 'samples/rust/rust_minimal.d': No such file or directory make[3]: *** [scripts/Makefile.build:326: samples/rust/rust_minimal.s] Error 1 make[2]: *** [scripts/Makefile.build:504: samples/rust] Error 2 make[1]: *** [scripts/Makefile.build:504: samples] Error 2 make: *** [Makefile:2008: .] Error 2 The reason for the error is that 4 threads running in parallel renames the same file, samples/rust/rust_minimal.d. This does not happen when compiling C or assembly files because -Wp,-MMD,$(depfile) explicitly specifies the dependency filepath. $(depfile) is a unique path for each target. Currently, rustc is only given --out-dir and --emit=<list-of-types> So, all the rust build rules output the dep-info into the default <CRATE_NAME>.d, which causes the path conflict. Fortunately, the --emit option is able to specify the output path individually, with the form --emit=<type>=<path>. Add --emit=dep-info=$(depfile) to the common part. Also, remove the redundant --out-dir because the output path is specified for each type. The code gets much cleaner because we do not need to rename *.d files. Signed-off-by: Masahiro Yamada <[email protected]> Reviewed-by: Miguel Ojeda <[email protected]> Tested-by: Miguel Ojeda <[email protected]> Reviewed-by: Vincenzo Palazzo <[email protected]> Stable-dep-of: ded103c ("kbuild: rust: force `alloc` extern to allow "empty" Rust files") Signed-off-by: Sasha Levin <[email protected]>
1 parent b008800 commit e64af88

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

rust/Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,9 @@ $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
322322
quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
323323
cmd_rustc_procmacro = \
324324
$(RUSTC_OR_CLIPPY) $(rust_common_flags) \
325-
--emit=dep-info,link --extern proc_macro \
326-
--crate-type proc-macro --out-dir $(objtree)/$(obj) \
325+
--emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
326+
--crate-type proc-macro \
327327
--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \
328-
mv $(objtree)/$(obj)/$(patsubst lib%.so,%,$(notdir $@)).d $(depfile); \
329328
sed -i '/^\#/d' $(depfile)
330329

331330
# Procedural macros can only be used with the `rustc` that compiled it.
@@ -339,10 +338,10 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
339338
OBJTREE=$(abspath $(objtree)) \
340339
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
341340
$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
342-
--emit=dep-info,obj,metadata --crate-type rlib \
343-
--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \
341+
--emit=dep-info=$(depfile) --emit=obj=$@ \
342+
--emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
343+
--crate-type rlib -L$(objtree)/$(obj) \
344344
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
345-
mv $(objtree)/$(obj)/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \
346345
sed -i '/^\#/d' $(depfile) \
347346
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
348347

scripts/Makefile.build

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,11 @@ rust_common_cmd = \
283283
-Zcrate-attr=no_std \
284284
-Zcrate-attr='feature($(rust_allowed_features))' \
285285
--extern alloc --extern kernel \
286-
--crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \
287-
--crate-name $(basename $(notdir $@))
286+
--crate-type rlib -L $(objtree)/rust/ \
287+
--crate-name $(basename $(notdir $@)) \
288+
--emit=dep-info=$(depfile)
288289

289290
rust_handle_depfile = \
290-
mv $(obj)/$(basename $(notdir $@)).d $(depfile); \
291291
sed -i '/^\#/d' $(depfile)
292292

293293
# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
@@ -300,15 +300,15 @@ rust_handle_depfile = \
300300

301301
quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
302302
cmd_rustc_o_rs = \
303-
$(rust_common_cmd) --emit=dep-info,obj $<; \
303+
$(rust_common_cmd) --emit=obj=$@ $<; \
304304
$(rust_handle_depfile)
305305

306306
$(obj)/%.o: $(src)/%.rs FORCE
307307
$(call if_changed_dep,rustc_o_rs)
308308

309309
quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
310310
cmd_rustc_rsi_rs = \
311-
$(rust_common_cmd) --emit=dep-info -Zunpretty=expanded $< >$@; \
311+
$(rust_common_cmd) -Zunpretty=expanded $< >$@; \
312312
command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@; \
313313
$(rust_handle_depfile)
314314

@@ -317,15 +317,15 @@ $(obj)/%.rsi: $(src)/%.rs FORCE
317317

318318
quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
319319
cmd_rustc_s_rs = \
320-
$(rust_common_cmd) --emit=dep-info,asm $<; \
320+
$(rust_common_cmd) --emit=asm=$@ $<; \
321321
$(rust_handle_depfile)
322322

323323
$(obj)/%.s: $(src)/%.rs FORCE
324324
$(call if_changed_dep,rustc_s_rs)
325325

326326
quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
327327
cmd_rustc_ll_rs = \
328-
$(rust_common_cmd) --emit=dep-info,llvm-ir $<; \
328+
$(rust_common_cmd) --emit=llvm-ir=$@ $<; \
329329
$(rust_handle_depfile)
330330

331331
$(obj)/%.ll: $(src)/%.rs FORCE

scripts/Makefile.host

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ hostc_flags = -Wp,-MMD,$(depfile) \
8686
hostcxx_flags = -Wp,-MMD,$(depfile) \
8787
$(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
8888
$(HOSTCXXFLAGS_$(target-stem).o)
89-
hostrust_flags = $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
89+
hostrust_flags = --emit=dep-info=$(depfile) \
90+
$(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
9091
$(HOSTRUSTFLAGS_$(target-stem))
9192

9293
# $(objtree)/$(obj) for including generated headers from checkin source files
@@ -147,9 +148,7 @@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
147148
# host-rust -> Executable
148149
quiet_cmd_host-rust = HOSTRUSTC $@
149150
cmd_host-rust = \
150-
$(HOSTRUSTC) $(hostrust_flags) --emit=dep-info,link \
151-
--out-dir=$(obj)/ $<; \
152-
mv $(obj)/$(target-stem).d $(depfile); \
151+
$(HOSTRUSTC) $(hostrust_flags) --emit=link=$@ $<; \
153152
sed -i '/^\#/d' $(depfile)
154153
$(host-rust): $(obj)/%: $(src)/%.rs FORCE
155154
$(call if_changed_dep,host-rust)

0 commit comments

Comments
 (0)