Skip to content

Commit b0ec4c8

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 <bjorn3_gh@protonmail.com>
1 parent fcbf909 commit b0ec4c8

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

rust/Makefile

Lines changed: 23 additions & 10 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 $<
@@ -135,6 +137,9 @@ rusttestlib-macros: private rustc_test_library_proc = yes
135137
rusttestlib-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
136138
$(call if_changed,rustc_test_library)
137139

140+
rusttestlib-bindings: $(src)/bindings/lib.rs rusttest-prepare FORCE
141+
$(call if_changed,rustc_test_library)
142+
138143
quiet_cmd_rustdoc_test = RUSTDOC T $<
139144
cmd_rustdoc_test = \
140145
OBJTREE=$(abspath $(objtree)) \
@@ -154,6 +159,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
154159
@$(objtree)/include/generated/rustc_cfg \
155160
-L$(objtree)/$(obj) --extern alloc --extern kernel \
156161
--extern build_error --extern macros \
162+
--extern bindings \
157163
--no-run --crate-name kernel -Zunstable-options \
158164
--test-builder $(srctree)/scripts/rustdoc_test_builder.py \
159165
$< $(rustdoc_test_kernel_quiet); \
@@ -234,10 +240,9 @@ rusttest-macros: $(src)/macros/lib.rs rusttest-prepare FORCE
234240
$(call if_changed,rustdoc_test)
235241

236242
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_
243+
--extern build_error --extern macros --extern bindings
239244
rusttest-kernel: $(src)/kernel/lib.rs rusttest-prepare \
240-
rusttestlib-build_error rusttestlib-macros FORCE
245+
rusttestlib-build_error rusttestlib-macros rusttestlib-bindings FORCE
241246
$(call if_changed,rustc_test)
242247
$(call if_changed,rustc_test_library)
243248

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

343+
$(obj)/exports_bindings_generated.h: $(obj)/bindings.o FORCE
344+
$(call if_changed,exports)
345+
338346
$(obj)/exports_kernel_generated.h: $(obj)/kernel.o FORCE
339347
$(call if_changed,exports)
340348

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

399+
$(obj)/bindings.o: $(src)/kernel/bindings.rs \
400+
$(obj)/compiler_builtins.o \
401+
$(obj)/bindings_generated.rs \
402+
$(obj)/bindings_helpers_generated.rs FORCE
403+
$(call if_changed_dep,rustc_library)
404+
391405
$(obj)/kernel.o: private rustc_target_flags = --extern alloc \
392-
--extern build_error --extern macros
406+
--extern build_error --extern macros --extern bindings
393407
$(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
408+
$(obj)/libmacros.so $(obj)/bindings.o FORCE
396409
$(call if_changed_dep,rustc_library)
397410

398411
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)