Skip to content

Commit 82cbfb1

Browse files
committed
rust: depend on syn
add `syn` and others as dependencies of `macros` crate, and use cargo to build `macros` crate. Only host-facing `libmacros.so` is built this way, not any of the other crates that are compiled for the target. This cargo invocation has all crate versions locked and has `--offline` option specified, so it won't access Internet during the build. The current `module!` crate already shows it tedious and limited for writing proc macros without `syn`. Pinning and vtable handling could likely be drastically improved with the help of proc macros, and they will require parsing Rust struct/trait/impl blocks. A dependency on `syn` is highly desirable to avoid us essentially reinventing the wheel when building our procedural macros. A `make rust-fetch-deps` command is added and listed in the documentation as a build requirement. Signed-off-by: Gary Guo <gary@garyguo.net>
1 parent 62498e0 commit 82cbfb1

File tree

7 files changed

+99
-16
lines changed

7 files changed

+99
-16
lines changed

.github/workflows/ci.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ jobs:
205205
- run: |
206206
curl -o bin/bindgen https://raw.githubusercontent.com/Rust-for-Linux/ci-bin/master/bindgen-0.56.0/bin/bindgen
207207
chmod u+x bin/bindgen
208+
209+
# Setup: Procedural macro dependencies
210+
- run: |
211+
make rust-fetch-deps
208212
209213
# Setup: ccache
210214
- run: |

Documentation/rust/quick-start.rst

+12
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ Install it via (this will build the tool from source)::
8989
cargo install --locked --version 0.56.0 bindgen
9090

9191

92+
Procedural macro dependencies
93+
*****************************
94+
95+
We use procedural macros that need to parse Rust code. We depends on a few
96+
well-established crate in Rust community (`syn`, `quote` and `proc-macro2`)
97+
for this task.
98+
99+
Fetch it via::
100+
101+
make rust-fetch-deps
102+
103+
92104
Requirements: Developing
93105
------------------------
94106

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -1832,6 +1832,11 @@ rustfmt:
18321832
rustfmtcheck:
18331833
find $(srctree) -type f -name '*.rs' | xargs $(RUSTFMT) --check
18341834

1835+
# Procedural macros dependency fetch
1836+
PHONY += rust-fetch-deps
1837+
rust-fetch-deps:
1838+
$(Q)$(MAKE) $(build)=rust $@
1839+
18351840
# IDE support targets
18361841
PHONY += rust-analyzer
18371842
rust-analyzer: prepare0

rust/.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
bindings_generated.rs
44
exports_*_generated.h
5-
doc/
5+
doc/
6+
target/

rust/Makefile

+15-15
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,15 @@ quiet_cmd_rustdoc = RUSTDOC $<
3535
--output $(objtree)/rust/doc --crate-name $(subst rustdoc-,,$@) \
3636
-Fmissing-docs @$(objtree)/include/generated/rustc_cfg $<
3737

38-
rustdoc: rustdoc-macros rustdoc-compiler_builtins rustdoc-kernel
39-
40-
rustdoc-macros: private rustdoc_target_flags = --crate-type proc-macro \
41-
--extern proc_macro
42-
rustdoc-macros: $(srctree)/rust/macros/lib.rs FORCE
43-
$(call if_changed,rustdoc_host)
38+
rustdoc: rustdoc-compiler_builtins rustdoc-kernel
4439

4540
rustdoc-compiler_builtins: $(srctree)/rust/compiler_builtins.rs FORCE
4641
$(call if_changed,rustdoc)
4742

4843
rustdoc-kernel: private rustdoc_target_flags = --extern alloc \
4944
--extern build_error \
5045
--extern macros=$(objtree)/rust/libmacros.so
51-
rustdoc-kernel: $(srctree)/rust/kernel/lib.rs rustdoc-macros \
46+
rustdoc-kernel: $(srctree)/rust/kernel/lib.rs \
5247
$(objtree)/rust/libmacros.so $(objtree)/rust/bindings_generated.rs FORCE
5348
$(call if_changed,rustdoc)
5449

@@ -113,22 +108,27 @@ $(objtree)/rust/exports_alloc_generated.h: $(objtree)/rust/alloc.o FORCE
113108
$(objtree)/rust/exports_kernel_generated.h: $(objtree)/rust/kernel.o FORCE
114109
$(call if_changed,exports)
115110

111+
CARGO = cargo
112+
116113
# `-Cpanic=unwind -Cforce-unwind-tables=y` overrides `rustc_flags` in order to
117114
# avoid the https://github.com/rust-lang/rust/issues/82320 rustc crash.
118-
quiet_cmd_rustc_procmacro = $(RUSTC_OR_CLIPPY_QUIET) P $@
115+
quiet_cmd_rustc_procmacro = CARGO P $@
119116
cmd_rustc_procmacro = \
120-
$(RUSTC_OR_CLIPPY) $(rustc_flags) \
121-
--emit=dep-info,link --extern proc_macro \
122-
-Cpanic=unwind -Cforce-unwind-tables=y \
123-
--crate-type proc-macro --out-dir $(objtree)/rust/ \
124-
--crate-name $(patsubst lib%.so,%,$(notdir $@)) $<; \
125-
mv $(objtree)/rust/$(patsubst lib%.so,%,$(notdir $@)).d $(depfile); \
117+
$(CARGO) build --locked --offline --release \
118+
--manifest-path $< \
119+
--target-dir $(objtree)/rust/target; \
120+
cp $(objtree)/rust/target/release/$(notdir $@) $@; \
121+
mv $(objtree)/rust/target/release/$(patsubst %.so,%,$(notdir $@)).d $(depfile); \
122+
sed -i 's/build.rs//' $(depfile); \
126123
sed -i '/^\#/d' $(depfile)
127124

125+
rust-fetch-deps:
126+
$(CARGO) fetch --locked --manifest-path $(srctree)/rust/macros/Cargo.toml
127+
128128
# Procedural macros can only be used with the `rustc` that compiled it.
129129
# Therefore, to get `libmacros.so` automatically recompiled when the compiler
130130
# version changes, we add `core.o` as a dependency (even if it is not needed).
131-
$(objtree)/rust/libmacros.so: $(srctree)/rust/macros/lib.rs \
131+
$(objtree)/rust/libmacros.so: $(srctree)/rust/macros/Cargo.toml \
132132
$(objtree)/rust/core.o FORCE
133133
$(call if_changed_dep,rustc_procmacro)
134134

rust/macros/Cargo.lock

+47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/macros/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "macros"
3+
version = "0.0.0"
4+
edition = "2018"
5+
license = "GPL-2.0-only"
6+
7+
[lib]
8+
path = "lib.rs"
9+
proc_macro = true
10+
11+
[dependencies]
12+
syn = { version = "1.0", features = ["full"] }
13+
quote = "1.0"
14+
proc-macro2 = "1.0"

0 commit comments

Comments
 (0)