From f6a5726b7d14a5d001b435cd6dcccf5c189f1962 Mon Sep 17 00:00:00 2001 From: Athos Bacchiocchi Date: Tue, 21 Nov 2023 17:17:08 +0100 Subject: [PATCH] Allow code generation without android SDK/NDK --- WORKSPACE | 7 +++-- bzl/android_configure.bzl | 58 +++++++++++++++++++++++++++++++++++++++ examples/README.md | 2 ++ 3 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 bzl/android_configure.bzl diff --git a/WORKSPACE b/WORKSPACE index 6c5f6169..25a1a4fc 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -9,9 +9,10 @@ djinni_setup_deps() # --- Everything below is only used for examples and tests -# android_sdk_repository fails to find build_tools if we don't explicitly set a version. -android_sdk_repository(name = "androidsdk") -android_ndk_repository(name = "androidndk", api_level = 21) +load("//bzl:android_configure.bzl", "android_configure") +android_configure(name = "local_config_android") +load("@local_config_android//:android_configure.bzl", "android_workspace") +android_workspace() load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") diff --git a/bzl/android_configure.bzl b/bzl/android_configure.bzl new file mode 100644 index 00000000..13550d9e --- /dev/null +++ b/bzl/android_configure.bzl @@ -0,0 +1,58 @@ +"""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/envoyproxy/envoy-mobile/pull/2039/ +# And: 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 _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) + + if sdk_home == "" or ndk_home == "": + print("ANDROID_HOME or ANDROID_NDK_HOME not set. Building android examples will fail.") + + sdk_rule = "" + if sdk_home: + sdk_rule = """ + native.android_sdk_repository( + name="androidsdk", + path="{}", + api_level=30, + build_tools_version="30.0.2", + ) +""".format(sdk_home) + + ndk_rule = "" + if ndk_home: + ndk_rule = """ + native.android_ndk_repository( + name="androidndk", + path="{}", + api_level=21, + ) +""".format(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/examples/README.md b/examples/README.md index 5d7b26da..abf2cb87 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,5 +1,7 @@ ## Building the Android example app +First make sure that the ANDROID_HOME and ANDROID_NDK_HOME environment variables are set and pointing to working installations of the Android SDK and NDK, respectively. + Build with bazel: `bazel build //examples:android-app`. Build and deploy to device: `bazel mobile-install //examples:android-app`.