github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/systemlibs/syslibs_configure.bzl (about)

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