Skip to content

Commit 7e9535e

Browse files
Darksonnojeda
authored andcommitted
rust: support overriding crate_name
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>
1 parent 96f4e74 commit 7e9535e

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

scripts/Makefile.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ rust_common_cmd = \
333333
-Zcrate-attr='feature($(rust_allowed_features))' \
334334
-Zunstable-options --extern pin_init --extern kernel \
335335
--crate-type rlib -L $(objtree)/rust/ \
336-
--crate-name $(basename $(notdir $@)) \
337336
--sysroot=/dev/null \
338337
--out-dir $(dir $@) --emit=dep-info=$(depfile)
339338

scripts/generate_rust_analyzer.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
import sys
1313
from typing import Dict, Iterable, List, Literal, Optional, TypedDict
1414

15+
def invoke_rustc(args: List[str]) -> str:
16+
return subprocess.check_output(
17+
[os.environ["RUSTC"]] + args,
18+
stdin=subprocess.DEVNULL,
19+
).decode('utf-8').strip()
20+
1521
def args_crates_cfgs(cfgs: List[str]) -> Dict[str, List[str]]:
1622
crates_cfgs = {}
1723
for cfg in cfgs:
@@ -69,6 +75,9 @@ def generate_crates(
6975
crates: List[Crate] = []
7076
crates_cfgs = args_crates_cfgs(cfgs)
7177

78+
def get_crate_name(path: pathlib.Path) -> str:
79+
return invoke_rustc(["--print", "crate-name", str(path)])
80+
7281
def build_crate(
7382
display_name: str,
7483
root_module: pathlib.Path,
@@ -112,23 +121,15 @@ def append_proc_macro_crate(
112121
is_workspace_member=is_workspace_member,
113122
edition=edition,
114123
)
115-
proc_macro_dylib_name = (
116-
subprocess.check_output(
117-
[
118-
os.environ["RUSTC"],
119-
"--print",
120-
"file-names",
121-
"--crate-name",
122-
display_name,
123-
"--crate-type",
124-
"proc-macro",
125-
"-",
126-
],
127-
stdin=subprocess.DEVNULL,
128-
)
129-
.decode("utf-8")
130-
.strip()
131-
)
124+
proc_macro_dylib_name = invoke_rustc([
125+
"--print",
126+
"file-names",
127+
"--crate-name",
128+
display_name,
129+
"--crate-type",
130+
"proc-macro",
131+
"-",
132+
])
132133
proc_macro_crate: ProcMacroCrate = {
133134
**crate,
134135
"is_proc_macro": True,
@@ -324,16 +325,17 @@ def is_root_crate(build_file: pathlib.Path, target: str) -> bool:
324325
for folder in extra_dirs:
325326
for path in folder.rglob("*.rs"):
326327
logging.info("Checking %s", path)
327-
name = path.stem
328+
file_name = path.stem
328329

329330
# Skip those that are not crate roots.
330-
if not is_root_crate(path.parent / "Makefile", name) and \
331-
not is_root_crate(path.parent / "Kbuild", name):
331+
if not is_root_crate(path.parent / "Makefile", file_name) and \
332+
not is_root_crate(path.parent / "Kbuild", file_name):
332333
continue
333334

334-
logging.info("Adding %s", name)
335+
crate_name = get_crate_name(path)
336+
logging.info("Adding %s", crate_name)
335337
append_crate(
336-
name,
338+
crate_name,
337339
path,
338340
[core, kernel, pin_init],
339341
cfg=generated_cfg,

0 commit comments

Comments
 (0)