Skip to content

Commit 736e435

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 736e435

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

rust/Makefile

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ 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
19-
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_kernel_generated.h
18+
obj-$(CONFIG_RUST) += alloc.o bindings.o kernel.o
19+
always-$(CONFIG_RUST) += exports_alloc_generated.h exports_bindings_generated.h \
20+
exports_kernel_generated.h
2021

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

112113
rustdoc-kernel: private rustc_target_flags = --extern alloc \
113-
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so
114+
--extern build_error --extern macros=$(objtree)/$(obj)/libmacros.so \
115+
--extern bindings
114116
rustdoc-kernel: $(src)/kernel/lib.rs rustdoc-core rustdoc-macros \
115117
rustdoc-compiler_builtins rustdoc-alloc $(obj)/libmacros.so \
116-
$(obj)/bindings_generated.rs $(obj)/bindings_helpers_generated.rs FORCE
118+
$(obj)/bindings.o FORCE
117119
$(call if_changed,rustdoc)
118120

119121
quiet_cmd_rustc_test_library = RUSTC TL $<
@@ -154,6 +156,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
154156
@$(objtree)/include/generated/rustc_cfg \
155157
-L$(objtree)/$(obj) --extern alloc --extern kernel \
156158
--extern build_error --extern macros \
159+
--extern bindings \
157160
--no-run --crate-name kernel -Zunstable-options \
158161
--test-builder $(srctree)/scripts/rustdoc_test_builder.py \
159162
$< $(rustdoc_test_kernel_quiet); \
@@ -234,8 +237,7 @@ rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
234237
$(call if_changed,rustdoc_test)
235238

236239
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_
240+
--extern build_error --extern macros --extern bindings
239241
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
240242
rusttestlib-build_error rusttestlib-macros FORCE
241243
$(call if_changed,rustc_test)
@@ -335,6 +337,9 @@ $(obj)/exports_core_generated.h: $(obj)/core.o FORCE
335337
$(obj)/exports_alloc_generated.h: $(obj)/alloc.o FORCE
336338
$(call if_changed,exports)
337339

340+
$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
341+
$(call if_changed,exports)
342+
338343
$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
339344
$(call if_changed,exports)
340345

@@ -388,11 +393,16 @@ $(obj)/alloc.o: $(src)/alloc/lib.rs $(obj)/compiler_builtins.o FORCE
388393
$(obj)/build_error.o: $(src)/build_error.rs $(obj)/compiler_builtins.o FORCE
389394
$(call if_changed_dep,rustc_library)
390395

396+
$(obj)/bindings.o: $(src)/kernel/bindings.rs \
397+
$(obj)/compiler_builtins.o \
398+
$(obj)/bindings_generated.rs \
399+
$(obj)/bindings_helpers_generated.rs FORCE
400+
$(call if_changed_dep,rustc_library)
401+
391402
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
392-
--extern build_error --extern macros
403+
--extern build_error --extern macros --extern bindings
393404
$(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
405+
$(obj)/libmacros.so $(obj)/bindings.o FORCE
396406
$(call if_changed_dep,rustc_library)
397407

398408
endif # CONFIG_RUST

rust/exports.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@
1717

1818
#include "exports_core_generated.h"
1919
#include "exports_alloc_generated.h"
20+
#include "exports_bindings_generated.h"
2021
#include "exports_kernel_generated.h"

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)