Skip to content

Commit 046d82f

Browse files
committed
kbuild: support host programs written in Rust
This allows us to use Rust for any scripts used during compilation as long as Rust is already available, instead of e.g. C, shell scripts or Perl (Python does not seem to be required for compilation, which would be another natural & modern option to consider). After all, Rust is a good language for scripts too and the goal is to have first-class support for Rust in the kernel, so one should be able to write host programs in it too, just like with C. Not to mention the other benefits (e.g. static typing) compared to the currently available options. Eventually, if having a Rust toolchain ever becomes mandatory (whether or not Rust support is optional and/or enabled), this becomes even a better idea. Ideally, other kernel developers will also want to start using Rust for kernel tooling, which may help making that happen. A downside of Rust for this use case is `rustc`'s compilation speed, which may become concerning if e.g. a lot of programs are used. Another downside, shared with C, is the small standard library. I considered adding C object linking support, but it requires creating an archive of the C objects just for linking and a bunch of other bits which become hard to justify when we do not have a current use case for it. Like above, if having a Rust toolchain ever becomes mandatory for kernel compilation, then finding a use case where C code is needed becomes much easier. Or even the other way around, we could take advantage of Rust inside existing C programs, too. Signed-off-by: Miguel Ojeda <[email protected]>
1 parent a95777a commit 046d82f

26 files changed

+141
-21
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,5 +405,8 @@ jobs:
405405
grep -F '.text' ${{ env.BUILD_DIR }}samples/rust/rust_minimal.s
406406
grep -F '; ModuleID' ${{ env.BUILD_DIR }}samples/rust/rust_minimal.ll
407407
408+
# Rust host programs
409+
- run: ${{ env.BUILD_DIR }}samples/rust/hostprogs/single | grep -F 'The number is 42.'
410+
408411
# View changes to ccache
409412
- run: ccache -s

.github/workflows/kernel-arm-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
17801780
CONFIG_SAMPLE_RUST_SEMAPHORE=m
17811781
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
17821782
CONFIG_SAMPLE_RUST_RANDOM=m
1783+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
17831784
# CONFIG_STRICT_DEVMEM is not set
17841785

17851786
#

.github/workflows/kernel-arm-release.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
17041704
CONFIG_SAMPLE_RUST_SEMAPHORE=m
17051705
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
17061706
CONFIG_SAMPLE_RUST_RANDOM=m
1707+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
17071708
# CONFIG_STRICT_DEVMEM is not set
17081709

17091710
#

.github/workflows/kernel-arm64-debug-thinlto.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
14371437
CONFIG_SAMPLE_RUST_SEMAPHORE=m
14381438
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
14391439
CONFIG_SAMPLE_RUST_RANDOM=m
1440+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14401441

14411442
#
14421443
# arm64 Debugging

.github/workflows/kernel-arm64-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
14321432
CONFIG_SAMPLE_RUST_SEMAPHORE=m
14331433
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
14341434
CONFIG_SAMPLE_RUST_RANDOM=m
1435+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14351436

14361437
#
14371438
# arm64 Debugging

.github/workflows/kernel-arm64-release-thinlto.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
13551355
CONFIG_SAMPLE_RUST_SEMAPHORE=m
13561356
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
13571357
CONFIG_SAMPLE_RUST_RANDOM=m
1358+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
13581359

13591360
#
13601361
# arm64 Debugging

.github/workflows/kernel-arm64-release.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
13501350
CONFIG_SAMPLE_RUST_SEMAPHORE=m
13511351
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
13521352
CONFIG_SAMPLE_RUST_RANDOM=m
1353+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
13531354

13541355
#
13551356
# arm64 Debugging

.github/workflows/kernel-ppc64le-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
14921492
CONFIG_SAMPLE_RUST_SEMAPHORE=m
14931493
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
14941494
CONFIG_SAMPLE_RUST_RANDOM=m
1495+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14951496
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
14961497
# CONFIG_STRICT_DEVMEM is not set
14971498

