Skip to content

Commit 9c80547

Browse files
committed
Kbuild: add single target support for Rust (.{o,i,s,ll})
`.i` maps to macro expansion, which is somewhat similar to what is done for C and the preprocessor. The output is automatically reformatted if `rustfmt` is installed, which mimics what `cargo expand` does by default. The user can override `RUSTFMT` to avoid it in the odd case is required. We could consider adding other outputs such as `.mir`/`.hir` if they happen to be useful in the future. Signed-off-by: Miguel Ojeda <[email protected]>
1 parent f3171fd commit 9c80547

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

scripts/Makefile.build

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -333,23 +333,61 @@ rust_cross_flags := --target=$(realpath $(KBUILD_RUST_TARGET))
333333

334334
rust_allowed_features := allocator_api,bench_black_box,concat_idents,generic_associated_types,global_asm
335335

336+
rust_common_cmd = \
337+
RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) \
338+
$(rust_flags) $(rust_cross_flags) \
339+
-Zallow-features=$(rust_allowed_features) \
340+
-Zcrate-attr=no_std \
341+
-Zcrate-attr='feature($(rust_allowed_features))' \
342+
--extern alloc --extern kernel \
343+
--crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \
344+
--crate-name $(basename $(notdir $@))
345+
346+
rust_handle_depfile = \
347+
mv $(obj)/$(basename $(notdir $@)).d $(depfile); \
348+
sed -i '/^\#/d' $(depfile)
349+
350+
# `--emit=obj`, `--emit=asm` and `--emit=llvm-ir` imply a single codegen unit
351+
# will be used. We explicitly request `-Ccodegen-units=1` in any case, and
352+
# the compiler shows a warning if it is not 1. However, if we ever stop
353+
# requesting it explicitly and we start using some other `--emit` that does not
354+
# imply it (and for which codegen is performed), then we would be out of sync,
355+
# i.e. the outputs we would get for the different single targets (e.g. `.ll`)
356+
# would not match each other.
357+
336358
quiet_cmd_rustc_o_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
337359
cmd_rustc_o_rs = \
338-
RUST_MODFILE=$(modfile) \
339-
$(RUSTC_OR_CLIPPY) $(rust_flags) $(rust_cross_flags) \
340-
--emit=dep-info,obj,metadata \
341-
-Zallow-features=$(rust_allowed_features) \
342-
-Zcrate-attr=no_std \
343-
-Zcrate-attr='feature($(rust_allowed_features))' \
344-
--extern alloc --extern kernel \
345-
--crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \
346-
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
347-
mv $(obj)/$(subst .o,,$(notdir $@)).d $(depfile); \
348-
sed -i '/^\#/d' $(depfile)
360+
$(rust_common_cmd) --emit=dep-info,obj,metadata $<; \
361+
$(rust_handle_depfile)
349362

350363
$(obj)/%.o: $(src)/%.rs FORCE
351364
$(call if_changed_dep,rustc_o_rs)
352365

366+
quiet_cmd_rustc_i_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
367+
cmd_rustc_i_rs = \
368+
$(rust_common_cmd) --emit=dep-info -Zunpretty=expanded $< >$@; \
369+
command -v $(RUSTFMT) >/dev/null && $(RUSTFMT) $@; \
370+
$(rust_handle_depfile)
371+
372+
$(obj)/%.i: $(src)/%.rs FORCE
373+
$(call if_changed_dep,rustc_i_rs)
374+
375+
quiet_cmd_rustc_s_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
376+
cmd_rustc_s_rs = \
377+
$(rust_common_cmd) --emit=dep-info,asm $<; \
378+
$(rust_handle_depfile)
379+
380+
$(obj)/%.s: $(src)/%.rs FORCE
381+
$(call if_changed_dep,rustc_s_rs)
382+
383+
quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@
384+
cmd_rustc_ll_rs = \
385+
$(rust_common_cmd) --emit=dep-info,llvm-ir $<; \
386+
$(rust_handle_depfile)
387+
388+
$(obj)/%.ll: $(src)/%.rs FORCE
389+
$(call if_changed_dep,rustc_ll_rs)
390+
353391
# Compile assembler sources (.S)
354392
# ---------------------------------------------------------------------------
355393

0 commit comments

Comments
 (0)