xref: /aosp_15_r20/external/tensorflow/third_party/systemlibs/syslibs_configure.bzl (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1"""Repository rule for system library autoconfiguration.
2
3`syslibs_configure` depends on the following environment variables:
4
5  * `TF_SYSTEM_LIBS`: list of third party dependencies that should use
6    the system version instead
7"""
8
9_TF_SYSTEM_LIBS = "TF_SYSTEM_LIBS"
10
11VALID_LIBS = [
12    "absl_py",
13    "astor_archive",
14    "astunparse_archive",
15    "boringssl",
16    "com_github_googlecloudplatform_google_cloud_cpp",
17    "com_github_grpc_grpc",
18    "com_google_absl",
19    "com_google_protobuf",
20    "com_googlesource_code_re2",
21    "curl",
22    "cython",
23    "dill_archive",
24    "double_conversion",
25    "flatbuffers",
26    "functools32_archive",
27    "gast_archive",
28    "gif",
29    "hwloc",
30    "icu",
31    "jsoncpp_git",
32    "libjpeg_turbo",
33    "lmdb",
34    "nasm",
35    "nsync",
36    "opt_einsum_archive",
37    "org_sqlite",
38    "pasta",
39    "png",
40    "pybind11",
41    "six_archive",
42    "snappy",
43    "tblib_archive",
44    "termcolor_archive",
45    "typing_extensions_archive",
46    "wrapt",
47    "zlib",
48]
49
50def auto_configure_fail(msg):
51    """Output failure message when syslibs configuration fails."""
52    red = "\033[0;31m"
53    no_color = "\033[0m"
54    fail("\n%sSystem Library Configuration Error:%s %s\n" % (red, no_color, msg))
55
56def _is_windows(repository_ctx):
57    """Returns true if the host operating system is windows."""
58    os_name = repository_ctx.os.name.lower()
59    if os_name.find("windows") != -1:
60        return True
61    return False
62
63def _enable_syslibs(repository_ctx):
64    s = repository_ctx.os.environ.get(_TF_SYSTEM_LIBS, "").strip()
65    if not _is_windows(repository_ctx) and s != None and s != "":
66        return True
67    return False
68
69def _get_system_lib_list(repository_ctx):
70    """Gets the list of deps that should use the system lib.
71
72    Args:
73      repository_ctx: The repository context.
74
75    Returns:
76      A string version of a python list
77    """
78    if _TF_SYSTEM_LIBS not in repository_ctx.os.environ:
79        return []
80
81    libenv = repository_ctx.os.environ[_TF_SYSTEM_LIBS].strip()
82    libs = []
83
84    for lib in list(libenv.split(",")):
85        lib = lib.strip()
86        if lib == "":
87            continue
88        if lib not in VALID_LIBS:
89            auto_configure_fail("Invalid system lib set: %s" % lib)
90            return []
91        libs.append(lib)
92
93    return libs
94
95def _format_system_lib_list(repository_ctx):
96    """Formats the list of deps that should use the system lib.
97
98    Args:
99      repository_ctx: The repository context.
100
101    Returns:
102      A list of the names of deps that should use the system lib.
103    """
104    libs = _get_system_lib_list(repository_ctx)
105    ret = ""
106    for lib in libs:
107        ret += "'%s',\n" % lib
108
109    return ret
110
111def _tpl(repository_ctx, tpl, substitutions = {}, out = None):
112    if not out:
113        out = tpl.replace(":", "")
114    repository_ctx.template(
115        out,
116        Label("//third_party/systemlibs%s.tpl" % tpl),
117        substitutions,
118        False,
119    )
120
121def _create_dummy_repository(repository_ctx):
122    """Creates the dummy repository to build with all bundled libraries."""
123
124    _tpl(repository_ctx, ":BUILD")
125    _tpl(
126        repository_ctx,
127        ":build_defs.bzl",
128        {
129            "%{syslibs_enabled}": "False",
130            "%{syslibs_list}": "",
131        },
132    )
133
134def _create_local_repository(repository_ctx):
135    """Creates the repository to build with system libraries."""
136
137    _tpl(repository_ctx, ":BUILD")
138    _tpl(
139        repository_ctx,
140        ":build_defs.bzl",
141        {
142            "%{syslibs_enabled}": "True",
143            "%{syslibs_list}": _format_system_lib_list(repository_ctx),
144        },
145    )
146
147def _syslibs_autoconf_impl(repository_ctx):
148    """Implementation of the syslibs_configure repository rule."""
149    if not _enable_syslibs(repository_ctx):
150        _create_dummy_repository(repository_ctx)
151    else:
152        _create_local_repository(repository_ctx)
153
154syslibs_configure = repository_rule(
155    implementation = _syslibs_autoconf_impl,
156    environ = [
157        _TF_SYSTEM_LIBS,
158    ],
159)
160
161"""Configures the build to link to system libraries
162instead of using bundled versions.
163
164Add the following to your WORKSPACE FILE:
165
166```python
167syslibs_configure(name = "local_config_syslibs")
168```
169
170Args:
171  name: A unique name for this workspace rule.
172"""
173