Make labels relative

Prepare protobuf for bzlmod

Self-reference to `@com_google_protobuf` doesn't work with bzlmod.

In case this is a rule attribute, no reference is needed.

When the label is a default parameter value in a macro it needs to be wrapped with `Label` call, to relativise it correctly.

PiperOrigin-RevId: 496687786
diff --git a/protobuf.bzl b/protobuf.bzl
index f638f39..4ed08fc 100644
--- a/protobuf.bzl
+++ b/protobuf.bzl
@@ -328,7 +328,7 @@
         "_protoc": attr.label(
             executable = True,
             cfg = "exec",
-            default = "@com_google_protobuf//:protoc",
+            default = "//:protoc",
         ),
     },
 )
@@ -402,8 +402,8 @@
         outs = [],
         proto_deps = [],
         includes = ["."],
-        default_runtime = "@com_google_protobuf//:protobuf_objc",
-        protoc = "@com_google_protobuf//:protoc",
+        default_runtime = Label("//:protobuf_objc"),
+        protoc = Label("//:protoc"),
         testonly = None,
         visibility = ["//visibility:public"],
         **kwargs):
@@ -485,6 +485,15 @@
         **kwargs
     )
 
+# When canonical labels are in use, use additional "@" prefix
+_canonical_label_prefix = "@" if str(Label("//:protoc")).startswith("@@") else ""
+
+def _to_label(label_str):
+    """Converts a string to a label using the repository of the calling thread"""
+    if type(label_str) == type(Label("//:foo")):
+        return label_str
+    return Label(_canonical_label_prefix + native.repository_name() + "//" + native.package_name() + ":foo").relative(label_str)
+
 def internal_py_proto_library(
         name,
         srcs = [],
@@ -492,8 +501,8 @@
         py_libs = [],
         py_extra_srcs = [],
         include = None,
-        default_runtime = "@com_google_protobuf//:protobuf_python",
-        protoc = "@com_google_protobuf//:protoc",
+        default_runtime = Label("//:protobuf_python"),
+        protoc = Label("//:protoc"),
         use_grpc_plugin = False,
         testonly = None,
         **kargs):
@@ -546,8 +555,12 @@
         plugin_language = "grpc",
     )
 
-    if default_runtime and not default_runtime in py_libs + deps:
-        py_libs = py_libs + [default_runtime]
+    if default_runtime:
+        # Resolve non-local labels
+        labels = [_to_label(lib) for lib in py_libs + deps]
+        if not _to_label(default_runtime) in labels:
+            py_libs = py_libs + [default_runtime]
+
     py_library(
         name = name,
         testonly = testonly,
@@ -580,7 +593,7 @@
         outs = [],
         lang = None,
         includes = ["."],
-        protoc = "@com_google_protobuf//:protoc",
+        protoc = Label("//:protoc"),
         testonly = None,
         visibility = ["//visibility:public"],
         **kwargs):