Skip to content

Commit

Permalink
Add //rust/private/BUILD (therefore create a package there) (bazelbui…
Browse files Browse the repository at this point in the history
…ld#569)

This PR achieves the following:

* buildifier bzl-visibility check was not detecting violations as it assumes private package, not just a directory
* adds //rust:common.bzl file where all the publicly available and supported API for writing custom rules interacting with Rust will reside
* the private package now sets the stability expectations, and it forces us to think about API layering and abstractions.

This turned out to be much bigger PR than expected :(
  • Loading branch information
hlopko authored Feb 3, 2021
1 parent 932a389 commit acfce01
Show file tree
Hide file tree
Showing 25 changed files with 126 additions and 73 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@

# npm
**/node_modules/

# vscode
.vscode
*.code-workspace
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("//rust:private/rustc.bzl", "error_format")
load("//rust/private:rustc.bzl", "error_format")

bzl_library(
name = "rules",
Expand Down
4 changes: 3 additions & 1 deletion bindgen/bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
# limitations under the License.

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

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "find_toolchain", "get_libs_for_static_executable")

def rust_bindgen_library(
name,
header,
Expand Down
12 changes: 9 additions & 3 deletions cargo/cargo_build_script.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# 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("//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")

# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "name_to_crate_name")

# buildifier: disable=bzl-visibility
load("//rust/private:rustc.bzl", "BuildInfo", "DepInfo", "get_cc_toolchain", "get_compilation_mode_opts", "get_linker_and_args")

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "expand_locations", "find_toolchain")

def get_cc_compile_env(cc_toolchain, feature_configuration):
"""Gather cc environment variables from the given `cc_toolchain`
Expand Down
8 changes: 4 additions & 4 deletions examples/cargo/BUILD
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
load(
"@rules_rust//cargo:cargo_build_script.bzl",
"cargo_build_script",
)
load(
"@rules_rust//rust:rust.bzl",
"rust_library",
"rust_test",
)
load(
"@rules_rust//cargo:cargo_build_script.bzl",
"cargo_build_script",
)

cargo_build_script(
name = "build_script",
Expand Down
10 changes: 5 additions & 5 deletions examples/env_locations/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")
load(
"@rules_rust//rust:rust.bzl",
"rust_test",
)
load("@rules_rust//cargo:cargo_build_script.bzl", "cargo_build_script")

# generate a file
genrule(
Expand All @@ -25,10 +25,10 @@ cargo_build_script(
name = "build",
srcs = ["build.rs"],
build_script_env = {
# both execpath and location should work
"SOURCE_FILE": "$(execpath source.file)",
"GENERATED_DATA": "$(location generated.data)",
"SOME_TOOL": "$(execpath @com_google_protobuf//:protoc)",
# both execpath and location should work
"SOURCE_FILE": "$(execpath source.file)",
},
data = _data,
)
Expand All @@ -41,10 +41,10 @@ rust_test(
data = _data,
edition = "2018",
rustc_env = {
"SOURCE_FILE": "$(rootpath source.file)",
"GENERATED_DATA_ROOT": "$(rootpath generated.data)",
"GENERATED_DATA_ABS": "$(execpath generated.data)",
"GENERATED_DATA_ROOT": "$(rootpath generated.data)",
"SOME_TOOL": "$(rootpath @com_google_protobuf//:protoc)",
"SOURCE_FILE": "$(rootpath source.file)",
},
deps = [
":build",
Expand Down
2 changes: 1 addition & 1 deletion examples/ffi/rust_calling_c/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ cc_test(

cc_import(
name = "native_matrix_so",
shared_library = ":libnative_matrix_so.so",
hdrs = ["matrix.h"],
shared_library = ":libnative_matrix_so.so",
)

cc_binary(
Expand Down
2 changes: 1 addition & 1 deletion examples/ffi/rust_calling_c/simple/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_library")
load("@rules_rust//rust:rust.bzl", "rust_binary", "rust_test")
load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "simple",
Expand Down
2 changes: 1 addition & 1 deletion examples/proto/helloworld/greeter_client/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_rust//rust:rust.bzl", "rust_binary")
load("@rules_rust//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")
load("@rules_rust//rust:rust.bzl", "rust_binary")

rust_binary(
name = "greeter_client",
Expand Down
2 changes: 1 addition & 1 deletion examples/proto/helloworld/greeter_server/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_rust//rust:rust.bzl", "rust_binary")
load("@rules_rust//proto:toolchain.bzl", "GRPC_COMPILE_DEPS")
load("@rules_rust//rust:rust.bzl", "rust_binary")

rust_binary(
name = "greeter_server",
Expand Down
9 changes: 7 additions & 2 deletions proto/proto.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ load(
_generate_proto = "rust_generate_proto",
_generated_file_stem = "generated_file_stem",
)
load("//rust:private/rustc.bzl", "CrateInfo", "rustc_compile_action")
load("//rust:private/utils.bzl", "determine_output_hash", "find_toolchain")
load("//rust:common.bzl", "CrateInfo")

# buildifier: disable=bzl-visibility
load("//rust/private:rustc.bzl", "rustc_compile_action")

# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "determine_output_hash", "find_toolchain")

RustProtoInfo = provider(
doc = "Rust protobuf provider info",
Expand Down
3 changes: 2 additions & 1 deletion proto/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

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

load("//rust:private/rust.bzl", "name_to_crate_name")
# buildifier: disable=bzl-visibility
load("//rust/private:rust.bzl", "name_to_crate_name")

def generated_file_stem(f):
basename = f.rsplit("/", 2)[-1]
Expand Down
1 change: 1 addition & 0 deletions rust/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ bzl_library(
srcs = glob(["**/*.bzl"]),
deps = [
"//rust/platform:rules",
"//rust/private:rules",
],
)
32 changes: 32 additions & 0 deletions rust/common.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2015 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module with Rust definitions required to write custom Rust rules."""

CrateInfo = provider(
doc = "A provider containing general Crate information.",
fields = {
"aliases": "Dict[Label, String]: Renamed and aliased crates",
"deps": "List[Provider]: This crate's (rust or cc) dependencies' providers.",
"edition": "str: The edition of this crate.",
"is_test": "bool: If the crate is being compiled in a test context",
"name": "str: The name of this crate.",
"output": "File: The output File that will be produced, depends on crate type.",
"proc_macro_deps": "List[CrateInfo]: This crate's rust proc_macro dependencies' providers.",
"root": "File: The source File entrypoint to this crate, eg. lib.rs",
"rustc_env": "Dict[String, String]: Additional `\"key\": \"value\"` environment variables to set for rustc.",
"srcs": "List[File]: All source Files that are part of the crate.",
"type": "str: The type of this crate. eg. lib or bin",
},
)
4 changes: 2 additions & 2 deletions rust/platform/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load(":platform.bzl", "declare_config_settings")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":platform.bzl", "declare_config_settings")

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

