Skip to content

Commit aa35a45

Browse files
committed
Split bindings crate out of the main kernel crate
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]>
1 parent fcbf909 commit aa35a45

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

rust/Makefile

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ always-$(CONFIG_RUST) += libmacros.so
1515
no-clean-files += libmacros.so
1616

1717
always-$(CONFIG_RUST) += bindings_generated.rs bindings_helpers_generated.rs
18-
obj-$(CONFIG_RUST) += alloc.o kernel.o
18+
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
1919
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_kernel_generated.h
2020

2121
ifdef CONFIG_RUST_BUILD_ASSERT_DENY
@@ -110,10 +110,11 @@ rustdoc-alloc: $(src)/alloc/lib.rs rustdoc-core rustdoc-compiler_builtins FORCE
110110
$(call if_changed,rustdoc)
111111

112112
rustdoc-kernel: private rustc_target_flags = --extern alloc \
113-
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so
113+
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
114+
--extern bindings
114115
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
115116
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
116-
$(obj)/bindings_generated.rs $(obj)/bindings_helpers_generated.rs FORCE
117+
$(obj)/bindings.o FORCE
117118
$(call if_changed,rustdoc)
118119

119120
quiet_cmd_rustc_test_library = RUSTC TL $<
@@ -154,6 +155,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
154155
@$(objtree)/include/generated/rustc_cfg \
155156
-L$(objtree)/$(obj) --extern alloc --extern kernel \
156157
--extern build_error --extern macros \
158+
--extern bindings \
157159
--no-run --crate-name kernel -Zunstable-options \
158160
--test-builder $(srctree)/scripts/rustdoc_test_builder.py \
159161
$< $(rustdoc_test_kernel_quiet); \
@@ -234,8 +236,7 @@ rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
234236
$(call if_changed,rustdoc_test)
235237

236238
rusttest-kernel: private rustc_target_flags = --extern alloc \
237-
--extern build_error --extern macros
238-
rusttest-kernel: private rustc_test_run_flags = --skip bindgen_test_layout_
239+
--extern build_error --extern macros --extern bindings
239240
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
240241
rusttestlib-build_error rusttestlib-macros FORCE
241242
$(call if_changed,rustc_test)
@@ -388,11 +389,16 @@ $(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
388389
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
389390
$(call if_changed_dep,rustc_library)
390391

392+
$(obj)/bindings.o: $(src)/kernel/bindings.rs \
393+
$(obj)/compiler_builtins.o \
394+
$(obj)/bindings_generated.rs \
395+
$(obj)/bindings_helpers_generated.rs FORCE
396+
$(call if_changed_dep,rustc_library)
397+
391398
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
392-
--extern build_error --extern macros
399+
--extern build_error --extern macros --extern bindings
393400
$(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \
394-
$(obj)/libmacros.so $(obj)/bindings_generated.rs \
395-
$(obj)/bindings_helpers_generated.rs FORCE
401+
$(obj)/libmacros.so $(obj)/bindings.o FORCE
396402
$(call if_changed_dep,rustc_library)
397403

398404
endif # CONFIG_RUST

rust/kernel/bindings.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
//! Bindings.
44
//!
55
//! Imports the generated bindings by `bindgen`.
6+
//!
7+
//! This crate may not be directly used. If you need a kernel C API that is
8+
//! not ported or wrapped in the kernel crate, then do so first instead of
9+
//! using this crate.
610
11+
#![no_std]
12+
#![feature(core_ffi_c)]
713
// See https://github.com/rust-lang/rust-bindgen/issues/1651.
814
#![cfg_attr(test, allow(deref_nullptr))]
915
#![cfg_attr(test, allow(unaligned_references))]
1016
#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
1117
#![allow(
1218
clippy::all,
19+
missing_docs,
1320
non_camel_case_types,
1421
non_upper_case_globals,
1522
non_snake_case,

rust/kernel/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ compile_error!("Missing kernel configuration for conditional compilation");
3939
mod allocator;
4040

4141
#[doc(hidden)]
42-
pub mod bindings;
42+
pub use bindings;
4343

4444
#[cfg(CONFIG_ARM_AMBA)]
4545
pub mod amba;

scripts/generate_rust_analyzer.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,20 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
7373
["core", "compiler_builtins"],
7474
)
7575

76+
append_crate(
77+
"bindings",
78+
srctree / "rust"/ "kernel" / "bindings.rs",
79+
["core"],
80+
cfg=cfg,
81+
)
82+
crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
83+
7684
append_crate(
7785
"kernel",
7886
srctree / "rust" / "kernel" / "lib.rs",
79-
["core", "alloc", "macros", "build_error"],
87+
["core", "alloc", "macros", "build_error", "bindings"],
8088
cfg=cfg,
8189
)
82-
crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
8390
crates[-1]["source"] = {
8491
"include_dirs": [
8592
str(srctree / "rust" / "kernel"),

0 commit comments

Comments
 (0)