.github/workflows/kernel-ppc64le-release.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
14541454
CONFIG_SAMPLE_RUST_SEMAPHORE=m
14551455
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
14561456
CONFIG_SAMPLE_RUST_RANDOM=m
1457+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14571458
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
14581459
# CONFIG_STRICT_DEVMEM is not set
14591460

.github/workflows/kernel-riscv64-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
12861286
CONFIG_SAMPLE_RUST_SEMAPHORE=m
12871287
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
12881288
CONFIG_SAMPLE_RUST_RANDOM=m
1289+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
12891290
# CONFIG_STRICT_DEVMEM is not set
12901291

12911292
#

.github/workflows/kernel-riscv64-release.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
12021202
CONFIG_SAMPLE_RUST_SEMAPHORE=m
12031203
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
12041204
CONFIG_SAMPLE_RUST_RANDOM=m
1205+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
12051206
# CONFIG_STRICT_DEVMEM is not set
12061207

12071208
#

.github/workflows/kernel-x86_64-debug-thinlto.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
14411441
CONFIG_SAMPLE_RUST_SEMAPHORE=m
14421442
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
14431443
CONFIG_SAMPLE_RUST_RANDOM=m
1444+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14441445
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
14451446
# CONFIG_STRICT_DEVMEM is not set
14461447

.github/workflows/kernel-x86_64-debug.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
14441444
CONFIG_SAMPLE_RUST_SEMAPHORE=m
14451445
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
14461446
CONFIG_SAMPLE_RUST_RANDOM=m
1447+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14471448
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
14481449
# CONFIG_STRICT_DEVMEM is not set
14491450

.github/workflows/kernel-x86_64-release-thinlto.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
13971397
CONFIG_SAMPLE_RUST_SEMAPHORE=m
13981398
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
13991399
CONFIG_SAMPLE_RUST_RANDOM=m
1400+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
14001401
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
14011402
# CONFIG_STRICT_DEVMEM is not set
14021403

.github/workflows/kernel-x86_64-release.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,7 @@ CONFIG_SAMPLE_RUST_STACK_PROBING=m
13921392
CONFIG_SAMPLE_RUST_SEMAPHORE=m
13931393
CONFIG_SAMPLE_RUST_SEMAPHORE_C=m
13941394
CONFIG_SAMPLE_RUST_RANDOM=m
1395+
CONFIG_SAMPLE_RUST_HOSTPROGS=y
13951396
CONFIG_ARCH_HAS_DEVMEM_IS_ALLOWED=y
13961397
# CONFIG_STRICT_DEVMEM is not set
13971398

Documentation/kbuild/kbuild.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ HOSTCXXFLAGS
8282
------------
8383
Additional flags to be passed to $(HOSTCXX) when building host programs.
8484

85+
HOSTRUSTFLAGS
86+
-------------
87+
Additional flags to be passed to $(HOSTRUSTC) when building host programs.
88+
8589
HOSTLDFLAGS
8690
-----------
8791
Additional flags to be passed when linking host programs.

Documentation/kbuild/makefiles.rst

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ This document describes the Linux kernel Makefiles.
2929
--- 4.1 Simple Host Program
3030
--- 4.2 Composite Host Programs
3131
--- 4.3 Using C++ for host programs
32-
--- 4.4 Controlling compiler options for host programs
33-
--- 4.5 When host programs are actually built
32+
--- 4.4 Using Rust for host programs
33+
--- 4.5 Controlling compiler options for host programs
34+
--- 4.6 When host programs are actually built
3435
3536
=== 5 Userspace Program support
3637
--- 5.1 Simple Userspace Program
@@ -835,7 +836,24 @@ Both possibilities are described in the following.
835836
qconf-cxxobjs := qconf.o
836837
qconf-objs := check.o
837838

