Skip to content

Use relative labels and Label macro wherever possible #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 21, 2021
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
2 changes: 1 addition & 1 deletion BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "error_format")
load("//rust:private/rustc.bzl", "error_format")

bzl_library(
name = "rules",
Expand Down
10 changes: 5 additions & 5 deletions bindgen/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@io_bazel_rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//bindgen:bindgen.bzl", "rust_bindgen_toolchain")

package(default_visibility = ["//visibility:public"])

Expand All @@ -8,18 +8,18 @@ toolchain_type(name = "bindgen_toolchain")
bzl_library(
name = "rules",
srcs = glob(["**/*.bzl"]),
deps = ["@io_bazel_rules_rust//rust:rules"],
deps = ["//rust:rules"],
)

rust_bindgen_toolchain(
name = "default_bindgen_toolchain_impl",
bindgen = "//bindgen/raze:cargo_bin_bindgen",
clang = select({
"@io_bazel_rules_rust//rust/platform:osx": "@bindgen_clang_osx//:clang",
"//rust/platform:osx": "@bindgen_clang_osx//:clang",
"//conditions:default": "@bindgen_clang_linux//:clang",
}),
libclang = select({
"@io_bazel_rules_rust//rust/platform:osx": "@bindgen_clang_osx//:libclang.so",
"//rust/platform:osx": "@bindgen_clang_osx//:libclang.so",
"//conditions:default": "@bindgen_clang_linux//:libclang.so",
}),
libstdcxx = "@local_libstdcpp//:libstdc++",
Expand All @@ -28,5 +28,5 @@ rust_bindgen_toolchain(
toolchain(
name = "default_bindgen_toolchain",
toolchain = "default_bindgen_toolchain_impl",
toolchain_type = "@io_bazel_rules_rust//bindgen:bindgen_toolchain",
toolchain_type = "//bindgen:bindgen_toolchain",
)
12 changes: 6 additions & 6 deletions bindgen/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.

# buildifier: disable=module-docstring
load("@io_bazel_rules_rust//rust:private/utils.bzl", "find_toolchain", "get_libs_for_static_executable")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library")
load("//rust:private/utils.bzl", "find_toolchain", "get_libs_for_static_executable")
load("//rust:rust.bzl", "rust_library")

def rust_bindgen_library(
name,
Expand Down Expand Up @@ -61,7 +61,7 @@ def _rust_bindgen_impl(ctx):
if header not in cc_header_list:
fail("Header {} is not in {}'s transitive headers.".format(ctx.attr.header, cc_lib), "header")

toolchain = ctx.toolchains["@io_bazel_rules_rust//bindgen:bindgen_toolchain"]
toolchain = ctx.toolchains[Label("//bindgen:bindgen_toolchain")]
bindgen_bin = toolchain.bindgen
rustfmt_bin = toolchain.rustfmt or rust_toolchain.rustfmt
clang_bin = toolchain.clang
Expand Down Expand Up @@ -168,16 +168,16 @@ rust_bindgen = rule(
doc = "Flags to pass directly to the clang executable.",
),
"_process_wrapper": attr.label(
default = "@io_bazel_rules_rust//util/process_wrapper",
default = Label("//util/process_wrapper"),
executable = True,
allow_single_file = True,
cfg = "exec",
),
},
outputs = {"out": "%{name}.rs"},
toolchains = [
"@io_bazel_rules_rust//bindgen:bindgen_toolchain",
"@io_bazel_rules_rust//rust:toolchain",
str(Label("//bindgen:bindgen_toolchain")),
Copy link
Collaborator Author

@UebelAndre UebelAndre Dec 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm shopping around for some thoughts here and generally more insight on Label. I'm not sure if this is an appropriate use of Label. Anyone have any experiences like this they can share?

Specifically around str(Label("..."))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfectly acceptable. I remember having a funny discussion with several senior engineer in the Bazel team about this "hack" and having the manager answer: I don't think this is a hack, which just settled our decision to keep this as an idiom, although I left the team 3 years ago so I don't know what is the status now.

str(Label("//rust:toolchain")),
],
)

Expand Down
2 changes: 1 addition & 1 deletion bindgen/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def rust_bindgen_repositories():

rules_rust_bindgen_fetch_remote_crates()

native.register_toolchains("@io_bazel_rules_rust//bindgen:default_bindgen_toolchain")
native.register_toolchains(str(Label("//bindgen:default_bindgen_toolchain")))

_COMMON_WORKSPACE = """\
workspace(name = "{}")
Expand Down
10 changes: 5 additions & 5 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# buildifier: disable=module-docstring
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "C_COMPILE_ACTION_NAME")
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("@io_bazel_rules_rust//rust:private/rust.bzl", "name_to_crate_name")
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "BuildInfo", "DepInfo", "get_cc_toolchain", "get_compilation_mode_opts", "get_linker_and_args")
load("@io_bazel_rules_rust//rust:private/utils.bzl", "expand_locations", "find_toolchain")
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary")
load("//rust:private/rust.bzl", "name_to_crate_name")
load("//rust:private/rustc.bzl", "BuildInfo", "DepInfo", "get_cc_toolchain", "get_compilation_mode_opts", "get_linker_and_args")
load("//rust:private/utils.bzl", "expand_locations", "find_toolchain")
load("//rust:rust.bzl", "rust_binary")

def get_cc_compile_env(cc_toolchain, feature_configuration):
"""Gather cc environment variables from the given `cc_toolchain`
Expand Down Expand Up @@ -232,7 +232,7 @@ _build_script_run = rule(
},
fragments = ["cpp"],
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
str(Label("//rust:toolchain")),
"@bazel_tools//tools/cpp:toolchain_type",
],
)
Expand Down
2 changes: 1 addition & 1 deletion proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rust_binary(
toolchain(
name = "default-proto-toolchain",
toolchain = ":default-proto-toolchain-impl",
toolchain_type = "@io_bazel_rules_rust//proto:toolchain",
toolchain_type = "//proto:toolchain",
)

rust_proto_toolchain(name = "default-proto-toolchain-impl")
Expand Down
38 changes: 17 additions & 21 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ To use the Rust proto rules, add the following to your `WORKSPACE` file to add t
external repositories for the Rust proto toolchain (in addition to the [rust rules setup](..)):

```python
load("@io_bazel_rules_rust//proto:repositories.bzl", "rust_proto_repositories")
load("//proto:repositories.bzl", "rust_proto_repositories")

rust_proto_repositories()
```
"""

load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load(
"@io_bazel_rules_rust//proto:toolchain.bzl",
"//proto:toolchain.bzl",
"GRPC_COMPILE_DEPS",
"PROTO_COMPILE_DEPS",
_generate_proto = "rust_generate_proto",
_generated_file_stem = "generated_file_stem",
)
load("@io_bazel_rules_rust//rust:private/rustc.bzl", "CrateInfo", "rustc_compile_action")
load("@io_bazel_rules_rust//rust:private/utils.bzl", "determine_output_hash", "find_toolchain")
load("@rules_proto//proto:defs.bzl", "ProtoInfo")
load("//rust:private/rustc.bzl", "CrateInfo", "rustc_compile_action")
load("//rust:private/utils.bzl", "determine_output_hash", "find_toolchain")

RustProtoInfo = provider(
doc = "Rust protobuf provider info",
Expand Down Expand Up @@ -182,7 +182,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
"""

# Create all the source in a specific folder
proto_toolchain = ctx.toolchains["@io_bazel_rules_rust//proto:toolchain"]
proto_toolchain = ctx.toolchains[Label("//proto:toolchain")]
output_dir = "%s.%s.rust" % (crate_name, "grpc" if is_grpc else "proto")

# Generate the proto stubs
Expand Down Expand Up @@ -283,27 +283,25 @@ rust_proto_library = rule(
default = PROTO_COMPILE_DEPS,
),
"_cc_toolchain": attr.label(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_process_wrapper": attr.label(
default = "@io_bazel_rules_rust//util/process_wrapper",
default = Label("//util/process_wrapper"),
executable = True,
allow_single_file = True,
cfg = "exec",
),
"_optional_output_wrapper": attr.label(
executable = True,
cfg = "exec",
default = Label(
"@io_bazel_rules_rust//proto:optional_output_wrapper",
),
default = Label("//proto:optional_output_wrapper"),
),
},
fragments = ["cpp"],
host_fragments = ["cpp"],
toolchains = [
"@io_bazel_rules_rust//proto:toolchain",
"@io_bazel_rules_rust//rust:toolchain",
str(Label("//proto:toolchain")),
str(Label("//rust:toolchain")),
"@bazel_tools//tools/cpp:toolchain_type",
],
doc = """\
Expand Down Expand Up @@ -365,24 +363,22 @@ rust_grpc_library = rule(
default = "@bazel_tools//tools/cpp:current_cc_toolchain",
),
"_process_wrapper": attr.label(
default = "@io_bazel_rules_rust//util/process_wrapper",
default = Label("//util/process_wrapper"),
executable = True,
allow_single_file = True,
cfg = "exec",
),
"_optional_output_wrapper": attr.label(
executable = True,
cfg = "exec",
default = Label(
"@io_bazel_rules_rust//proto:optional_output_wrapper",
),
default = Label("//proto:optional_output_wrapper"),
),
},
fragments = ["cpp"],
host_fragments = ["cpp"],
toolchains = [
"@io_bazel_rules_rust//proto:toolchain",
"@io_bazel_rules_rust//rust:toolchain",
str(Label("//proto:toolchain")),
str(Label("//rust:toolchain")),
"@bazel_tools//tools/cpp:toolchain_type",
],
doc = """\
Expand All @@ -391,8 +387,8 @@ Builds a Rust library crate from a set of `proto_library`s suitable for gRPC.
Example:

