Skip to content

Commit 1abff97

Browse files
committed
Soft-fail when path is not set
Declare an empty repository with empty toolchains and an error message when the path to the NDK was missing.
1 parent b33f43b commit 1abff97

2 files changed

Lines changed: 62 additions & 10 deletions

File tree

BUILD.empty.tpl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
This module adds no-op repository rules when the Android NDK is not installed.
3+
"""
4+
5+
package(default_visibility = ["//visibility:public"])
6+
7+
load("//:target_systems.bzl", "CPU_CONSTRAINT", "TARGET_SYSTEM_NAMES")
8+
9+
# android_ndk_repository was used without a valid Android NDK being set.
10+
# Either the path attribute of android_ndk_repository or the ANDROID_NDK_HOME
11+
# environment variable must be set.
12+
# This is a minimal BUILD file to allow non-Android builds to continue.
13+
14+
filegroup(
15+
name = "files",
16+
srcs = [":error_message"],
17+
)
18+
19+
filegroup(
20+
name = "sdk",
21+
srcs = [":error_message"],
22+
)
23+
24+
# Loop over TARGET_SYSTEM_NAMES and define all empty toolchain targets.
25+
[toolchain(
26+
name = "toolchain_%s" % target_system_name,
27+
toolchain = ":error_message",
28+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
29+
target_compatible_with = [
30+
"@platforms//os:android",
31+
CPU_CONSTRAINT[target_system_name],
32+
],
33+
) for target_system_name in TARGET_SYSTEM_NAMES]
34+
35+
genrule(
36+
name = "invalid_android_ndk_repository_error",
37+
outs = [
38+
"error_message",
39+
],
40+
cmd = """echo \
41+
android_ndk_repository was used without a valid Android NDK being set. \
42+
Either the path attribute of android_ndk_repository or the ANDROID_NDK_HOME \
43+
environment variable must be set. ; \
44+
exit 1 """,
45+
)

rules.bzl

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@ def _android_ndk_repository_impl(ctx):
2323
Returns:
2424
A final dict of configuration attributes and values.
2525
"""
26+
27+
ctx.template(
28+
"target_systems.bzl",
29+
ctx.attr._template_target_systems,
30+
{
31+
},
32+
executable = False,
33+
)
34+
2635
ndk_path = ctx.attr.path or ctx.os.environ.get("ANDROID_NDK_HOME", None)
2736
if not ndk_path:
28-
fail("Either the ANDROID_NDK_HOME environment variable or the " +
29-
"path attribute of android_ndk_repository must be set.")
37+
ctx.template(
38+
"BUILD.bazel",
39+
ctx.attr._template_empty_repository,
40+
{},
41+
executable = False,
42+
)
43+
return
3044

3145
if ctx.os.name == "linux":
3246
clang_directory = "toolchains/llvm/prebuilt/linux-x86_64"
@@ -60,14 +74,6 @@ def _android_ndk_repository_impl(ctx):
6074
executable = False,
6175
)
6276

63-
ctx.template(
64-
"target_systems.bzl",
65-
ctx.attr._template_target_systems,
66-
{
67-
},
68-
executable = False,
69-
)
70-
7177
ctx.template(
7278
"%s/BUILD.bazel" % clang_directory,
7379
ctx.attr._template_ndk_clang,
@@ -121,6 +127,7 @@ android_ndk_repository = repository_rule(
121127
"_template_target_systems": attr.label(default = ":target_systems.bzl.tpl", allow_single_file = True),
122128
"_template_ndk_clang": attr.label(default = ":BUILD.ndk_clang.tpl", allow_single_file = True),
123129
"_template_ndk_sysroot": attr.label(default = ":BUILD.ndk_sysroot.tpl", allow_single_file = True),
130+
"_template_empty_repository": attr.label(default = ":BUILD.empty.tpl", allow_single_file = True),
124131
},
125132
local = True,
126133
implementation = _android_ndk_repository_impl,

0 commit comments

Comments
 (0)