838-
4.4 Controlling compiler options for host programs
839+
4.4 Using Rust for host programs
840+
--------------------------------
841+
842+
Kbuild offers support for host programs written in Rust. However,
843+
since a Rust toolchain is not mandatory for kernel compilation,
844+
it may only be used in scenarios where Rust is required to be
845+
available (e.g. when ``CONFIG_RUST`` is enabled).
846+
847+
Example::
848+
849+
hostprogs := target
850+
target-rust := y
851+
852+
Kbuild will compile ``target`` using ``target.rs`` as the crate root,
853+
located in the same directory as the ``Makefile``. The crate may
854+
consist of several source files (see ``samples/rust/hostprogs``).
855+
856+
4.5 Controlling compiler options for host programs
839857
--------------------------------------------------
840858

841859
When compiling host programs, it is possible to set specific flags.
@@ -867,7 +885,7 @@ Both possibilities are described in the following.
867885
When linking qconf, it will be passed the extra option
868886
"-L$(QTDIR)/lib".
869887

870-
4.5 When host programs are actually built
888+
4.6 When host programs are actually built
871889
-----------------------------------------
872890

873891
Kbuild will only build host-programs when they are referenced

Makefile

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -437,13 +437,32 @@ else
437437
HOSTCC = gcc
438438
HOSTCXX = g++
439439
endif
440+
HOSTRUSTC = rustc
440441

441442
export KBUILD_USERCFLAGS := -Wall -Wmissing-prototypes -Wstrict-prototypes \
442443
-O2 -fomit-frame-pointer -std=gnu89
443444
export KBUILD_USERLDFLAGS :=
444445

446+
# These flags apply to all Rust code in the tree, including the kernel and
447+
# host programs.
448+
rust_common_flags := --edition=2021 \
449+
-Zbinary_dep_depinfo=y \
450+
-Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
451+
-Dunreachable_pub -Dnon_ascii_idents \
452+
-Wmissing_docs \
453+
-Drustdoc::missing_crate_level_docs \
454+
-Dclippy::correctness -Dclippy::style \
455+
-Dclippy::suspicious -Dclippy::complexity \
456+
-Dclippy::perf \
457+
-Dclippy::let_unit_value -Dclippy::mut_mut \
458+
-Dclippy::needless_bitwise_bool \
459+
-Dclippy::needless_continue \
460+
-Wclippy::dbg_macro
461+
445462
KBUILD_HOSTCFLAGS := $(KBUILD_USERCFLAGS) $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
446463
KBUILD_HOSTCXXFLAGS := -Wall -O2 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
464+
KBUILD_HOSTRUSTFLAGS := $(rust_common_flags) -O -Zallow-features= \
465+
$(HOSTRUSTFLAGS)
447466
KBUILD_HOSTLDFLAGS := $(HOST_LFS_LDFLAGS) $(HOSTLDFLAGS)
448467
KBUILD_HOSTLDLIBS := $(HOST_LFS_LIBS) $(HOSTLDLIBS)
449468

@@ -533,20 +552,12 @@ KBUILD_CFLAGS := -Wall -Wundef -Werror=strict-prototypes -Wno-trigraphs \
533552
-std=gnu89
534553
KBUILD_CPPFLAGS := -D__KERNEL__
535554
KBUILD_RUST_TARGET := $(srctree)/arch/$(SRCARCH)/rust/target.json
536-
KBUILD_RUSTFLAGS := --edition=2021 \
537-
-Cpanic=abort -Cembed-bitcode=n -Clto=n -Crpath=n \
538-
-Cforce-unwind-tables=n -Ccodegen-units=1 \
539-
-Csymbol-mangling-version=v0 \
540-
-Zbinary_dep_depinfo=y \
541-
-Dunsafe_op_in_unsafe_fn -Drust_2018_idioms \
542-
-Dunreachable_pub -Dnon_ascii_idents \
543-
-Wmissing_docs \
544-
-Drustdoc::missing_crate_level_docs \
545-
-Dclippy::correctness -Dclippy::style -Dclippy::suspicious \
546-
-Dclippy::complexity -Dclippy::perf -Dclippy::float_arithmetic \
547-
-Dclippy::let_unit_value -Dclippy::mut_mut \
548-
-Dclippy::needless_bitwise_bool -Dclippy::needless_continue \
549-
-Wclippy::dbg_macro
555+
KBUILD_RUSTFLAGS := $(rust_common_flags) \
556+
-Cpanic=abort -Cembed-bitcode=n -Clto=n -Crpath=n \
557+
-Cforce-unwind-tables=n -Ccodegen-units=1 \
558+
-Csymbol-mangling-version=v0 \
559+
-Dclippy::float_arithmetic
560+
550561
KBUILD_AFLAGS_KERNEL :=
551562
KBUILD_CFLAGS_KERNEL :=
552563
KBUILD_RUSTFLAGS_KERNEL :=
@@ -573,6 +584,7 @@ export RUSTC_BOOTSTRAP := 1
573584

