Skip to content

Commit

Permalink
Add cfg = proc_macro_host_transition to all rules (bazelbuild#301)
Browse files Browse the repository at this point in the history
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 bazelbuild#294 and issue bazelbuild#300.  The issue was introduced
in commit 7cde1e4.

This adds the example from @colin353 in PR bazelbuild#294 as an example that would
otherwise fail.

Fixes bazelbuild#300
  • Loading branch information
dfreese committed Apr 11, 2020
1 parent fe50d3b commit a1d8936
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
23 changes: 23 additions & 0 deletions examples/proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@io_bazel_rules_rust//proto:proto.bzl", "rust_proto_library")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")

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

proto_library(
name = "common",
srcs = ["common.proto"],
)

rust_proto_library(
name = "common_proto_rust",
deps = [":common"],
)

rust_library(
name = "common_lib",
srcs = ["lib.rs"],
deps = [":common_proto_rust"],
)

rust_binary(
name = "common_bin",
srcs = ["main.rs"],
deps = [
":common_lib",
":common_proto_rust",
],
)
5 changes: 5 additions & 0 deletions examples/proto/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extern crate common_proto_rust;

pub fn do_something(x: &common_proto_rust::Config) -> bool {
true
}
6 changes: 6 additions & 0 deletions examples/proto/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extern crate common_lib;
extern crate common_proto_rust;

pub fn main() {
common_lib::do_something(&common_proto_rust::Config::new());
}
9 changes: 6 additions & 3 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ _rust_common_attrs = {
],
),
"_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
"_whitelist_function_transition": attr.label(
default = "//tools/whitelists/function_transition_whitelist",
),
}

_rust_library_attrs = {
Expand All @@ -333,9 +336,6 @@ _rust_library_attrs = {
"""),
default = "rlib",
),
"_whitelist_function_transition": attr.label(
default = "//tools/whitelists/function_transition_whitelist",
),
}

_rust_test_attrs = {
Expand Down Expand Up @@ -443,6 +443,7 @@ rust_binary = rule(
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
cfg = proc_macro_host_transition,
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
"@bazel_tools//tools/cpp:toolchain_type",
Expand Down Expand Up @@ -540,6 +541,7 @@ rust_test = rule(
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
cfg = proc_macro_host_transition,
test = True,
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
Expand Down Expand Up @@ -688,6 +690,7 @@ rust_benchmark = rule(
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
cfg = proc_macro_host_transition,
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
"@bazel_tools//tools/cpp:toolchain_type",
Expand Down
2 changes: 1 addition & 1 deletion rust/private/transitions.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
def _proc_macro_host_transition(settings, attr):
if attr.crate_type == "proc-macro":
if hasattr(attr, "crate_type") and attr.crate_type == "proc-macro":
return {"//command_line_option:platforms": "@local_config_platform//:host"}
else:
return settings
Expand Down

0 comments on commit a1d8936

Please sign in to comment.