Re: [PATCH v2 6/7] scripts: generate_rust_analyzer.py: identify crates explicitly

From: Fiona Behrens
Date: Wed Mar 12 2025 - 15:07:31 EST


Tamir Duberstein <tamird@xxxxxxxxx> writes:

> Use the return of `append_crate` to declare dependency on that crate.
> This allows multiple crates with the same display_name be defined, which
> we'll use to define host crates separately from target crates.
>
> Signed-off-by: Tamir Duberstein <tamird@xxxxxxxxx>

Reviewed-by: Fiona Behrens <me@xxxxxxxxxx>

> ---
> scripts/generate_rust_analyzer.py | 60 +++++++++++++++++++--------------------
> 1 file changed, 30 insertions(+), 30 deletions(-)
>
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index 1bb185ae2e87..e1002867735b 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -68,40 +68,38 @@ def generate_crates(
> line = line.replace("\n", "")
> cfg.append(line)
>
> - # Now fill the crates list -- dependencies need to come first.
> - #
> - # Avoid O(n^2) iterations by keeping a map of indexes.
> + # Now fill the crates list.
> crates: list[Crate] = []
> - crates_indexes: dict[str, int] = {}
> crates_cfgs = args_crates_cfgs(cfgs)
>
> def append_crate(
> display_name: str,
> root_module: pathlib.Path,
> - deps: list[str],
> + deps: list[Dependency],
> cfg: list[str] = [],
> is_workspace_member: bool = True,
> - ) -> None:
> - crates_indexes[display_name] = len(crates)
> + ) -> Dependency:
> + index = len(crates)
> crates.append({
> "display_name": display_name,
> "root_module": str(root_module),
> "is_workspace_member": is_workspace_member,
> - "deps": [{"crate": crates_indexes[dep], "name": dep} for dep in deps],
> + "deps": deps,
> "cfg": cfg,
> "edition": "2021",
> "env": {
> "RUST_MODFILE": "This is only for rust-analyzer"
> },
> })
> + return {"crate": index, "name": display_name}
>
> def append_proc_macro_crate(
> display_name: str,
> root_module: pathlib.Path,
> - deps: list[str],
> + deps: list[Dependency],
> cfg: list[str] = [],
> - ) -> None:
> - append_crate(display_name, root_module, deps, cfg)
> + ) -> Dependency:
> + dep = append_crate(display_name, root_module, deps, cfg)
> proc_macro_dylib_name = subprocess.check_output(
> [os.environ["RUSTC"], "--print", "file-names", "--crate-name", display_name, "--crate-type", "proc-macro", "-"],
> stdin=subprocess.DEVNULL,
> @@ -112,13 +110,14 @@ def generate_crates(
> "proc_macro_dylib_path": str(objtree / "rust" / proc_macro_dylib_name),
> }
> crates[-1] = crate
> + return dep
>
> def append_sysroot_crate(
> display_name: str,
> - deps: list[str],
> + deps: list[Dependency],
> cfg: list[str] = [],
> - ) -> None:
> - append_crate(
> + ) -> Dependency:
> + return append_crate(
> display_name,
> sysroot_src / display_name / "src" / "lib.rs",
> deps,
> @@ -129,34 +128,34 @@ def generate_crates(
> # NB: sysroot crates reexport items from one another so setting up our transitive dependencies
> # here is important for ensuring that rust-analyzer can resolve symbols. The sources of truth
> # for this dependency graph are `(sysroot_src / crate / "Cargo.toml" for crate in crates)`.
> - append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []))
> - append_sysroot_crate("alloc", ["core"])
> - append_sysroot_crate("std", ["alloc", "core"])
> - append_sysroot_crate("proc_macro", ["core", "std"])
> + core = append_sysroot_crate("core", [], cfg=crates_cfgs.get("core", []))
> + alloc = append_sysroot_crate("alloc", [core])
> + std = append_sysroot_crate("std", [alloc, core])
> + proc_macro = append_sysroot_crate("proc_macro", [core, std])
>
> - append_crate(
> + compiler_builtins = append_crate(
> "compiler_builtins",
> srctree / "rust" / "compiler_builtins.rs",
> [],
> )
>
> - append_proc_macro_crate(
> + macros = append_proc_macro_crate(
> "macros",
> srctree / "rust" / "macros" / "lib.rs",
> - ["std", "proc_macro"],
> + [std, proc_macro],
> )
>
> - append_crate(
> + build_error = append_crate(
> "build_error",
> srctree / "rust" / "build_error.rs",
> - ["core", "compiler_builtins"],
> + [core, compiler_builtins],
> )
>
> def append_crate_with_generated(
> display_name: str,
> - deps: list[str],
> - ) -> None:
> - append_crate(
> + deps: list[Dependency],
> + ) -> Dependency:
> + dep = append_crate(
> display_name,
> srctree / "rust" / display_name / "lib.rs",
> deps,
> @@ -174,10 +173,11 @@ def generate_crates(
> }
> }
> crates[-1] = crate
> + return dep
>
> - append_crate_with_generated("bindings", ["core"])
> - append_crate_with_generated("uapi", ["core"])
> - append_crate_with_generated("kernel", ["core", "macros", "build_error", "bindings", "uapi"])
> + bindings = append_crate_with_generated("bindings", [core])
> + uapi = append_crate_with_generated("uapi", [core])
> + kernel = append_crate_with_generated("kernel", [bindings, build_error, core, macros, uapi])
>
> def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
> try:
> @@ -207,7 +207,7 @@ def generate_crates(
> append_crate(
> name,
> path,
> - ["core", "kernel"],
> + [core, kernel],
> cfg=cfg,
> )