Expand All @@ -15,5 +15,5 @@ package_group(
bzl_library(
name = "rules",
srcs = glob(["**/*.bzl"]),
visibility = ["//rust:__pkg__"],
visibility = ["//rust:__subpackages__"],
)
12 changes: 12 additions & 0 deletions rust/private/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

bzl_library(
name = "rules",
srcs = glob(
["**/*.bzl"],
),
visibility = ["//rust:__subpackages__"],
deps = [
"//rust/platform:rules",
],
)
8 changes: 4 additions & 4 deletions rust/private/clippy.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
# limitations under the License.

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

_rust_extensions = [
"rs",
Expand Down
17 changes: 9 additions & 8 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
# limitations under the License.

# buildifier: disable=module-docstring
load("//rust:private/rustc.bzl", "CrateInfo", "rustc_compile_action")
load("//rust:private/utils.bzl", "determine_output_hash", "find_toolchain")
load("//rust:common.bzl", "CrateInfo")
load("//rust/private:rustc.bzl", "rustc_compile_action")
load("//rust/private:utils.bzl", "determine_output_hash", "find_toolchain")

# TODO(marco): Separate each rule into its own file.

Expand Down Expand Up @@ -355,7 +356,7 @@ def _tidy(doc_string):
"""
return "\n".join([line.strip() for line in doc_string.splitlines()])

_rust_common_attrs = {
_common_attrs = {
"aliases": attr.label_keyed_string_dict(
doc = _tidy("""
Remap crates to a new name or moniker for linkage to this target
Expand Down Expand Up @@ -501,7 +502,7 @@ _rust_test_attrs = {

rust_library = rule(
implementation = _rust_library_impl,
attrs = dict(_rust_common_attrs.items() +
attrs = dict(_common_attrs.items() +
_rust_library_attrs.items()),
fragments = ["cpp"],
host_fragments = ["cpp"],
Expand Down Expand Up @@ -591,7 +592,7 @@ _rust_binary_attrs = {

rust_binary = rule(
implementation = _rust_binary_impl,
attrs = dict(_rust_common_attrs.items() + _rust_binary_attrs.items()),
attrs = dict(_common_attrs.items() + _rust_binary_attrs.items()),
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
Expand Down Expand Up @@ -687,7 +688,7 @@ Hello world

rust_test = rule(
implementation = _rust_test_impl,
attrs = dict(_rust_common_attrs.items() +
attrs = dict(_common_attrs.items() +
_rust_test_attrs.items()),
executable = True,
fragments = ["cpp"],
Expand Down Expand Up @@ -836,7 +837,7 @@ Run the test with `bazel build //hello_lib:hello_lib_test`.

rust_test_binary = rule(
implementation = _rust_test_impl,
attrs = dict(_rust_common_attrs.items() +
attrs = dict(_common_attrs.items() +
_rust_test_attrs.items()),
executable = True,
fragments = ["cpp"],
Expand All @@ -860,7 +861,7 @@ See `rust_test` for example usage.

rust_benchmark = rule(
implementation = _rust_benchmark_impl,
attrs = _rust_common_attrs,
attrs = _common_attrs,
executable = True,
fragments = ["cpp"],
host_fragments = ["cpp"],
Expand Down
20 changes: 2 additions & 18 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,16 @@ load(
"CPP_LINK_EXECUTABLE_ACTION_NAME",
)
load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
load("//rust:common.bzl", "CrateInfo")
load(
"//rust:private/utils.bzl",
"//rust/private:utils.bzl",
"expand_locations",
"get_lib_name",
"get_libs_for_static_executable",
"relativize",
"rule_attrs",
)

CrateInfo = provider(
doc = "A provider containing general Crate information.",
fields = {
"aliases": "Dict[Label, String]: Renamed and aliased crates",
"deps": "List[Provider]: This crate's (rust or cc) dependencies' providers.",
"edition": "str: The edition of this crate.",
"is_test": "bool: If the crate is being compiled in a test context",
"name": "str: The name of this crate.",
"output": "File: The output File that will be produced, depends on crate type.",
"proc_macro_deps": "List[CrateInfo]: This crate's rust proc_macro dependencies' providers.",
"root": "File: The source File entrypoint to this crate, eg. lib.rs",
"rustc_env": "Dict[String, String]: Additional `\"key\": \"value\"` environment variables to set for rustc.",
"srcs": "List[File]: All source Files that are part of the crate.",
"type": "str: The type of this crate. eg. lib or bin",
},
)

BuildInfo = provider(
doc = "A provider containing `rustc` build settings for a given Crate.",
fields = {
Expand Down
5 changes: 3 additions & 2 deletions rust/private/rustdoc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
# limitations under the License.

# buildifier: disable=module-docstring
load("//rust:private/rustc.bzl", "CrateInfo", "DepInfo", "add_crate_link_flags", "add_edition_flags")
load("//rust:private/utils.bzl", "find_toolchain")
load("//rust:common.bzl", "CrateInfo")
load("//rust/private:rustc.bzl", "DepInfo", "add_crate_link_flags", "add_edition_flags")
load("//rust/private:utils.bzl", "find_toolchain")

_rust_doc_doc = """Generates code documentation.
Expand Down
5 changes: 3 additions & 2 deletions rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
# limitations under the License.

# buildifier: disable=module-docstring
load("//rust:private/rustc.bzl", "CrateInfo", "DepInfo")
load("//rust:private/utils.bzl", "find_toolchain", "get_lib_name")
load("//rust:common.bzl", "CrateInfo")
load("//rust/private:rustc.bzl", "DepInfo")
load("//rust/private:utils.bzl", "find_toolchain", "get_lib_name")

def _rust_doc_test_impl(ctx):
"""The implementation for the `rust_doc_test` rule
Expand Down
Loading

0 comments on commit acfce01

Please sign in to comment.