Commit 295d8398 authored by Masahiro Yamada's avatar Masahiro Yamada

kbuild: specify output names separately for each emission type from rustc

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: default avatarMasahiro Yamada <masahiroy@kernel.org>
Reviewed-by: default avatarMiguel Ojeda <ojeda@kernel.org>
Tested-by: default avatarMiguel Ojeda <ojeda@kernel.org>
Reviewed-by: default avatarVincenzo Palazzo <vincenzopalazzodev@gmail.com>
parent 16169a47
...@@ -331,10 +331,9 @@ $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE ...@@ -331,10 +331,9 @@ $(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@ quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
cmd_rustc_procmacro = \ cmd_rustc_procmacro = \
$(RUSTC_OR_CLIPPY) $(rust_common_flags) \ $(RUSTC_OR_CLIPPY) $(rust_common_flags) \
--emit=dep-info,link --extern proc_macro \ --emit=dep-info=$(depfile) --emit=link=$@ --extern proc_macro \
--crate-type proc-macro --out-dir $(objtree)/$(obj) \ --crate-type proc-macro \
--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \ --crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \
mv $(objtree)/$(obj)/$(patsubst lib%.so,%,$(notdir $@)).d $(depfile); \
sed -i '/^\#/d' $(depfile) sed -i '/^\#/d' $(depfile)
# Procedural macros can only be used with the `rustc` that compiled it. # Procedural macros can only be used with the `rustc` that compiled it.
...@@ -348,10 +347,10 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L ...@@ -348,10 +347,10 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
OBJTREE=$(abspath $(objtree)) \ OBJTREE=$(abspath $(objtree)) \
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \ $(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
$(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \ $(filter-out $(skip_flags),$(rust_flags) $(rustc_target_flags)) \
--emit=dep-info,obj,metadata --crate-type rlib \ --emit=dep-info=$(depfile) --emit=obj=$@ \
--out-dir $(objtree)/$(obj) -L$(objtree)/$(obj) \ --emit=metadata=$(dir $@)$(patsubst %.o,lib%.rmeta,$(notdir $@)) \
--crate-type rlib -L$(objtree)/$(obj) \
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \ --crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
mv $(objtree)/$(obj)/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \
sed -i '/^\#/d' $(depfile) \ sed -i '/^\#/d' $(depfile) \
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@) $(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
......
...@@ -285,11 +285,11 @@ rust_common_cmd = \ ...@@ -285,11 +285,11 @@ rust_common_cmd = \
-Zcrate-attr=no_std \ -Zcrate-attr=no_std \
-Zcrate-attr='feature($(rust_allowed_features))' \ -Zcrate-attr='feature($(rust_allowed_features))' \
--extern alloc --extern kernel \ --extern alloc --extern kernel \
--crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \ --crate-type rlib -L $(objtree)/rust/ \
--crate-name $(basename $(notdir $@)) --crate-name $(basename $(notdir $@)) \
--emit=dep-info=$(depfile)
rust_handle_depfile = \ rust_handle_depfile = \
mv $(obj)/$(basename $(notdir $@)).d $(depfile); \
sed -i '/^\#/d' $(depfile) sed -i '/^\#/d' $(depfile)
# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit # `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
...@@ -302,7 +302,7 @@ rust_handle_depfile = \ ...@@ -302,7 +302,7 @@ rust_handle_depfile = \
quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
cmd_rustc_o_rs = \ cmd_rustc_o_rs = \
$(rust_common_cmd) --emit=dep-info,obj $<; \ $(rust_common_cmd) --emit=obj=$@ $<; \
$(rust_handle_depfile) $(rust_handle_depfile)
$(obj)/%.o: $(src)/%.rs FORCE $(obj)/%.o: $(src)/%.rs FORCE
...@@ -310,7 +310,7 @@ $(obj)/%.o: $(src)/%.rs FORCE ...@@ -310,7 +310,7 @@ $(obj)/%.o: $(src)/%.rs FORCE
quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ quiet_cmd_rustc_rsi_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
cmd_rustc_rsi_rs = \ cmd_rustc_rsi_rs = \
$(rust_common_cmd) --emit=dep-info -Zunpretty=expanded $< >$@; \ $(rust_common_cmd) -Zunpretty=expanded $< >$@; \
command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@; \ command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@; \
$(rust_handle_depfile) $(rust_handle_depfile)
...@@ -319,7 +319,7 @@ $(obj)/%.rsi: $(src)/%.rs FORCE ...@@ -319,7 +319,7 @@ $(obj)/%.rsi: $(src)/%.rs FORCE
quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
cmd_rustc_s_rs = \ cmd_rustc_s_rs = \
$(rust_common_cmd) --emit=dep-info,asm $<; \ $(rust_common_cmd) --emit=asm=$@ $<; \
$(rust_handle_depfile) $(rust_handle_depfile)
$(obj)/%.s: $(src)/%.rs FORCE $(obj)/%.s: $(src)/%.rs FORCE
...@@ -327,7 +327,7 @@ $(obj)/%.s: $(src)/%.rs FORCE ...@@ -327,7 +327,7 @@ $(obj)/%.s: $(src)/%.rs FORCE
quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
cmd_rustc_ll_rs = \ cmd_rustc_ll_rs = \
$(rust_common_cmd) --emit=dep-info,llvm-ir $<; \ $(rust_common_cmd) --emit=llvm-ir=$@ $<; \
$(rust_handle_depfile) $(rust_handle_depfile)
$(obj)/%.ll: $(src)/%.rs FORCE $(obj)/%.ll: $(src)/%.rs FORCE
......
...@@ -86,7 +86,8 @@ hostc_flags = -Wp,-MMD,$(depfile) \ ...@@ -86,7 +86,8 @@ hostc_flags = -Wp,-MMD,$(depfile) \
hostcxx_flags = -Wp,-MMD,$(depfile) \ hostcxx_flags = -Wp,-MMD,$(depfile) \
$(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \ $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
$(HOSTCXXFLAGS_$(target-stem).o) $(HOSTCXXFLAGS_$(target-stem).o)
hostrust_flags = $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \ hostrust_flags = --emit=dep-info=$(depfile) \
$(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
$(HOSTRUSTFLAGS_$(target-stem)) $(HOSTRUSTFLAGS_$(target-stem))
# $(objtree)/$(obj) for including generated headers from checkin source files # $(objtree)/$(obj) for including generated headers from checkin source files
...@@ -147,9 +148,7 @@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE ...@@ -147,9 +148,7 @@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
# host-rust -> Executable # host-rust -> Executable
quiet_cmd_host-rust = HOSTRUSTC $@ quiet_cmd_host-rust = HOSTRUSTC $@
cmd_host-rust = \ cmd_host-rust = \
$(HOSTRUSTC) $(hostrust_flags) --emit=dep-info,link \ $(HOSTRUSTC) $(hostrust_flags) --emit=link=$@ $<; \
--out-dir=$(obj)/ $<; \
mv $(obj)/$(target-stem).d $(depfile); \
sed -i '/^\#/d' $(depfile) sed -i '/^\#/d' $(depfile)
$(host-rust): $(obj)/%: $(src)/%.rs FORCE $(host-rust): $(obj)/%: $(src)/%.rs FORCE
$(call if_changed_dep,host-rust) $(call if_changed_dep,host-rust)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment