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.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 edition="2021",
65 ):
66 append_crate(
67 display_name,
68 sysroot_src / display_name / "src" / "lib.rs",
69 deps,
70 cfg,
71 is_workspace_member=False,
72 edition=edition,
73 )
74
75 # NB: sysroot crates reexport items from one another so setting up our transitive dependencies
76 # here is important for ensuring that rust-analyzer can resolve symbols. The sources of truth
77 # for this dependency graph are `(sysroot_src / crate / "Cargo.toml" for crate in crates)`.
78 append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []), edition=core_edition)
79 append_sysroot_crate("alloc", ["core"])
80 append_sysroot_crate("std", ["alloc", "core"])
81 append_sysroot_crate("proc_macro", ["core", "std"])
82
83 append_crate(
84 "compiler_builtins",
85 srctree / "rust" / "compiler_builtins.rs",
86 [],
87 )
88
89 append_crate(
90 "proc_macro2",
91 srctree / "rust" / "proc-macro2" / "lib.rs",
92 ["core", "alloc", "std", "proc_macro"],
93 cfg=crates_cfgs["proc_macro2"],
94 )
95
96 append_crate(
97 "quote",
98 srctree / "rust" / "quote" / "lib.rs",
99 ["alloc", "proc_macro", "proc_macro2"],
100 cfg=crates_cfgs["quote"],
101 )
102
103 append_crate(
104 "syn",
105 srctree / "rust" / "syn" / "lib.rs",
106 ["proc_macro", "proc_macro2", "quote"],
107 cfg=crates_cfgs["syn"],
108 )
109
110 append_crate(
111 "macros",
112 srctree / "rust" / "macros" / "lib.rs",
113 ["std", "proc_macro", "proc_macro2", "quote", "syn"],
114 is_proc_macro=True,
115 )
116
117 append_crate(
118 "build_error",
119 srctree / "rust" / "build_error.rs",
120 ["core", "compiler_builtins"],
121 )
122
123 append_crate(
124 "pin_init_internal",
125 srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
126 [],
127 cfg=["kernel"],
128 is_proc_macro=True,
129 )
130
131 append_crate(
132 "pin_init",
133 srctree / "rust" / "pin-init" / "src" / "lib.rs",
134 ["core", "pin_init_internal", "macros"],
135 cfg=["kernel"],
136 )
137
138 append_crate(
139 "ffi",
140 srctree / "rust" / "ffi.rs",
141 ["core", "compiler_builtins"],
142 )
143
144 def append_crate_with_generated(
145 display_name,
146 deps,
147 ):
148 append_crate(
149 display_name,
150 srctree / "rust"/ display_name / "lib.rs",
151 deps,
152 cfg=cfg,
153 )
154 crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
155 crates[-1]["source"] = {
156 "include_dirs": [
157 str(srctree / "rust" / display_name),
158 str(objtree / "rust")
159 ],
160 "exclude_dirs": [],
161 }
162
163 append_crate_with_generated("bindings", ["core", "ffi", "pin_init"])
164 append_crate_with_generated("uapi", ["core", "ffi", "pin_init"])
165 append_crate_with_generated("kernel", ["core", "macros", "build_error", "pin_init", "ffi", "bindings", "uapi"])
166
167 def is_root_crate(build_file, target):
168 try:
169 return f"{target}.o" in open(build_file).read()
170 except FileNotFoundError:
171 return False
172
173 # Then, the rest outside of `rust/`.
174 #
175 # We explicitly mention the top-level folders we want to cover.
176 extra_dirs = map(lambda dir: srctree / dir, ("samples", "drivers"))
177 if external_src is not None:
178 extra_dirs = [external_src]
179 for folder in extra_dirs:
180 for path in folder.rglob("*.rs"):
181 logging.info("Checking %s", path)
182 name = path.name.replace(".rs", "")
183
184 # Skip those that are not crate roots.
185 if not is_root_crate(path.parent / "Makefile", name) and \
186 not is_root_crate(path.parent / "Kbuild", name):
187 continue
188
189 logging.info("Adding %s", name)
190 append_crate(
191 name,
192 path,
193 ["core", "kernel"],
194 cfg=cfg,
195 )
196
197 return crates
198
199def main():
200 parser = argparse.ArgumentParser()
201 parser.add_argument('--verbose', '-v', action='store_true')
202 parser.add_argument('--cfgs', action='append', default=[])
203 parser.add_argument("core_edition")
204 parser.add_argument("srctree", type=pathlib.Path)
205 parser.add_argument("objtree", type=pathlib.Path)
206 parser.add_argument("sysroot", type=pathlib.Path)
207 parser.add_argument("sysroot_src", type=pathlib.Path)
208 parser.add_argument("exttree", type=pathlib.Path, nargs="?")
209 args = parser.parse_args()
210
211 logging.basicConfig(
212 format="[%(asctime)s] [%(levelname)s] %(message)s",
213 level=logging.INFO if args.verbose else logging.WARNING
214 )
215
216 # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
217 assert args.sysroot in args.sysroot_src.parents
218
219 rust_project = {
220 "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.core_edition),
221 "sysroot": str(args.sysroot),
222 }
223
224 json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)
225
226if __name__ == "__main__":
227 main()