|
| 1 | +# This file is used to define a custom repository rule for TensorFlow submodule used by LiteRT. |
| 2 | +# |
| 3 | +# The function below allows us to select between a local TensorFlow source from local_repository |
| 4 | +# or a remote http_archive based on the 'USE_LOCAL_TF' environment variable. |
| 5 | +# |
| 6 | +# To use this rule, you must set the 'USE_LOCAL_TF' environment variable to |
| 7 | +# 'true' and the 'TF_LOCAL_SOURCE_PATH' environment variable to the absolute path of |
| 8 | +# the tensorflow source directory. We need to pass the absolute path because we cannot use |
| 9 | +# ctx.path() on a relative path for lower bazel versions. |
| 10 | + |
| 11 | +""" |
| 12 | +Implementation function for custom TensorFlow source repository rule. |
| 13 | +This rule is used to select between a local TensorFlow source from local_repository |
| 14 | +or a remote http_archive based on the 'USE_LOCAL_TF' environment variable. |
| 15 | +""" |
| 16 | + |
| 17 | +def _tensorflow_source_repo_impl(ctx): |
| 18 | + use_local_tf = ctx.os.environ.get("USE_LOCAL_TF", "false") == "true" |
| 19 | + |
| 20 | + if use_local_tf: |
| 21 | + # TF_LOCAL_SOURCE_PATH must be set to the absolute path to the tensorflow source directory. |
| 22 | + TF_LOCAL_SOURCE_PATH_ENV = ctx.os.environ.get("TF_LOCAL_SOURCE_PATH", "") |
| 23 | + if not TF_LOCAL_SOURCE_PATH_ENV: |
| 24 | + fail("""ERROR: USE_LOCAL_TF is true, but TF_LOCAL_SOURCE_PATH environment variable |
| 25 | + is not set with the absolute path to TensorFlow source.""") |
| 26 | + |
| 27 | + local_path_str = TF_LOCAL_SOURCE_PATH_ENV # Get the path from the environment variable |
| 28 | + resolved_local_path = ctx.path(local_path_str) |
| 29 | + |
| 30 | + for f in resolved_local_path.readdir(): |
| 31 | + ctx.symlink(f, f.basename) |
| 32 | + else: |
| 33 | + ctx.download_and_extract( |
| 34 | + url = ctx.attr.urls[0], |
| 35 | + sha256 = ctx.attr.sha256, |
| 36 | + stripPrefix = ctx.attr.strip_prefix, |
| 37 | + ) |
| 38 | + |
| 39 | +tensorflow_source_repo = repository_rule( |
| 40 | + implementation = _tensorflow_source_repo_impl, |
| 41 | + local = False, |
| 42 | + attrs = { |
| 43 | + "sha256": attr.string(mandatory = False), |
| 44 | + "strip_prefix": attr.string(mandatory = False), |
| 45 | + "urls": attr.string_list(mandatory = True), |
| 46 | + }, |
| 47 | + doc = """ |
| 48 | + A custom repository rule to select between a local TensorFlow source or a remote http_archive |
| 49 | + based on the'USE_LOCAL_TF' environment variable and TF_LOCAL_SOURCE_PATH flag. |
| 50 | + """, |
| 51 | +) |
0 commit comments