Restructure rules_proto

Design doc: https://docs.google.com/document/d/1L1JFgjpZ7SrBinb24DC_5nTIELeYDacikcme-YcA7xs/edit

PiperOrigin-RevId: 606958336
diff --git a/proto/BUILD b/proto/BUILD
index bc863ea..e858d94 100644
--- a/proto/BUILD
+++ b/proto/BUILD
@@ -9,13 +9,15 @@
     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",
     ],
 )
 
@@ -25,34 +27,6 @@
     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 69a1969..c330f81 100644
--- a/proto/defs.bzl
+++ b/proto/defs.bzl
@@ -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")
+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")
 
-_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))
-
+# Rules
+proto_library = _proto_library
 proto_descriptor_set = _proto_descriptor_set
 
+# Toolchain rules
+proto_toolchain = _proto_toolchain
 proto_lang_toolchain = _proto_lang_toolchain
 
-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
+# Modules
+proto_common = _proto_common
+ProtoInfo = _ProtoInfo
diff --git a/proto/modules/BUILD b/proto/modules/BUILD
new file mode 100644
index 0000000..ac2edb0
--- /dev/null
+++ b/proto/modules/BUILD
@@ -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"],
+)
diff --git a/proto/proto_common.bzl b/proto/modules/proto_common.bzl
similarity index 95%
rename from proto/proto_common.bzl
rename to proto/modules/proto_common.bzl
index 3e15665..826dea1 100644
--- a/proto/proto_common.bzl
+++ b/proto/modules/proto_common.bzl
@@ -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)
 
diff --git a/proto/modules/proto_info.bzl b/proto/modules/proto_info.bzl
new file mode 100644
index 0000000..ead2be7
--- /dev/null
+++ b/proto/modules/proto_info.bzl
@@ -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
diff --git a/proto/modules/proto_lang_toolchain_info.bzl b/proto/modules/proto_lang_toolchain_info.bzl
new file mode 100644
index 0000000..20eac87
--- /dev/null
+++ b/proto/modules/proto_lang_toolchain_info.bzl
@@ -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
diff --git a/proto/private/rules/BUILD b/proto/private/rules/BUILD
index 437109b..61ec702 100644
--- a/proto/private/rules/BUILD
+++ b/proto/private/rules/BUILD
@@ -18,7 +18,7 @@
     srcs = [
         "proto_toolchain.bzl",
         "proto_toolchain_rule.bzl",
-        "@bazel_features//:features",
+        "@bazel_features//:bzl_files",
     ],
     visibility = [
         "//proto:__subpackages__",
diff --git a/proto/private/rules/proto_toolchain_rule.bzl b/proto/private/rules/proto_toolchain_rule.bzl
index 70d55fc..7443085 100644
--- a/proto/private/rules/proto_toolchain_rule.bzl
+++ b/proto/private/rules/proto_toolchain_rule.bzl
@@ -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 = {}
diff --git a/proto/proto_descriptor_set.bzl b/proto/proto_descriptor_set.bzl
new file mode 100644
index 0000000..c8358c6
--- /dev/null
+++ b/proto/proto_descriptor_set.bzl
@@ -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
diff --git a/proto/proto_lang_toolchain.bzl b/proto/proto_lang_toolchain.bzl
index a2cd479..f390e97 100644
--- a/proto/proto_lang_toolchain.bzl
+++ b/proto/proto_lang_toolchain.bzl
@@ -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
diff --git a/proto/proto_library.bzl b/proto/proto_library.bzl
new file mode 100644
index 0000000..f3d8eb0
--- /dev/null
+++ b/proto/proto_library.bzl
@@ -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))
diff --git a/proto/proto_toolchain.bzl b/proto/proto_toolchain.bzl
index e1a853c..c61d8ec 100644
--- a/proto/proto_toolchain.bzl
+++ b/proto/proto_toolchain.bzl
@@ -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
diff --git a/proto/toolchains/BUILD b/proto/toolchains/BUILD
new file mode 100644
index 0000000..ec63c8a
--- /dev/null
+++ b/proto/toolchains/BUILD
@@ -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",
+    ],
+)
diff --git a/proto/toolchains/proto_lang_toolchain.bzl b/proto/toolchains/proto_lang_toolchain.bzl
new file mode 100644
index 0000000..690c11a
--- /dev/null
+++ b/proto/toolchains/proto_lang_toolchain.bzl
@@ -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,
+        )
diff --git a/proto/toolchains/proto_toolchain.bzl b/proto/toolchains/proto_toolchain.bzl
new file mode 100644
index 0000000..e1a853c
--- /dev/null
+++ b/proto/toolchains/proto_toolchain.bzl
@@ -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
diff --git a/tests/BUILD b/tests/BUILD
index 57bb308..9ef193d 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:proto_common.bzl", "proto_common")
+load("//proto/modules: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 1f01370..769424d 100644
--- a/tests/proto_common/toolchains.bzl
+++ b/tests/proto_common/toolchains.bzl
@@ -1,8 +1,22 @@
+# 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)