Skip to content

Commit 75c0fb2

Browse files
committed
scripts: generate_rust_analyzer.py: identify crates explicitly
Use the return of `append_crate` to declare dependency on that crate. This removes the need to build an index of crates and allows multiple crates with the same display_name be defined, which allows e.g. host crates to be defined separately from target crates. Reviewed-by: Fiona Behrens <me@kloenk.dev> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Tested-by: Daniel Almeida <daniel.almeida@collabora.com> Reviewed-by: Trevor Gross <tmgross@umich.edu> Reviewed-by: Jesung Yang <y.j3ms.n@gmail.com> Tested-by: Jesung Yang <y.j3ms.n@gmail.com> Link: https://patch.msgid.link/20260122-rust-analyzer-types-v1-4-29cc2e91dcd5@kernel.org Signed-off-by: Tamir Duberstein <tamird@kernel.org>
1 parent 94a3b2d commit 75c0fb2

1 file changed

Lines changed: 42 additions & 42 deletions

File tree

scripts/generate_rust_analyzer.py

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,14 @@ def generate_crates(
6565
line = line.replace("\n", "")
6666
cfg.append(line)
6767

68-
# Now fill the crates list -- dependencies need to come first.
69-
#
70-
# Avoid O(n^2) iterations by keeping a map of indexes.
68+
# Now fill the crates list.
7169
crates: List[Crate] = []
72-
crates_indexes: Dict[str, int] = {}
7370
crates_cfgs = args_crates_cfgs(cfgs)
7471

7572
def build_crate(
7673
display_name: str,
7774
root_module: pathlib.Path,
78-
deps: List[str],
75+
deps: List[Dependency],
7976
*,
8077
cfg: Optional[List[str]],
8178
is_workspace_member: Optional[bool],
@@ -90,7 +87,7 @@ def build_crate(
9087
"display_name": display_name,
9188
"root_module": str(root_module),
9289
"is_workspace_member": is_workspace_member,
93-
"deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
90+
"deps": deps,
9491
"cfg": cfg,
9592
"edition": edition,
9693
"env": {
@@ -101,12 +98,12 @@ def build_crate(
10198
def append_proc_macro_crate(
10299
display_name: str,
103100
root_module: pathlib.Path,
104-
deps: List[str],
101+
deps: List[Dependency],
105102
*,
106103
cfg: Optional[List[str]] = None,
107104
is_workspace_member: Optional[bool] = None,
108105
edition: Optional[str] = None,
109-
) -> None:
106+
) -> Dependency:
110107
crate = build_crate(
111108
display_name,
112109
root_module,
@@ -139,19 +136,20 @@ def append_proc_macro_crate(
139136
}
140137
return register_crate(proc_macro_crate)
141138

142-
def register_crate(crate: Crate) -> None:
143-
crates_indexes[crate["display_name"]] = len(crates)
139+
def register_crate(crate: Crate) -> Dependency:
140+
index = len(crates)
144141
crates.append(crate)
142+
return {"crate": index, "name": crate["display_name"]}
145143

146144
def append_crate(
147145
display_name: str,
148146
root_module: pathlib.Path,
149-
deps: List[str],
147+
deps: List[Dependency],
150148
*,
151149
cfg: Optional[List[str]] = None,
152150
is_workspace_member: Optional[bool] = None,
153151
edition: Optional[str] = None,
154-
) -> None:
152+
) -> Dependency:
155153
return register_crate(
156154
build_crate(
157155
display_name,
@@ -165,10 +163,10 @@ def append_crate(
165163

166164
def append_sysroot_crate(
167165
display_name: str,
168-
deps: List[str],
166+
deps: List[Dependency],
169167
*,
170168
cfg: Optional[List[str]] = None,
171-
) -> None:
169+
) -> Dependency:
172170
return append_crate(
173171
display_name,
174172
sysroot_src / display_name / "src" / "lib.rs",
@@ -205,75 +203,75 @@ def append_sysroot_crate(
205203
# NB: sysroot crates reexport items from one another so setting up our transitive dependencies
206204
# here is important for ensuring that rust-analyzer can resolve symbols. The sources of truth
207205
# for this dependency graph are `(sysroot_src / crate / "Cargo.toml" for crate in crates)`.
208-
append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []))
209-
append_sysroot_crate("alloc", ["core"])
210-
append_sysroot_crate("std", ["alloc", "core"])
211-
append_sysroot_crate("proc_macro", ["core", "std"])
206+
core = append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []))
207+
alloc = append_sysroot_crate("alloc", [core])
208+
std = append_sysroot_crate("std", [alloc, core])
209+
proc_macro = append_sysroot_crate("proc_macro", [core, std])
212210

213-
append_crate(
211+
compiler_builtins = append_crate(
214212
"compiler_builtins",
215213
srctree / "rust" / "compiler_builtins.rs",
216-
["core"],
214+
[core],
217215
)
218216

219-
append_crate(
217+
proc_macro2 = append_crate(
220218
"proc_macro2",
221219
srctree / "rust" / "proc-macro2" / "lib.rs",
222-
["core", "alloc", "std", "proc_macro"],
220+
[core, alloc, std, proc_macro],
223221
cfg=crates_cfgs["proc_macro2"],
224222
)
225223

226-
append_crate(
224+
quote = append_crate(
227225
"quote",
228226
srctree / "rust" / "quote" / "lib.rs",
229-
["core", "alloc", "std", "proc_macro", "proc_macro2"],
227+
[core, alloc, std, proc_macro, proc_macro2],
230228
cfg=crates_cfgs["quote"],
231229
edition="2018",
232230
)
233231

234-
append_crate(
232+
syn = append_crate(
235233
"syn",
236234
srctree / "rust" / "syn" / "lib.rs",
237-
["std", "proc_macro", "proc_macro2", "quote"],
235+
[std, proc_macro, proc_macro2, quote],
238236
cfg=crates_cfgs["syn"],
239237
)
240238

241-
append_proc_macro_crate(
239+
macros = append_proc_macro_crate(
242240
"macros",
243241
srctree / "rust" / "macros" / "lib.rs",
244-
["std", "proc_macro", "proc_macro2", "quote", "syn"],
242+
[std, proc_macro, proc_macro2, quote, syn],
245243
)
246244

247-
append_crate(
245+
build_error = append_crate(
248246
"build_error",
249247
srctree / "rust" / "build_error.rs",
250-
["core", "compiler_builtins"],
248+
[core, compiler_builtins],
251249
)
252250

253-
append_proc_macro_crate(
251+
pin_init_internal = append_proc_macro_crate(
254252
"pin_init_internal",
255253
srctree / "rust" / "pin-init" / "internal" / "src" / "lib.rs",
256-
["std", "proc_macro", "proc_macro2", "quote", "syn"],
254+
[std, proc_macro, proc_macro2, quote, syn],
257255
cfg=["kernel"],
258256
)
259257

260-
append_crate(
258+
pin_init = append_crate(
261259
"pin_init",
262260
srctree / "rust" / "pin-init" / "src" / "lib.rs",
263-
["core", "compiler_builtins", "pin_init_internal", "macros"],
261+
[core, compiler_builtins, pin_init_internal, macros],
264262
cfg=["kernel"],
265263
)
266264

267-
append_crate(
265+
ffi = append_crate(
268266
"ffi",
269267
srctree / "rust" / "ffi.rs",
270-
["core", "compiler_builtins"],
268+
[core, compiler_builtins],
271269
)
272270

273271
def append_crate_with_generated(
274272
display_name: str,
275-
deps: List[str],
276-
) -> None:
273+
deps: List[Dependency],
274+
) -> Dependency:
277275
crate = build_crate(
278276
display_name,
279277
srctree / "rust"/ display_name / "lib.rs",
@@ -295,9 +293,11 @@ def append_crate_with_generated(
295293
}
296294
return register_crate(crate_with_generated)
297295

298-
append_crate_with_generated("bindings", ["core", "ffi", "pin_init"])
299-
append_crate_with_generated("uapi", ["core", "ffi", "pin_init"])
300-
append_crate_with_generated("kernel", ["core", "macros", "build_error", "pin_init", "ffi", "bindings", "uapi"])
296+
bindings = append_crate_with_generated("bindings", [core, ffi, pin_init])
297+
uapi = append_crate_with_generated("uapi", [core, ffi, pin_init])
298+
kernel = append_crate_with_generated(
299+
"kernel", [core, macros, build_error, pin_init, ffi, bindings, uapi]
300+
)
301301

302302
def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
303303
try:
@@ -327,7 +327,7 @@ def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
327327
append_crate(
328328
name,
329329
path,
330-
["core", "kernel", "pin_init"],
330+
[core, kernel, pin_init],
331331
cfg=cfg,
332332
)
333333

0 commit comments

Comments
 (0)