574585
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC
575586
export RUSTC RUSTDOC RUSTFMT RUSTC_OR_CLIPPY_QUIET RUSTC_OR_CLIPPY BINDGEN CARGO
587+
export HOSTRUSTC KBUILD_HOSTRUSTFLAGS
576588
export CPP AR NM STRIP OBJCOPY OBJDUMP READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
577589
export PERL PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
578590
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD

samples/rust/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,11 @@ config SAMPLE_RUST_PLATFORM
120120

121121
If unsure, say N.
122122

123+
config SAMPLE_RUST_HOSTPROGS
124+
bool "Host programs"
125+
help
126+
This option builds the Rust host program samples.
127+
128+
If unsure, say N.
129+
123130
endif # SAMPLES_RUST

samples/rust/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE) += rust_semaphore.o
1111
obj-$(CONFIG_SAMPLE_RUST_SEMAPHORE_C) += rust_semaphore_c.o
1212
obj-$(CONFIG_SAMPLE_RUST_RANDOM) += rust_random.o
1313
obj-$(CONFIG_SAMPLE_RUST_PLATFORM) += rust_platform.o
14+
15+
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/hostprogs/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
single

samples/rust/hostprogs/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
3+
hostprogs-always-y := single
4+
5+
single-rust := y

samples/rust/hostprogs/a.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust single host program sample: module `a`.
4+
5+
pub(crate) fn f(x: i32) {
6+
println!("The number is {}.", x);
7+
}

samples/rust/hostprogs/b.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust single host program sample: module `b`.
4+
5+
pub(crate) const CONSTANT: i32 = 42;

samples/rust/hostprogs/single.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust single host program sample.
4+
5+
mod a;
6+
mod b;
7+
8+
fn main() {
9+
println!("Hello world!");
10+
11+
a::f(b::CONSTANT);
12+
}

scripts/Makefile.host

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
2222
# to preprocess a data file.
2323
#
2424
# Both C and C++ are supported, but preferred language is C for such utilities.
25+
# Rust is also supported, but it may only be used in scenarios where a Rust
26+
# toolchain is required to be available (e.g. when `CONFIG_RUST` is enabled).
2527
#
2628
# Sample syntax (see Documentation/kbuild/makefiles.rst for reference)
2729
# hostprogs := bin2hex
@@ -37,15 +39,20 @@ $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
3739
# qconf-objs := menu.o
3840
# Will compile qconf as a C++ program, and menu as a C program.
3941
# They are linked as C++ code to the executable qconf
42+
#
43+
# hostprogs := target
44+
# target-rust := y
45+
# Will compile `target` as a Rust program, using `target.rs` as the crate root.
46+
# The crate may consist of several source files.
4047

4148
# C code
4249
# Executables compiled from a single .c file
4350
host-csingle := $(foreach m,$(hostprogs), \
44-
$(if $($(m)-objs)$($(m)-cxxobjs),,$(m)))
51+
$(if $($(m)-objs)$($(m)-cxxobjs)$($(m)-rust),,$(m)))
4552

