aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Michael Case <mikecase@google.com>2018-06-05 17:47:19 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-06-05 17:50:28 -0700
commit5105350be955422169de1f22bb99f928c1f4c2ae (patch)
tree1b1d48dda99f14a5bc6a02355d8b479cced761c2
parent8a141854d81a9135a3658255c5813c5277364d01 (diff)
Moves generated android_sdk() and android_ndk() repo rules out of WORKSPACE.
These rules currently get written by configure.py script to WORKSPACE file which is not ideal since (1) WORKSPACE file is tracked by git and (2) we require users to manually delete the rules in order to update/regenerate them. Moving these rules into an external repo that is generated based on several ENV variables set by the configure.py script. Modifying any of these ENV variables will cause the rules to be updated. PiperOrigin-RevId: 199388460
-rw-r--r--WORKSPACE24
-rw-r--r--configure.py94
-rw-r--r--third_party/android/BUILD0
-rw-r--r--third_party/android/android.bzl.tpl9
-rw-r--r--third_party/android/android_configure.BUILD.tpl0
-rw-r--r--third_party/android/android_configure.bzl87
6 files changed, 129 insertions, 85 deletions
diff --git a/WORKSPACE b/WORKSPACE
index 4ddfb9a383..fd7570a80a 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -22,26 +22,10 @@ check_bazel_version_at_least("0.10.0")
load("//tensorflow:workspace.bzl", "tf_workspace")
-# Uncomment and update the paths in these entries to build the Android demo.
-#android_sdk_repository(
-# name = "androidsdk",
-# api_level = 23,
-# # Ensure that you have the build_tools_version below installed in the
-# # SDK manager as it updates periodically.
-# build_tools_version = "26.0.1",
-# # Replace with path to Android SDK on your system
-# path = "<PATH_TO_SDK>",
-#)
-#
-#android_ndk_repository(
-# name="androidndk",
-# path="<PATH_TO_NDK>",
-# # This needs to be 14 or higher to compile TensorFlow.
-# # Please specify API level to >= 21 to build for 64-bit
-# # archtectures or the Android NDK will automatically select biggest
-# # API level that it supports without notice.
-# # Note that the NDK version is not the API level.
-# api_level=14)
+load("//third_party/android:android_configure.bzl", "android_configure")
+android_configure(name="local_config_android")
+load("@local_config_android//:android.bzl", "android_workspace")
+android_workspace()
# Please add all new TensorFlow dependencies in workspace.bzl.
tf_workspace()
diff --git a/configure.py b/configure.py
index b6c32543cf..bde7af8c0e 100644
--- a/configure.py
+++ b/configure.py
@@ -670,8 +670,9 @@ def create_android_ndk_rule(environ_cp):
error_msg=('The path %s or its child file "source.properties" '
'does not exist.')
)
-
- write_android_ndk_workspace_rule(android_ndk_home_path)
+ write_action_env_to_bazelrc('ANDROID_NDK_HOME', android_ndk_home_path)
+ write_action_env_to_bazelrc('ANDROID_NDK_API_LEVEL',
+ check_ndk_level(android_ndk_home_path))
def create_android_sdk_rule(environ_cp):
@@ -733,41 +734,12 @@ def create_android_sdk_rule(environ_cp):
error_msg=('The selected SDK does not have build-tools version %s '
'available.'))
- write_android_sdk_workspace_rule(android_sdk_home_path,
- android_build_tools_version,
- android_api_level)
-
-
-def write_android_sdk_workspace_rule(android_sdk_home_path,
- android_build_tools_version,
- android_api_level):
- print('Writing android_sdk_workspace rule.\n')
- with open(_TF_WORKSPACE, 'a') as f:
- f.write("""
-android_sdk_repository(
- name="androidsdk",
- api_level=%s,
- path="%s",
- build_tools_version="%s")\n
-""" % (android_api_level, android_sdk_home_path, android_build_tools_version))
-
-
-def write_android_ndk_workspace_rule(android_ndk_home_path):
- print('Writing android_ndk_workspace rule.')
- ndk_api_level = check_ndk_level(android_ndk_home_path)
- if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS:
- print('WARNING: The API level of the NDK in %s is %s, which is not '
- 'supported by Bazel (officially supported versions: %s). Please use '
- 'another version. Compiling Android targets may result in confusing '
- 'errors.\n' % (android_ndk_home_path, ndk_api_level,
- _SUPPORTED_ANDROID_NDK_VERSIONS))
- with open(_TF_WORKSPACE, 'a') as f:
- f.write("""
-android_ndk_repository(
- name="androidndk",
- path="%s",
- api_level=%s)\n
-""" % (android_ndk_home_path, ndk_api_level))
+ write_action_env_to_bazelrc('ANDROID_BUILD_TOOLS_VERSION',
+ android_build_tools_version)
+ write_action_env_to_bazelrc('ANDROID_SDK_API_LEVEL',
+ android_api_level)
+ write_action_env_to_bazelrc('ANDROID_SDK_HOME',
+ android_sdk_home_path)
def check_ndk_level(android_ndk_home_path):
@@ -780,18 +752,16 @@ def check_ndk_level(android_ndk_home_path):
revision = re.search(r'Pkg.Revision = (\d+)', filedata)
if revision:
- return revision.group(1)
- return None
-
-
-def workspace_has_any_android_rule():
- """Check the WORKSPACE for existing android_*_repository rules."""
- with open(_TF_WORKSPACE, 'r') as f:
- workspace = f.read()
- has_any_rule = re.search(r'^android_[ns]dk_repository',
- workspace,
- re.MULTILINE)
- return has_any_rule
+ ndk_api_level = revision.group(1)
+ else:
+ raise Exception('Unable to parse NDK revision.')
+ if int(ndk_api_level) not in _SUPPORTED_ANDROID_NDK_VERSIONS:
+ print('WARNING: The API level of the NDK in %s is %s, which is not '
+ 'supported by Bazel (officially supported versions: %s). Please use '
+ 'another version. Compiling Android targets may result in confusing '
+ 'errors.\n' % (android_ndk_home_path, ndk_api_level,
+ _SUPPORTED_ANDROID_NDK_VERSIONS))
+ return ndk_api_level
def set_gcc_host_compiler_path(environ_cp):
@@ -1223,7 +1193,7 @@ def set_tf_cuda_compute_capabilities(environ_cp):
# Check whether all capabilities from the input is valid
all_valid = True
# Remove all whitespace characters before splitting the string
- # that users may insert by accident, as this will result in error
+ # that users may insert by accident, as this will result in error
tf_cuda_compute_capabilities = ''.join(tf_cuda_compute_capabilities.split())
for compute_capability in tf_cuda_compute_capabilities.split(','):
m = re.match('[0-9]+.[0-9]+', compute_capability)
@@ -1551,21 +1521,15 @@ def main():
set_cc_opt_flags(environ_cp)
set_windows_build_flags()
- if workspace_has_any_android_rule():
- print('The WORKSPACE file has at least one of ["android_sdk_repository", '
- '"android_ndk_repository"] already set. Will not ask to help '
- 'configure the WORKSPACE. Please delete the existing rules to '
- 'activate the helper.\n')
- else:
- if get_var(
- environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace',
- False,
- ('Would you like to interactively configure ./WORKSPACE for '
- 'Android builds?'),
- 'Searching for NDK and SDK installations.',
- 'Not configuring the WORKSPACE for Android builds.'):
- create_android_ndk_rule(environ_cp)
- create_android_sdk_rule(environ_cp)
+ if get_var(
+ environ_cp, 'TF_SET_ANDROID_WORKSPACE', 'android workspace',
+ False,
+ ('Would you like to interactively configure ./WORKSPACE for '
+ 'Android builds?'),
+ 'Searching for NDK and SDK installations.',
+ 'Not configuring the WORKSPACE for Android builds.'):
+ create_android_ndk_rule(environ_cp)
+ create_android_sdk_rule(environ_cp)
print('Preconfigured Bazel build configs. You can use any of the below by '
'adding "--config=<>" to your build command. See tools/bazel.rc for '
diff --git a/third_party/android/BUILD b/third_party/android/BUILD
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/third_party/android/BUILD
diff --git a/third_party/android/android.bzl.tpl b/third_party/android/android.bzl.tpl
new file mode 100644
index 0000000000..e6ed4994f3
--- /dev/null
+++ b/third_party/android/android.bzl.tpl
@@ -0,0 +1,9 @@
+"""Set up configurable Android SDK and NDK dependencies."""
+
+def android_workspace():
+ # String for replacement in Bazel template.
+ # These will either be replaced by android_sdk_repository if various ENV
+ # variables are set when `local_config_android` repo_rule is run, or they
+ # will be replaced by noops otherwise.
+ MAYBE_ANDROID_SDK_REPOSITORY
+ MAYBE_ANDROID_NDK_REPOSITORY
diff --git a/third_party/android/android_configure.BUILD.tpl b/third_party/android/android_configure.BUILD.tpl
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/third_party/android/android_configure.BUILD.tpl
diff --git a/third_party/android/android_configure.bzl b/third_party/android/android_configure.bzl
new file mode 100644
index 0000000000..da09bdf39e
--- /dev/null
+++ b/third_party/android/android_configure.bzl
@@ -0,0 +1,87 @@
+"""Repository rule for Android SDK and NDK autoconfiguration.
+
+`android_configure` depends on the following environment variables:
+
+ * `ANDROID_NDK_HOME`: Location of Android NDK root.
+ * `ANDROID_SDK_HOME`: Location of Android SDK root.
+ * `ANDROID_SDK_API_LEVEL`: Desired Android SDK API version.
+ * `ANDROID_NDK_API_LEVEL`: Desired Android NDK API version.
+ * `ANDROID_BUILD_TOOLS_VERSION`: Desired Android build tools version.
+"""
+
+# TODO(mikecase): Move logic for getting default values for the env variables
+# from configure.py script into this rule.
+
+_ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
+_ANDROID_SDK_HOME = "ANDROID_SDK_HOME"
+_ANDROID_NDK_API_VERSION = "ANDROID_NDK_API_LEVEL"
+_ANDROID_SDK_API_VERSION = "ANDROID_SDK_API_LEVEL"
+_ANDROID_BUILD_TOOLS_VERSION = "ANDROID_BUILD_TOOLS_VERSION"
+
+_ANDROID_SDK_REPO_TEMPLATE = """
+ native.android_sdk_repository(
+ name="androidsdk",
+ path="%s",
+ api_level=%s,
+ build_tools_version="%s",
+ )
+"""
+
+_ANDROID_NDK_REPO_TEMPLATE = """
+ native.android_ndk_repository(
+ name="androidndk",
+ path="%s",
+ api_level=%s,
+ )
+"""
+
+def _android_autoconf_impl(repository_ctx):
+ """Implementation of the android_autoconf repository rule."""
+ sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
+ sdk_api_level = repository_ctx.os.environ.get(_ANDROID_SDK_API_VERSION)
+ build_tools_version = repository_ctx.os.environ.get(
+ _ANDROID_BUILD_TOOLS_VERSION)
+ ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
+ ndk_api_level = repository_ctx.os.environ.get(_ANDROID_NDK_API_VERSION)
+
+ sdk_rule = "pass"
+ if all([sdk_home, sdk_api_level, build_tools_version]):
+ sdk_rule = _ANDROID_SDK_REPO_TEMPLATE % (
+ sdk_home, sdk_api_level, build_tools_version)
+
+ ndk_rule = "pass"
+ if all([ndk_home, ndk_api_level]):
+ ndk_rule = _ANDROID_NDK_REPO_TEMPLATE % (ndk_home, ndk_api_level)
+
+ repository_ctx.template(
+ "BUILD",
+ Label("//third_party/android:android_configure.BUILD.tpl"))
+ repository_ctx.template(
+ "android.bzl",
+ Label("//third_party/android:android.bzl.tpl"),
+ substitutions={
+ "MAYBE_ANDROID_SDK_REPOSITORY": sdk_rule,
+ "MAYBE_ANDROID_NDK_REPOSITORY": ndk_rule,
+ })
+
+android_configure = repository_rule(
+ implementation = _android_autoconf_impl,
+ environ = [
+ _ANDROID_SDK_API_VERSION,
+ _ANDROID_NDK_API_VERSION,
+ _ANDROID_BUILD_TOOLS_VERSION,
+ _ANDROID_NDK_HOME,
+ _ANDROID_SDK_HOME,
+ ],
+)
+"""Writes Android SDK and NDK rules.
+
+Add the following to your WORKSPACE FILE:
+
+```python
+android_configure(name = "local_config_android")
+```
+
+Args:
+ name: A unique name for this workspace rule.
+"""