• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2017 The TensorFlow Authors. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#    http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Utilities for defining TensorFlow Bazel dependencies."""
16
17def _get_env_var(ctx, name):
18    if name in ctx.os.environ:
19        return ctx.os.environ[name]
20    else:
21        return None
22
23# Checks if we should use the system lib instead of the bundled one
24def _use_system_lib(ctx, name):
25    syslibenv = _get_env_var(ctx, "TF_SYSTEM_LIBS")
26    if not syslibenv:
27        return False
28    return name in [n.strip() for n in syslibenv.split(",")]
29
30def _get_link_dict(ctx, link_files, build_file):
31    link_dict = {ctx.path(v): ctx.path(Label(k)) for k, v in link_files.items()}
32    if build_file:
33        # Use BUILD.bazel because it takes precedence over BUILD.
34        link_dict[ctx.path("BUILD.bazel")] = ctx.path(Label(build_file))
35    return link_dict
36
37def _tf_http_archive_impl(ctx):
38    # Construct all paths early on to prevent rule restart. We want the
39    # attributes to be strings instead of labels because they refer to files
40    # in the TensorFlow repository, not files in repos depending on TensorFlow.
41    # See also https://github.com/bazelbuild/bazel/issues/10515.
42    link_dict = _get_link_dict(ctx, ctx.attr.link_files, ctx.attr.build_file)
43
44    if _use_system_lib(ctx, ctx.attr.name):
45        link_dict.update(_get_link_dict(
46            ctx = ctx,
47            link_files = ctx.attr.system_link_files,
48            build_file = ctx.attr.system_build_file,
49        ))
50    else:
51        patch_file = ctx.attr.patch_file
52        patch_file = ctx.path(Label(patch_file)) if patch_file else None
53        ctx.download_and_extract(
54            url = ctx.attr.urls,
55            sha256 = ctx.attr.sha256,
56            type = ctx.attr.type,
57            stripPrefix = ctx.attr.strip_prefix,
58        )
59        if patch_file:
60            ctx.patch(patch_file, strip = 1)
61
62    for dst, src in link_dict.items():
63        ctx.delete(dst)
64        ctx.symlink(src, dst)
65
66_tf_http_archive = repository_rule(
67    implementation = _tf_http_archive_impl,
68    attrs = {
69        "sha256": attr.string(mandatory = True),
70        "urls": attr.string_list(mandatory = True),
71        "strip_prefix": attr.string(),
72        "type": attr.string(),
73        "patch_file": attr.string(),
74        "build_file": attr.string(),
75        "system_build_file": attr.string(),
76        "link_files": attr.string_dict(),
77        "system_link_files": attr.string_dict(),
78    },
79    environ = ["TF_SYSTEM_LIBS"],
80)
81
82def tf_http_archive(name, sha256, urls, **kwargs):
83    """Downloads and creates Bazel repos for dependencies.
84
85    This is a swappable replacement for both http_archive() and
86    new_http_archive() that offers some additional features. It also helps
87    ensure best practices are followed.
88
89    File arguments are relative to the TensorFlow repository by default. Dependent
90    repositories that use this rule should refer to files either with absolute
91    labels (e.g. '@foo//:bar') or from a label created in their repository (e.g.
92    'str(Label("//:bar"))').
93    """
94    if len(urls) < 2:
95        fail("tf_http_archive(urls) must have redundant URLs.")
96
97    if not any([mirror in urls[0] for mirror in (
98        "mirror.tensorflow.org",
99        "mirror.bazel.build",
100        "storage.googleapis.com",
101    )]):
102        fail("The first entry of tf_http_archive(urls) must be a mirror " +
103             "URL, preferrably mirror.tensorflow.org. Even if you don't have " +
104             "permission to mirror the file, please put the correctly " +
105             "formatted mirror URL there anyway, because someone will come " +
106             "along shortly thereafter and mirror the file.")
107
108    if native.existing_rule(name):
109        print("\n\033[1;33mWarning:\033[0m skipping import of repository '" +
110              name + "' because it already exists.\n")
111        return
112
113    _tf_http_archive(
114        name = name,
115        sha256 = sha256,
116        urls = urls,
117        **kwargs
118    )
119