Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 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 )
72
73 # NB: sysroot crates reexport items from one another so setting up our transitive dependencies
74 # here is important for ensuring that rust-analyzer can resolve symbols. The sources of truth
75 # for this dependency graph are `(sysroot_src / crate / "Cargo.toml" for crate in crates)`.
76 append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []))
77 append_sysroot_crate("alloc", ["core"])
78 append_sysroot_crate("std", ["alloc", "core"])
79 append_sysroot_crate("proc_macro", ["core", "std"])
80
81 append_crate(
82 "compiler_builtins",
83 srctree / "rust" / "compiler_builtins.rs",
84 [],
85 )
86
87 append_crate(
88 "macros",
89 srctree / "rust" / "macros" / "lib.rs",
90 ["std", "proc_macro"],
91 is_proc_macro=True,
92 )
93
94 append_crate(
95 "build_error",
96 srctree / "rust" / "build_error.rs",
97 ["core", "compiler_builtins"],
98 )
99
100 def append_crate_with_generated(
101 display_name,
102 deps,
103 ):
104 append_crate(
105 display_name,
106 srctree / "rust"/ display_name / "lib.rs",
107 deps,
108 cfg=cfg,
109 )
110 crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
111 crates[-1]["source"] = {
112 "include_dirs": [
113 str(srctree / "rust" / display_name),
114 str(objtree / "rust")
115 ],
116 "exclude_dirs": [],
117 }
118
119 append_crate_with_generated("bindings", ["core"])
120 append_crate_with_generated("uapi", ["core"])
121 append_crate_with_generated("kernel", ["core", "macros", "build_error", "bindings", "uapi"])
122
123 def is_root_crate(build_file, target):
124 try:
125 return f"{target}.o" in open(build_file).read()
126 except FileNotFoundError:
127 return False
128
129 # Then, the rest outside of `rust/`.
130 #
131 # We explicitly mention the top-level folders we want to cover.
132 extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
133 if external_src is not None:
134 extra_dirs = [external_src]
135 for folder in extra_dirs:
136 for path in folder.rglob("*.rs"):
137 logging.info("Checking %s", path)
138 name = path.name.replace(".rs", "")
139
140 # Skip those that are not crate roots.
141 if not is_root_crate(path.parent / "Makefile", name) and \
142 not is_root_crate(path.parent / "Kbuild", name):
143 continue
144
145 logging.info("Adding %s", name)
146 append_crate(
147 name,
148 path,
149 ["core", "kernel"],
150 cfg=cfg,
151 )
152
153 return crates
154
155def main():
156 parser = argparse.ArgumentParser()
157 parser.add_argument('--verbose', '-v', action='store_true')
158 parser.add_argument('--cfgs', action='append', default=[])
159 parser.add_argument("srctree", type=pathlib.Path)
160 parser.add_argument("objtree", type=pathlib.Path)
161 parser.add_argument("sysroot", type=pathlib.Path)
162 parser.add_argument("sysroot_src", type=pathlib.Path)
163 parser.add_argument("exttree", type=pathlib.Path, nargs="?")
164 args = parser.parse_args()
165
166 logging.basicConfig(
167 format="[%(asctime)s] [%(levelname)s] %(message)s",
168 level=logging.INFO if args.verbose else logging.WARNING
169 )
170
171 # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
172 assert args.sysroot in args.sysroot_src.parents
173
174 rust_project = {
175 "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
176 "sysroot": str(args.sysroot),
177 }
178
179 json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
180
181if __name__ == "__main__":
182 main()