[PATCH v4 11/11] scripts: generate_rust_analyzer.py: use `cfg_groups`

Tamir Duberstein posted 11 patches 9 months ago
There is a newer version of this series
[PATCH v4 11/11] scripts: generate_rust_analyzer.py: use `cfg_groups`
Posted by Tamir Duberstein 9 months 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
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
 scripts/generate_rust_analyzer.py | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 957b413fe0b6..3d89c0198db4 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -37,6 +37,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]
@@ -59,15 +60,8 @@ def generate_crates(
     sysroot_src: pathlib.Path,
     external_src: pathlib.Path,
     cfgs: 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] = []
     crates_cfgs = args_crates_cfgs(cfgs)
@@ -77,6 +71,7 @@ def generate_crates(
         root_module: pathlib.Path,
         deps: List[Dependency],
         cfg: List[str] = [],
+        cfg_groups: List[str] = [],
         is_workspace_member: bool = True,
     ) -> Crate:
         return {
@@ -85,6 +80,7 @@ def generate_crates(
             "is_workspace_member": is_workspace_member,
             "deps": deps,
             "cfg": cfg,
+            "cfg_groups": cfg_groups,
             "edition": "2021",
             "env": {
                 "RUST_MODFILE": "This is only for rust-analyzer"
@@ -101,10 +97,13 @@ def generate_crates(
         root_module: pathlib.Path,
         deps: List[Dependency],
         cfg: List[str] = [],
+        cfg_groups: List[str] = [],
         is_workspace_member: bool = True,
     ) -> Dependency:
         return register_crate(
-            build_crate(display_name, root_module, deps, cfg, is_workspace_member)
+            build_crate(
+                display_name, root_module, deps, cfg, cfg_groups, is_workspace_member
+            )
         )
 
     def append_proc_macro_crate(
@@ -190,7 +189,7 @@ def generate_crates(
             display_name,
             srctree / "rust" / display_name / "lib.rs",
             deps,
-            cfg=cfg,
+            cfg_groups=cfg_groups,
         )
         crate["env"]["OBJTREE"] = str(objtree.resolve(True))
         crate_with_generated: CrateWithGenerated = {
@@ -252,7 +251,7 @@ def generate_crates(
                 name,
                 path,
                 [core, kernel],
-                cfg=cfg,
+                cfg_groups=cfg_groups,
             )
 
     return crates
@@ -277,9 +276,21 @@ 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, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
+        "crates": generate_crates(
+            args.srctree,
+            args.objtree,
+            args.sysroot_src,
+            args.exttree,
+            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.48.1
Re: [PATCH v4 11/11] scripts: generate_rust_analyzer.py: use `cfg_groups`
Posted by Daniel Almeida 8 months, 4 weeks ago

> On 22 Mar 2025, at 10:23, 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
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>
> ---
> scripts/generate_rust_analyzer.py | 35 +++++++++++++++++++++++------------
> 1 file changed, 23 insertions(+), 12 deletions(-)
> 
> diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
> index 957b413fe0b6..3d89c0198db4 100755
> --- a/scripts/generate_rust_analyzer.py
> +++ b/scripts/generate_rust_analyzer.py
> @@ -37,6 +37,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]
> @@ -59,15 +60,8 @@ def generate_crates(
>     sysroot_src: pathlib.Path,
>     external_src: pathlib.Path,
>     cfgs: 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] = []
>     crates_cfgs = args_crates_cfgs(cfgs)
> @@ -77,6 +71,7 @@ def generate_crates(
>         root_module: pathlib.Path,
>         deps: List[Dependency],
>         cfg: List[str] = [],
> +        cfg_groups: List[str] = [],
>         is_workspace_member: bool = True,
>     ) -> Crate:
>         return {
> @@ -85,6 +80,7 @@ def generate_crates(
>             "is_workspace_member": is_workspace_member,
>             "deps": deps,
>             "cfg": cfg,
> +            "cfg_groups": cfg_groups,
>             "edition": "2021",
>             "env": {
>                 "RUST_MODFILE": "This is only for rust-analyzer"
> @@ -101,10 +97,13 @@ def generate_crates(
>         root_module: pathlib.Path,
>         deps: List[Dependency],
>         cfg: List[str] = [],
> +        cfg_groups: List[str] = [],
>         is_workspace_member: bool = True,
>     ) -> Dependency:
>         return register_crate(
> -            build_crate(display_name, root_module, deps, cfg, is_workspace_member)
> +            build_crate(
> +                display_name, root_module, deps, cfg, cfg_groups, is_workspace_member
> +            )
>         )
> 
>     def append_proc_macro_crate(
> @@ -190,7 +189,7 @@ def generate_crates(
>             display_name,
>             srctree / "rust" / display_name / "lib.rs",
>             deps,
> -            cfg=cfg,
> +            cfg_groups=cfg_groups,
>         )
>         crate["env"]["OBJTREE"] = str(objtree.resolve(True))
>         crate_with_generated: CrateWithGenerated = {
> @@ -252,7 +251,7 @@ def generate_crates(
>                 name,
>                 path,
>                 [core, kernel],
> -                cfg=cfg,
> +                cfg_groups=cfg_groups,
>             )
> 
>     return crates
> @@ -277,9 +276,21 @@ 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, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
> +        "crates": generate_crates(
> +            args.srctree,
> +            args.objtree,
> +            args.sysroot_src,
> +            args.exttree,
> +            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.48.1
> 
> 

Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
Tested-by: Daniel Almeida <daniel.almeida@collabora.com>
Re: [PATCH v4 11/11] scripts: generate_rust_analyzer.py: use `cfg_groups`
Posted by Miguel Ojeda 9 months ago
On Sat, Mar 22, 2025 at 2:24 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
> Signed-off-by: Tamir Duberstein <tamird@gmail.com>

A note in case it matters for someone out there: that commit appeared
in tag 2024-08-26, but our minimum Rust version is dated 2024-05-02.

I think many developers are using a rust-analyzer that is newer (i.e.
installed independently), but if others install the component that
gets distributed alongside Rust, then I think it appeared in Rust
1.82.0.

So I wonder if we should wait for the MSRV bump to start using this.

Thanks!

Cheers,
Miguel