Skip to content

Commit

Permalink
Restructure rules_proto
Browse files Browse the repository at this point in the history
  • Loading branch information
A Googler authored and Blaze Rules Copybara committed Mar 6, 2024
1 parent d4c3498 commit f889a1b
Show file tree
Hide file tree
Showing 17 changed files with 103 additions and 232 deletions.
34 changes: 30 additions & 4 deletions proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ bzl_library(
name = "defs",
srcs = [
"defs.bzl",
"proto_descriptor_set.bzl",
"proto_library.bzl",
],
visibility = ["//visibility:public"],
deps = [
":proto_lang_toolchain",
":proto_toolchain",
"//proto/private:native",
"//proto/private/rules:proto_descriptor_set",
"//proto/toolchains:proto_lang_toolchain",
"//proto/toolchains:proto_toolchain",
],
)

Expand All @@ -27,6 +25,34 @@ bzl_library(
visibility = ["//visibility:public"],
)

bzl_library(
name = "proto_lang_toolchain",
srcs = [
"proto_lang_toolchain.bzl",
],
deps = [
":proto_common",
],
)

bzl_library(
name = "proto_common",
srcs = [
"proto_common.bzl",
],
)

bzl_library(
name = "proto_toolchain",
srcs = [
"proto_toolchain.bzl",
],
visibility = ["//visibility:public"],
deps = [
"//proto/private/rules:proto_toolchain_bzl",
],
)

# Toolchain type provided by proto_toolchain rule and used by proto_library
toolchain_type(
name = "toolchain_type",
Expand Down
51 changes: 37 additions & 14 deletions proto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,44 @@

"""Starlark rules for building protocol buffers."""

load("//proto:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set")
load("//proto:proto_library.bzl", _proto_library = "proto_library")
load("//proto/modules:proto_common.bzl", _proto_common = "proto_common")
load("//proto/modules:proto_info.bzl", _ProtoInfo = "ProtoInfo")
load("//proto/toolchains:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
load("//proto/toolchains:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")

# Rules
proto_library = _proto_library
load("//proto:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
load("//proto:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")
load("//proto/private:native.bzl", "NativeProtoInfo", "native_proto_common")
load("//proto/private/rules:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set")

_MIGRATION_TAG = "__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"

def _add_migration_tag(attrs):
if "tags" in attrs and attrs["tags"] != None:
attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
else:
attrs["tags"] = [_MIGRATION_TAG]
return attrs

def proto_library(**attrs):
"""Bazel proto_library rule.
https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_library
Args:
**attrs: Rule attributes
"""

# buildifier: disable=native-proto
native.proto_library(**_add_migration_tag(attrs))

proto_descriptor_set = _proto_descriptor_set

# Toolchain rules
proto_toolchain = _proto_toolchain
proto_lang_toolchain = _proto_lang_toolchain

# Modules
proto_common = _proto_common
ProtoInfo = _ProtoInfo
proto_toolchain = _proto_toolchain

# Encapsulates information provided by `proto_library`.
#
# https://docs.bazel.build/versions/master/skylark/lib/ProtoInfo.html
ProtoInfo = NativeProtoInfo

# Utilities for protocol buffers.
#
# https://docs.bazel.build/versions/master/skylark/lib/proto_common.html
proto_common = native_proto_common
11 changes: 0 additions & 11 deletions proto/modules/BUILD

This file was deleted.

18 changes: 0 additions & 18 deletions proto/modules/proto_info.bzl

This file was deleted.

18 changes: 0 additions & 18 deletions proto/modules/proto_lang_toolchain_info.bzl

This file was deleted.

2 changes: 1 addition & 1 deletion proto/private/rules/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ bzl_library(
srcs = [
"proto_toolchain.bzl",
"proto_toolchain_rule.bzl",
"@bazel_features//:bzl_files",
"@bazel_features//:features",
],
visibility = [
"//proto:__subpackages__",
Expand Down
3 changes: 1 addition & 2 deletions proto/private/rules/proto_toolchain_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"""A Starlark implementation of the proto_toolchain rule."""

load("@bazel_features//:features.bzl", "bazel_features")
load("//proto/modules:proto_common.bzl", "proto_common")
load("//proto/modules:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
load("//proto:proto_common.bzl", "ProtoLangToolchainInfo", "proto_common")

def _impl(ctx):
kwargs = {}
Expand Down
3 changes: 2 additions & 1 deletion proto/modules/proto_common.bzl → proto/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
# https://docs.bazel.build/versions/master/skylark/lib/proto_common.html
"""proto_common module"""

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

proto_common = native_proto_common

ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo

def _incompatible_toolchains_enabled():
return getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)

Expand Down
18 changes: 0 additions & 18 deletions proto/proto_descriptor_set.bzl

This file was deleted.

33 changes: 30 additions & 3 deletions proto/proto_lang_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,35 @@
# limitations under the License.
"""proto_lang_toolchain rule"""

load("//proto/toolchains:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
load("//proto:proto_common.bzl", "proto_common")

# deprecated: load proto_lang_toolchain from toolchain package
def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs):
"""Creates a proto_lang_toolchain and corresponding toolchain target.
proto_lang_toolchain = _proto_lang_toolchain
Toolchain target is only created when toolchain_type is set.
https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain
Args:
name: name of the toolchain
toolchain_type: The toolchain type
exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with.
target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with.
**attrs: Rule attributes
"""

if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
attrs["toolchain_type"] = toolchain_type

# buildifier: disable=native-proto
native.proto_lang_toolchain(name = name, **attrs)

if toolchain_type:
native.toolchain(
name = name + "_toolchain",
toolchain_type = toolchain_type,
exec_compatible_with = exec_compatible_with,
target_compatible_with = target_compatible_with,
toolchain = name,
)
35 changes: 0 additions & 35 deletions proto/proto_library.bzl

This file was deleted.

2 changes: 0 additions & 2 deletions proto/proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@

load("//proto/private/rules:proto_toolchain.bzl", _proto_toolchain_macro = "proto_toolchain")

# deprecated: load proto_toolchain from toolchain package

proto_toolchain = _proto_toolchain_macro
23 changes: 0 additions & 23 deletions proto/toolchains/BUILD

This file was deleted.

47 changes: 0 additions & 47 deletions proto/toolchains/proto_lang_toolchain.bzl

This file was deleted.

19 changes: 0 additions & 19 deletions proto/toolchains/proto_toolchain.bzl

This file was deleted.

2 changes: 1 addition & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//proto:defs.bzl", "proto_library")
load("//proto/modules:proto_common.bzl", "proto_common")
load("//proto:proto_common.bzl", "proto_common")

config_setting(
name = "incompatible_enable_proto_toolchain_resolution",
Expand Down
Loading

0 comments on commit f889a1b

Please sign in to comment.