Skip to content

Commit 2bdbc8b

Browse files
committed
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.
1 parent f93bf4d commit 2bdbc8b

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

rust/private/rust.bzl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,6 @@ _rust_test_attrs = {
924924
E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`.
925925
"""),
926926
),
927-
"_use_grep_includes": attr.bool(default = True),
928927
} | _coverage_attrs | _experimental_use_cc_common_link_attrs
929928

930929
rust_library = rule(
@@ -1085,7 +1084,6 @@ rust_shared_library = rule(
10851084
"_allowlist_function_transition": attr.label(
10861085
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
10871086
),
1088-
"_use_grep_includes": attr.bool(default = True),
10891087
},
10901088
fragments = ["cpp"],
10911089
cfg = _rust_shared_library_transition,
@@ -1197,7 +1195,6 @@ _rust_binary_attrs = {
11971195
default = False,
11981196
),
11991197
"stamp": _stamp_attribute(default_value = -1),
1200-
"_use_grep_includes": attr.bool(default = True),
12011198
} | _experimental_use_cc_common_link_attrs
12021199

12031200
def _rust_binary_transition_impl(settings, attr):

rust/private/rustc.bzl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,11 @@ def get_compilation_mode_opts(ctx, toolchain):
184184

185185
return toolchain.compilation_mode_opts[comp_mode]
186186

187-
def _are_linkstamps_supported(feature_configuration, has_grep_includes):
187+
def _are_linkstamps_supported(feature_configuration):
188188
# Are linkstamps supported by the C++ toolchain?
189189
return (cc_common.is_enabled(feature_configuration = feature_configuration, feature_name = "linkstamps") and
190190
# Is Bazel recent enough to support Starlark linkstamps?
191-
hasattr(cc_common, "register_linkstamp_compile_action") and
192-
# The current rule doesn't define _grep_includes attribute; this
193-
# attribute is required for compiling linkstamps.
194-
has_grep_includes)
191+
hasattr(cc_common, "register_linkstamp_compile_action"))
195192

196193
def _should_use_pic(cc_toolchain, feature_configuration, crate_type, compilation_mode):
197194
"""Whether or not [PIC][pic] should be enabled
@@ -750,7 +747,7 @@ def collect_inputs(
750747

751748
# Register linkstamps when linking with rustc (when linking with
752749
# cc_common.link linkstamps are handled by cc_common.link itself).
753-
if not experimental_use_cc_common_link and crate_info.type in ("bin", "cdylib"):
750+
if not experimental_use_cc_common_link and crate_info.type in ("bin", "cdylib", "proc-macro"):
754751
# There is no other way to register an action for each member of a depset than
755752
# flattening the depset as of 2021-10-12. Luckily, usually there is only one linkstamp
756753
# in a build, and we only flatten the list on binary targets that perform transitive linking,
@@ -1254,10 +1251,7 @@ def rustc_compile_action(
12541251
# One or more of the transitive deps is a cc_library / cc_import
12551252
extra_disabled_features = []
12561253
cc_toolchain, feature_configuration = find_cc_toolchain(ctx, extra_disabled_features)
1257-
if not _are_linkstamps_supported(
1258-
feature_configuration = feature_configuration,
1259-
has_grep_includes = hasattr(ctx.attr, "_use_grep_includes"),
1260-
):
1254+
if not _are_linkstamps_supported(feature_configuration = feature_configuration):
12611255
linkstamps = depset([])
12621256

12631257
# Determine if the build is currently running with --stamp

test/linkstamp/BUILD.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load("@rules_cc//cc:defs.bzl", "cc_library")
2+
load(
3+
"@rules_rust//rust:defs.bzl",
4+
"rust_proc_macro",
5+
"rust_test",
6+
"rust_static_library",
7+
)
8+
9+
cc_library(
10+
name = "linkstamp",
11+
linkstamp = "linkstamp.c",
12+
)
13+
14+
rust_proc_macro(
15+
name = "linkstamp_proc_macro",
16+
srcs = ["linkstamp_proc_macro.rs"],
17+
deps = [":linkstamp"]
18+
)
19+
20+
rust_test(
21+
name = "linkstamp_proc_macro_test",
22+
srcs = ["linkstamp_proc_macro_test.rs"],
23+
proc_macro_deps = [":linkstamp_proc_macro"],
24+
)

test/linkstamp/linkstamp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int defined_by_linkstamp = 42;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern "C" {
2+
pub static defined_by_linkstamp : std::os::raw::c_int;
3+
}
4+
#[proc_macro]
5+
pub fn num_from_linkstamp(_: proc_macro::TokenStream) -> proc_macro::TokenStream {
6+
let tt: proc_macro::TokenTree = proc_macro::Literal::u32_suffixed(unsafe { defined_by_linkstamp } as u32).into();
7+
tt.into()
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test_linkstamp_proc_macro() {
3+
assert_eq!(42, linkstamp_proc_macro::num_from_linkstamp!());
4+
}

0 commit comments

Comments
 (0)