[PATCH v5 04/13] scripts: generate_rust_analyzer.py: extract `{build,register}_crate`

Tamir Duberstein
2025-03-25T20:06:46Z

Extract helpers from `append_crate` to avoid the need to peek into
`crates[-1]`. This improves readability.

Suggested-by: Trevor Gross <tmgross@umich.edu>
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
Reviewed-by: Fiona Behrens <me@kloenk.dev>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
scripts/generate_rust_analyzer.py | 35 ++++++++++++++++++++++++++++++-----
1 file changed, 30 insertions(+), 5 deletions(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 1b6b69501131..71e6038964f6 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -35,7 +35,14 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
crates_indexes = {}
crates_cfgs = args_crates_cfgs(cfgs)

- def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False):
+ def build_crate(
+ display_name,
+ root_module,
+ deps,
+ cfg=[],
+ is_workspace_member=True,
+ is_proc_macro=False,
+ ):
crate = {
"display_name": display_name,
"root_module": str(root_module),
@@ -54,9 +61,26 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
stdin=subprocess.DEVNULL,
).decode('utf-8').strip()
crate["proc_macro_dylib_path"] = f"{objtree}/rust/{proc_macro_dylib_name}"
- crates_indexes[display_name] = len(crates)
+ return crate
+
+ def register_crate(crate):
+ crates_indexes[crate["display_name"]] = len(crates)
crates.append(crate)

+ def append_crate(
+ display_name,
+ root_module,
+ deps,
+ cfg=[],
+ is_workspace_member=True,
+ is_proc_macro=False,
+ ):
+ register_crate(
+ build_crate(
+ display_name, root_module, deps, cfg, is_workspace_member, is_proc_macro
+ )
+ )
+
def append_sysroot_crate(
display_name,
deps,
@@ -116,20 +140,21 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs):
display_name,
deps,
):
- append_crate(
+ crate = build_crate(
display_name,
srctree / "rust" / display_name / "lib.rs",
deps,
cfg=cfg,
)
- crates[-1]["env"]["OBJTREE"] = str(objtree.resolve(True))
- crates[-1]["source"] = {
+ crate["env"]["OBJTREE"] = str(objtree.resolve(True))
+ crate["source"] = {
"include_dirs": [
str(srctree / "rust" / display_name),
str(objtree / "rust")
],
"exclude_dirs": [],
}
+ register_crate(crate)

append_crate_with_generated("bindings", ["core"])
append_crate_with_generated("uapi", ["core"])

--
2.49.0