Skip to content

Commit d812f19

Browse files
committed
rust-analyzer for out-of-tree modules
1 parent e50706a commit d812f19

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,11 +1902,17 @@ help:
19021902
@echo ' modules - default target, build the module(s)'
19031903
@echo ' modules_install - install the module'
19041904
@echo ' clean - remove generated files in module directory only'
1905+
@echo ' rust-analyzer - Generate rust-project.json rust-analyzer support file'
19051906
@echo ''
19061907

19071908
# no-op for external module builds
19081909
PHONY += modules_prepare
19091910

1911+
# IDE support targets
1912+
PHONY += rust-analyzer
1913+
rust-analyzer:
1914+
$(Q)$(MAKE) $(build)=rust rust-analyzer-extmod
1915+
19101916
endif # KBUILD_EXTMOD
19111917

19121918
# ---------------------------------------------------------------------------

rust/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ rust-analyzer:
386386
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
387387
$(RUST_LIB_SRC) > $(objtree)/rust-project.json
388388

389+
rust-analyzer-extmod:
390+
$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(abs_srctree) $(abs_objtree) \
391+
$(RUST_LIB_SRC) ${KBUILD_EXTMOD} > $(extmod_prefix)/rust-project.json
392+
389393
$(obj)/core.o: private skip_clippy = 1
390394
$(obj)/core.o: private skip_flags = -Dunreachable_pub
391395
$(obj)/core.o: private rustc_target_flags = $(core-cfgs)

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)