From: Eliot Courtney <ecourtney@nvidia.com>
To: "Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Nathan Chancellor" <nathan@kernel.org>,
"Nicolas Schier" <nsc@kernel.org>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-kbuild@vger.kernel.org,
Eliot Courtney <ecourtney@nvidia.com>
Subject: [PATCH 4/6] scripts: generate_rust_analyzer: plumb common crate-attrs for non-host crates
Date: Tue, 20 Jan 2026 17:52:53 +0900 [thread overview]
Message-ID: <20260120-ra-fix-v1-4-829e4e92818c@nvidia.com> (raw)
In-Reply-To: <20260120-ra-fix-v1-0-829e4e92818c@nvidia.com>
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 <ecourtney@nvidia.com>
---
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
next prev parent reply other threads:[~2026-01-20 8:54 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-20 8:52 [PATCH 0/6] scripts: generate_rust_analyzer: improve rust-project.json generation Eliot Courtney
2026-01-20 8:52 ` [PATCH 1/6] scripts: generate_rust_analyzer: rename cfg to generated_cfg Eliot Courtney
2026-01-20 15:28 ` Tamir Duberstein
2026-03-02 17:09 ` Tamir Duberstein
2026-01-20 8:52 ` [PATCH 2/6] scripts: generate_rust_analyzer: plumb editions via command line Eliot Courtney
2026-01-20 13:57 ` Miguel Ojeda
2026-01-20 15:28 ` Tamir Duberstein
2026-01-20 8:52 ` [PATCH 3/6] scripts: generate_rust_analyzer: plumb crate-attrs Eliot Courtney
2026-01-20 15:28 ` Tamir Duberstein
2026-01-20 8:52 ` Eliot Courtney [this message]
2026-01-20 15:28 ` [PATCH 4/6] scripts: generate_rust_analyzer: plumb common crate-attrs for non-host crates Tamir Duberstein
2026-01-20 8:52 ` [PATCH 5/6] scripts: generate_rust_analyzer: add pin_init to driver crate deps Eliot Courtney
2026-01-20 15:28 ` Tamir Duberstein
2026-01-20 8:52 ` [PATCH 6/6] scripts: generate_rust_analyzer: move sysroot crates to sysroot_project Eliot Courtney
2026-01-20 15:34 ` Tamir Duberstein
2026-01-21 0:01 ` Jesung Yang
2026-01-22 9:23 ` Eliot Courtney
2026-01-22 11:06 ` Jesung Yang
2026-01-23 5:45 ` Eliot Courtney
2026-01-23 12:08 ` Miguel Ojeda
2026-01-23 12:23 ` Gary Guo
2026-01-23 12:31 ` Miguel Ojeda
2026-01-20 13:38 ` [PATCH 0/6] scripts: generate_rust_analyzer: improve rust-project.json generation Miguel Ojeda
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260120-ra-fix-v1-4-829e4e92818c@nvidia.com \
--to=ecourtney@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nathan@kernel.org \
--cc=nsc@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®