Skip to content

Commit

Permalink
Use module_extension for androidsdk & prepare bzlmod for example/basi…
Browse files Browse the repository at this point in the history
…capp
  • Loading branch information
JiaYan Lin committed Feb 28, 2024
1 parent 7fbb09d commit f7c946f
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 26 deletions.
9 changes: 9 additions & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,12 @@ tasks:
<<: *rules_flags
build_targets:
- "//java/com/basicapp:basic_app"
basicapp_bzlmod:
platform: ubuntu2004
name: basicapp Bzlmod
working_directory: examples/basicapp
build_flags:
<<: *rules_flags
? "--enable_bzlmod"
build_targets:
- "//java/com/basicapp:basic_app"
28 changes: 28 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,31 @@ platform(
"@platforms//os:android",
],
)

# TODO: remove these alias when we no longer needs bind in WORKSPACE.bzlmod
# Because @androidsdk is not defined in WORKSPACE.bzl, where the only valid place
# we can call native function bind. Using these alias to forward the binding.
alias(
name = "androidsdk_sdk",
actual = "@androidsdk//:sdk",
)

alias(
name = "androidsdk_d8_jar_import",
actual = "@androidsdk//:d8_jar_import",
)

alias(
name = "androidsdk_dx_jar_import",
actual = "@androidsdk//:dx_jar_import",
)

alias(
name = "androidsdk_files",
actual = "@androidsdk//:files",
)

alias(
name = "androidsdk_has_android_sdk",
actual = "@androidsdk//:has_android_sdk",
)
13 changes: 12 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,15 @@ git_override(
module_name = "rules_robolectric",
remote = "https://github.com/robolectric/robolectric-bazel",
commit = "a5b25a8c27cc6add74bb01e62cd0dc72df8933ff",
)
)

android_sdk_repository_extension = use_extension("//rules/android_sdk_repository:rule.bzl", "android_sdk_repository_extension")
use_repo(android_sdk_repository_extension, "androidsdk")

register_toolchains("@androidsdk//:sdk-toolchain")
register_toolchains("@androidsdk//:all")

register_toolchains(
"@androidsdk//:sdk-toolchain",
"@androidsdk//:all",
)
16 changes: 10 additions & 6 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ workspace(name = "rules_android")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//rules:rules.bzl", "android_sdk_repository")

maybe(
android_sdk_repository,
name = "androidsdk",
)

maybe(
android_ndk_repository,
name = "androidndk",
)

# load("//rules/android_sdk_repository:rule.bzl", "android_sdk_repository")
# android_sdk_repository(name = "androidsdk")

# Temporary until we break all //external:android dependency
bind(name = "android/sdk", actual = "//:androidsdk_sdk")
bind(name = "android/d8_jar_import", actual = "//:androidsdk_d8_jar_import")
bind(name = "android/dx_jar_import", actual = "//:androidsdk_dx_jar_import")
bind(name = "android_sdk_for_testing", actual = "//:androidsdk_files")
bind(name = "has_android_sdk", actual = "//:androidsdk_has_android_sdk")
28 changes: 27 additions & 1 deletion examples/basicapp/BUILD
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
# Empty build file to satisfy gazelle for rules_go.
# Empty build file to satisfy gazelle for rules_go.

# TODO: remove these alias when ../..//BUILD remove them
alias(
name = "androidsdk_sdk",
actual = "@androidsdk//:sdk",
)

alias(
name = "androidsdk_d8_jar_import",
actual = "@androidsdk//:d8_jar_import",
)

alias(
name = "androidsdk_dx_jar_import",
actual = "@androidsdk//:dx_jar_import",
)

alias(
name = "androidsdk_files",
actual = "@androidsdk//:files",
)

alias(
name = "androidsdk_has_android_sdk",
actual = "@androidsdk//:has_android_sdk",
)
24 changes: 24 additions & 0 deletions examples/basicapp/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,27 @@ bazel_dep(name = "rules_jvm_external", version = "4.5")
bazel_dep(name = "bazel_skylib", version = "1.3.0")
remote_android_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")


bazel_dep(
name = "rules_android",
version = "0.1.1",
)

local_path_override(
module_name = "rules_android",
path = "../../",
)

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",
)
31 changes: 13 additions & 18 deletions examples/basicapp/WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
local_repository(
name = "rules_android",
path = "../..", # rules_android's WORKSPACE relative to this inner workspace
)
workspace(name = "basicapp")

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")
android_sdk_repository(
name = "androidsdk",
)

register_toolchains(
"@rules_android//toolchains/android:android_default_toolchain",
"@rules_android//toolchains/android_sdk:android_sdk_tools",
)
# local_repository(
# name = "rules_android",
# path = "../../"
# )

# load("@rules_android//rules/android_sdk_repository:rule.bzl", "android_sdk_repository")
# android_sdk_repository(name = "androidsdk")

# Temporary until we break all //external:android dependency
bind(name = "android/sdk", actual = "@//:androidsdk_sdk")
bind(name = "android/d8_jar_import", actual = "@//:androidsdk_d8_jar_import")
bind(name = "android/dx_jar_import", actual = "@//:androidsdk_dx_jar_import")
bind(name = "android_sdk_for_testing", actual = "//:androidsdk_files")
bind(name = "has_android_sdk", actual = "//:androidsdk_has_android_sdk")
32 changes: 32 additions & 0 deletions rules/android_sdk_repository/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,35 @@ def android_sdk_repository(

native.register_toolchains("@%s//:sdk-toolchain" % name)
native.register_toolchains("@%s//:all" % name)

def _android_sdk_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["build_tools_version"] = module.tags.configure[0].build_tools_version
kwargs["path"] = module.tags.configure[0].path

_android_sdk_repository(
name = "androidsdk",
**kwargs
)

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

0 comments on commit f7c946f

Please sign in to comment.