xref: /aosp_15_r20/external/jazzer-api/third_party/android/android_configure.bzl (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)
1"""Repository rule for Android SKD and NDK autoconfigure"""
2
3load("@build_bazel_rules_android//android:rules.bzl", "android_sdk_repository")
4load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
5
6_ANDROID_SDK_HOME = "ANDROID_HOME"
7_ANDROID_NDK_HOME = "ANDROID_NDK_HOME"
8
9_ANDROID_REPOS_TEMPLATE = """android_sdk_repository(
10        name="androidsdk",
11        path={sdk_home},
12    )
13    android_ndk_repository(
14        name="androidndk",
15        path={ndk_home},
16    )
17"""
18
19def _is_windows(repository_ctx):
20    """Returns true if the current platform is Windows"""
21    return repository_ctx.os.name.lower().startswith("windows")
22
23def _supports_android(repository_ctx):
24    sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
25    ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
26    return sdk_home and ndk_home and not _is_windows(repository_ctx)
27
28def _android_autoconf_impl(repository_ctx):
29    """Implementation of the android_autoconf repo rule"""
30    sdk_home = repository_ctx.os.environ.get(_ANDROID_SDK_HOME)
31    ndk_home = repository_ctx.os.environ.get(_ANDROID_NDK_HOME)
32
33    # rules_android_ndk does not support Windows yet.
34    if _supports_android(repository_ctx):
35        repos = _ANDROID_REPOS_TEMPLATE.format(
36            sdk_home = repr(sdk_home),
37            ndk_home = repr(ndk_home),
38        )
39    else:
40        repos = "pass"
41
42    repository_ctx.file("BUILD.bazel", "")
43    repository_ctx.file("android_configure.bzl", """
44load("@build_bazel_rules_android//android:rules.bzl", "android_sdk_repository")
45load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
46
47def android_workspace():
48    {repos}
49    """.format(repos = repos))
50
51android_configure = repository_rule(
52    implementation = _android_autoconf_impl,
53    environ = [
54        _ANDROID_SDK_HOME,
55        _ANDROID_NDK_HOME,
56    ],
57)
58