]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
rust: support overriding crate_name
authorAlice Ryhl <aliceryhl@google.com>
Thu, 2 Apr 2026 10:55:33 +0000 (10:55 +0000)
committerMiguel Ojeda <ojeda@kernel.org>
Fri, 3 Apr 2026 09:57:35 +0000 (11:57 +0200)
Currently you cannot filter out the crate-name argument
RUSTFLAGS_REMOVE_stem.o because the Rust filter-out invocation does not
include that particular argument. Since --crate-name is an argument that
can't be passed multiple times, this means that it's currently not
possible to override the crate name. Thus, remove the --crate-name
argument for drivers. This allows them to override the crate name using
the #![crate_name] annotation.

This affects symbol names, but has no effect on the filenames of object
files and other things generated by the build, as we always use --emit
with a fixed output filename.

The --crate-name argument is kept for the crates under rust/ for
simplicity and to avoid changing many of them by adding #![crate_name].

The rust analyzer script is updated to use rustc to obtain the crate
name of the driver crates, which picks up the right name whether it is
configured via #![crate_name] or not. For readability, the logic to
invoke 'rustc' is extracted to its own function.

Note that the crate name in the python script is not actually that
important - the only place where the name actually affects anything is
in the 'deps' array which specifies an index and name for each
dependency, and determines what that dependency is called in *this*
crate. (The same crate may be called different things in each
dependency.) Since driver crates are leaf crates, this doesn't apply and
the rustc invocation only affects the 'display_name' parameter.

Acked-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Jesung Yang <y.jems.n@gmail.com>
Acked-by: Tamir Duberstein <tamird@kernel.org>
Link: https://patch.msgid.link/20260402-binder-crate-name-v4-1-ec3919b87909@google.com
[ Applied Python type hints. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
scripts/Makefile.build
scripts/generate_rust_analyzer.py

index a6d1a2b210aaa4c1bc65523a665e8cf4b2595362..0b0245106d0127d1e22ad9743c1548d5f104e33b 100644 (file)
@@ -333,7 +333,6 @@ rust_common_cmd = \
        -Zcrate-attr='feature($(rust_allowed_features))' \
        -Zunstable-options --extern pin_init --extern kernel \
        --crate-type rlib -L $(objtree)/rust/ \
-       --crate-name $(basename $(notdir $@)) \
        --sysroot=/dev/null \
        --out-dir $(dir $@) --emit=dep-info=$(depfile)
 
index b4a55344688dd12ec701a54aeded635b2315e5ae..d5f9a0ca742c42aad4ca4256ffd9c18c8bcd4e98 100755 (executable)
@@ -12,6 +12,12 @@ import subprocess
 import sys
 from typing import Dict, Iterable, List, Literal, Optional, TypedDict
 
+def invoke_rustc(args: List[str]) -> str:
+    return subprocess.check_output(
+        [os.environ["RUSTC"]] + args,
+        stdin=subprocess.DEVNULL,
+    ).decode('utf-8').strip()
+
 def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
     crates_cfgs = {}
     for cfg in cfgs:
@@ -69,6 +75,9 @@ def generate_crates(
     crates: List[Crate] = []
     crates_cfgs = args_crates_cfgs(cfgs)
 
+    def get_crate_name(path: pathlib.Path) -> str:
+        return invoke_rustc(["--print", "crate-name", str(path)])
+
     def build_crate(
         display_name: str,
         root_module: pathlib.Path,
@@ -112,23 +121,15 @@ def generate_crates(
             is_workspace_member=is_workspace_member,
             edition=edition,
         )
-        proc_macro_dylib_name = (
-            subprocess.check_output(
-                [
-                    os.environ["RUSTC"],
-                    "--print",
-                    "file-names",
-                    "--crate-name",
-                    display_name,
-                    "--crate-type",
-                    "proc-macro",
-                    "-",
-                ],
-                stdin=subprocess.DEVNULL,
-            )
-            .decode("utf-8")
-            .strip()
-        )
+        proc_macro_dylib_name = invoke_rustc([
+            "--print",
+            "file-names",
+            "--crate-name",
+            display_name,
+            "--crate-type",
+            "proc-macro",
+            "-",
+        ])
         proc_macro_crate: ProcMacroCrate = {
             **crate,
             "is_proc_macro": True,
@@ -324,16 +325,17 @@ def generate_crates(
     for folder in extra_dirs:
         for path in folder.rglob("*.rs"):
             logging.info("Checking %s", path)
-            name = path.stem
+            file_name = path.stem
 
             # Skip those that are not crate roots.
-            if not is_root_crate(path.parent / "Makefile", name) and \
-               not is_root_crate(path.parent / "Kbuild", name):
+            if not is_root_crate(path.parent / "Makefile", file_name) and \
+               not is_root_crate(path.parent / "Kbuild", file_name):
                 continue
 
-            logging.info("Adding %s", name)
+            crate_name = get_crate_name(path)
+            logging.info("Adding %s", crate_name)
             append_crate(
-                name,
+                crate_name,
                 path,
                 [core, kernel, pin_init],
                 cfg=generated_cfg,