[PATCH v5 13/13] scripts: generate_rust_analyzer.py: use `cfg_groups`

Tamir Duberstein posted 13 patches 10 months, 2 weeks ago
There is a newer version of this series
[PATCH v5 13/13] scripts: generate_rust_analyzer.py: use `cfg_groups`
Posted by Tamir Duberstein 10 months, 2 weeks ago
Declare common `cfg`s just once to reduce the size of rust-analyzer.json
from 30619 to 2624 lines.

Link: https://github.com/rust-lang/rust-analyzer/commit/2607c09fddef36da0d6f0a84625db5e20a5ebde3
Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 scripts/generate_rust_analyzer.py | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 5c0056c265bb..4faf01e2cbc6 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -29,6 +29,7 @@ class Crate(TypedDict):
     root_module: str
     is_workspace_member: bool
     deps: List[Dependency]
+    cfg_groups: List[str]
     cfg: List[str]
     edition: Literal["2021"]
     env: Dict[str, str]
@@ -51,15 +52,8 @@ def generate_crates(
     sysroot_src: pathlib.Path,
     external_src: pathlib.Path,
     crates_cfgs: DefaultDict[str, List[str]],
+    cfg_groups: List[str],
 ) -> List[Crate]:
-    # Generate the configuration list.
-    cfg = []
-    with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
-        for line in fd:
-            line = line.replace("--cfg=", "")
-            line = line.replace("\n", "")
-            cfg.append(line)
-
     # Now fill the crates list.
     crates: List[Crate] = []
 
@@ -76,6 +70,7 @@ def generate_crates(
             "root_module": str(root_module),
             "is_workspace_member": is_workspace_member,
             "deps": deps,
+            "cfg_groups": cfg_groups if is_workspace_member else [],
             "cfg": cfg,
             "edition": "2021",
             "env": {
@@ -293,7 +288,7 @@ def generate_crates(
                 name,
                 path,
                 deps=[core, kernel],
-                cfg=cfg,
+                cfg=[],
             )
 
     return crates
@@ -328,6 +323,10 @@ def main() -> None:
     # Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
     assert args.sysroot in args.sysroot_src.parents
 
+    # Generate the configuration list.
+    with open(args.objtree / "include" / "generated" / "rustc_cfg") as fd:
+        cfg_groups = {"rustc_cfg": [line.lstrip("--cfg=").rstrip("\n") for line in fd]}
+
     rust_project = {
         "crates": generate_crates(
             args.srctree,
@@ -341,8 +340,10 @@ def main() -> None:
                     for crate, vals in map(lambda cfg: cfg.split("=", 1), args.cfgs)
                 },
             ),
+            list(cfg_groups.keys()),
         ),
         "sysroot": str(args.sysroot),
+        "cfg_groups": cfg_groups,
     }
 
     json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)

-- 
2.49.0
Re: [PATCH v5 13/13] scripts: generate_rust_analyzer.py: use `cfg_groups`
Posted by Trevor Gross 9 months, 3 weeks ago
On Tue, Mar 25, 2025 at 3:07 PM Tamir Duberstein <tamird@gmail.com> wrote:
>
> Declare common `cfg`s just once to reduce the size of rust-analyzer.json
> from 30619 to 2624 lines.
>
> Link: https://github.com/rust-lang/rust-analyzer/commit/2607c09fddef36da0d6f0a84625db5e20a5ebde3
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
>  scripts/generate_rust_analyzer.py | 19 ++++++++++---------
>  1 file changed, 10 insertions(+), 9 deletions(-)

> @@ -76,6 +70,7 @@ def generate_crates(
>              "root_module": str(root_module),
>              "is_workspace_member": is_workspace_member,
>              "deps": deps,
> +            "cfg_groups": cfg_groups if is_workspace_member else [],

Does r-a just expect cfg_groups for workspace members or is there
logic here? Maybe worth a comment.

Reviewed-by: Trevor Gross <tmgross@umich.edu>
Re: [PATCH v5 13/13] scripts: generate_rust_analyzer.py: use `cfg_groups`
Posted by Tamir Duberstein 9 months, 3 weeks ago
On Thu, Apr 17, 2025 at 3:29 AM Trevor Gross <tmgross@umich.edu> wrote:
>
> On Tue, Mar 25, 2025 at 3:07 PM Tamir Duberstein <tamird@gmail.com> wrote:
> >
> > Declare common `cfg`s just once to reduce the size of rust-analyzer.json
> > from 30619 to 2624 lines.
> >
> > Link: https://github.com/rust-lang/rust-analyzer/commit/2607c09fddef36da0d6f0a84625db5e20a5ebde3
> > Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> > Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
> > Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> > ---
> >  scripts/generate_rust_analyzer.py | 19 ++++++++++---------
> >  1 file changed, 10 insertions(+), 9 deletions(-)
>
> > @@ -76,6 +70,7 @@ def generate_crates(
> >              "root_module": str(root_module),
> >              "is_workspace_member": is_workspace_member,
> >              "deps": deps,
> > +            "cfg_groups": cfg_groups if is_workspace_member else [],
>
> Does r-a just expect cfg_groups for workspace members or is there
> logic here? Maybe worth a comment.

r-a doesn't have opinions, but our configs apply only to our crates as
opposed to sysroot crates. That's not entirely true for core, but the
configs we apply there are narrow, whereas our workspace crates get
all of KConfig, which is huge.

I'll add a comment.

> Reviewed-by: Trevor Gross <tmgross@umich.edu>

Thanks for the reviews! I need to rebase on rust-fixes which picked up
the ffi crate anyway, so I picked up your suggestions:
- generators instead of `map`.
- `Pathlib.path.stem`.
- `Pathlib.path.read_text`.
- Add a comment about cfg_groups.

Cheers.

Tamir