Skip to content

Commit

Permalink
Make rules_android_ndk compatible with bzlmod without needing a WORSP…
Browse files Browse the repository at this point in the history
…ACE file (#70)

Also updates the example use the Starlark Android rules.
  • Loading branch information
ahumesky authored Jul 3, 2024
1 parent f942689 commit d5c9d46
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 4 deletions.
2 changes: 1 addition & 1 deletion BUILD.ndk_clang.tpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Declarations for the NDK's Clang directory."""

load("@{repository_name}//:ndk_cc_toolchain_config.bzl", "ndk_cc_toolchain_config_rule")
load("@@{repository_name}//:ndk_cc_toolchain_config.bzl", "ndk_cc_toolchain_config_rule")
load("//:target_systems.bzl", "TARGET_SYSTEM_NAMES")

package(default_visibility = ["//visibility:public"])
Expand Down
2 changes: 2 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ module(
compatibility_level = 1,
repo_name = "rules_android_ndk",
)

bazel_dep(name = "platforms", version = "0.0.10")
1 change: 1 addition & 0 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty to avoid pulling in the non-bzlmod WORKSPACE file
6 changes: 6 additions & 0 deletions examples/basic/.bazelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# For bazel 5.3.0, not necessary for 6.0.0+
build --define=android_incremental_dexing_tool=d8_dexbuilder
build --define=android_dexmerger_tool=d8_dexmerger

# TODO: Remove once https://github.com/bazelbuild/rules_android/issues/219 is resolved.
common --experimental_google_legacy_api
common --experimental_enable_android_migration_apis

common --android_sdk=@androidsdk//:sdk
24 changes: 24 additions & 0 deletions examples/basic/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
module(name = "basic_example")

bazel_dep(name = "platforms", version = "0.0.10")

bazel_dep(name = "rules_android_ndk")
local_path_override(
module_name = "rules_android_ndk",
path = "../..",
)

android_ndk_repository_extension = use_extension("@rules_android_ndk//:extension.bzl", "android_ndk_repository_extension")
use_repo(android_ndk_repository_extension, "androidndk")
register_toolchains("@androidndk//:all")

bazel_dep(name = "rules_android", version = "0.0.0")
# TODO: Replace this with a released version once an updated
# version is available on BCR.
git_override(
module_name = "rules_android",
remote = "https://github.com/bazelbuild/rules_android",
commit = "e02da4d00ad81d44f1c8fd1b2ee46f057afdd392",
)

register_toolchains(
"@rules_android//toolchains/android:android_default_toolchain",
"@rules_android//toolchains/android_sdk:android_sdk_tools",
)

android_sdk_repository_extension = use_extension("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
use_repo(android_sdk_repository_extension, "androidsdk")
register_toolchains("@androidsdk//:sdk-toolchain", "@androidsdk//:all")
33 changes: 30 additions & 3 deletions examples/basic/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
android_sdk_repository(
name = "androidsdk",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# NDK

local_repository(
name = "rules_android_ndk",
Expand All @@ -12,3 +12,30 @@ load("@rules_android_ndk//:rules.bzl", "android_ndk_repository")
android_ndk_repository(name = "androidndk")

register_toolchains("@androidndk//:all")

# rules_android and Android SDK

RULES_ANDROID_COMMIT = "e02da4d00ad81d44f1c8fd1b2ee46f057afdd392"

http_archive(
name = "rules_android",
sha256 = "6f332645225c1a73b8a98136908822a25543ba21319ed5dc5c04f256e24aa8bd",
strip_prefix = "rules_android-" + RULES_ANDROID_COMMIT,
urls = ["https://github.com/bazelbuild/rules_android/archive/%s.zip" % RULES_ANDROID_COMMIT],
)

load("@rules_android//:prereqs.bzl", "rules_android_prereqs")
rules_android_prereqs()
load("@rules_android//:defs.bzl", "rules_android_workspace")
rules_android_workspace()

load("@rules_android//rules:rules.bzl", "android_sdk_repository")
# Requires that the ANDROID_HOME environment variable is set to the Android SDK path.
android_sdk_repository(
name = "androidsdk",
)

register_toolchains(
"@rules_android//toolchains/android:android_default_toolchain",
"@rules_android//toolchains/android_sdk:android_sdk_tools",
)
1 change: 1 addition & 0 deletions examples/basic/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty to avoid pulling in the non-bzlmod WORKSPACE file
2 changes: 2 additions & 0 deletions examples/basic/java/com/app/BUILD
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
load("@rules_android//rules:rules.bzl", "android_binary", "android_library")

android_binary(
name = "app",
manifest = "AndroidManifest.xml",
Expand Down
47 changes: 47 additions & 0 deletions extension.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A bzlmod extension for loading the NDK."""

load(":rules.bzl", "android_ndk_repository")

def _android_ndk_repository_extension_impl(module_ctx):
root_modules = [m for m in module_ctx.modules if m.is_root and m.tags.configure]
if len(root_modules) > 1:
fail("Expected at most one root module, found {}".format(", ".join([x.name for x in root_modules])))

if root_modules:
module = root_modules[0]
else:
module = module_ctx.modules[0]

kwargs = {}
if module.tags.configure:
kwargs["api_level"] = module.tags.configure[0].api_level
kwargs["path"] = module.tags.configure[0].path

android_ndk_repository(
name = "androidndk",
**kwargs,
)

android_ndk_repository_extension = module_extension(
implementation = _android_ndk_repository_extension_impl,
tag_classes = {
"configure": tag_class(attrs = {
"path": attr.string(),
"api_level": attr.int(),
}),
},
)

0 comments on commit d5c9d46

Please sign in to comment.