Skip to content

Commit

Permalink
Split bindings crate out of the main kernel crate
Browse files Browse the repository at this point in the history
The generated bindings are mostly static when working on new rust
abstractions. By splitting them off the main kernel crate, recompilation
of the main kernel crate takes ~2s rather than ~12s. While this loses
some optimizations without LTO, the fast majority of the binding
functions are already marked as `#[inline]`. Pretty much only
`Default` impls aren't marked as such. The cost of not inlining those
`Default` impls is likely neglectable.

Signed-off-by: Björn Roy Baron <[email protected]>
  • Loading branch information
bjorn3 committed Jul 10, 2022
1 parent fcbf909 commit b0ec4c8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
33 changes: 23 additions & 10 deletions rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ always-$(CONFIG_RUST) += libmacros.so
no-clean-files += libmacros.so

always-$(CONFIG_RUST) += bindings_generated.rs bindings_helpers_generated.rs
obj-$(CONFIG_RUST) += alloc.o kernel.o
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_kernel_generated.h
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
exports_kernel_generated.h

ifdef CONFIG_RUST_BUILD_ASSERT_DENY
always-$(CONFIG_RUST) += build_error.o
Expand Down Expand Up @@ -110,10 +111,11 @@ rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
$(call if_changed,rustdoc)

rustdoc-kernel: private rustc_target_flags = --extern alloc \
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
--extern bindings
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
$(obj)/bindings_generated.rs $(obj)/bindings_helpers_generated.rs FORCE
$(obj)/bindings.o FORCE
$(call if_changed,rustdoc)

quiet_cmd_rustc_test_library = RUSTC TL $<
Expand All @@ -135,6 +137,9 @@ rusttestlib-macros: private rustc_test_library_proc = yes
rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
$(call if_changed,rustc_test_library)

rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
$(call if_changed,rustc_test_library)

quiet_cmd_rustdoc_test = RUSTDOC T $<
cmd_rustdoc_test = \
OBJTREE=$(abspath $(objtree)) \
Expand All @@ -154,6 +159,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
@$(objtree)/include/generated/rustc_cfg \
-L$(objtree)/$(obj) --extern alloc --extern kernel \
--extern build_error --extern macros \
--extern bindings \
--no-run --crate-name kernel -Zunstable-options \
--test-builder $(srctree)/scripts/rustdoc_test_builder.py \
$< $(rustdoc_test_kernel_quiet); \
Expand Down Expand Up @@ -234,10 +240,9 @@ rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
$(call if_changed,rustdoc_test)

rusttest-kernel: private rustc_target_flags = --extern alloc \
--extern build_error --extern macros
rusttest-kernel: private rustc_test_run_flags = --skip bindgen_test_layout_
--extern build_error --extern macros --extern bindings
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
rusttestlib-build_error rusttestlib-macros FORCE
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
$(call if_changed,rustc_test)
$(call if_changed,rustc_test_library)

Expand Down Expand Up @@ -335,6 +340,9 @@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
$(call if_changed,exports)

$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
$(call if_changed,exports)

$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
$(call if_changed,exports)

Expand Down Expand Up @@ -388,11 +396,16 @@ $(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
$(call if_changed_dep,rustc_library)

$(obj)/bindings.o: $(src)/kernel/bindings.rs \
$(obj)/compiler_builtins.o \
$(obj)/bindings_generated.rs \
$(obj)/bindings_helpers_generated.rs FORCE
$(call if_changed_dep,rustc_library)

$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
--extern build_error --extern macros
--extern build_error --extern macros --extern bindings
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
$(obj)/libmacros.so $(obj)/bindings_generated.rs \
$(obj)/bindings_helpers_generated.rs FORCE
$(obj)/libmacros.so $(obj)/bindings.o FORCE
$(call if_changed_dep,rustc_library)

endif # CONFIG_RUST
1 change: 1 addition & 0 deletions rust/exports.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

#include "exports_core_generated.h"
#include "exports_alloc_generated.h"
#include "exports_bindings_generated.h"
#include "exports_kernel_generated.h"
7 changes: 7 additions & 0 deletions rust/kernel/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
//! Bindings.
//!
//! Imports the generated bindings by `bindgen`.
//!
//! This crate may not be directly used. If you need a kernel C API that is
//! not ported or wrapped in the `kernel` crate, then do so first instead of
//! using this crate.
#![no_std]
#![feature(core_ffi_c)]
// See https://github.com/rust-lang/rust-bindgen/issues/1651.
#![cfg_attr(test, allow(deref_nullptr))]
#![cfg_attr(test, allow(unaligned_references))]
#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
#![allow(
clippy::all,
missing_docs,
non_camel_case_types,
non_upper_case_globals,
non_snake_case,
Expand Down
2 changes: 1 addition & 1 deletion rust/kernel/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ compile_error!("Missing kernel configuration for conditional compilation");
mod allocator;

#[doc(hidden)]
pub mod bindings;
pub use bindings;

#[cfg(CONFIG_ARM_AMBA)]
pub mod amba;
Expand Down
11 changes: 9 additions & 2 deletions scripts/generate_rust_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,20 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
["core", "compiler_builtins"],
)

append_crate(
"bindings",
srctree / "rust"/ "kernel" / "bindings.rs",
["core"],
cfg=cfg,
)
crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))

append_crate(
"kernel",
srctree / "rust" / "kernel" / "lib.rs",
["core", "alloc", "macros", "build_error"],
["core", "alloc", "macros", "build_error", "bindings"],
cfg=cfg,
)
crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
crates[-1]["source"] = {
"include_dirs": [
str(srctree / "rust" / "kernel"),
Expand Down

0 comments on commit b0ec4c8

Please sign in to comment.