1"""Repository rule for Android SDK and NDK autoconfiguration. 2 3`android_configure` depends on the following environment variables: 4 5 * `ANDROID_NDK_HOME`: Location of Android NDK root. 6 * `ANDROID_SDK_HOME`: Location of Android SDK root. 7 * `ANDROID_SDK_API_LEVEL`: Desired Android SDK API version. 8 * `ANDROID_NDK_API_LEVEL`: Desired Android NDK API version. 9 * `ANDROID_BUILD_TOOLS_VERSION`: Desired Android build tools version. 10 11 12Writes Android SDK and NDK rules. 13 14Add the following to your WORKSPACE FILE: 15 16```python 17android_configure(name = "local_config_android") 18``` 19 20Args: 21 name: A unique name for this workspace rule. 22""" 23 24_ANDROID_NDK_HOME = "ANDROID_NDK_HOME" 25_ANDROID_SDK_HOME = "ANDROID_SDK_HOME" 26_ANDROID_NDK_API_VERSION = "ANDROID_NDK_API_LEVEL" 27_ANDROID_SDK_API_VERSION = "ANDROID_SDK_API_LEVEL" 28_ANDROID_BUILD_TOOLS_VERSION = "ANDROID_BUILD_TOOLS_VERSION" 29 30_ANDROID_SDK_REPO_TEMPLATE = """ 31 native.android_sdk_repository( 32 name="androidsdk", 33 path="%s", 34 api_level=%s, 35 build_tools_version="%s", 36 ) 37""" 38 39_ANDROID_NDK_REPO_TEMPLATE = """ 40 native.android_ndk_repository( 41 name="androidndk", 42 path="%s", 43 api_level=%s, 44 ) 45""" 46 47def _android_autoconf_impl(repository_ctx): 48 """Implementation of the android_autoconf repository rule.""" 49 sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME) 50 sdk_api_level = repository_ctx.os.environ.get(_ANDROID_SDK_API_VERSION) 51 build_tools_version = repository_ctx.os.environ.get( 52 _ANDROID_BUILD_TOOLS_VERSION, 53 ) 54 ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME) 55 ndk_api_level = repository_ctx.os.environ.get(_ANDROID_NDK_API_VERSION) 56 57 sdk_rule = "" 58 if all([sdk_home, sdk_api_level, build_tools_version]): 59 sdk_rule = _ANDROID_SDK_REPO_TEMPLATE % ( 60 sdk_home, 61 sdk_api_level, 62 build_tools_version, 63 ) 64 65 ndk_rule = "" 66 if all([ndk_home, ndk_api_level]): 67 ndk_rule = _ANDROID_NDK_REPO_TEMPLATE % (ndk_home, ndk_api_level) 68 69 if ndk_rule == "" and sdk_rule == "": 70 sdk_rule = "pass" 71 # TODO(xunkai): Add interactive configure script. 72 73 repository_ctx.template( 74 "BUILD", 75 Label("//third_party/android:android_configure.BUILD.tpl"), 76 ) 77 repository_ctx.template( 78 "android.bzl", 79 Label("//third_party/android:android.bzl.tpl"), 80 substitutions = { 81 "MAYBE_ANDROID_SDK_REPOSITORY": sdk_rule, 82 "MAYBE_ANDROID_NDK_REPOSITORY": ndk_rule, 83 }, 84 ) 85 86android_configure = repository_rule( 87 implementation = _android_autoconf_impl, 88 environ = [ 89 _ANDROID_SDK_API_VERSION, 90 _ANDROID_NDK_API_VERSION, 91 _ANDROID_BUILD_TOOLS_VERSION, 92 _ANDROID_NDK_HOME, 93 _ANDROID_SDK_HOME, 94 ], 95) 96