Debian Codegraph @

third_party/android/android_configure.bzl

raw
1 """Repository rule for Android SDK and NDK autoconfiguration.
2
3 This rule is a no-op unless the required android environment variables are set.
4 """
5
6 # Based on https://github.com/tensorflow/tensorflow/tree/34c03ed67692eb76cb3399cebca50ea8bcde064c/third_party/android
7 # Workaround for https://github.com/bazelbuild/bazel/issues/14260
8
9 _ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
10 _ANDROID_SDK_HOME = "ANDROID_HOME"
11
12 def _escape_for_windows(path):
13 """Properly escape backslashes for Windows.
14
15 Ideally, we would do this conditionally, but there is seemingly no way to
16 determine whether or not this is being called from Windows.
17 """
18 return path.replace("\\", "\\\\")
19
20 def _android_autoconf_impl(repository_ctx):
21 sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
22 ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
23
24 # version 31.0.0 won't work https://stackoverflow.com/a/68036845
25 sdk_rule = ""
26 if sdk_home:
27 sdk_rule = """
28 native.android_sdk_repository(
29 name="androidsdk",
30 path="{}",
31 build_tools_version="30.0.3",
32 )
33 """.format(_escape_for_windows(sdk_home))
34
35 # Note that Bazel does not support NDK 22 yet, and Bazel 3.7.1 only
36 # supports up to API level 29 for NDK 21
37 ndk_rule = ""
38 if ndk_home:
39 ndk_rule = """
40 native.android_ndk_repository(
41 name="androidndk",
42 path="{}",
43 )
44 """.format(_escape_for_windows(ndk_home))
45
46 if ndk_rule == "" and sdk_rule == "":
47 sdk_rule = "pass"
48
49 repository_ctx.file("BUILD.bazel", "")
50 repository_ctx.file("android_configure.bzl", """
51 def android_workspace():
52 {}
53 {}
54 """.format(sdk_rule, ndk_rule))
55
56 android_configure = repository_rule(
57 implementation = _android_autoconf_impl,
58 environ = [
59 _ANDROID_NDK_HOME,
60 _ANDROID_SDK_HOME,
61 ],
62 )