Skip to content

Commit 06be45c

Browse files
authored
Merge pull request torvalds#646 from ojeda/single-target
Add single target support for Rust (`.{o,i,s,ll}`)
2 parents 1b69584 + f6256dc commit 06be45c

File tree

4 files changed

+73
-15
lines changed

4 files changed

+73
-15
lines changed

.github/workflows/ci.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,5 +471,19 @@ jobs:
471471
# Formatting
472472
- run: make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_LLVM_IAS }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 rustfmtcheck
473473

474+
# Single targets
475+
- run: |
476+
rm ${{ env.BUILD_DIR }}samples/rust/rust_minimal.o
477+
478+
make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_LLVM_IAS }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 samples/rust/rust_minimal.o
479+
make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_LLVM_IAS }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 samples/rust/rust_minimal.i
480+
make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_LLVM_IAS }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 samples/rust/rust_minimal.s
481+
make ${{ env.MAKE_ARCH }} ${{ env.MAKE_CROSS_COMPILE }} ${{ env.MAKE_LLVM_IAS }} ${{ env.MAKE_TOOLCHAIN }} ${{ env.MAKE_OUTPUT }} ${{ env.MAKE_SYSROOT }} -j3 samples/rust/rust_minimal.ll
482+
483+
file ${{ env.BUILD_DIR }}samples/rust/rust_minimal.o | grep -F 'ELF'
484+
grep -F '#![feature(prelude_import)]' ${{ env.BUILD_DIR }}samples/rust/rust_minimal.i
485+
grep -F '.text' ${{ env.BUILD_DIR }}samples/rust/rust_minimal.s
486+
grep -F '; ModuleID' ${{ env.BUILD_DIR }}samples/rust/rust_minimal.ll
487+
474488
# View changes to ccache
475489
- run: ccache -s

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
533533
-std=gnu89
534534
KBUILD_CPPFLAGS := -D__KERNEL__
535535
KBUILD_RUST_TARGET := $(srctree)/arch/$(SRCARCH)/rust/target.json
536-
KBUILD_RUSTFLAGS := --emit=dep-info,obj,metadata --edition=2021 \
536+
KBUILD_RUSTFLAGS := --edition=2021 \
537537
-Cpanic=abort -Cembed-bitcode=n -Clto=n -Crpath=n \
538538
-Cforce-unwind-tables=n -Ccodegen-units=1 \
539539
-Zbinary_dep_depinfo=y -Zsymbol-mangling-version=v0 \
@@ -1707,6 +1707,10 @@ help:
17071707
@echo ' (requires kernel .config; downloads external repos)'
17081708
@echo ' rust-analyzer - Generate rust-project.json rust-analyzer support file'
17091709
@echo ' (requires kernel .config)'
1710+
@echo ' dir/file.[os] - Build specified target only'
1711+
@echo ' dir/file.i - Build macro expanded source, similar to C preprocessing'
1712+
@echo ' (run with RUSTFMT=n to skip reformatting if needed)'
1713+
@echo ' dir/file.ll - Build the LLVM assembly file'
17101714
@echo ''
17111715
@$(if $(dtstree), \
17121716
echo 'Devicetree:'; \

rust/Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
4444
cmd_rustdoc = \
4545
OBJTREE=$(abspath $(objtree)) \
4646
$(RUSTDOC) $(if $(rustdoc_host),,$(rust_cross_flags)) \
47-
$(filter-out -Cpanic=abort, $(filter-out --emit=%, $(rust_flags))) \
47+
$(filter-out -Cpanic=abort, $(rust_flags)) \
4848
$(rustc_target_flags) -L $(objtree)/rust \
4949
--output $(objtree)/rust/doc --crate-name $(subst rustdoc-,,$@) \
5050
@$(objtree)/include/generated/rustc_cfg $<
@@ -83,7 +83,7 @@ rustdoc-kernel: $(srctree)/rust/kernel/lib.rs rustdoc-core \
8383
quiet_cmd_rustc_test_library = RUSTC TL $<
8484
cmd_rustc_test_library = \
8585
OBJTREE=$(abspath $(objtree)) \
86-
$(RUSTC) $(filter-out --sysroot=%, $(filter-out -Cpanic=abort, $(filter-out --emit=%, $(rust_flags)))) \
86+
$(RUSTC) $(filter-out --sysroot=%, $(filter-out -Cpanic=abort, $(rust_flags))) \
8787
$(rustc_target_flags) --crate-type $(if $(rustc_test_library_proc),proc-macro,rlib) \
8888
--out-dir $(objtree)/rust/test/ --cfg testlib \
8989
--sysroot $(objtree)/rust/test/sysroot \
@@ -100,7 +100,7 @@ rusttestlib-macros: $(srctree)/rust/macros/lib.rs rusttest-prepare FORCE
100100
quiet_cmd_rustdoc_test = RUSTDOC T $<
101101
cmd_rustdoc_test = \
102102
OBJTREE=$(abspath $(objtree)) \
103-
$(RUSTDOC) --test $(filter-out --sysroot=%, $(filter-out -Cpanic=abort, $(filter-out --emit=%, $(rust_flags)))) \
103+
$(RUSTDOC) --test $(filter-out --sysroot=%, $(filter-out -Cpanic=abort, $(rust_flags))) \
104104
$(rustc_target_flags) $(rustdoc_test_target_flags) \
105105
--sysroot $(objtree)/rust/test/sysroot $(rustdoc_test_quiet) \
106106
-L $(objtree)/rust/test \
@@ -112,7 +112,7 @@ quiet_cmd_rustdoc_test = RUSTDOC T $<
112112
quiet_cmd_rustc_test = RUSTC T $<
113113
cmd_rustc_test = \
114114
OBJTREE=$(abspath $(objtree)) \
115-
$(RUSTC) --test $(filter-out --sysroot=%, $(filter-out -Cpanic=abort, $(filter-out --emit=%, $(rust_flags)))) \
115+
$(RUSTC) --test $(filter-out --sysroot=%, $(filter-out -Cpanic=abort, $(rust_flags))) \
116116
$(rustc_target_flags) --out-dir $(objtree)/rust/test \
117117
--sysroot $(objtree)/rust/test/sysroot \
118118
-L $(objtree)/rust/test/ --crate-name $(subst rusttest-,,$@) $<; \
@@ -299,6 +299,7 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
299299
OBJTREE=$(abspath $(objtree)) \
300300
$(if $(skip_clippy),$(RUSTC),$(RUSTC_OR_CLIPPY)) \
301301
$(filter-out $(skip_flags),$(rust_flags) $(rust_cross_flags) $(rustc_target_flags)) \
302+
--emit=dep-info,obj,metadata \
302303
--crate-type rlib --out-dir $(objtree)/rust/ -L $(objtree)/rust/ \
303304
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
304305
mv $(objtree)/rust/$(patsubst %.o,%,$(notdir $@)).d $(depfile); \

scripts/Makefile.build

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -333,22 +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-
-Zallow-features=$(rust_allowed_features) \
341-
-Zcrate-attr=no_std \
342-
-Zcrate-attr='feature($(rust_allowed_features))' \
343-
--extern alloc --extern kernel \
344-
--crate-type rlib --out-dir $(obj) -L $(objtree)/rust/ \
345-
--crate-name $(patsubst %.o,%,$(notdir $@)) $<; \
346-
mv $(obj)/$(subst .o,,$(notdir $@)).d $(depfile); \
347-
sed -i '/^\#/d' $(depfile)
360+
$(rust_common_cmd) --emit=dep-info,obj,metadata $<; \
361+
$(rust_handle_depfile)
348362

349363
$(obj)/%.o: $(src)/%.rs FORCE
350364
$(call if_changed_dep,rustc_o_rs)
351365

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+
352391
# Compile assembler sources (.S)
353392
# ---------------------------------------------------------------------------
354393

0 commit comments

Comments
 (0)