From f889a1b532fdca5f5051691f023a6a9f37ce494f Mon Sep 17 00:00:00 2001 From: A Googler Date: Wed, 6 Mar 2024 08:11:25 -0800 Subject: [PATCH] Restructure rules_proto Design doc: https://docs.google.com/document/d/1L1JFgjpZ7SrBinb24DC_5nTIELeYDacikcme-YcA7xs/edit PiperOrigin-RevId: 613220055 --- proto/BUILD | 34 +++++++++++-- proto/defs.bzl | 51 ++++++++++++++------ proto/modules/BUILD | 11 ----- proto/modules/proto_info.bzl | 18 ------- proto/modules/proto_lang_toolchain_info.bzl | 18 ------- proto/private/rules/BUILD | 2 +- proto/private/rules/proto_toolchain_rule.bzl | 3 +- proto/{modules => }/proto_common.bzl | 3 +- proto/proto_descriptor_set.bzl | 18 ------- proto/proto_lang_toolchain.bzl | 33 +++++++++++-- proto/proto_library.bzl | 35 -------------- proto/proto_toolchain.bzl | 2 - proto/toolchains/BUILD | 23 --------- proto/toolchains/proto_lang_toolchain.bzl | 47 ------------------ proto/toolchains/proto_toolchain.bzl | 19 -------- tests/BUILD | 2 +- tests/proto_common/toolchains.bzl | 16 +----- 17 files changed, 103 insertions(+), 232 deletions(-) delete mode 100644 proto/modules/BUILD delete mode 100644 proto/modules/proto_info.bzl delete mode 100644 proto/modules/proto_lang_toolchain_info.bzl rename proto/{modules => }/proto_common.bzl (95%) delete mode 100644 proto/proto_descriptor_set.bzl delete mode 100644 proto/proto_library.bzl delete mode 100644 proto/toolchains/BUILD delete mode 100644 proto/toolchains/proto_lang_toolchain.bzl delete mode 100644 proto/toolchains/proto_toolchain.bzl diff --git a/proto/BUILD b/proto/BUILD index e858d94..bc863ea 100644 --- a/proto/BUILD +++ b/proto/BUILD @@ -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", ], ) @@ -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", diff --git a/proto/defs.bzl b/proto/defs.bzl index c330f81..69a1969 100644 --- a/proto/defs.bzl +++ b/proto/defs.bzl @@ -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 diff --git a/proto/modules/BUILD b/proto/modules/BUILD deleted file mode 100644 index ac2edb0..0000000 --- a/proto/modules/BUILD +++ /dev/null @@ -1,11 +0,0 @@ -load("@bazel_skylib//:bzl_library.bzl", "bzl_library") - -bzl_library( - name = "proto_common", - srcs = [ - "proto_common.bzl", - "proto_info.bzl", - "proto_lang_toolchain_info.bzl", - ], - visibility = ["//visibility:public"], -) diff --git a/proto/modules/proto_info.bzl b/proto/modules/proto_info.bzl deleted file mode 100644 index ead2be7..0000000 --- a/proto/modules/proto_info.bzl +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2023 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. -"""ProtoInfo""" - -load("//proto/private:native.bzl", "NativeProtoInfo") - -ProtoInfo = NativeProtoInfo diff --git a/proto/modules/proto_lang_toolchain_info.bzl b/proto/modules/proto_lang_toolchain_info.bzl deleted file mode 100644 index 20eac87..0000000 --- a/proto/modules/proto_lang_toolchain_info.bzl +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2024 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. -"""ProtoLangToolchainInfo""" - -load("//proto/private:native.bzl", "native_proto_common") - -ProtoLangToolchainInfo = native_proto_common.ProtoLangToolchainInfo diff --git a/proto/private/rules/BUILD b/proto/private/rules/BUILD index 61ec702..437109b 100644 --- a/proto/private/rules/BUILD +++ b/proto/private/rules/BUILD @@ -18,7 +18,7 @@ bzl_library( srcs = [ "proto_toolchain.bzl", "proto_toolchain_rule.bzl", - "@bazel_features//:bzl_files", + "@bazel_features//:features", ], visibility = [ "//proto:__subpackages__", diff --git a/proto/private/rules/proto_toolchain_rule.bzl b/proto/private/rules/proto_toolchain_rule.bzl index 7443085..70d55fc 100644 --- a/proto/private/rules/proto_toolchain_rule.bzl +++ b/proto/private/rules/proto_toolchain_rule.bzl @@ -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 = {} diff --git a/proto/modules/proto_common.bzl b/proto/proto_common.bzl similarity index 95% rename from proto/modules/proto_common.bzl rename to proto/proto_common.bzl index 826dea1..3e15665 100644 --- a/proto/modules/proto_common.bzl +++ b/proto/proto_common.bzl @@ -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) diff --git a/proto/proto_descriptor_set.bzl b/proto/proto_descriptor_set.bzl deleted file mode 100644 index c8358c6..0000000 --- a/proto/proto_descriptor_set.bzl +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright 2024 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. -"""proto_descriptor_set rule""" - -load("//proto/private/rules:proto_descriptor_set.bzl", _proto_descriptor_set = "proto_descriptor_set") - -proto_descriptor_set = _proto_descriptor_set diff --git a/proto/proto_lang_toolchain.bzl b/proto/proto_lang_toolchain.bzl index f390e97..a2cd479 100644 --- a/proto/proto_lang_toolchain.bzl +++ b/proto/proto_lang_toolchain.bzl @@ -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, + ) diff --git a/proto/proto_library.bzl b/proto/proto_library.bzl deleted file mode 100644 index f3d8eb0..0000000 --- a/proto/proto_library.bzl +++ /dev/null @@ -1,35 +0,0 @@ -# Copyright 2024 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. -"""proto_library rule""" - -_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)) diff --git a/proto/proto_toolchain.bzl b/proto/proto_toolchain.bzl index c61d8ec..e1a853c 100644 --- a/proto/proto_toolchain.bzl +++ b/proto/proto_toolchain.bzl @@ -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 diff --git a/proto/toolchains/BUILD b/proto/toolchains/BUILD deleted file mode 100644 index ec63c8a..0000000 --- a/proto/toolchains/BUILD +++ /dev/null @@ -1,23 +0,0 @@ -load("@bazel_skylib//:bzl_library.bzl", "bzl_library") - -bzl_library( - name = "proto_lang_toolchain", - srcs = [ - "proto_lang_toolchain.bzl", - ], - visibility = ["//visibility:public"], - deps = [ - "//proto/modules:proto_common", - ], -) - -bzl_library( - name = "proto_toolchain", - srcs = [ - "proto_toolchain.bzl", - ], - visibility = ["//visibility:public"], - deps = [ - "//proto/private/rules:proto_toolchain_bzl", - ], -) diff --git a/proto/toolchains/proto_lang_toolchain.bzl b/proto/toolchains/proto_lang_toolchain.bzl deleted file mode 100644 index 690c11a..0000000 --- a/proto/toolchains/proto_lang_toolchain.bzl +++ /dev/null @@ -1,47 +0,0 @@ -# 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. -"""proto_lang_toolchain rule""" - -load("//proto/modules:proto_common.bzl", "proto_common") - -def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs): - """Creates a proto_lang_toolchain and corresponding toolchain target. - - 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, - ) diff --git a/proto/toolchains/proto_toolchain.bzl b/proto/toolchains/proto_toolchain.bzl deleted file mode 100644 index e1a853c..0000000 --- a/proto/toolchains/proto_toolchain.bzl +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2023 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. - -"""Export for proto_toolchain""" - -load("//proto/private/rules:proto_toolchain.bzl", _proto_toolchain_macro = "proto_toolchain") - -proto_toolchain = _proto_toolchain_macro diff --git a/tests/BUILD b/tests/BUILD index 9ef193d..57bb308 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -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", diff --git a/tests/proto_common/toolchains.bzl b/tests/proto_common/toolchains.bzl index 769424d..1f01370 100644 --- a/tests/proto_common/toolchains.bzl +++ b/tests/proto_common/toolchains.bzl @@ -1,22 +1,8 @@ -# Copyright 2024 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. - "unit tests for proto_common.toolchains" -load("//proto/modules:proto_common.bzl", "toolchains") load("@bazel_skylib//lib:partial.bzl", "partial") load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest") +load("//proto:proto_common.bzl", "toolchains") def _test_toolchains_without_incompatible_flag(ctx): env = unittest.begin(ctx)