Skip to content

Commit

Permalink
Create proto_toolchain and expose values of command-line flags on it
Browse files Browse the repository at this point in the history
Working towards bazelbuild/bazel#10005
  • Loading branch information
Yannic committed Dec 18, 2019
1 parent 2c04683 commit fe54705
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
18 changes: 17 additions & 1 deletion proto/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Intentionally left empty (for now).
load("//proto:defs.bzl", "proto_toolchain")

toolchain_type(
name = "toolchain",
visibility = ["//visibility:public"],
)

proto_toolchain(
name = "default_toolchain_impl",
visibility = ["//visibility:public"],
)

toolchain(
name = "default_toolchain",
toolchain = ":default_toolchain_impl",
toolchain_type = "@rules_proto//proto:toolchain",
)
3 changes: 3 additions & 0 deletions proto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Starlark rules for building protocol buffers."""

load("//proto/private:native.bzl", "NativeProtoInfo", "native_proto_common")
load("//proto/private/rules:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")

_MIGRATION_TAG = "__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"

Expand Down Expand Up @@ -49,6 +50,8 @@ def proto_library(**attrs):
# buildifier: disable=native-proto
native.proto_library(**_add_migration_tag(attrs))

proto_toolchain = _proto_toolchain

# Encapsulates information provided by `proto_library`.
#
# https://docs.bazel.build/versions/master/skylark/lib/ProtoInfo.html
Expand Down
1 change: 1 addition & 0 deletions proto/private/rules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally left empty (for now).
63 changes: 63 additions & 0 deletions proto/private/rules/proto_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2019 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.

load("//proto/private:native.bzl", "native_proto_common")

_has_protoc_key = "has_protoc_do_not_use_or_we_will_break_you_without_mercy"
_protoc_key = "protoc_do_not_use_or_we_will_break_you_without_mercy"
_protocopt_key = "protocopt_do_not_use_or_we_will_break_you_without_mercy"
_strict_deps_key = "strict_deps_do_not_use_or_we_will_break_you_without_mercy"

def _proto_toolchain_impl(ctx):
config = ctx.fragments.proto
return [
platform_common.ToolchainInfo(
compiler = ctx.attr._compiler.files_to_run,
compiler_options = getattr(config, _protocopt_key, []),
strict_deps = getattr(config, _strict_deps_key, "ERROR"),
),
]

def _compiler_default_value():
"""Returns the value of `--proto_compiler`, or it's default value in case
Bazel does not support accessing the actual value.
Returns: The value of `--proto_compiler`.
"""

if not hasattr(native_proto_common, _has_protoc_key):
# Use a sane default in case the value of `--proto_compiler` is not
# available.
return "@com_google_protobuf//:protoc"

return configuration_field("proto", _protoc_key)

proto_toolchain = rule(
implementation = _proto_toolchain_impl,
attrs = {
# Currently configured by `--proto_compiler`.
"_compiler": attr.label(
mandatory = False,
executable = True,
cfg = "host",
default = _compiler_default_value(),
),
},
fragments = [
"proto",
],
provides = [
platform_common.ToolchainInfo,
],
)
5 changes: 3 additions & 2 deletions proto/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ def rules_proto_dependencies():
maybe(http_archive, name, **dependencies[name])

def rules_proto_toolchains():
# Nothing to do here (yet).
pass
native.register_toolchains(
"@rules_proto//proto:default_toolchain",
)

0 comments on commit fe54705

Please sign in to comment.