[PATCH 4/6] scripts: generate_rust_analyzer: plumb common crate-attrs for non-host crates
From: Eliot Courtney <hidden>
Date: 2026-01-20 08:54:34
Also in:
linux-kbuild, lkml
Subsystem:
kernel build + files below scripts/ (unless maintained elsewhere), rust, rust [rust-analyzer], the rest · Maintainers:
Nathan Chancellor, Nicolas Schier, Miguel Ojeda, Tamir Duberstein, Linus Torvalds
Add --common-crate-attrs argument to specify crate attributes that apply to all non-host crates. This means no_std will be specified where it should be, for example. Proc macro and libraries used by proc macro crates run on the host and do not get this set of common crate-attrs. This mirrors rust_procmacrolibrary in the build system. Create scripts/Makefile.rust to hold shared Rust definitions. Signed-off-by: Eliot Courtney <redacted> --- rust/Makefile | 3 +++ scripts/Makefile.build | 14 +------------- scripts/Makefile.rust | 20 ++++++++++++++++++++ scripts/generate_rust_analyzer.py | 20 +++++++++++++++----- 4 files changed, 39 insertions(+), 18 deletions(-)
diff --git a/rust/Makefile b/rust/Makefile
index e6c5108ab625..750db2885ba2 100644
--- a/rust/Makefile
+++ b/rust/Makefile@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 +include $(srctree)/scripts/Makefile.rust + # Where to place rustdoc generated documentation rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc
@@ -578,6 +580,7 @@ rust-analyzer: --editions='core=$(core-edition)' \ --editions='quote=$(quote-edition)' \ --crate-attrs='proc_macro2=$(proc_macro2-crate-attrs)' \ + --common-crate-attrs='$(rust_common_crate_attrs)' \ $(realpath $(srctree)) $(realpath $(objtree)) \ $(rustc_sysroot) $(RUST_LIB_SRC) $(if $(KBUILD_EXTMOD),$(srcroot)) \ > rust-project.json
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 0c838c467c76..e9af8a11f2dc 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build@@ -34,6 +34,7 @@ subdir-ccflags-y := include $(srctree)/scripts/Kbuild.include include $(srctree)/scripts/Makefile.compiler +include $(srctree)/scripts/Makefile.rust include $(kbuild-file) include $(srctree)/scripts/Makefile.lib
@@ -306,19 +307,6 @@ $(obj)/%.lst: $(obj)/%.c FORCE # Compile Rust sources (.rs) # --------------------------------------------------------------------------- -# The features in this list are the ones allowed for non-`rust/` code. -# -# - Stable since Rust 1.81.0: `feature(lint_reasons)`. -# - Stable since Rust 1.82.0: `feature(asm_const)`, -# `feature(offset_of_nested)`, `feature(raw_ref_op)`. -# - Stable since Rust 1.87.0: `feature(asm_goto)`. -# - Expected to become stable: `feature(arbitrary_self_types)`. -# - To be determined: `feature(used_with_arg)`. -# -# Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on -# the unstable features in use. -rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op,used_with_arg - # `--out-dir` is required to avoid temporaries being created by `rustc` in the # current working directory, which may be not accessible in the out-of-tree # modules case.
diff --git a/scripts/Makefile.rust b/scripts/Makefile.rust
new file mode 100644
index 000000000000..d96662d1ac17
--- /dev/null
+++ b/scripts/Makefile.rust@@ -0,0 +1,20 @@ +# SPDX-License-Identifier: GPL-2.0 +# ========================================================================== +# Rust shared definitions +# ========================================================================== + +# The features in this list are the ones allowed for non-`rust/` code. +# +# - Stable since Rust 1.81.0: `feature(lint_reasons)`. +# - Stable since Rust 1.82.0: `feature(asm_const)`, +# `feature(offset_of_nested)`, `feature(raw_ref_op)`. +# - Stable since Rust 1.87.0: `feature(asm_goto)`. +# - Expected to become stable: `feature(arbitrary_self_types)`. +# - To be determined: `feature(used_with_arg)`. +# +# Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on +# the unstable features in use. +rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op,used_with_arg + +# Common crate attrs for non-host, non-sysroot crates. +rust_common_crate_attrs := no_std feature($(rust_allowed_features))
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index e8c50812fb9f..0d93e8a8e4bd 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py@@ -27,7 +27,7 @@ def args_crates_cfgs(cfgs): return crates_cfgs -def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, crate_attrs): +def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, crate_attrs, common_crate_attrs): # Generate the configuration list. generated_cfg = [] with open(objtree / "include" / "generated" / "rustc_cfg") as fd:
@@ -45,7 +45,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, crates_editions = args_single(editions) crates_crate_attrs = args_crates_cfgs(crate_attrs) - def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False): + def append_crate(display_name, root_module, deps, cfg=[], is_workspace_member=True, is_proc_macro=False, is_proc_macro_library=False): # Miguel Ojeda writes: # # > ... in principle even the sysroot crates may have different
@@ -72,6 +72,11 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, # assumption if future edition moves span multiple rust versions. edition = crates_editions.get(display_name, "2021") + crate_attrs = crates_crate_attrs.get(display_name, []) + # Apply common crate attrs to non-host crates. + if not is_proc_macro_library and not is_proc_macro: + crate_attrs = common_crate_attrs + crate_attrs + crate = { "display_name": display_name, "root_module": str(root_module),
@@ -81,7 +86,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, "cfg": cfg, "edition": edition, # Crate attributes were introduced in 1.94.0 but older versions will silently ignore this. - "crate_attrs": crates_crate_attrs.get(display_name, []), + "crate_attrs": crate_attrs, "env": { "RUST_MODFILE": "This is only for rust-analyzer" }
@@ -127,13 +132,15 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, srctree / "rust" / "proc-macro2" / "lib.rs", ["core", "alloc", "std", "proc_macro"], cfg=crates_cfgs["proc_macro2"], + is_proc_macro_library=True, ) append_crate( "quote", srctree / "rust" / "quote" / "lib.rs", ["core", "alloc", "std", "proc_macro", "proc_macro2"], - cfg=crates_cfgs["quote"] + cfg=crates_cfgs["quote"], + is_proc_macro_library=True, ) append_crate(
@@ -141,6 +148,7 @@ def generate_crates(srctree, objtree, sysroot_src, external_src, cfgs, editions, srctree / "rust" / "syn" / "lib.rs", ["std", "proc_macro", "proc_macro2", "quote"], cfg=crates_cfgs["syn"], + is_proc_macro_library=True, ) append_crate(
@@ -238,6 +246,7 @@ def main(): parser.add_argument('--cfgs', action='append', default=[]) parser.add_argument('--editions', action='append', default=[]) parser.add_argument('--crate-attrs', action='append', default=[]) + parser.add_argument('--common-crate-attrs', default='') parser.add_argument("srctree", type=pathlib.Path) parser.add_argument("objtree", type=pathlib.Path) parser.add_argument("sysroot", type=pathlib.Path)
@@ -250,8 +259,9 @@ def main(): level=logging.INFO if args.verbose else logging.WARNING ) + common_crate_attrs = args.common_crate_attrs.split() if args.common_crate_attrs else [] rust_project = { - "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.editions, args.crate_attrs), + "crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs, args.editions, args.crate_attrs, common_crate_attrs), "sysroot": str(args.sysroot), }
--
2.52.0