4653
# C executables linked based on several .o files
4754
host-cmulti := $(foreach m,$(hostprogs),\
48-
$(if $($(m)-cxxobjs),,$(if $($(m)-objs),$(m))))
55+
$(if $($(m)-cxxobjs)$($(m)-rust),,$(if $($(m)-objs),$(m))))
4956

5057
# Object (.o) files compiled from .c files
5158
host-cobjs := $(sort $(foreach m,$(hostprogs),$($(m)-objs)))
@@ -58,11 +65,17 @@ host-cxxmulti := $(foreach m,$(hostprogs),$(if $($(m)-cxxobjs),$(m)))
5865
# C++ Object (.o) files compiled from .cc files
5966
host-cxxobjs := $(sort $(foreach m,$(host-cxxmulti),$($(m)-cxxobjs)))
6067

68+
# Rust code
69+
# Executables compiled from a single Rust crate (which may consist of
70+
# one or more .rs files)
71+
host-rust := $(foreach m,$(hostprogs),$(if $($(m)-rust),$(m)))
72+
6173
host-csingle := $(addprefix $(obj)/,$(host-csingle))
6274
host-cmulti := $(addprefix $(obj)/,$(host-cmulti))
6375
host-cobjs := $(addprefix $(obj)/,$(host-cobjs))
6476
host-cxxmulti := $(addprefix $(obj)/,$(host-cxxmulti))
6577
host-cxxobjs := $(addprefix $(obj)/,$(host-cxxobjs))
78+
host-rust := $(addprefix $(obj)/,$(host-rust))
6679

6780
#####
6881
# Handle options to gcc. Support building with separate output directory
@@ -71,6 +84,8 @@ _hostc_flags = $(KBUILD_HOSTCFLAGS) $(HOST_EXTRACFLAGS) \
7184
$(HOSTCFLAGS_$(target-stem).o)
7285
_hostcxx_flags = $(KBUILD_HOSTCXXFLAGS) $(HOST_EXTRACXXFLAGS) \
7386
$(HOSTCXXFLAGS_$(target-stem).o)
87+
_hostrust_flags = $(KBUILD_HOSTRUSTFLAGS) $(HOST_EXTRARUSTFLAGS) \
88+
$(HOSTRUSTFLAGS_$(target-stem))
7489

7590
# $(objtree)/$(obj) for including generated headers from checkin source files
7691
ifeq ($(KBUILD_EXTMOD),)
@@ -82,6 +97,7 @@ endif
8297

8398
hostc_flags = -Wp,-MMD,$(depfile) $(_hostc_flags)
8499
hostcxx_flags = -Wp,-MMD,$(depfile) $(_hostcxx_flags)
100+
hostrust_flags = $(_hostrust_flags)
85101

86102
#####
87103
# Compile programs on the host
@@ -128,5 +144,17 @@ quiet_cmd_host-cxxobjs = HOSTCXX $@
128144
$(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
129145
$(call if_changed_dep,host-cxxobjs)
130146

147+
# Create executable from a single Rust crate (which may consist of
148+
# one or more `.rs` files)
149+
# host-rust -> Executable
150+
quiet_cmd_host-rust = HOSTRUSTC $@
151+
cmd_host-rust = \
152+
$(HOSTRUSTC) $(hostrust_flags) --emit=dep-info,link \
153+
--out-dir=$(obj)/ $<; \
154+
mv $(obj)/$(target-stem).d $(depfile); \
155+
sed -i '/^\#/d' $(depfile)
156+
$(host-rust): $(obj)/%: $(src)/%.rs FORCE
157+
$(call if_changed_dep,host-rust)
158+
131159
targets += $(host-csingle) $(host-cmulti) $(host-cobjs) \
132-
$(host-cxxmulti) $(host-cxxobjs)
160+
$(host-cxxmulti) $(host-cxxobjs) $(host-rust)

0 commit comments

Comments
 (0)