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>
---
scripts/Makefile.build | 1 -
scripts/generate_rust_analyzer.py | 28 +++++++++++++++++++---------
2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 32e209bc7985..adc3e2d1ac78 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -332,7 +332,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)
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index f9b545104f21..ac7f1677acc3 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -19,6 +19,12 @@ def args_crates_cfgs(cfgs):
return crates_cfgs
+def invoke_rustc(args):
+ return subprocess.check_output(
+ [os.environ["RUSTC"]] + args,
+ stdin=subprocess.DEVNULL,
+ ).decode('utf-8').strip()
+
def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edition):
# Generate the configuration list.
cfg = []
@@ -49,10 +55,9 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
}
}
if is_proc_macro:
- 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", "-"]
+ )
crate["proc_macro_dylib_path"] = f"{objtree}/rust/{proc_macro_dylib_name}"
crates_indexes[display_name] = len(crates)
crates.append(crate)
@@ -194,6 +199,9 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
except FileNotFoundError:
return False
+ def get_crate_name(path):
+ return invoke_rustc(["--print", "crate-name", path])
+
# Then, the rest outside of `rust/`.
#
# We explicitly mention the top-level folders we want to cover.
@@ -203,16 +211,18 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, core_edit
for folder in extra_dirs:
for path in folder.rglob("*.rs"):
logging.info("Checking %s", path)
- name = path.name.replace(".rs", "")
+ file_name = path.name.replace(".rs", "")
# 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=cfg,
--
2.53.0.959.g497ff81fa9-goog
© 2016 - 2026 Red Hat, Inc.