Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
14 changes: 4 additions & 10 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions test/linkstamp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
)
3 changes: 3 additions & 0 deletions test/linkstamp/linkstamp.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern "C" {
int defined_by_linkstamp = 42;
}
9 changes: 9 additions & 0 deletions test/linkstamp/linkstamp_proc_macro.rs
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 4 additions & 0 deletions test/linkstamp/linkstamp_proc_macro_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test_linkstamp_proc_macro() {
assert_eq!(42, linkstamp_proc_macro::num_from_linkstamp!());
}