From 2bdbc8b9e8d2a18591ad33b33d179cc1ea4ac0f6 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Thu, 27 Nov 2025 23:48:54 +0100 Subject: [PATCH 1/5] Fix linkstamp deps in proc macros These were not being linked in for two reasons: - missing `_use_grep_includes` attribute on the `rust_proc_macro` rule: this attribute & condition are entirely dead code now ==> delete them. - the explicit crate type list doesn't include `proc-macro` ==> add it. --- rust/private/rust.bzl | 3 --- rust/private/rustc.bzl | 14 ++++-------- test/linkstamp/BUILD.bazel | 24 +++++++++++++++++++++ test/linkstamp/linkstamp.c | 1 + test/linkstamp/linkstamp_proc_macro.rs | 8 +++++++ test/linkstamp/linkstamp_proc_macro_test.rs | 4 ++++ 6 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 test/linkstamp/BUILD.bazel create mode 100644 test/linkstamp/linkstamp.c create mode 100644 test/linkstamp/linkstamp_proc_macro.rs create mode 100644 test/linkstamp/linkstamp_proc_macro_test.rs diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 32ce62256b..4b457ccf79 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -924,7 +924,6 @@ _rust_test_attrs = { E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`. """), ), - "_use_grep_includes": attr.bool(default = True), } | _coverage_attrs | _experimental_use_cc_common_link_attrs rust_library = rule( @@ -1085,7 +1084,6 @@ rust_shared_library = rule( "_allowlist_function_transition": attr.label( default = "@bazel_tools//tools/allowlists/function_transition_allowlist", ), - "_use_grep_includes": attr.bool(default = True), }, fragments = ["cpp"], cfg = _rust_shared_library_transition, @@ -1197,7 +1195,6 @@ _rust_binary_attrs = { default = False, ), "stamp": _stamp_attribute(default_value = -1), - "_use_grep_includes": attr.bool(default = True), } | _experimental_use_cc_common_link_attrs def _rust_binary_transition_impl(settings, attr): diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 561ee54f46..f2fe58c2c5 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -184,14 +184,11 @@ def get_compilation_mode_opts(ctx, toolchain): return toolchain.compilation_mode_opts[comp_mode] -def _are_linkstamps_supported(feature_configuration, has_grep_includes): +def _are_linkstamps_supported(feature_configuration): # Are linkstamps supported by the C++ toolchain? return (cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "linkstamps") and # Is Bazel recent enough to support Starlark linkstamps? - hasattr(cc_common, "register_linkstamp_compile_action") and - # The current rule doesn't define _grep_includes attribute; this - # attribute is required for compiling linkstamps. - has_grep_includes) + hasattr(cc_common, "register_linkstamp_compile_action")) def _should_use_pic(cc_toolchain, feature_configuration, crate_type, compilation_mode): """Whether or not [PIC][pic] should be enabled @@ -750,7 +747,7 @@ def collect_inputs( # Register linkstamps when linking with rustc (when linking with # cc_common.link linkstamps are handled by cc_common.link itself). - if not experimental_use_cc_common_link and crate_info.type in ("bin", "cdylib"): + if not experimental_use_cc_common_link and crate_info.type in ("bin", "cdylib", "proc-macro"): # There is no other way to register an action for each member of a depset than # flattening the depset as of 2021-10-12. Luckily, usually there is only one linkstamp # in a build, and we only flatten the list on binary targets that perform transitive linking, @@ -1254,10 +1251,7 @@ def rustc_compile_action( # One or more of the transitive deps is a cc_library / cc_import extra_disabled_features = [] cc_toolchain, feature_configuration = find_cc_toolchain(ctx, extra_disabled_features) - if not _are_linkstamps_supported( - feature_configuration = feature_configuration, - has_grep_includes = hasattr(ctx.attr, "_use_grep_includes"), - ): + if not _are_linkstamps_supported(feature_configuration = feature_configuration): linkstamps = depset([]) # Determine if the build is currently running with --stamp diff --git a/test/linkstamp/BUILD.bazel b/test/linkstamp/BUILD.bazel new file mode 100644 index 0000000000..74681e4442 --- /dev/null +++ b/test/linkstamp/BUILD.bazel @@ -0,0 +1,24 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") +load( + "@rules_rust//rust:defs.bzl", + "rust_proc_macro", + "rust_test", + "rust_static_library", +) + +cc_library( + name = "linkstamp", + linkstamp = "linkstamp.c", +) + +rust_proc_macro( + name = "linkstamp_proc_macro", + srcs = ["linkstamp_proc_macro.rs"], + deps = [":linkstamp"] +) + +rust_test( + name = "linkstamp_proc_macro_test", + srcs = ["linkstamp_proc_macro_test.rs"], + proc_macro_deps = [":linkstamp_proc_macro"], +) diff --git a/test/linkstamp/linkstamp.c b/test/linkstamp/linkstamp.c new file mode 100644 index 0000000000..6dd744d934 --- /dev/null +++ b/test/linkstamp/linkstamp.c @@ -0,0 +1 @@ +int defined_by_linkstamp = 42; diff --git a/test/linkstamp/linkstamp_proc_macro.rs b/test/linkstamp/linkstamp_proc_macro.rs new file mode 100644 index 0000000000..d62c67271b --- /dev/null +++ b/test/linkstamp/linkstamp_proc_macro.rs @@ -0,0 +1,8 @@ +extern "C" { + pub static defined_by_linkstamp : std::os::raw::c_int; +} +#[proc_macro] +pub fn num_from_linkstamp(_: proc_macro::TokenStream) -> proc_macro::TokenStream { + let tt: proc_macro::TokenTree = proc_macro::Literal::u32_suffixed(unsafe { defined_by_linkstamp } as u32).into(); + tt.into() +} diff --git a/test/linkstamp/linkstamp_proc_macro_test.rs b/test/linkstamp/linkstamp_proc_macro_test.rs new file mode 100644 index 0000000000..3f94ec08c1 --- /dev/null +++ b/test/linkstamp/linkstamp_proc_macro_test.rs @@ -0,0 +1,4 @@ +#[test] +fn test_linkstamp_proc_macro() { + assert_eq!(42, linkstamp_proc_macro::num_from_linkstamp!()); +} From d31748b2e9664097327abf1772ab9fe2259329b3 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 28 Nov 2025 01:32:01 +0100 Subject: [PATCH 2/5] Format, fight ci --- test/linkstamp/BUILD.bazel | 5 ++--- test/linkstamp/{linkstamp.c => linkstamp.cc} | 0 test/linkstamp/linkstamp_proc_macro.rs | 5 +++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename test/linkstamp/{linkstamp.c => linkstamp.cc} (100%) diff --git a/test/linkstamp/BUILD.bazel b/test/linkstamp/BUILD.bazel index 74681e4442..45aac7c211 100644 --- a/test/linkstamp/BUILD.bazel +++ b/test/linkstamp/BUILD.bazel @@ -3,18 +3,17 @@ load( "@rules_rust//rust:defs.bzl", "rust_proc_macro", "rust_test", - "rust_static_library", ) cc_library( name = "linkstamp", - linkstamp = "linkstamp.c", + linkstamp = "linkstamp.cc", ) rust_proc_macro( name = "linkstamp_proc_macro", srcs = ["linkstamp_proc_macro.rs"], - deps = [":linkstamp"] + deps = [":linkstamp"], ) rust_test( diff --git a/test/linkstamp/linkstamp.c b/test/linkstamp/linkstamp.cc similarity index 100% rename from test/linkstamp/linkstamp.c rename to test/linkstamp/linkstamp.cc diff --git a/test/linkstamp/linkstamp_proc_macro.rs b/test/linkstamp/linkstamp_proc_macro.rs index d62c67271b..2ddaabd43e 100644 --- a/test/linkstamp/linkstamp_proc_macro.rs +++ b/test/linkstamp/linkstamp_proc_macro.rs @@ -1,8 +1,9 @@ extern "C" { - pub static defined_by_linkstamp : std::os::raw::c_int; + pub static defined_by_linkstamp: std::os::raw::c_int; } #[proc_macro] pub fn num_from_linkstamp(_: proc_macro::TokenStream) -> proc_macro::TokenStream { - let tt: proc_macro::TokenTree = proc_macro::Literal::u32_suffixed(unsafe { defined_by_linkstamp } as u32).into(); + let tt: proc_macro::TokenTree = + proc_macro::Literal::u32_suffixed(unsafe { defined_by_linkstamp } as u32).into(); tt.into() } From 63b3773872b5b8e521ba11ecf74286ba912dfb45 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 28 Nov 2025 02:38:20 +0100 Subject: [PATCH 3/5] Specify edition, mark variable `extern "C"` (matters on windows). --- test/linkstamp/BUILD.bazel | 1 + test/linkstamp/linkstamp.cc | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/test/linkstamp/BUILD.bazel b/test/linkstamp/BUILD.bazel index 45aac7c211..1dc2a8869e 100644 --- a/test/linkstamp/BUILD.bazel +++ b/test/linkstamp/BUILD.bazel @@ -14,6 +14,7 @@ rust_proc_macro( name = "linkstamp_proc_macro", srcs = ["linkstamp_proc_macro.rs"], deps = [":linkstamp"], + edition = "2018", ) rust_test( diff --git a/test/linkstamp/linkstamp.cc b/test/linkstamp/linkstamp.cc index 6dd744d934..b22e8a4ffe 100644 --- a/test/linkstamp/linkstamp.cc +++ b/test/linkstamp/linkstamp.cc @@ -1 +1,3 @@ -int defined_by_linkstamp = 42; +extern "C" { + int defined_by_linkstamp = 42; +} From e0346510724f64d76ddc2dc87af9531979c1cdbd Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 28 Nov 2025 09:04:40 +0100 Subject: [PATCH 4/5] more CI fixes - why do different buildbots require different things --- test/linkstamp/BUILD.bazel | 3 ++- test/linkstamp/linkstamp.cc | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/linkstamp/BUILD.bazel b/test/linkstamp/BUILD.bazel index 1dc2a8869e..7d0a8b6d20 100644 --- a/test/linkstamp/BUILD.bazel +++ b/test/linkstamp/BUILD.bazel @@ -13,12 +13,13 @@ cc_library( rust_proc_macro( name = "linkstamp_proc_macro", srcs = ["linkstamp_proc_macro.rs"], - deps = [":linkstamp"], edition = "2018", + deps = [":linkstamp"], ) rust_test( name = "linkstamp_proc_macro_test", srcs = ["linkstamp_proc_macro_test.rs"], proc_macro_deps = [":linkstamp_proc_macro"], + edition = "2018", ) diff --git a/test/linkstamp/linkstamp.cc b/test/linkstamp/linkstamp.cc index b22e8a4ffe..61799aa54e 100644 --- a/test/linkstamp/linkstamp.cc +++ b/test/linkstamp/linkstamp.cc @@ -1,3 +1,3 @@ extern "C" { - int defined_by_linkstamp = 42; +int defined_by_linkstamp = 42; } From 256292057e90f9ddeb5331274c9e46b14be79c36 Mon Sep 17 00:00:00 2001 From: Sam McCall Date: Fri, 28 Nov 2025 09:12:40 +0100 Subject: [PATCH 5/5] sigh --- test/linkstamp/BUILD.bazel | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/linkstamp/BUILD.bazel b/test/linkstamp/BUILD.bazel index 7d0a8b6d20..89be98f5f2 100644 --- a/test/linkstamp/BUILD.bazel +++ b/test/linkstamp/BUILD.bazel @@ -20,6 +20,6 @@ rust_proc_macro( rust_test( name = "linkstamp_proc_macro_test", srcs = ["linkstamp_proc_macro_test.rs"], - proc_macro_deps = [":linkstamp_proc_macro"], edition = "2018", + proc_macro_deps = [":linkstamp_proc_macro"], )