Make Compatible with Bazel 5.0 (#28683)
* See what happens when we remove NDK
* Try to debug Kokoro in a super hacky way
* And echo the external IP too
* Attempt to fix NDK installation
* And actually run the portion of the code I need to test out
* Clean up
* Actually test against bazel 5
* Put export in proper file
* Make android an optional dependency
* Escape paths for Windows
* Revert switch to Bazel 5.0
diff --git a/WORKSPACE b/WORKSPACE
index c528f35..782abc6 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -53,18 +53,13 @@
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)
-android_sdk_repository(
- name = "androidsdk",
- # version 31.0.0 won't work https://stackoverflow.com/a/68036845
- build_tools_version = "30.0.3",
-)
+load("//third_party/android:android_configure.bzl", "android_configure")
-android_ndk_repository(
- name = "androidndk",
- # Note that Bazel does not support NDK 22 yet, and Bazel 3.7.1 only
- # supports up to API level 29 for NDK 21
- # https://github.com/bazelbuild/bazel/issues/13421
-)
+android_configure(name = "local_config_android")
+
+load("@local_config_android//:android_configure.bzl", "android_workspace")
+
+android_workspace()
# Prevents bazel's '...' expansion from including the following folder.
# This is required because the BUILD file in the following folder
diff --git a/third_party/android/BUILD b/third_party/android/BUILD
new file mode 100644
index 0000000..12a7f70
--- /dev/null
+++ b/third_party/android/BUILD
@@ -0,0 +1,3 @@
+exports_files([
+ "android_configure.bzl",
+])
diff --git a/third_party/android/android_configure.bzl b/third_party/android/android_configure.bzl
new file mode 100644
index 0000000..40e7f10
--- /dev/null
+++ b/third_party/android/android_configure.bzl
@@ -0,0 +1,62 @@
+"""Repository rule for Android SDK and NDK autoconfiguration.
+
+This rule is a no-op unless the required android environment variables are set.
+"""
+
+# Based on https://github.com/tensorflow/tensorflow/tree/34c03ed67692eb76cb3399cebca50ea8bcde064c/third_party/android
+# Workaround for https://github.com/bazelbuild/bazel/issues/14260
+
+_ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
+_ANDROID_SDK_HOME = "ANDROID_HOME"
+
+def _escape_for_windows(path):
+ """Properly escape backslashes for Windows.
+
+ Ideally, we would do this conditionally, but there is seemingly no way to
+ determine whether or not this is being called from Windows.
+ """
+ return path.replace("\\", "\\\\")
+
+def _android_autoconf_impl(repository_ctx):
+ sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
+ ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
+
+ # version 31.0.0 won't work https://stackoverflow.com/a/68036845
+ sdk_rule = ""
+ if sdk_home:
+ sdk_rule = """
+ native.android_sdk_repository(
+ name="androidsdk",
+ path="{}",
+ build_tools_version="30.0.3",
+ )
+""".format(_escape_for_windows(sdk_home))
+
+ # Note that Bazel does not support NDK 22 yet, and Bazel 3.7.1 only
+ # supports up to API level 29 for NDK 21
+ ndk_rule = ""
+ if ndk_home:
+ ndk_rule = """
+ native.android_ndk_repository(
+ name="androidndk",
+ path="{}",
+ )
+""".format(_escape_for_windows(ndk_home))
+
+ if ndk_rule == "" and sdk_rule == "":
+ sdk_rule = "pass"
+
+ repository_ctx.file("BUILD.bazel", "")
+ repository_ctx.file("android_configure.bzl", """
+def android_workspace():
+ {}
+ {}
+ """.format(sdk_rule, ndk_rule))
+
+android_configure = repository_rule(
+ implementation = _android_autoconf_impl,
+ environ = [
+ _ANDROID_NDK_HOME,
+ _ANDROID_SDK_HOME,
+ ],
+)
diff --git a/tools/internal_ci/helper_scripts/prepare_build_linux_rc b/tools/internal_ci/helper_scripts/prepare_build_linux_rc
index 01e9dd7..125f011 100644
--- a/tools/internal_ci/helper_scripts/prepare_build_linux_rc
+++ b/tools/internal_ci/helper_scripts/prepare_build_linux_rc
@@ -18,6 +18,9 @@
# Need to increase open files limit for c tests
ulimit -n 32768
+# This is required by the android_ndk_repository repo rule.
+export ANDROID_NDK_HOME=$ANDROID_HOME/ndk-bundle
+
# 1. Move docker's storage location to scratch disk so we don't run out of space.
# 2. Use container registry mirror for pulling docker images (should make downloads faster)
# See https://cloud.google.com/container-registry/docs/using-dockerhub-mirroring