Skip to content

Commit f889a1b

Browse files
A GooglerBlaze Rules Copybara
A Googler
authored and
Blaze Rules Copybara
committed
Restructure rules_proto
Design doc: https://docs.google.com/document/d/1L1JFgjpZ7SrBinb24DC_5nTIELeYDacikcme-YcA7xs/edit PiperOrigin-RevId: 613220055
1 parent d4c3498 commit f889a1b

17 files changed

+103
-232
lines changed

proto/BUILD

+30-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ bzl_library(
99
name = "defs",
1010
srcs = [
1111
"defs.bzl",
12-
"proto_descriptor_set.bzl",
13-
"proto_library.bzl",
1412
],
1513
visibility = ["//visibility:public"],
1614
deps = [
15+
":proto_lang_toolchain",
16+
":proto_toolchain",
1717
"//proto/private:native",
1818
"//proto/private/rules:proto_descriptor_set",
19-
"//proto/toolchains:proto_lang_toolchain",
20-
"//proto/toolchains:proto_toolchain",
2119
],
2220
)
2321

@@ -27,6 +25,34 @@ bzl_library(
2725
visibility = ["//visibility:public"],
2826
)
2927

28+
bzl_library(
29+
name = "proto_lang_toolchain",
30+
srcs = [
31+
"proto_lang_toolchain.bzl",
32+
],
33+
deps = [
34+
":proto_common",
35+
],
36+
)
37+
38+
bzl_library(
39+
name = "proto_common",
40+
srcs = [
41+
"proto_common.bzl",
42+
],
43+
)
44+
45+
bzl_library(
46+
name = "proto_toolchain",
47+
srcs = [
48+
"proto_toolchain.bzl",
49+
],
50+
visibility = ["//visibility:public"],
51+
deps = [
52+
"//proto/private/rules:proto_toolchain_bzl",
53+
],
54+
)
55+
3056
# Toolchain type provided by proto_toolchain rule and used by proto_library
3157
toolchain_type(
3258
name = "toolchain_type",

proto/defs.bzl

+37-14
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,44 @@
1414

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

17-
load("//proto:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set")
18-
load("//proto:proto_library.bzl", _proto_library = "proto_library")
19-
load("//proto/modules:proto_common.bzl", _proto_common = "proto_common")
20-
load("//proto/modules:proto_info.bzl", _ProtoInfo = "ProtoInfo")
21-
load("//proto/toolchains:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
22-
load("//proto/toolchains:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")
23-
24-
# Rules
25-
proto_library = _proto_library
17+
load("//proto:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
18+
load("//proto:proto_toolchain.bzl", _proto_toolchain = "proto_toolchain")
19+
load("//proto/private:native.bzl", "NativeProtoInfo", "native_proto_common")
20+
load("//proto/private/rules:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set")
21+
22+
_MIGRATION_TAG = "__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
23+
24+
def _add_migration_tag(attrs):
25+
if "tags" in attrs and attrs["tags"] != None:
26+
attrs["tags"] = attrs["tags"] + [_MIGRATION_TAG]
27+
else:
28+
attrs["tags"] = [_MIGRATION_TAG]
29+
return attrs
30+
31+
def proto_library(**attrs):
32+
"""Bazel proto_library rule.
33+
34+
https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_library
35+
36+
Args:
37+
**attrs: Rule attributes
38+
"""
39+
40+
# buildifier: disable=native-proto
41+
native.proto_library(**_add_migration_tag(attrs))
42+
2643
proto_descriptor_set = _proto_descriptor_set
2744

28-
# Toolchain rules
29-
proto_toolchain = _proto_toolchain
3045
proto_lang_toolchain = _proto_lang_toolchain
3146

32-
# Modules
33-
proto_common = _proto_common
34-
ProtoInfo = _ProtoInfo
47+
proto_toolchain = _proto_toolchain
48+
49+
# Encapsulates information provided by `proto_library`.
50+
#
51+
# https://docs.bazel.build/versions/master/skylark/lib/ProtoInfo.html
52+
ProtoInfo = NativeProtoInfo
53+
54+
# Utilities for protocol buffers.
55+
#
56+
# https://docs.bazel.build/versions/master/skylark/lib/proto_common.html
57+
proto_common = native_proto_common

proto/modules/BUILD

-11
This file was deleted.

proto/modules/proto_info.bzl

-18
This file was deleted.

proto/modules/proto_lang_toolchain_info.bzl

-18
This file was deleted.

proto/private/rules/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ bzl_library(
1818
srcs = [
1919
"proto_toolchain.bzl",
2020
"proto_toolchain_rule.bzl",
21-
"@bazel_features//:bzl_files",
21+
"@bazel_features//:features",
2222
],
2323
visibility = [
2424
"//proto:__subpackages__",

proto/private/rules/proto_toolchain_rule.bzl

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"""A Starlark implementation of the proto_toolchain rule."""
1616

1717
load("@bazel_features//:features.bzl", "bazel_features")
18-
load("//proto/modules:proto_common.bzl", "proto_common")
19-
load("//proto/modules:proto_lang_toolchain_info.bzl", "ProtoLangToolchainInfo")
18+
load("//proto:proto_common.bzl", "ProtoLangToolchainInfo", "proto_common")
2019

2120
def _impl(ctx):
2221
kwargs = {}

proto/modules/proto_common.bzl proto/proto_common.bzl

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
# https://docs.bazel.build/versions/master/skylark/lib/proto_common.html
1818
"""proto_common module"""
1919

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

2322
proto_common = native_proto_common
2423

24+
ProtoLangToolchainInfo = proto_common.ProtoLangToolchainInfo
25+
2526
def _incompatible_toolchains_enabled():
2627
return getattr(proto_common, "INCOMPATIBLE_ENABLE_PROTO_TOOLCHAIN_RESOLUTION", False)
2728

proto/proto_descriptor_set.bzl

-18
This file was deleted.

proto/proto_lang_toolchain.bzl

+30-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,35 @@
1313
# limitations under the License.
1414
"""proto_lang_toolchain rule"""
1515

16-
load("//proto/toolchains:proto_lang_toolchain.bzl", _proto_lang_toolchain = "proto_lang_toolchain")
16+
load("//proto:proto_common.bzl", "proto_common")
1717

18-
# deprecated: load proto_lang_toolchain from toolchain package
18+
def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs):
19+
"""Creates a proto_lang_toolchain and corresponding toolchain target.
1920
20-
proto_lang_toolchain = _proto_lang_toolchain
21+
Toolchain target is only created when toolchain_type is set.
22+
23+
https://docs.bazel.build/versions/master/be/protocol-buffer.html#proto_lang_toolchain
24+
25+
Args:
26+
27+
name: name of the toolchain
28+
toolchain_type: The toolchain type
29+
exec_compatible_with: ([constraints]) List of constraints the prebuild binaries is compatible with.
30+
target_compatible_with: ([constraints]) List of constraints the target libraries are compatible with.
31+
**attrs: Rule attributes
32+
"""
33+
34+
if getattr(proto_common, "INCOMPATIBLE_PASS_TOOLCHAIN_TYPE", False):
35+
attrs["toolchain_type"] = toolchain_type
36+
37+
# buildifier: disable=native-proto
38+
native.proto_lang_toolchain(name = name, **attrs)
39+
40+
if toolchain_type:
41+
native.toolchain(
42+
name = name + "_toolchain",
43+
toolchain_type = toolchain_type,
44+
exec_compatible_with = exec_compatible_with,
45+
target_compatible_with = target_compatible_with,
46+
toolchain = name,
47+
)

proto/proto_library.bzl

-35
This file was deleted.

proto/proto_toolchain.bzl

-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@
1616

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

19-
# deprecated: load proto_toolchain from toolchain package
20-
2119
proto_toolchain = _proto_toolchain_macro

proto/toolchains/BUILD

-23
This file was deleted.

proto/toolchains/proto_lang_toolchain.bzl

-47
This file was deleted.

proto/toolchains/proto_toolchain.bzl

-19
This file was deleted.

tests/BUILD

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
22
load("//proto:defs.bzl", "proto_library")
3-
load("//proto/modules:proto_common.bzl", "proto_common")
3+
load("//proto:proto_common.bzl", "proto_common")
44

55
config_setting(
66
name = "incompatible_enable_proto_toolchain_resolution",

0 commit comments

Comments
 (0)