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 Feb 14, 2024
1 parent dec2b5c commit d4c3498
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 103 deletions.
34 changes: 4 additions & 30 deletions proto/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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 @@ -25,34 +27,6 @@ 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: 14 additions & 37 deletions proto/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,21 @@

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

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))

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
proto_descriptor_set = _proto_descriptor_set

proto_lang_toolchain = _proto_lang_toolchain

# Toolchain rules
proto_toolchain = _proto_toolchain
proto_lang_toolchain = _proto_lang_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
# Modules
proto_common = _proto_common
ProtoInfo = _ProtoInfo
11 changes: 11 additions & 0 deletions proto/modules/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"],
)
3 changes: 1 addition & 2 deletions proto/proto_common.bzl → proto/modules/proto_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
# 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: 18 additions & 0 deletions proto/modules/proto_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
18 changes: 18 additions & 0 deletions proto/modules/proto_lang_toolchain_info.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
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//:features",
"@bazel_features//:bzl_files",
],
visibility = [
"//proto:__subpackages__",
Expand Down
3 changes: 2 additions & 1 deletion proto/private/rules/proto_toolchain_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"""A Starlark implementation of the proto_toolchain rule."""

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

def _impl(ctx):
kwargs = {}
Expand Down
18 changes: 18 additions & 0 deletions proto/proto_descriptor_set.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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
33 changes: 3 additions & 30 deletions proto/proto_lang_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,8 @@
# limitations under the License.
"""proto_lang_toolchain rule"""

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

def proto_lang_toolchain(*, name, toolchain_type = None, exec_compatible_with = [], target_compatible_with = [], **attrs):
"""Creates a proto_lang_toolchain and corresponding toolchain target.
# deprecated: load proto_lang_toolchain from toolchain package

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,
)
proto_lang_toolchain = _proto_lang_toolchain
35 changes: 35 additions & 0 deletions proto/proto_library.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# 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))
2 changes: 2 additions & 0 deletions proto/proto_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@

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: 23 additions & 0 deletions proto/toolchains/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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",
],
)
47 changes: 47 additions & 0 deletions proto/toolchains/proto_lang_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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,
)
19 changes: 19 additions & 0 deletions proto/toolchains/proto_toolchain.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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
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:proto_common.bzl", "proto_common")
load("//proto/modules:proto_common.bzl", "proto_common")

config_setting(
name = "incompatible_enable_proto_toolchain_resolution",
Expand Down
Loading

2 comments on commit d4c3498

@alexeagle
Copy link
Collaborator

@alexeagle alexeagle commented on d4c3498 Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change, and needs some partial reverts in order to let users continue to use rules_proto.

For example, no version of rules_python can work before and after this commit because there's no stable load location for the proto_common#toolchains helper: https://github.com/bazelbuild/rules_python/pull/1577/files#diff-9cb07a0e8453ad8b4f429bee2ccf9de818a78cfb6e5d9bb908d8518ea189e2a7R18

Asking developers to sort out the version skew themselves in a low-level library like rules_proto is putting far too much burden on them. Bazel Slack is going to be filled with developers tripping over "what version of rules_somelang can I use" due to this.

Alternatively, if proto_common.bzl is a private API, then we need to expose this toolchains helper in some other public API, or tell @thesayyn that it may not be used in a language repo to implement the toolchainization?

@alexeagle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing no one is paying attention on this repo, so the answer is not to use that toolchains helper and treat it as private API.

Please sign in to comment.