Skip to content

Commit a1d8936

Browse files
authored
Add cfg = proc_macro_host_transition to all rules (#301)
The rust_library rule specifies a cfg option. This performs transitions in order to support WASM. Other rules, such as rust_binary, do not have this configuration, which causes Bazel to build and link some libraries into the final binary twice, resulting in compilation errors. This is detailed in pull request #294 and issue #300. The issue was introduced in commit 7cde1e4. This adds the example from @colin353 in PR #294 as an example that would otherwise fail. Fixes #300
1 parent fe50d3b commit a1d8936

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

examples/proto/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
11
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@io_bazel_rules_rust//proto:proto.bzl", "rust_proto_library")
3+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
4+
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")
25

36
package(default_visibility = ["//proto:__subpackages__"])
47

58
proto_library(
69
name = "common",
710
srcs = ["common.proto"],
811
)
12+
13+
rust_proto_library(
14+
name = "common_proto_rust",
15+
deps = [":common"],
16+
)
17+
18+
rust_library(
19+
name = "common_lib",
20+
srcs = ["lib.rs"],
21+
deps = [":common_proto_rust"],
22+
)
23+
24+
rust_binary(
25+
name = "common_bin",
26+
srcs = ["main.rs"],
27+
deps = [
28+
":common_lib",
29+
":common_proto_rust",
30+
],
31+
)

examples/proto/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate common_proto_rust;
2+
3+
pub fn do_something(x: &common_proto_rust::Config) -> bool {
4+
true
5+
}

examples/proto/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate common_lib;
2+
extern crate common_proto_rust;
3+
4+
pub fn main() {
5+
common_lib::do_something(&common_proto_rust::Config::new());
6+
}

rust/private/rust.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ _rust_common_attrs = {
321321
],
322322
),
323323
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
324+
"_whitelist_function_transition": attr.label(
325+
default = "//tools/whitelists/function_transition_whitelist",
326+
),
324327
}
325328

326329
_rust_library_attrs = {
@@ -333,9 +336,6 @@ _rust_library_attrs = {
333336
"""),
334337
default = "rlib",
335338
),
336-
"_whitelist_function_transition": attr.label(
337-
default = "//tools/whitelists/function_transition_whitelist",
338-
),
339339
}
340340

341341
_rust_test_attrs = {
@@ -443,6 +443,7 @@ rust_binary = rule(
443443
executable = True,
444444
fragments = ["cpp"],
445445
host_fragments = ["cpp"],
446+
cfg = proc_macro_host_transition,
446447
toolchains = [
447448
"@io_bazel_rules_rust//rust:toolchain",
448449
"@bazel_tools//tools/cpp:toolchain_type",
@@ -540,6 +541,7 @@ rust_test = rule(
540541
executable = True,
541542
fragments = ["cpp"],
542543
host_fragments = ["cpp"],
544+
cfg = proc_macro_host_transition,
543545
test = True,
544546
toolchains = [
545547
"@io_bazel_rules_rust//rust:toolchain",
@@ -688,6 +690,7 @@ rust_benchmark = rule(
688690
executable = True,
689691
fragments = ["cpp"],
690692
host_fragments = ["cpp"],
693+
cfg = proc_macro_host_transition,
691694
toolchains = [
692695
"@io_bazel_rules_rust//rust:toolchain",
693696
"@bazel_tools//tools/cpp:toolchain_type",

rust/private/transitions.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
def _proc_macro_host_transition(settings, attr):
2-
if attr.crate_type == "proc-macro":
2+
if hasattr(attr, "crate_type") and attr.crate_type == "proc-macro":
33
return {"//command_line_option:platforms": "@local_config_platform//:host"}
44
else:
55
return settings

0 commit comments

Comments
 (0)