```python
load("@io_bazel_rules_rust//proto:proto.bzl", "rust_grpc_library")
load("@io_bazel_rules_rust//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")
load("//proto:proto.bzl", "rust_grpc_library")
load("//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")

proto_library(
name = "my_proto",
Expand Down
4 changes: 1 addition & 3 deletions proto/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,4 @@ def rust_proto_repositories():
rules_rust_proto_fetch_remote_crates()

# Register toolchains
native.register_toolchains(
"@io_bazel_rules_rust//proto:default-proto-toolchain",
)
native.register_toolchains(str(Label("//proto:default-proto-toolchain")))
18 changes: 7 additions & 11 deletions proto/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

"""Toolchain for compiling rust stubs from protobuf and gRPC."""

load("@io_bazel_rules_rust//rust:private/rust.bzl", "name_to_crate_name")
load("//rust:private/rust.bzl", "name_to_crate_name")

def generated_file_stem(f):
basename = f.rsplit("/", 2)[-1]
Expand Down Expand Up @@ -113,14 +113,14 @@ def _rust_proto_toolchain_impl(ctx):

# Default dependencies needed to compile protobuf stubs.
PROTO_COMPILE_DEPS = [
"@io_bazel_rules_rust//proto/raze:protobuf",
Label("//proto/raze:protobuf"),
]

# Default dependencies needed to compile gRPC stubs.
GRPC_COMPILE_DEPS = PROTO_COMPILE_DEPS + [
"@io_bazel_rules_rust//proto/raze:grpc",
"@io_bazel_rules_rust//proto/raze:tls_api",
"@io_bazel_rules_rust//proto/raze:tls_api_stub",
Label("//proto/raze:grpc"),
Label("//proto/raze:tls_api"),
Label("//proto/raze:tls_api_stub"),
]

# TODO(damienmg): Once bazelbuild/bazel#6889 is fixed, reintroduce
Expand All @@ -139,17 +139,13 @@ rust_proto_toolchain = rule(
doc = "The location of the Rust protobuf compiler plugin used to generate rust sources.",
allow_single_file = True,
cfg = "exec",
default = Label(
"@io_bazel_rules_rust//proto:protoc_gen_rust",
),
default = Label("//proto:protoc_gen_rust"),
),
"grpc_plugin": attr.label(
doc = "The location of the Rust protobuf compiler plugin to generate rust gRPC stubs.",
allow_single_file = True,
cfg = "exec",
default = Label(
"@io_bazel_rules_rust//proto:protoc_gen_rust_grpc",
),
default = Label("//proto:protoc_gen_rust_grpc"),
),
"edition": attr.string(
doc = "The edition used by the generated rust source.",
Expand Down
2 changes: 1 addition & 1 deletion rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ bzl_library(
name = "rules",
srcs = glob(["**/*.bzl"]),
deps = [
"@io_bazel_rules_rust//rust/platform:rules",
"//rust/platform:rules",
],
)
2 changes: 1 addition & 1 deletion rust/platform/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ package_group(
bzl_library(
name = "rules",
srcs = glob(["**/*.bzl"]),
visibility = ["@io_bazel_rules_rust//rust:__pkg__"],
visibility = ["//rust:__pkg__"],
)
8 changes: 4 additions & 4 deletions rust/platform/platform.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ def declare_config_settings():
native.platform(
name = "wasm",
constraint_values = [
"@io_bazel_rules_rust//rust/platform/cpu:wasm32",
"@io_bazel_rules_rust//rust/platform/os:unknown",
str(Label("//rust/platform/cpu:wasm32")),
str(Label("//rust/platform/os:unknown")),
],
)

native.platform(
name = "wasi",
constraint_values = [
"@io_bazel_rules_rust//rust/platform/cpu:wasm32",
"@io_bazel_rules_rust//rust/platform/os:wasi",
str(Label("//rust/platform/cpu:wasm32")),
str(Label("//rust/platform/os:wasi")),
],
)

Expand Down
10 changes: 8 additions & 2 deletions rust/platform/triple_mappings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,15 @@ def triple_to_constraint_set(triple):
list: A list of constraints (each represented by a list of strings)
"""
if triple == "wasm32-wasi":
return ["@io_bazel_rules_rust//rust/platform/cpu:wasm32", "@io_bazel_rules_rust//rust/platform/os:wasi"]
return [
"@io_bazel_rules_rust//rust/platform/cpu:wasm32",
"@io_bazel_rules_rust//rust/platform/os:wasi",
]
if triple == "wasm32-unknown-unknown":
return ["@io_bazel_rules_rust//rust/platform/cpu:wasm32", "@io_bazel_rules_rust//rust/platform/os:unknown"]
return [
"@io_bazel_rules_rust//rust/platform/cpu:wasm32",
"@io_bazel_rules_rust//rust/platform/os:unknown",
]

component_parts = triple.split("-")
if len(component_parts) < 3:
Expand Down
10 changes: 5 additions & 5 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

# buildifier: disable=module-docstring
load(
"@io_bazel_rules_rust//rust:private/rustc.bzl",
"//rust:private/rustc.bzl",
"CrateInfo",
"collect_deps",
"collect_inputs",
"construct_arguments",
"get_cc_toolchain",
)
load(
"@io_bazel_rules_rust//rust:private/rust.bzl",
"//rust:private/rust.bzl",
"crate_root_src",
)
load("@io_bazel_rules_rust//rust:private/utils.bzl", "determine_output_hash", "find_toolchain")
load("//rust:private/utils.bzl", "determine_output_hash", "find_toolchain")

_rust_extensions = [
"rs",
Expand Down Expand Up @@ -144,15 +144,15 @@ rust_clippy_aspect = aspect(
default = Label("@bazel_tools//tools/cpp:current_cc_toolchain"),
),
"_process_wrapper": attr.label(
default = "@io_bazel_rules_rust//util/process_wrapper",
default = Label("//util/process_wrapper"),
executable = True,
allow_single_file = True,
cfg = "exec",
),
"_error_format": attr.label(default = "//:error_format"),
},
toolchains = [
"@io_bazel_rules_rust//rust:toolchain",
str(Label("//rust:toolchain")),
"@bazel_tools//tools/cpp:toolchain_type",
],
implementation = _clippy_aspect_impl,
Expand Down
2 changes: 1 addition & 1 deletion rust/private/dummy_cc_toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dummy_cc_toolchain(name = "dummy_cc_wasm32")
# TODO([email protected]): Need to support linking C code to rust code when compiling for wasm32.
toolchain(
name = "dummy_cc_wasm32_toolchain",
target_compatible_with = ["@io_bazel_rules_rust//rust/platform/cpu:wasm32"],
target_compatible_with = ["//rust/platform/cpu:wasm32"],
toolchain = ":dummy_cc_wasm32",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)
Loading