Skip to content

Commit 26381ff

Browse files
vvarmaintel-lab-lkp
authored andcommitted
scripts: make rust-analyzer for out-of-tree modules
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#914 Link: Rust-for-Linux/rust-out-of-tree-module#2 Signed-off-by: Vinay Varma <[email protected]>
1 parent 009795d commit 26381ff

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
@@ -1856,11 +1856,6 @@ rustfmt:
18561856
rustfmtcheck: rustfmt_flags = --check
18571857
rustfmtcheck: rustfmt
18581858

1859-
# IDE support targets
1860-
PHONY += rust-analyzer
1861-
rust-analyzer:
1862-
$(Q)$(MAKE) $(build)=rust $@
1863-
18641859
# Misc
18651860
# ---------------------------------------------------------------------------
18661861

@@ -1921,6 +1916,7 @@ help:
19211916
@echo ' modules - default target, build the module(s)'
19221917
@echo ' modules_install - install the module'
19231918
@echo ' clean - remove generated files in module directory only'
1919+
@echo ' rust-analyzer - generate rust-project.json rust-analyzer support file'
19241920
@echo ''
19251921

19261922
endif # KBUILD_EXTMOD
@@ -2058,6 +2054,11 @@ quiet_cmd_tags = GEN $@
20582054
tags TAGS cscope gtags: FORCE
20592055
$(call cmd,tags)
20602056

2057+
# IDE support targets
2058+
PHONY += rust-analyzer
2059+
rust-analyzer:
2060+
$(Q)$(MAKE) $(build)=rust $@
2061+
20612062
# Script to generate missing namespace dependencies
20622063
# ---------------------------------------------------------------------------
20632064

rust/Makefile

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

362362
rust-analyzer:
363-
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
364-
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
363+
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
364+
$(abs_srctree) $(abs_objtree) \
365+
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
366+
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
365367

366368
redirect-intrinsics = \
367369
__eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \

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
"build_error",
@@ -95,19 +96,26 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
9596
"exclude_dirs": [],
9697
}
9798

99+
def is_root_crate(build_file, target):
100+
try:
101+
return f"{target}.o" in open(build_file).read()
102+
except FileNotFoundError:
103+
return False
104+
98105
# Then, the rest outside of `rust/`.
99106
#
100107
# We explicitly mention the top-level folders we want to cover.
101-
for folder in ("samples", "drivers"):
102-
for path in (srctree / folder).rglob("*.rs"):
108+
extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
109+
if external_src is not None:
110+
extra_dirs = [external_src]
111+
for folder in extra_dirs:
112+
for path in folder.rglob("*.rs"):
103113
logging.info("Checking %s", path)
104114
name = path.name.replace(".rs", "")
105115

106116
# Skip those that are not crate roots.
107-
try:
108-
if f"{name}.o" not in open(path.parent / "Makefile").read():
109-
continue
110-
except FileNotFoundError:
117+
if not is_root_crate(path.parent / "Makefile", name) and \
118+
not is_root_crate(path.parent / "Kbuild", name):
111119
continue
112120

113121
logging.info("Adding %s", name)
@@ -126,6 +134,7 @@ def main():
126134
parser.add_argument("srctree", type=pathlib.Path)
127135
parser.add_argument("objtree", type=pathlib.Path)
128136
parser.add_argument("sysroot_src", type=pathlib.Path)
137+
parser.add_argument("exttree", type=pathlib.Path, nargs="?")
129138
args = parser.parse_args()
130139

131140
logging.basicConfig(
@@ -134,7 +143,7 @@ def main():
134143
)
135144

136145
rust_project = {
137-
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src),
146+
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree),
138147
"sysroot_src": str(args.sysroot_src),
139148
}
140149

0 commit comments

Comments
 (0)