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..89be98f5f2 --- /dev/null +++ b/test/linkstamp/BUILD.bazel @@ -0,0 +1,25 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") +load( + "@rules_rust//rust:defs.bzl", + "rust_proc_macro", + "rust_test", +) + +cc_library( + name = "linkstamp", + linkstamp = "linkstamp.cc", +) + +rust_proc_macro( + name = "linkstamp_proc_macro", + srcs = ["linkstamp_proc_macro.rs"], + edition = "2018", + deps = [":linkstamp"], +) + +rust_test( + name = "linkstamp_proc_macro_test", + srcs = ["linkstamp_proc_macro_test.rs"], + edition = "2018", + proc_macro_deps = [":linkstamp_proc_macro"], +) diff --git a/test/linkstamp/linkstamp.cc b/test/linkstamp/linkstamp.cc new file mode 100644 index 0000000000..61799aa54e --- /dev/null +++ b/test/linkstamp/linkstamp.cc @@ -0,0 +1,3 @@ +extern "C" { +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..2ddaabd43e --- /dev/null +++ b/test/linkstamp/linkstamp_proc_macro.rs @@ -0,0 +1,9 @@ +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!()); +}