Skip to content

Commit

Permalink
Enable maven_install to use the Starlark version of aar_import to fac…
Browse files Browse the repository at this point in the history
…ilitate migrating to the Starlark Android rules (#526)

* Add starlark_aar_import and aar_import_bzl_label attributes to maven_install to enable switching to the Starlark version of aar_import from rules_android

* Fix unmerged code

* Rename starlark_aar_import to use_starlark_android_rules

* Support jetifier + starlark aar_import. Validate the passed build label
  • Loading branch information
ahumesky authored Mar 1, 2021
1 parent d0104e3 commit fe3f5e5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
40 changes: 40 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,46 @@ maven_install(
],
)

maven_install(
name = "starlark_aar_import_test",
artifacts = [
"com.android.support:appcompat-v7:28.0.0",
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
],
use_starlark_android_rules = True,
# Not actually necessary since this is the default value, but useful for
# testing.
aar_import_bzl_label = "@build_bazel_rules_android//android:rules.bzl",
)

maven_install(
name = "starlark_aar_import_with_jetify_test",
artifacts = [
"com.android.support:appcompat-v7:28.0.0",
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
],
use_starlark_android_rules = True,
# Not actually necessary since this is the default value, but useful for
# testing.
aar_import_bzl_label = "@build_bazel_rules_android//android:rules.bzl",
jetify = True,
)

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

# https://github.com/bazelbuild/rules_jvm_external/issues/351
maven_install(
name = "json_artifacts_testing",
Expand Down
22 changes: 22 additions & 0 deletions coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,18 @@ package(default_visibility = ["//visibility:{visibility}"])
load("@rules_jvm_external//private/rules:jvm_import.bzl", "jvm_import")
load("@rules_jvm_external//private/rules:jetifier.bzl", "jetify_aar_import", "jetify_jvm_import")
{aar_import_statement}
{imports}
"""

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

_AAR_IMPORT_STATEMENT = """\
load("%s", "aar_import")
"""


_BUILD_PIN = """
genrule(
name = "jq-binary",
Expand Down Expand Up @@ -119,6 +127,14 @@ def _relativize_and_symlink_file(repository_ctx, absolute_path):
repository_ctx.symlink(absolute_path, repository_ctx.path(artifact_relative_path))
return artifact_relative_path

def _get_aar_import_statement_or_empty_str(repository_ctx):
if repository_ctx.attr.use_starlark_android_rules:
# 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
else:
return ""

# Generate the base `coursier` command depending on the OS, JAVA_HOME or the
# location of `java`.
def _generate_java_jar_command(repository_ctx, jar_path):
Expand Down Expand Up @@ -489,6 +505,7 @@ def _pinned_coursier_fetch_impl(repository_ctx):
visibility = "private" if repository_ctx.attr.strict_visibility else "public",
repository_name = repository_ctx.name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
),
executable = False,
)
Expand Down Expand Up @@ -957,6 +974,7 @@ def _coursier_fetch_impl(repository_ctx):
visibility = "private" if repository_ctx.attr.strict_visibility else "public",
repository_name = repository_name,
imports = generated_imports,
aar_import_statement = _get_aar_import_statement_or_empty_str(repository_ctx),
),
executable = False,
)
Expand Down Expand Up @@ -1057,6 +1075,8 @@ pinned_coursier_fetch = repository_rule(
"jetify_include_list": attr.string_list(doc = "List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.", default = JETIFY_INCLUDE_LIST_JETIFY_ALL),
"additional_netrc_lines": attr.string_list(doc = "Additional lines prepended to the netrc file used by `http_file` (with `maven_install_json` only).", default = []),
"fail_if_repin_required": attr.bool(doc = "Whether to fail the build if the maven_artifact inputs have changed but the lock file has not been repinned.", default = False),
"use_starlark_android_rules": attr.bool(default = False, doc = "Whether to use the native or Starlark version of the Android rules."),
"aar_import_bzl_label": attr.string(default = DEFAULT_AAR_IMPORT_LABEL, doc = "The label (as a string) to use to import aar_import from"),
},
implementation = _pinned_coursier_fetch_impl,
)
Expand Down Expand Up @@ -1100,6 +1120,8 @@ coursier_fetch = repository_rule(
"resolve_timeout": attr.int(default = 600),
"jetify": attr.bool(doc = "Runs the AndroidX [Jetifier](https://developer.android.com/studio/command-line/jetifier) tool on artifacts specified in jetify_include_list. If jetify_include_list is not specified, run Jetifier on all artifacts.", default = False),
"jetify_include_list": attr.string_list(doc = "List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.", default = JETIFY_INCLUDE_LIST_JETIFY_ALL),
"use_starlark_android_rules": attr.bool(default = False, doc = "Whether to use the native or Starlark version of the Android rules."),
"aar_import_bzl_label": attr.string(default = DEFAULT_AAR_IMPORT_LABEL, doc = "The label (as a string) to use to import aar_import from"),
},
environ = [
"JAVA_HOME",
Expand Down
15 changes: 13 additions & 2 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load(":coursier.bzl", "coursier_fetch", "pinned_coursier_fetch")
load(":coursier.bzl", "coursier_fetch", "pinned_coursier_fetch", "DEFAULT_AAR_IMPORT_LABEL")
load(":specs.bzl", "json", "parse")
load("//:private/dependency_tree_parser.bzl", "JETIFY_INCLUDE_LIST_JETIFY_ALL")
load("//private/rules:java_export.bzl", _java_export = "java_export")
Expand Down Expand Up @@ -40,7 +40,9 @@ def maven_install(
jetify = False,
jetify_include_list = JETIFY_INCLUDE_LIST_JETIFY_ALL,
additional_netrc_lines = [],
fail_if_repin_required = False):
fail_if_repin_required = False,
use_starlark_android_rules = False,
aar_import_bzl_label = DEFAULT_AAR_IMPORT_LABEL):
"""Resolves and fetches artifacts transitively from Maven repositories.
This macro runs a repository rule that invokes the Coursier CLI to resolve
Expand Down Expand Up @@ -77,6 +79,13 @@ def maven_install(
jetify_include_list: List of artifacts that need to be jetified in `groupId:artifactId` format. By default all artifacts are jetified if `jetify` is set to True.
additional_netrc_lines: Additional lines prepended to the netrc file used by `http_file` (with `maven_install_json` only).
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.
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".
"""
repositories_json_strings = []
for repository in parse.parse_repository_spec_list(repositories):
Expand Down Expand Up @@ -125,6 +134,8 @@ def maven_install(
resolve_timeout = resolve_timeout,
jetify = jetify,
jetify_include_list = jetify_include_list,
use_starlark_android_rules = use_starlark_android_rules,
aar_import_bzl_label = aar_import_bzl_label,
)

