Skip to content

Commit

Permalink
Soft-fail when path is not set
Browse files Browse the repository at this point in the history
Declare an empty repository with empty toolchains and an error message when the path to the NDK was missing.
  • Loading branch information
gferon committed Jan 31, 2024
1 parent b33f43b commit 1abff97
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
45 changes: 45 additions & 0 deletions BUILD.empty.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
This module adds no-op repository rules when the Android NDK is not installed.
"""

package(default_visibility = ["//visibility:public"])

load("//:target_systems.bzl", "CPU_CONSTRAINT", "TARGET_SYSTEM_NAMES")

# android_ndk_repository was used without a valid Android NDK being set.
# Either the path attribute of android_ndk_repository or the ANDROID_NDK_HOME
# environment variable must be set.
# This is a minimal BUILD file to allow non-Android builds to continue.

filegroup(
name = "files",
srcs = [":error_message"],
)

filegroup(
name = "sdk",
srcs = [":error_message"],
)

# Loop over TARGET_SYSTEM_NAMES and define all empty toolchain targets.
[toolchain(
name = "toolchain_%s" % target_system_name,
toolchain = ":error_message",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
target_compatible_with = [
"@platforms//os:android",
CPU_CONSTRAINT[target_system_name],
],
) for target_system_name in TARGET_SYSTEM_NAMES]

genrule(
name = "invalid_android_ndk_repository_error",
outs = [
"error_message",
],
cmd = """echo \
android_ndk_repository was used without a valid Android NDK being set. \
Either the path attribute of android_ndk_repository or the ANDROID_NDK_HOME \
environment variable must be set. ; \
exit 1 """,
)
27 changes: 17 additions & 10 deletions rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@ def _android_ndk_repository_impl(ctx):
Returns:
A final dict of configuration attributes and values.
"""

ctx.template(
"target_systems.bzl",
ctx.attr._template_target_systems,
{
},
executable = False,
)

ndk_path = ctx.attr.path or ctx.os.environ.get("ANDROID_NDK_HOME", None)
if not ndk_path:
fail("Either the ANDROID_NDK_HOME environment variable or the " +
"path attribute of android_ndk_repository must be set.")
ctx.template(
"BUILD.bazel",
ctx.attr._template_empty_repository,
{},
executable = False,
)
return

if ctx.os.name == "linux":
clang_directory = "toolchains/llvm/prebuilt/linux-x86_64"
Expand Down Expand Up @@ -60,14 +74,6 @@ def _android_ndk_repository_impl(ctx):
executable = False,
)

ctx.template(
"target_systems.bzl",
ctx.attr._template_target_systems,
{
},
executable = False,
)

ctx.template(
"%s/BUILD.bazel" % clang_directory,
ctx.attr._template_ndk_clang,
Expand Down Expand Up @@ -121,6 +127,7 @@ android_ndk_repository = repository_rule(
"_template_target_systems": attr.label(default = ":target_systems.bzl.tpl", allow_single_file = True),
"_template_ndk_clang": attr.label(default = ":BUILD.ndk_clang.tpl", allow_single_file = True),
"_template_ndk_sysroot": attr.label(default = ":BUILD.ndk_sysroot.tpl", allow_single_file = True),
"_template_empty_repository": attr.label(default = ":BUILD.empty.tpl", allow_single_file = True),
},
local = True,
implementation = _android_ndk_repository_impl,
Expand Down

0 comments on commit 1abff97

Please sign in to comment.