Merge third_party_http_archive and tf_http_archive repository rules.

PiperOrigin-RevId: 359331157
Change-Id: I34f0fad687b13900b42a60abe9573ae662bfb75e
This commit is contained in:
Christian Sigg
2021-02-24 11:33:01 -08:00
committed by TensorFlower Gardener
parent 00ec3ffbaa
commit 61e0a79713
4 changed files with 34 additions and 130 deletions

View File

@@ -830,7 +830,6 @@ def _tf_repositories():
tf_http_archive(
name = "cython",
build_file = "//third_party:cython.BUILD",
patch_file = "//third_party:cython.patch",
sha256 = "e2e38e1f0572ca54d6085df3dec8b607d20e81515fb80215aed19c81e8fe2079",
strip_prefix = "cython-0.29.21",
system_build_file = "//third_party/systemlibs:cython.BUILD",

View File

@@ -13,7 +13,6 @@ def repo():
],
build_file = "//third_party/flatbuffers:BUILD.bazel",
system_build_file = "//third_party/flatbuffers:BUILD.system",
patch_file = "//third_party/flatbuffers:flatbuffers.patch",
link_files = {
"//third_party/flatbuffers:build_defs.bzl": "build_defs.bzl",
},

View File

@@ -15,7 +15,7 @@ def repo(name):
"https://storage.googleapis.com/mirror.tensorflow.org/github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT),
"https://github.com/llvm/llvm-project/archive/{commit}.tar.gz".format(commit = LLVM_COMMIT),
],
additional_build_files = {
link_files = {
"//third_party/llvm:llvm.autogenerated.BUILD": "llvm/BUILD",
"//third_party/mlir:BUILD": "mlir/BUILD",
"//third_party/mlir:test.BUILD": "mlir/test/BUILD",

160
third_party/repo.bzl vendored
View File

@@ -27,27 +27,13 @@ def _use_system_lib(ctx, name):
return False
return name in [n.strip() for n in syslibenv.split(",")]
# Apply a patch_file to the repository root directory.
def _apply_patch(ctx, patch_file):
ctx.patch(patch_file, strip = 1)
def _maybe_label(label_string):
return Label(label_string) if label_string else None
def _label_path_dict(ctx, dict):
return {Label(k): ctx.path(v) for k, v in dict.items()}
def _tf_http_archive(ctx):
# Construct all labels early on to prevent rule restart. We want the
# attributes to be strings instead of labels because they refer to files
# in the TensorFlow repository, not files in repos depending on TensorFlow.
# See also https://github.com/bazelbuild/bazel/issues/10515.
patch_file = _maybe_label(ctx.attr.patch_file)
build_file = _maybe_label(ctx.attr.build_file)
system_build_file = _maybe_label(ctx.attr.system_build_file)
system_link_files = _label_path_dict(ctx, ctx.attr.system_link_files)
additional_build_files = _label_path_dict(ctx, ctx.attr.additional_build_files)
def _get_link_dict(ctx, link_files, build_file):
if build_file:
# Use BUILD.bazel because it takes precedence over BUILD.
link_files = dict(link_files, **{build_file: "BUILD.bazel"})
return {ctx.path(v): Label(k) for k, v in link_files.items()}
def _tf_http_archive_impl(ctx):
if len(ctx.attr.urls) < 2 or "mirror.tensorflow.org" not in ctx.attr.urls[0]:
fail("tf_http_archive(urls) must have redundant URLs. The " +
"mirror.tensorflow.org URL must be present and it must come first. " +
@@ -55,36 +41,36 @@ def _tf_http_archive(ctx):
"put the correctly formatted mirror URL there anyway, because " +
"someone will come along shortly thereafter and mirror the file.")
use_syslib = _use_system_lib(ctx, ctx.attr.name)
# Construct all labels early on to prevent rule restart. We want the
# attributes to be strings instead of labels because they refer to files
# in the TensorFlow repository, not files in repos depending on TensorFlow.
# See also https://github.com/bazelbuild/bazel/issues/10515.
link_dict = _get_link_dict(ctx, ctx.attr.link_files, ctx.attr.build_file)
if not use_syslib:
if _use_system_lib(ctx, ctx.attr.name):
link_dict.update(_get_link_dict(
ctx = ctx,
link_files = ctx.attr.system_link_files,
build_file = ctx.attr.system_build_file,
))
else:
patch_file = ctx.attr.patch_file
patch_file = Label(patch_file) if patch_file else None
ctx.download_and_extract(
ctx.attr.urls,
"",
ctx.attr.sha256,
ctx.attr.type,
ctx.attr.strip_prefix,
url = ctx.attr.urls,
sha256 = ctx.attr.sha256,
type = ctx.attr.type,
stripPrefix = ctx.attr.strip_prefix,
)
if patch_file:
_apply_patch(ctx, patch_file)
ctx.patch(patch_file, strip = 1)
if use_syslib and system_build_file:
# Use BUILD.bazel to avoid conflict with third party projects with
# BUILD or build (directory) underneath.
ctx.template("BUILD.bazel", system_build_file, executable = False)
elif build_file:
# Use BUILD.bazel to avoid conflict with third party projects with
# BUILD or build (directory) underneath.
ctx.template("BUILD.bazel", build_file, executable = False)
if use_syslib:
for label, path in system_link_files.items():
ctx.symlink(label, path)
for label, path in additional_build_files.items():
for path, label in link_dict.items():
ctx.delete(path)
ctx.symlink(label, path)
tf_http_archive = repository_rule(
implementation = _tf_http_archive_impl,
attrs = {
"sha256": attr.string(mandatory = True),
"urls": attr.string_list(mandatory = True),
@@ -93,13 +79,10 @@ tf_http_archive = repository_rule(
"patch_file": attr.string(),
"build_file": attr.string(),
"system_build_file": attr.string(),
"link_files": attr.string_dict(),
"system_link_files": attr.string_dict(),
"additional_build_files": attr.string_dict(),
},
environ = [
"TF_SYSTEM_LIBS",
],
implementation = _tf_http_archive,
environ = ["TF_SYSTEM_LIBS"],
doc = """Downloads and creates Bazel repos for dependencies.
This is a swappable replacement for both http_archive() and
@@ -112,83 +95,6 @@ labels (e.g. '@foo//:bar') or from a label created in their repository (e.g.
'str(Label("//:bar"))').""",
)
def _third_party_http_archive(ctx):
# Construct all labels early on to prevent rule restart. We want the
# attributes to be strings instead of labels because they refer to files
# in the TensorFlow repository, not files in repos depending on TensorFlow.
# See also https://github.com/bazelbuild/bazel/issues/10515.
build_file = _maybe_label(ctx.attr.build_file)
system_build_file = _maybe_label(ctx.attr.system_build_file)
patch_file = _maybe_label(ctx.attr.patch_file)
link_files = _label_path_dict(ctx, ctx.attr.link_files)
system_link_files = _label_path_dict(ctx, ctx.attr.system_link_files)
if len(ctx.attr.urls) < 2 or "mirror.tensorflow.org" not in ctx.attr.urls[0]:
fail("tf_http_archive(urls) must have redundant URLs. The " +
"mirror.tensorflow.org URL must be present and it must come first. " +
"Even if you don't have permission to mirror the file, please " +
"put the correctly formatted mirror URL there anyway, because " +
"someone will come along shortly thereafter and mirror the file.")
use_syslib = _use_system_lib(ctx, ctx.attr.name)
# Use "BUILD.bazel" to avoid conflict with third party projects that contain a
# file or directory called "BUILD"
buildfile_path = ctx.path("BUILD.bazel")
if use_syslib:
if ctx.attr.system_build_file == None:
fail("Bazel was configured with TF_SYSTEM_LIBS to use a system " +
"library for %s, but no system build file for %s was configured. " +
"Please add a system_build_file attribute to the repository rule" +
"for %s." % (ctx.attr.name, ctx.attr.name, ctx.attr.name))
ctx.symlink(Label(ctx.attr.system_build_file), buildfile_path)
else:
ctx.download_and_extract(
ctx.attr.urls,
"",
ctx.attr.sha256,
ctx.attr.type,
ctx.attr.strip_prefix,
)
if ctx.attr.patch_file:
_apply_patch(ctx, Label(ctx.attr.patch_file))
ctx.symlink(Label(ctx.attr.build_file), buildfile_path)
link_dict = {}
if use_syslib:
link_dict.update(system_link_files)
for label, path in link_files.items():
# if syslib and link exists in both, use the system one
if path not in link_dict.values():
link_dict[label] = path
for label, path in link_dict.items():
ctx.symlink(label, path)
# Downloads and creates Bazel repos for dependencies.
#
# This is an upgrade for tf_http_archive that works with go/tfbr-thirdparty.
#
# For link_files, specify each dict entry as:
# "//path/to/source:file": "localfile"
third_party_http_archive = repository_rule(
attrs = {
"sha256": attr.string(mandatory = True),
"urls": attr.string_list(
mandatory = True,
allow_empty = False,
),
"strip_prefix": attr.string(),
"type": attr.string(),
"build_file": attr.string(mandatory = True),
"system_build_file": attr.string(),
"patch_file": attr.string(),
"link_files": attr.string_dict(),
"system_link_files": attr.string_dict(),
},
environ = ["TF_SYSTEM_LIBS"],
implementation = _third_party_http_archive,
)
# Introduced for go/tfbr-thirdparty, now alias of tf_http_archive.
# TODO(csigg): Update call sites and remove.
third_party_http_archive = tf_http_archive