Skip to content

Commit 96f2342

Browse files
committed
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. Signed-off-by: Vinay Varma <[email protected]>
1 parent d9b2e84 commit 96f2342

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,11 +1831,6 @@ rustfmt:
18311831
rustfmtcheck: rustfmt_flags = --check
18321832
rustfmtcheck: rustfmt
18331833

1834-
# IDE support targets
1835-
PHONY += rust-analyzer
1836-
rust-analyzer:
1837-
$(Q)$(MAKE) $(build)=rust $@
1838-
18391834
# Misc
18401835
# ---------------------------------------------------------------------------
18411836

@@ -1888,6 +1883,7 @@ help:
18881883
@echo ' modules - default target, build the module(s)'
18891884
@echo ' modules_install - install the module'
18901885
@echo ' clean - remove generated files in module directory only'
1886+
@echo ' rust-analyzer - generate rust-project.json rust-analyzer support file'
18911887
@echo ''
18921888

18931889
endif # KBUILD_EXTMOD
@@ -2022,6 +2018,12 @@ quiet_cmd_tags = GEN $@
20222018
tags TAGS cscope gtags: FORCE
20232019
$(call cmd,tags)
20242020

2021+
# IDE support targets
2022+
PHONY += rust-analyzer
2023+
rust-analyzer:
2024+
$(Q)$(MAKE) $(build)=rust $@
2025+
2026+
20252027
# Script to generate missing namespace dependencies
20262028
# ---------------------------------------------------------------------------
20272029

rust/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,14 @@ quiet_cmd_rustc_library = $(if $(skip_clippy),RUSTC,$(RUSTC_OR_CLIPPY_QUIET)) L
388388
sed -i '/^\#/d' $(depfile) \
389389
$(if $(rustc_objcopy),;$(OBJCOPY) $(rustc_objcopy) $@)
390390

391+
# We conditionally use abs_{srctree,objtree} for external modules so that
392+
# the lsp (rust-analyzer) can find the path to the kernel source files
391393
rust-analyzer:
392-
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
393-
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
394+
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
395+
$(if $(KBUILD_EXTMOD),$(abs_srctree),$(srctree)) \
396+
$(if $(KBUILD_EXTMOD),$(abs_objtree),$(objtree)) \
397+
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
398+
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
394399

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

scripts/generate_rust_analyzer.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import logging
99
import pathlib
1010
import sys
11+
import os
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",
@@ -98,13 +99,15 @@ def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=Tr
9899
# Then, the rest outside of `rust/`.
99100
#
100101
# We explicitly mention the top-level folders we want to cover.
101-
for folder in ("samples", "drivers"):
102+
extra_src_dirs = ["samples", "drivers"] if external_src is None else [external_src]
103+
104+
for folder in extra_src_dirs:
102105
for path in (srctree / folder).rglob("*.rs"):
103106
logging.info("Checking %s", path)
104107
name = path.name.replace(".rs", "")
105108

106109
# Skip those that are not crate roots.
107-
if f"{name}.o" not in open(path.parent / "Makefile").read():
110+
if os.path.exists(path.parent / "Makefile") and f"{name}.o" not in open(path.parent / "Makefile").read():
108111
continue
109112

110113
logging.info("Adding %s", name)
@@ -123,6 +126,7 @@ def main():
123126
parser.add_argument("srctree", type=pathlib.Path)
124127
parser.add_argument("objtree", type=pathlib.Path)
125128
parser.add_argument("sysroot_src", type=pathlib.Path)
129+
parser.add_argument("exttree", type=pathlib.Path, nargs='?')
126130
args = parser.parse_args()
127131

128132
logging.basicConfig(
@@ -131,7 +135,7 @@ def main():
131135
)
132136

133137
rust_project = {
134-
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src),
138+
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree),
135139
"sysroot_src": str(args.sysroot_src),
136140
}
137141

0 commit comments

Comments
 (0)