Skip to content

Commit

Permalink
replace build.rs with bindgen
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Beyer <mail@beyermatthias.de>
Signed-off-by: Finn Behrens <me@kloenk.de>
  • Loading branch information
kloenk and matthiasbeyer committed Oct 18, 2020
1 parent 255713b commit c5cfab4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 144 deletions.
10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ STRIP = $(CROSS_COMPILE)strip
endif
RUSTC = rustc
CARGO = cargo
BINDGEN = bindgen
PAHOLE = pahole
RESOLVE_BTFIDS = $(objtree)/tools/bpf/resolve_btfids/resolve_btfids
LEX = flex
Expand Down Expand Up @@ -513,8 +514,9 @@ KBUILD_RUSTCFLAGS :=
# TODO: another option is using explicit target specs, e.g.
# `--target=$(srctree)/arch/$(SRCARCH)/rust-target-spec.json`
KBUILD_CARGOFLAGS := $(CARGO_VERBOSE) --locked \
-Z build-std=core,alloc -Z unstable-options \
--out-dir=out --target=x86_64-linux-kernel
-Z build-std=core,alloc \
-Z unstable-options \
--target=x86_64-linux-kernel
KBUILD_AFLAGS_KERNEL :=
KBUILD_CFLAGS_KERNEL :=
KBUILD_RUSTCFLAGS_KERNEL :=
Expand All @@ -528,7 +530,7 @@ export KBUILD_LDS_MODULE := $(srctree)/scripts/module-common.lds
KBUILD_LDFLAGS :=
CLANG_FLAGS :=

export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC CARGO
export ARCH SRCARCH CONFIG_SHELL BASH HOSTCC KBUILD_HOSTCFLAGS CROSS_COMPILE LD CC RUSTC CARGO BINDGEN
export CPP AR NM STRIP OBJCOPY OBJDUMP OBJSIZE READELF PAHOLE RESOLVE_BTFIDS LEX YACC AWK INSTALLKERNEL
export PERL PYTHON PYTHON3 CHECK CHECKFLAGS MAKE UTS_MACHINE HOSTCXX
export KGZIP KBZIP2 KLZOP LZMA LZ4 XZ ZSTD
Expand Down Expand Up @@ -728,7 +730,7 @@ $(KCONFIG_CONFIG):
# This exploits the 'multi-target pattern rule' trick.
# The syncconfig should be executed only once to make all the targets.
# (Note: use the grouped target '&:' when we bump to GNU Make 4.3)
%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG)
%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/generated/rust_cfg: $(KCONFIG_CONFIG)
$(Q)$(MAKE) -f $(srctree)/Makefile syncconfig
else # !may-sync-config
# External modules and some install targets need include/generated/autoconf.h
Expand Down
134 changes: 0 additions & 134 deletions rust/kernel/build.rs

This file was deleted.

2 changes: 1 addition & 1 deletion rust/kernel/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
)]
mod bindings_raw {
use crate::c_types;
include!("bindings_gen.rs");
include!(env!("RUST_BINDGEN_FILE"));
}
pub use bindings_raw::*;

Expand Down
10 changes: 7 additions & 3 deletions scripts/Makefile.build
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,18 @@ $(obj)/%.lst: $(src)/%.c FORCE
# ---------------------------------------------------------------------------

quiet_cmd_cargo = CARGO $(quiet_modtag) $@
cmd_cargo = cd $(src) && $(CARGO) build $(cargo_flags)
cmd_cargo = export RUST_BINDGEN_FILE=$(shell readlink -f rust_bindings.rs) && $(CARGO) build $(cargo_flags) -p $(shell basename $(basename $(src))) --out-dir $(src)

rust_bindings.rs: FORCE
$(Q)$(BINDGEN) $(srctree)/rust/kernel/src/bindings_helper.h --opaque-type xregs_state --opaque-type desc_struct --use-core --ctypes-prefix c_types -o rust_bindings.rs --size_t-is-usize -- $(c_flags)

# The .o from the Rust staticlib
$(obj)/%.o: $(src)/out/lib%.a
#$(obj)/%.o: $(src)/out/lib%.a
$(obj)/%.o: $(obj)/lib%.a
$(Q)$(LD) -r -o $@ --whole-archive $<

# The Rust staticlib from cargo
$(obj)/out/lib%.a: FORCE
$(obj)/lib%.a: FORCE rust_bindings.rs
$(call cmd,cargo)

# Compile assembler sources (.S)
Expand Down
6 changes: 4 additions & 2 deletions scripts/Makefile.lib
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,11 @@ c_flags = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) \
RUST_BINDGEN_CFLAGS = $(c_flags) $(KBUILD_CFLAGS_MODULE)
export RUST_BINDGEN_CFLAGS

rustc_cfg_flags = $(shell sed -nE 's/^(CONFIG_[^=]+)=(y|m)$$/--cfg \1/p' $(srctree)/include/config/auto.conf | xargs)
KCONFIG_RUSTCONFIG ?= include/generated/rust_cfg

rustc_flags = $(_rustc_flags) $(modkern_rustcflags) $(rustc_cfg_flags)
rustconfig_path = $(shell readlink -f $(KCONFIG_RUSTCONFIG))

rustc_flags = $(_rustc_flags) $(modkern_rustcflags) @$(rustconfig_path)

# Passed by cargo
RUSTFLAGS = $(rustc_flags)
Expand Down

0 comments on commit c5cfab4

Please sign in to comment.