xref: /linux/scripts/generate_rust_analyzer.py (revision d7659acca7a390b5830f0b67f3aa4a5f9929ab79)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0
3"""generate_rust_analyzer - Generates the `rust-project.json` file for `rust-analyzer`.
4"""
5
6import argparse
7import json
8import logging
9import os
10import pathlib
11import subprocess
12import sys
13
14def args_crates_cfgs(cfgs):
15    crates_cfgs = {}
16    for cfg in cfgs:
17        crate, vals = cfg.split("=", 1)
18        crates_cfgs[crate] = vals.replace("--cfg", "").split()
19
20    return crates_cfgs
21
22def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
23    # Generate the configuration list.
24    cfg = []
25    with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
26        for line in fd:
27            line = line.replace("--cfg=", "")
28            line = line.replace("\n", "")
29            cfg.append(line)
30
31    # Now fill the crates list -- dependencies need to come first.
32    #
33    # Avoid O(n^2) iterations by keeping a map of indexes.
34    crates = []
35    crates_indexes = {}
36    crates_cfgs = args_crates_cfgs(cfgs)
37
38    def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
39        crate = {
40            "display_name": display_name,
41            "root_module": str(root_module),
42            "is_workspace_member": is_workspace_member,
43            "is_proc_macro": is_proc_macro,
44            "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
45            "cfg": cfg,
46            "edition": "2021",
47            "env": {
48                "RUST_MODFILE": "This is only for rust-analyzer"
49            }
50        }
51        if is_proc_macro:
52            proc_macro_dylib_name = subprocess.check_output(
53                [os.environ["RUSTC"], "--print", "file-names", "--crate-name", display_name, "--crate-type", "proc-macro", "-"],
54                stdin=subprocess.DEVNULL,
55            ).decode('utf-8').strip()
56            crate["proc_macro_dylib_path"] = f"{objtree}/rust/{proc_macro_dylib_name}"
57        crates_indexes[display_name] = len(crates)
58        crates.append(crate)
59
60    # First, the ones in `rust/` since they are a bit special.
61    append_crate(
62        "core",
63        sysroot_src / "core" / "src" / "lib.rs",
64        [],
65        cfg=crates_cfgs.get("core", []),
66        is_workspace_member=False,
67    )
68
69    append_crate(
70        "compiler_builtins",
71        srctree / "rust" / "compiler_builtins.rs",
72        [],
73    )
74
75    append_crate(
76        "macros",
77        srctree / "rust" / "macros" / "lib.rs",
78        [],
79        is_proc_macro=True,
80    )
81
82    append_crate(
83        "build_error",
84        srctree / "rust" / "build_error.rs",
85        ["core", "compiler_builtins"],
86    )
87
88    append_crate(
89        "bindings",
90        srctree / "rust"/ "bindings" / "lib.rs",
91        ["core"],
92        cfg=cfg,
93    )
94    crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
95
96    append_crate(
97        "pin_init_internal",
98        srctree / "rust" / "pin-init" / "internal" / "src" / "_lib.rs",
99        [],
100        cfg=["kernel"],
101        is_proc_macro=True,
102    )
103
104    append_crate(
105        "pin_init",
106        srctree / "rust" / "pin-init" / "src" / "_lib.rs",
107        ["core", "pin_init_internal", "macros"],
108        cfg=["kernel"],
109    )
110
111    append_crate(
112        "kernel",
113        srctree / "rust" / "kernel" / "lib.rs",
114        ["core", "macros", "build_error", "bindings", "pin_init"],
115        cfg=cfg,
116    )
117    crates[-1]["source"] = {
118        "include_dirs": [
119            str(srctree / "rust" / "kernel"),
120            str(objtree / "rust")
121        ],
122        "exclude_dirs": [],
123    }
124
125    def is_root_crate(build_file, target):
126        try:
127            return f"{target}.o" in open(build_file).read()
128        except FileNotFoundError:
129            return False
130
131    # Then, the rest outside of `rust/`.
132    #
133    # We explicitly mention the top-level folders we want to cover.
134    extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
135    if external_src is not None:
136        extra_dirs = [external_src]
137    for folder in extra_dirs:
138        for path in folder.rglob("*.rs"):
139            logging.info("Checking %s", path)
140            name = path.name.replace(".rs", "")
141
142            # Skip those that are not crate roots.
143            if not is_root_crate(path.parent / "Makefile", name) and \
144               not is_root_crate(path.parent / "Kbuild", name):
145                continue
146
147            logging.info("Adding %s", name)
148            append_crate(
149                name,
150                path,
151                ["core", "kernel"],
152                cfg=cfg,
153            )
154
155    return crates
156
157def main():
158    parser = argparse.ArgumentParser()
159    parser.add_argument('--verbose', '-v', action='store_true')
160    parser.add_argument('--cfgs', action='append', default=[])
161    parser.add_argument("srctree", type=pathlib.Path)
162    parser.add_argument("objtree", type=pathlib.Path)
163    parser.add_argument("sysroot", type=pathlib.Path)
164    parser.add_argument("sysroot_src", type=pathlib.Path)
165    parser.add_argument("exttree", type=pathlib.Path, nargs="?")
166    args = parser.parse_args()
167
168    logging.basicConfig(
169        format="[%(asctime)s] [%(levelname)s] %(message)s",
170        level=logging.INFO if args.verbose else logging.WARNING
171    )
172
173    # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
174    assert args.sysroot in args.sysroot_src.parents
175
176    rust_project = {
177        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
178        "sysroot": str(args.sysroot),
179    }
180
181    json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
182
183if __name__ == "__main__":
184    main()
185