Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove direct access to android_sdk configuration field #201

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ bazel: last_green

tools_flags: &tools_flags
? "--enable_bzlmod=false"
? "--incompatible_enable_android_toolchain_resolution=false"
# Sandboxed SDK tools depend on libraries that require Java runtime 17 or higher.
? "--java_runtime_version=17"
rules_flags: &rules_flags
? "--enable_bzlmod=false"
? "--incompatible_enable_android_toolchain_resolution=false"

tools: &tools
name: "Tools"
Expand Down Expand Up @@ -105,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")
2 changes: 0 additions & 2 deletions kokoro/presubmit/presubmit_main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,9 @@ function main() {
"--java_language_version=11"
"--java_runtime_version=17"
"--noenable_bzlmod"
"--noincompatible_enable_android_toolchain_resolution"
)
RULE_ARGS=(
"--noenable_bzlmod"
"--noincompatible_enable_android_toolchain_resolution"
)

# Go to rules_android workspace and run relevant tests.
Expand Down
3 changes: 2 additions & 1 deletion rules/aar_import/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ load(
load(
"//rules:utils.bzl",
"ANDROID_TOOLCHAIN_TYPE",
"get_android_sdk",
_get_android_toolchain = "get_android_toolchain",
_utils = "utils",
)
Expand Down Expand Up @@ -158,7 +159,7 @@ def _process_resources(

# Tool and Processing related inputs
aapt = _get_android_toolchain(ctx).aapt2.files_to_run,
android_jar = ctx.attr._android_sdk[AndroidSdkInfo].android_jar,
android_jar = get_android_sdk(ctx).android_jar,
android_kit = _get_android_toolchain(ctx).android_kit.files_to_run,
busybox = _get_android_toolchain(ctx).android_resources_busybox.files_to_run,
java_toolchain = _common.get_java_toolchain(ctx),
Expand Down
5 changes: 5 additions & 0 deletions rules/aar_import/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

"""aar_import rule."""

load(
"//rules:utils.bzl",
"ANDROID_SDK_TOOLCHAIN_TYPE",
)
load(":attrs.bzl", _ATTRS = "ATTRS")
load(":impl.bzl", _impl = "impl")

Expand Down Expand Up @@ -56,5 +60,6 @@ aar_import = rule(
toolchains = [
"//toolchains/android:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
ANDROID_SDK_TOOLCHAIN_TYPE,
],
)
7 changes: 5 additions & 2 deletions rules/android_application/android_application_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ load(
)
load(
"//rules:utils.bzl",
"ANDROID_SDK_TOOLCHAIN_TYPE",
"get_android_sdk",
"get_android_toolchain",
_log = "log",
)
Expand Down Expand Up @@ -84,7 +86,7 @@ def _process_feature_module(
base_apk,
java_package,
feature_target,
ctx.attr._android_sdk[AndroidSdkInfo].aapt2,
get_android_toolchain(ctx).aapt2.files_to_run,
ctx.executable._feature_manifest_script,
ctx.executable._priority_feature_manifest_script,
get_android_toolchain(ctx).android_resources_busybox,
Expand Down Expand Up @@ -124,7 +126,7 @@ def _process_feature_module(
transitive_r_txts = [res.transitive_r_txts],
additional_apks_to_link_against = [base_apk],
proto_format = True, # required for aab.
android_jar = ctx.attr._android_sdk[AndroidSdkInfo].android_jar,
android_jar = get_android_sdk(ctx).android_jar,
aapt = get_android_toolchain(ctx).aapt2.files_to_run,
busybox = get_android_toolchain(ctx).android_resources_busybox.files_to_run,
host_javabase = _common.get_host_javabase(ctx),
Expand Down Expand Up @@ -403,6 +405,7 @@ android_application = rule(
toolchains = [
"//toolchains/android:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
ANDROID_SDK_TOOLCHAIN_TYPE,
],
_skylark_testable = True,
)
Expand Down
3 changes: 2 additions & 1 deletion rules/android_library/rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ load(
"//rules:attrs.bzl",
_attrs = "attrs",
)
load("//rules:utils.bzl", "ANDROID_SDK_TOOLCHAIN_TYPE")
load(":attrs.bzl", _ATTRS = "ATTRS")
load(":impl.bzl", _impl = "impl")

Expand Down Expand Up @@ -174,8 +175,8 @@ def make_rule(
outputs = outputs,
toolchains = [
"//toolchains/android:toolchain_type",
"//toolchains/android_sdk:toolchain_type",
"@bazel_tools//tools/jdk:toolchain_type",
ANDROID_SDK_TOOLCHAIN_TYPE,
] + additional_toolchains,
_skylark_testable = True,
)
Expand Down
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(),
}),
},
)
8 changes: 0 additions & 8 deletions rules/attrs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,6 @@ _JAVA_RUNTIME = dict(

# Android SDK attribute.
_ANDROID_SDK = dict(
_android_sdk = attr.label(
allow_rules = ["android_sdk"],
default = configuration_field(
fragment = "android",
name = "android_sdk_label",
),
providers = [AndroidSdkInfo],
),
_dummy_android_sdk = attr.label(
default = "//toolchains/android_sdk:dummy_sdk_tools",
),
Expand Down
4 changes: 3 additions & 1 deletion rules/utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ load(":providers.bzl", "FailureInfo")

ANDROID_TOOLCHAIN_TYPE = Label("//toolchains/android:toolchain_type")

ANDROID_SDK_TOOLCHAIN_TYPE = Label("//toolchains/android_sdk:toolchain_type")

_CUU = "\033[A"
_EL = "\033[K"
_DEFAULT = "\033[0m"
Expand Down Expand Up @@ -420,7 +422,7 @@ def get_android_toolchain(ctx):

def get_android_sdk(ctx):
if hasattr(ctx.fragments.android, "incompatible_use_toolchain_resolution") and ctx.fragments.android.incompatible_use_toolchain_resolution:
return ctx.toolchains["//toolchains/android_sdk:toolchain_type"].android_sdk_info
return ctx.toolchains[ANDROID_SDK_TOOLCHAIN_TYPE].android_sdk_info
else:
return ctx.attr._android_sdk[AndroidSdkInfo]

Expand Down
Loading