Skip to content

Commit 1893d40

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 7adf14d commit 1893d40

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,11 +1854,6 @@ rustfmt:
18541854
rustfmtcheck: rustfmt_flags = --check
18551855
rustfmtcheck: rustfmt
18561856

1857-
# IDE support targets
1858-
PHONY += rust-analyzer
1859-
rust-analyzer:
1860-
$(Q)$(MAKE) $(build)=rust $@
1861-
18621857
# Misc
18631858
# ---------------------------------------------------------------------------
18641859

@@ -1917,6 +1912,7 @@ help:
19171912
@echo ' modules - default target, build the module(s)'
19181913
@echo ' modules_install - install the module'
19191914
@echo ' clean - remove generated files in module directory only'
1915+
@echo ' rust-analyzer - generate rust-project.json rust-analyzer support file'
19201916
@echo ''
19211917

19221918
endif # KBUILD_EXTMOD
@@ -2054,6 +2050,11 @@ quiet_cmd_tags = GEN $@
20542050
tags TAGS cscope gtags: FORCE
20552051
$(call cmd,tags)
20562052

2053+
# IDE support targets
2054+
PHONY += rust-analyzer
2055+
rust-analyzer:
2056+
$(Q)$(MAKE) $(build)=rust $@
2057+
20572058
# Script to generate missing namespace dependencies
20582059
# ---------------------------------------------------------------------------
20592060

rust/Makefile

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

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

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

scripts/generate_rust_analyzer.py

Lines changed: 18 additions & 13 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,25 +96,28 @@ 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+
return os.path.exists(build_file) and target in open(build_file).read()
101+
98102
# Then, the rest outside of `rust/`.
99103
#
100104
# We explicitly mention the top-level folders we want to cover.
101-
for folder in ("samples", "drivers"):
105+
for folder in ("samples", "drivers") if external_src is None else [external_src]:
102106
for path in (srctree / folder).rglob("*.rs"):
103107
logging.info("Checking %s", path)
104108
name = path.name.replace(".rs", "")
105109

106110
# Skip those that are not crate roots.
107-
if f"{name}.o" not in open(path.parent / "Makefile").read():
108-
continue
111+
if is_root_crate(path.parent / "Makefile", f"{name}.o") or \
112+
is_root_crate(path.parent / "Kbuild", f"{name}.o"):
109113

110-
logging.info("Adding %s", name)
111-
append_crate(
112-
name,
113-
path,
114-
["core", "alloc", "kernel"],
115-
cfg=cfg,
116-
)
114+
logging.info("Adding %s", name)
115+
append_crate(
116+
name,
117+
path,
118+
["core", "alloc", "kernel"],
119+
cfg=cfg,
120+
)
117121

118122
return crates
119123

@@ -123,6 +127,7 @@ def main():
123127
parser.add_argument("srctree", type=pathlib.Path)
124128
parser.add_argument("objtree", type=pathlib.Path)
125129
parser.add_argument("sysroot_src", type=pathlib.Path)
130+
parser.add_argument("exttree", type=pathlib.Path, nargs="?")
126131
args = parser.parse_args()
127132

128133
logging.basicConfig(
@@ -131,7 +136,7 @@ def main():
131136
)
132137

133138
rust_project = {
134-
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src),
139+
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree),
135140
"sysroot_src": str(args.sysroot_src),
136141
}
137142

0 commit comments

Comments
 (0)