if maven_install_json != None:
Expand Down
8 changes: 7 additions & 1 deletion private/dependency_tree_parser.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
import_rule = "aar_import"
else:
fail("Unsupported packaging type: " + packaging)
if jetify_all or (repository_ctx.attr.jetify and simple_coord in jetify_include_dict):
jetify = jetify_all or (repository_ctx.attr.jetify and simple_coord in jetify_include_dict)
if jetify:
import_rule = "jetify_" + import_rule
target_import_string = [import_rule + "("]

Expand All @@ -167,6 +168,11 @@ def _generate_imports(repository_ctx, dep_tree, explicit_artifacts, neverlink_ar
target_import_string.append("\tsrcjar = \"%s\"," % srcjar_paths[target_label])
elif packaging == "aar":
target_import_string.append("\taar = \"%s\"," % artifact_path)
if jetify and repository_ctx.attr.use_starlark_android_rules:
# Because jetifier.bzl cannot conditionally import the starlark rules
# (it's not a generated file), inject the aar_import rule from
# the load statement in the generated file.
target_import_string.append("\t_aar_import = aar_import,")

# 4. Generate the deps attribute with references to other target labels.
#
Expand Down
7 changes: 5 additions & 2 deletions private/rules/jetifier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ jetify = rule(
implementation = _jetify_impl,
)

def jetify_aar_import(name, aar, **kwargs):
def jetify_aar_import(name, aar, _aar_import=None, **kwargs):
jetify(
name = "jetified_" + name,
srcs = [aar],
)

native.aar_import(
if not _aar_import:
_aar_import = native.aar_import

_aar_import(
name = name,
aar = ":jetified_" + name,
**kwargs
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/aar_import/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")

build_test(
name = "starlark_aar_import_test",
targets = [
"@starlark_aar_import_test//:com_android_support_appcompat_v7_28_0_0",
],
)

build_test(
name = "starlark_aar_import_with_jetify_test",
targets = [
"@starlark_aar_import_with_jetify_test//:com_android_support_appcompat_v7_28_0_0",
],
)

0 comments on commit fe3f5e5

Please sign in to comment.