Skip to content

Commit

Permalink
Adding make target and script for extending rust-project.json from th…
Browse files Browse the repository at this point in the history
…e linux/rust Makefile
  • Loading branch information
thepacketgeek committed Dec 10, 2022
1 parent 427f696 commit a75095a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
*.rmeta
Module.symvers
modules.order
rust-project.json
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ KDIR ?= /lib/modules/`uname -r`/build

default:
$(MAKE) -C $(KDIR) M=$$PWD

rust-analyzer:
$(MAKE) -C $(KDIR) rust-analyzer
$(Q) ./scripts/generate_rust_analyzer.py $(KDIR) `ls *.rs | head -n 1` > rust-project.json

clean:
$(MAKE) -C $(KDIR) M=$$PWD clean
67 changes: 67 additions & 0 deletions scripts/generate_rust_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
"""generate_rust_analyzer - Generates an out-of-tree module `rust-project.json` file for `rust-analyzer`
based on the kernel rust-project.json.
"""

import argparse
import json
import logging
import os
import pathlib
import sys

def generate_rust_project(kdir, root_module):
with open(kdir / "rust-project.json") as fd:
rust_project = json.loads(fd.read())

crate_indices = {}

for i, crate in enumerate(rust_project["crates"]):
crate_indices[crate["display_name"]] = i

# Prepend kdir to existing root_module
crate["root_module"] = os.path.join(kdir, crate["root_module"])
if crate.get("source"):
for e_dir in crate["source"].get("exclude_dirs", []):
e_dir = os.path.join(kdir, e_dir)
for i_dir in crate["source"].get("incude_dirs", []):
i_dir = os.path.join(kdir, i_dir)

# Finally, append this module as a crate
rust_project["crates"].append({
"display_name": root_module.removesuffix(".rs"),
"root_module": root_module,
"is_workspace_member": False,
"is_proc_macro": False,
"deps": [
{"crate": index, "name": name}
for name, index in crate_indices.items()
if name == "kernel"
],
"cfg": [],
"edition": "2021",
"env": {
"RUST_MODFILE": "This is only for rust-analyzer"
}
})
return rust_project


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument("kdir", type=pathlib.Path)
parser.add_argument("root_module", type=str)
args = parser.parse_args()

logging.basicConfig(
format="[%(asctime)s] [%(levelname)s] %(message)s",
level=logging.INFO if args.verbose else logging.WARNING
)

rust_project = generate_rust_project(args.kdir, args.root_module)
json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)

if __name__ == "__main__":
main()

0 comments on commit a75095a

Please sign in to comment.