Skip to content

Commit

Permalink
Update rules_jvm_external to use the Starlark version of aar_import a…
Browse files Browse the repository at this point in the history
…utomatically

if the version of Bazel used does not contain the native version of aar_import.

This also updates the version of rules_android to the latest commit, because
version 0.1.1 is basically just wrappers around the native versions of the rules.

This also disbles `--experimental_sibling_repository_layout` added in
bazel-contrib@b6631f9
because the latest rules_android uses protos in go, and there is a problem compiling protos in go
with `--experimental_sibling_repository_layout`: bazel-contrib/rules_go#3947

Fixes bazel-contrib#1139.
  • Loading branch information
ahumesky committed May 23, 2024
1 parent 71e08c8 commit dc493e4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 41 deletions.
31 changes: 1 addition & 30 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
build --java_language_version=11
build --java_runtime_version=remotejdk_11

build --tool_java_language_version=11
build --tool_java_runtime_version=remotejdk_11

build --experimental_strict_java_deps=strict
build --explicit_java_test_deps

build --experimental_sibling_repository_layout

# Make sure we get something helpful when tests fail
test --verbose_failures
test --test_output=errors

# Every JVM creates a temporary performance instrumentation file in
# /tmp/hsperfdata_$USERNAME/$PID. When we use sandboxing, we use PID
# namespaces, which means that the PIDs are virtualized and all
# running JVMs believe they are PID 2.
#
# This means that they all open/ftruncate/mmap the same file and that
# gives you SIGBUS eventually, because "It tries to read an address
# that no longer exists from an mmap'd file".
#
# https://github.com/bazelbuild/bazel/issues/3236#issuecomment-310776024

build --sandbox_tmpfs_path=/tmp

# Allows the examples to extend the default bazelrc
try-import %workspace%/.bazelrc.example
common --experimental_google_legacy_api
6 changes: 6 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ bazel_dep(

# Remove this once rules_android has rolled out official Bzlmod support
remote_android_extensions = use_extension("@bazel_tools//tools/android:android_extensions.bzl", "remote_android_tools_extensions")
# Remove this once rules_android has an updated BCR release.
git_override(
module_name = "rules_android",
remote = "https://github.com/bazelbuild/rules_android",
commit = "93e27030d3f0defa39cbbc35195638cb772b0c27",
)

use_repo(remote_android_extensions, "android_gmaven_r8", "android_tools")

Expand Down
13 changes: 7 additions & 6 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ maven_install(
name = "starlark_aar_import_with_sources_test",
# Not actually necessary since this is the default value, but useful for
# testing.
aar_import_bzl_label = "@build_bazel_rules_android//android:rules.bzl",
aar_import_bzl_label = "@rules_android//rules:rules.bzl",
artifacts = [
"androidx.work:work-runtime:2.6.0",
],
Expand All @@ -524,7 +524,7 @@ maven_install(
name = "starlark_aar_import_test",
# Not actually necessary since this is the default value, but useful for
# testing.
aar_import_bzl_label = "@build_bazel_rules_android//android:rules.bzl",
aar_import_bzl_label = "@rules_android//rules:rules.bzl",
artifacts = [
"com.android.support:appcompat-v7:28.0.0",
],
Expand All @@ -538,11 +538,12 @@ maven_install(

# for the above "starlark_aar_import_test" maven_install with
# use_starlark_android_rules = True
RULES_ANDROID_COMMIT = "93e27030d3f0defa39cbbc35195638cb772b0c27"
http_archive(
name = "build_bazel_rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
name = "rules_android",
sha256 = "71cae2413868a24f17d43fd595af6f3905d2e5b3235f76514f54800bfd90c903",
strip_prefix = "rules_android-" + RULES_ANDROID_COMMIT,
urls = ["https://github.com/bazelbuild/rules_android/archive/%s.zip" % RULES_ANDROID_COMMIT],
)

# https://github.com/bazelbuild/rules_jvm_external/issues/351
Expand Down
6 changes: 4 additions & 2 deletions private/rules/coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bzl_library(
)
"""

DEFAULT_AAR_IMPORT_LABEL = "@build_bazel_rules_android//android:rules.bzl"
DEFAULT_AAR_IMPORT_LABEL = "@rules_android//rules:rules.bzl"

_AAR_IMPORT_STATEMENT = """\
load("%s", "aar_import")
Expand Down Expand Up @@ -242,7 +242,9 @@ def _relativize_and_symlink_file_in_maven_local(repository_ctx, absolute_path):
return artifact_relative_path

def _get_aar_import_statement_or_empty_str(repository_ctx):
if repository_ctx.attr.use_starlark_android_rules:
# Use the Starlark version of aar_import is requested, or if this version of Bazel
# does not have native aar_import.
if repository_ctx.attr.use_starlark_android_rules or getattr(native, "aar_import", None) == None:
# parse the label to validate it
_ = Label(repository_ctx.attr.aar_import_bzl_label)
return _AAR_IMPORT_STATEMENT % repository_ctx.attr.aar_import_bzl_label
Expand Down
12 changes: 9 additions & 3 deletions private/rules/maven_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ def maven_install(
use_credentials_from_home_netrc_file: Whether to pass machine login credentials from the ~/.netrc file to coursier.
fail_if_repin_required: Whether to fail the build if the required maven artifacts have been changed but not repinned. Requires the `maven_install_json` to have been set.
use_starlark_android_rules: Whether to use the native or Starlark version
of the Android rules. Default is False.
of the Android rules. Default is False if the running version of Bazel supports native aar_import.
If the running version of Bazel does not support native aar_import, this parameter is ignored and the
Starlark Android rules is used.
aar_import_bzl_label: The label (as a string) to use to import aar_import
from. This is usually needed only if the top-level workspace file does
not use the typical default repository name to import the Android
Starlark rules. Default is
"@build_bazel_rules_android//rules:rules.bzl".
Starlark rules. Default is "@rules_android//rules:rules.bzl".
duplicate_version_warning: What to do if an artifact is specified multiple times. If "error" then
fail the build, if "warn" then print a message and continue, if "none" then do nothing. The default
is "warn".
Expand Down Expand Up @@ -106,6 +107,11 @@ def maven_install(
if additional_netrc_lines and maven_install_json == None:
fail("`additional_netrc_lines` is only supported with `maven_install_json` specified", "additional_netrc_lines")

if getattr(native, "aar_import", None) == None:
# If this version of bazel does not have the native version of
# aar_import, then the Starlark version of aar_import must be used.
use_starlark_android_rules = True

# The first coursier_fetch generates the @unpinned_maven
# repository, which executes Coursier.
#
Expand Down
3 changes: 3 additions & 0 deletions tests/bazel_run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ TESTS=(
)

function run_tests() {
# Remove once https://github.com/bazelbuild/rules_android/issues/219 is fixed
echo "common --experimental_google_legacy_api" > .bazelrc

printf "Running bazel run tests:\n"
for test in ${TESTS[@]}; do
printf " ${test} "
Expand Down

0 comments on commit dc493e4

Please sign in to comment.