Skip to content

Commit 615f4f2

Browse files
committed
Adding make target and script for extending rust-project.json from the linux/rust Makefile
1 parent 427f696 commit 615f4f2

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
*.rmeta
99
Module.symvers
1010
modules.order
11+
rust-project.json

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ KDIR ?= /lib/modules/`uname -r`/build
44

55
default:
66
$(MAKE) -C $(KDIR) M=$$PWD
7+
8+
rust-analyzer:
9+
$(MAKE) -C $(KDIR) rust-analyzer
10+
$(Q) ./scripts/generate_rust_analyzer.py $(KDIR) `ls *.rs | head -n 1` > rust-project.json
11+
12+
clean:
13+
$(MAKE) -C $(KDIR) M=$$PWD clean

scripts/generate_rust_analyzer.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: GPL-2.0
3+
"""generate_rust_analyzer - Generates an out-of-tree module `rust-project.json` file for `rust-analyzer`
4+
based on the kernel rust-project.json.
5+
"""
6+
7+
import argparse
8+
import json
9+
import logging
10+
import os
11+
import pathlib
12+
import sys
13+
14+
def generate_rust_project(kdir, root_module):
15+
with open(kdir / "rust-project.json") as fd:
16+
rust_project = json.loads(fd.read())
17+
18+
crate_indices = {}
19+
20+
for i, crate in enumerate(rust_project["crates"]):
21+
crate_indices[crate["display_name"]] = i
22+
23+
# Prepend kdir to existing root_module
24+
crate["root_module"] = os.path.join(kdir, crate["root_module"])
25+
if crate.get("source"):
26+
if "exclude_dirs" in crate["source"]:
27+
crate["source"]["exclude_dirs"] = [
28+
os.path.join(kdir, e_dir) for e_dir in crate["source"]["exclude_dirs"]
29+
]
30+
if "include_dirs" in crate["source"]:
31+
crate["source"]["include_dirs"] = [
32+
os.path.join(kdir, i_dir) for i_dir in crate["source"]["include_dirs"]
33+
]
34+
35+
# Finally, append this module as a crate
36+
rust_project["crates"].append({
37+
"display_name": root_module.removesuffix(".rs"),
38+
"root_module": root_module,
39+
"is_workspace_member": False,
40+
"is_proc_macro": False,
41+
"deps": [
42+
{"crate": index, "name": name}
43+
for name, index in crate_indices.items()
44+
if name == "kernel"
45+
],
46+
"cfg": [],
47+
"edition": "2021",
48+
"env": {
49+
"RUST_MODFILE": "This is only for rust-analyzer"
50+
}
51+
})
52+
return rust_project
53+
54+
55+
def main():
56+
parser = argparse.ArgumentParser()
57+
parser.add_argument('--verbose', '-v', action='store_true')
58+
parser.add_argument("kdir", type=pathlib.Path)
59+
parser.add_argument("root_module", type=str)
60+
args = parser.parse_args()
61+
62+
logging.basicConfig(
63+
format="[%(asctime)s] [%(levelname)s] %(message)s",
64+
level=logging.INFO if args.verbose else logging.WARNING
65+
)
66+
67+
rust_project = generate_rust_project(args.kdir, args.root_module)
68+
json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
69+
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)