File Index
Files and directories
Sign in
FILE

generate_rust_analyzer.py

File preview with syntax highlighting.

linux-6.19.14/scripts/generate_rust_analyzer.py PYTHON
  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.split()
 19
 20    return crates_cfgs
 21
 22def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edition):
 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, edition="2021"):
 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": edition,
 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    def append_sysroot_crate(
 61        display_name,
 62        deps,
 63        cfg=[],
 64    ):
 65        append_crate(
 66            display_name,
 67            sysroot_src / display_name / "src" / "lib.rs",
 68            deps,
 69            cfg,
 70            is_workspace_member=False,
 71            # Miguel Ojeda writes:
 72            #
 73            # > ... in principle even the sysroot crates may have different
 74            # > editions.
 75            # >
 76            # > For instance, in the move to 2024, it seems all happened at once
 77            # > in 1.87.0 in these upstream commits:
 78            # >
 79            # >     0e071c2c6a58 ("Migrate core to Rust 2024")
 80            # >     f505d4e8e380 ("Migrate alloc to Rust 2024")
 81            # >     0b2489c226c3 ("Migrate proc_macro to Rust 2024")
 82            # >     993359e70112 ("Migrate std to Rust 2024")
 83            # >
 84            # > But in the previous move to 2021, `std` moved in 1.59.0, while
 85            # > the others in 1.60.0:
 86            # >
 87            # >     b656384d8398 ("Update stdlib to the 2021 edition")
 88            # >     06a1c14d52a8 ("Switch all libraries to the 2021 edition")
 89            #
 90            # Link: https://lore.kernel.org/all/CANiq72kd9bHdKaAm=8xCUhSHMy2csyVed69bOc4dXyFAW4sfuw@mail.gmail.com/
 91            #
 92            # At the time of writing all rust versions we support build the
 93            # sysroot crates with the same edition. We may need to relax this
 94            # assumption if future edition moves span multiple rust versions.
 95            edition=core_edition,
 96        )
 97
 98    # NB: sysroot crates reexport items from one another so setting up our transitive dependencies
 99    # here is important for ensuring that rust-analyzer can resolve symbols. The sources of truth
100    # for this dependency graph are `(sysroot_src / crate / "Cargo.toml" for crate in crates)`.
101    append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []))
102    append_sysroot_crate("alloc", ["core"])
103    append_sysroot_crate("std", ["alloc", "core"])
104    append_sysroot_crate("proc_macro", ["core", "std"])
105
106    append_crate(
107        "compiler_builtins",
108        srctree / "rust" / "compiler_builtins.rs",
109        ["core"],
110    )
111
112    append_crate(
113        "proc_macro2",
114        srctree / "rust" / "proc-macro2" / "lib.rs",
115        ["core", "alloc", "std", "proc_macro"],
116        cfg=crates_cfgs["proc_macro2"],
117    )
118
119    append_crate(
120        "quote",
121        srctree / "rust" / "quote" / "lib.rs",
122        ["core", "alloc", "std", "proc_macro", "proc_macro2"],
123        cfg=crates_cfgs["quote"],
124        edition="2018",
125    )
126
127    append_crate(
128        "syn",
129        srctree / "rust" / "syn" / "lib.rs",
130        ["std", "proc_macro", "proc_macro2", "quote"],
131        cfg=crates_cfgs["syn"],
132    )
133
134    append_crate(
135        "macros",
136        srctree / "rust" / "macros" / "lib.rs",
137        ["std", "proc_macro", "proc_macro2", "quote", "syn"],
138        is_proc_macro=True,
139    )
140
141    append_crate(
142        "build_error",
143        srctree / "rust" / "build_error.rs",
144        ["core", "compiler_builtins"],
145    )
146
147    append_crate(
148        "pin_init_internal",
149        srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
150        ["std", "proc_macro"],
151        cfg=["kernel"],
152        is_proc_macro=True,
153    )
154
155    append_crate(
156        "pin_init",
157        srctree / "rust" / "pin-init" / "src" / "lib.rs",
158        ["core", "compiler_builtins", "pin_init_internal", "macros"],
159        cfg=["kernel"],
160    )
161
162    append_crate(
163        "ffi",
164        srctree / "rust" / "ffi.rs",
165        ["core", "compiler_builtins"],
166    )
167
168    def append_crate_with_generated(
169        display_name,
170        deps,
171    ):
172        append_crate(
173            display_name,
174            srctree / "rust"/ display_name / "lib.rs",
175            deps,
176            cfg=cfg,
177        )
178        crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
179        crates[-1]["source"] = {
180            "include_dirs": [
181                str(srctree / "rust" / display_name),
182                str(objtree / "rust")
183            ],
184            "exclude_dirs": [],
185        }
186
187    append_crate_with_generated("bindings", ["core", "ffi", "pin_init"])
188    append_crate_with_generated("uapi", ["core", "ffi", "pin_init"])
189    append_crate_with_generated("kernel", ["core", "macros", "build_error", "pin_init", "ffi", "bindings", "uapi"])
190
191    def is_root_crate(build_file, target):
192        try:
193            contents = build_file.read_text()
194        except FileNotFoundError:
195            return False
196        return f"{target}.o" in contents
197
198    # Then, the rest outside of `rust/`.
199    #
200    # We explicitly mention the top-level folders we want to cover.
201    extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
202    if external_src is not None:
203        extra_dirs = [external_src]
204    for folder in extra_dirs:
205        for path in folder.rglob("*.rs"):
206            logging.info("Checking %s", path)
207            name = path.name.replace(".rs", "")
208
209            # Skip those that are not crate roots.
210            if not is_root_crate(path.parent / "Makefile", name) and \
211               not is_root_crate(path.parent / "Kbuild", name):
212                continue
213
214            logging.info("Adding %s", name)
215            append_crate(
216                name,
217                path,
218                ["core", "kernel", "pin_init"],
219                cfg=cfg,
220            )
221
222    return crates
223
224def main():
225    parser = argparse.ArgumentParser()
226    parser.add_argument('--verbose', '-v', action='store_true')
227    parser.add_argument('--cfgs', action='append', default=[])
228    parser.add_argument("core_edition")
229    parser.add_argument("srctree", type=pathlib.Path)
230    parser.add_argument("objtree", type=pathlib.Path)
231    parser.add_argument("sysroot", type=pathlib.Path)
232    parser.add_argument("sysroot_src", type=pathlib.Path)
233    parser.add_argument("exttree", type=pathlib.Path, nargs="?")
234    args = parser.parse_args()
235
236    logging.basicConfig(
237        format="[%(asctime)s] [%(levelname)s] %(message)s",
238        level=logging.INFO if args.verbose else logging.WARNING
239    )
240
241    rust_project = {
242        "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.core_edition),
243        "sysroot": str(args.sysroot),
244    }
245
246    json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
247
248if __name__ == "__main__":
249    main()
~/ Read-only file preview.