Skip to content

Commit 72a19d6

Browse files
authored
Merge pull request #822 from bjorn3/split_bindings_off
Split bindings crate out of the main kernel crate
2 parents fcbf909 + 521024c commit 72a19d6

File tree

6 files changed

+53
-22
lines changed

6 files changed

+53
-22
lines changed

rust/Makefile

+30-17
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ CFLAGS_REMOVE_helpers.o = -Wmissing-prototypes -Wmissing-declarations
1414
always-$(CONFIG_RUST) += libmacros.so
1515
no-clean-files += libmacros.so
1616

17-
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
17+
always-$(CONFIG_RUST) += bindings/bindings_generated.rs bindings/bindings_helpers_generated.rs
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

@@ -302,24 +307,24 @@ quiet_cmd_bindgen = BINDGEN $@
302307
--size_t-is-usize -o $@ -- $(bindgen_c_flags_final) -DMODULE \
303308
$(bindgen_target_cflags) $(bindgen_target_extra)
304309

305-
$(obj)/bindings_generated.rs: private bindgen_target_flags = \
310+
$(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \
306311
$(shell grep -v '^\#\|^$$' $(srctree)/$(src)/bindgen_parameters)
307-
$(obj)/bindings_generated.rs: $(src)/kernel/bindings_helper.h \
312+
$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \
308313
$(src)/bindgen_parameters FORCE
309314
$(call if_changed_dep,bindgen)
310315

311316
# See `CFLAGS_REMOVE_helpers.o` above. In addition, Clang on C does not warn
312317
# with `-Wmissing-declarations` (unlike GCC), so it is not strictly needed here
313318
# given it is `libclang`; but for consistency, future Clang changes and/or
314319
# a potential future GCC backend for `bindgen`, we disable it too.
315-
$(obj)/bindings_helpers_generated.rs: private bindgen_target_flags = \
320+
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_flags = \
316321
--blacklist-type '.*' --whitelist-var '' \
317322
--whitelist-function 'rust_helper_.*'
318-
$(obj)/bindings_helpers_generated.rs: private bindgen_target_cflags = \
323+
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_cflags = \
319324
-I$(objtree)/$(obj) -Wno-missing-prototypes -Wno-missing-declarations
320-
$(obj)/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
325+
$(obj)/bindings/bindings_helpers_generated.rs: private bindgen_target_extra = ; \
321326
sed -Ei 's/pub fn rust_helper_([a-zA-Z0-9_]*)/#[link_name="rust_helper_\1"]\n pub fn \1/g' $@
322-
$(obj)/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
327+
$(obj)/bindings/bindings_helpers_generated.rs: $(src)/helpers.c FORCE
323328
$(call if_changed_dep,bindgen)
324329

325330
quiet_cmd_exports = EXPORTS $@
@@ -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)/bindings/lib.rs \
400+
$(obj)/compiler_builtins.o \
401+
$(obj)/bindings/bindings_generated.rs \
402+
$(obj)/bindings/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
File renamed without changes.

rust/kernel/bindings.rs renamed to rust/bindings/lib.rs

+12-2
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,
@@ -22,7 +29,10 @@ mod bindings_raw {
2229
// Use glob import here to expose all helpers.
2330
// Symbols defined within the module will take precedence to the glob import.
2431
pub use super::bindings_helper::*;
25-
include!(concat!(env!("OBJTREE"), "/rust/bindings_generated.rs"));
32+
include!(concat!(
33+
env!("OBJTREE"),
34+
"/rust/bindings/bindings_generated.rs"
35+
));
2636
}
2737

2838
// When both a directly exposed symbol and a helper exists for the same function,
@@ -34,7 +44,7 @@ mod bindings_helper {
3444
use super::bindings_raw::*;
3545
include!(concat!(
3646
env!("OBJTREE"),
37-
"/rust/bindings_helpers_generated.rs"
47+
"/rust/bindings/bindings_helpers_generated.rs"
3848
));
3949
}
4050

rust/exports.c

+1
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/lib.rs

+1-1
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

+9-2
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"/ "bindings" / "lib.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)