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
pull/11363/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 28e9b29de5
commit 64ae02c7fe
  1. 4
      BUILD.bazel
  2. 27
      protobuf.bzl
  3. 4
      protobuf_deps.bzl

@ -306,8 +306,8 @@ alias(
proto_lang_toolchain(
name = "cc_toolchain",
blacklisted_protos = [
"@com_google_protobuf//:compiler_plugin_proto",
"@com_google_protobuf//:descriptor_proto",
"//:compiler_plugin_proto",
"//:descriptor_proto",
],
command_line = "--cpp_out=$(OUT)",
runtime = ":protobuf",

@ -328,7 +328,7 @@ internal_gen_well_known_protos_java = rule(
"_protoc": attr.label(
executable = True,
cfg = "exec",
default = "@com_google_protobuf//:protoc",
default = "//:protoc",
),
},
)
@ -402,8 +402,8 @@ def internal_objc_proto_library(
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 @@ def internal_objc_proto_library(
**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 @@ def internal_py_proto_library(
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 @@ def internal_py_proto_library(
plugin_language = "grpc",
)
if default_runtime and not default_runtime in py_libs + deps:
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 @@ def _source_proto_library(
outs = [],
lang = None,
includes = ["."],
protoc = "@com_google_protobuf//:protoc",
protoc = Label("//:protoc"),
testonly = None,
visibility = ["//visibility:public"],
**kwargs):

@ -48,7 +48,7 @@ def protobuf_deps():
if not native.existing_rule("zlib"):
http_archive(
name = "zlib",
build_file = "@com_google_protobuf//:third_party/zlib.BUILD",
build_file = "//:third_party/zlib.BUILD",
sha256 = "d14c38e313afc35a9a8760dadf26042f51ea0f5d154b0630a31da0540107fb98",
strip_prefix = "zlib-1.2.13",
urls = [
@ -60,7 +60,7 @@ def protobuf_deps():
if not native.existing_rule("jsoncpp"):
http_archive(
name = "jsoncpp",
build_file = "@com_google_protobuf//:third_party/jsoncpp.BUILD",
build_file = "//:third_party/jsoncpp.BUILD",
sha256 = "e34a628a8142643b976c7233ef381457efad79468c67cb1ae0b83a33d7493999",
strip_prefix = "jsoncpp-1.9.4",
urls = ["https://github.com/open-source-parsers/jsoncpp/archive/refs/tags/1.9.4.tar.gz"],

Loading…
Cancel
Save