Skip to content

Commit 49a9ef7

Browse files
vvarmaojeda
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: #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]>
1 parent 0beaf54 commit 49a9ef7

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

Makefile

+6-5
Original file line numberDiff line numberDiff line change
@@ -1859,11 +1859,6 @@ rustfmt:
18591859
rustfmtcheck: rustfmt_flags = --check
18601860
rustfmtcheck: rustfmt
18611861

1862-
# IDE support targets
1863-
PHONY += rust-analyzer
1864-
rust-analyzer:
1865-
$(Q)$(MAKE) $(build)=rust $@
1866-
18671862
# Misc
18681863
# ---------------------------------------------------------------------------
18691864

@@ -1924,6 +1919,7 @@ help:
19241919
@echo ' modules - default target, build the module(s)'
19251920
@echo ' modules_install - install the module'
19261921
@echo ' clean - remove generated files in module directory only'
1922+
@echo ' rust-analyzer - generate rust-project.json rust-analyzer support file'
19271923
@echo ''
19281924

19291925
__external_modules_error:
@@ -2065,6 +2061,11 @@ quiet_cmd_tags = GEN $@
20652061
tags TAGS cscope gtags: FORCE
20662062
$(call cmd,tags)
20672063

2064+
# IDE support targets
2065+
PHONY += rust-analyzer
2066+
rust-analyzer:
2067+
$(Q)$(MAKE) $(build)=rust $@
2068+
20682069
# Script to generate missing namespace dependencies
20692070
# ---------------------------------------------------------------------------
20702071

rust/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,10 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
373373
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
374374

375375
rust-analyzer:
376-
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
377-
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
376+
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
377+
$(abs_srctree) $(abs_objtree) \
378+
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
379+
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
378380

379381
redirect-intrinsics = \
380382
__eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \

scripts/generate_rust_analyzer.py

+18-9
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)