Skip to content

Commit b6b312e

Browse files
vvarmaSasha Levin
authored and
Sasha Levin
committed
scripts: make rust-analyzer for out-of-tree modules
[ Upstream commit 49a9ef7 ] Adds support for out-of-tree rust modules to use the `rust-analyzer` make target to generate the rust-project.json file. The change involves adding an optional parameter `external_src` to the `generate_rust_analyzer.py` which expects the path to the out-of-tree module's source directory. When this parameter is passed, I have chosen not to add the non-core modules (samples and drivers) into the result since these are not expected to be used in third party modules. Related changes are also made to the Makefile and rust/Makefile allowing the `rust-analyzer` target to be used for out-of-tree modules as well. Link: Rust-for-Linux/linux#914 Link: Rust-for-Linux/rust-out-of-tree-module#2 Signed-off-by: Vinay Varma <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]> Stable-dep-of: 2e0f91a ("scripts: generate_rust_analyzer: add missing macros deps") Signed-off-by: Sasha Levin <[email protected]>
1 parent 0b23a00 commit b6b312e

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,11 +1851,6 @@ rustfmt:
18511851
rustfmtcheck: rustfmt_flags = --check
18521852
rustfmtcheck: rustfmt
18531853

1854-
# IDE support targets
1855-
PHONY += rust-analyzer
1856-
rust-analyzer:
1857-
$(Q)$(MAKE) $(build)=rust $@
1858-
18591854
# Misc
18601855
# ---------------------------------------------------------------------------
18611856

@@ -1908,6 +1903,7 @@ help:
19081903
@echo ' modules - default target, build the module(s)'
19091904
@echo ' modules_install - install the module'
19101905
@echo ' clean - remove generated files in module directory only'
1906+
@echo ' rust-analyzer - generate rust-project.json rust-analyzer support file'
19111907
@echo ''
19121908

19131909
endif # KBUILD_EXTMOD
@@ -2044,6 +2040,11 @@ quiet_cmd_tags = GEN $@
20442040
tags TAGS cscope gtags: FORCE
20452041
$(call cmd,tags)
20462042

2043+
# IDE support targets
2044+
PHONY += rust-analyzer
2045+
rust-analyzer:
2046+
$(Q)$(MAKE) $(build)=rust $@
2047+
20472048
# Script to generate missing namespace dependencies
20482049
# ---------------------------------------------------------------------------
20492050

rust/Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
344344
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
345345

346346
rust-analyzer:
347-
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
348-
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
347+
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
348+
$(abs_srctree) $(abs_objtree) \
349+
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
350+
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
349351

350352
$(obj)/core.o: private skip_clippy = 1
351353
$(obj)/core.o: private skip_flags = -Dunreachable_pub

scripts/generate_rust_analyzer.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import argparse
77
import json
88
import logging
9+
import os
910
import pathlib
1011
import sys
1112

12-
def generate_crates(srctree, objtree, sysroot_src):
13+
def generate_crates(srctree, objtree, sysroot_src, external_src):
1314
# Generate the configuration list.
1415
cfg = []
1516
with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
@@ -65,7 +66,7 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
6566
[],
6667
is_proc_macro=True,
6768
)
68-
crates[-1]["proc_macro_dylib_path"] = "rust/libmacros.so"
69+
crates[-1]["proc_macro_dylib_path"] = f"{objtree}/rust/libmacros.so"
6970

7071
append_crate(
7172
"bindings",
@@ -89,19 +90,26 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
8990
"exclude_dirs": [],
9091
}
9192

93+
def is_root_crate(build_file, target):
94+
try:
95+
return f"{target}.o" in open(build_file).read()
96+
except FileNotFoundError:
97+
return False
98+
9299
# Then, the rest outside of `rust/`.
93100
#
94101
# We explicitly mention the top-level folders we want to cover.
95-
for folder in ("samples", "drivers"):
96-
for path in (srctree / folder).rglob("*.rs"):
102+
extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
103+
if external_src is not None:
104+
extra_dirs = [external_src]
105+
for folder in extra_dirs:
106+
for path in folder.rglob("*.rs"):
97107
logging.info("Checking %s", path)
98108
name = path.name.replace(".rs", "")
99109

100110
# Skip those that are not crate roots.
101-
try:
102-
if f"{name}.o" not in open(path.parent / "Makefile").read():
103-
continue
104-
except FileNotFoundError:
111+
if not is_root_crate(path.parent / "Makefile", name) and \
112+
not is_root_crate(path.parent / "Kbuild", name):
105113
continue
106114

107115
logging.info("Adding %s", name)
@@ -120,6 +128,7 @@ def main():
120128
parser.add_argument("srctree", type=pathlib.Path)
121129
parser.add_argument("objtree", type=pathlib.Path)
122130
parser.add_argument("sysroot_src", type=pathlib.Path)
131+
parser.add_argument("exttree", type=pathlib.Path, nargs="?")
123132
args = parser.parse_args()
124133

125134
logging.basicConfig(
@@ -128,7 +137,7 @@ def main():
128137
)
129138

130139
rust_project = {
131-
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src),
140+
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree),
132141
"sysroot_src": str(args.sysroot_src),
133142
}
134143

0 commit comments

Comments
 (0)