From fe3f5e5c3988caafef14eb51136c4b8bd9fffc88 Mon Sep 17 00:00:00 2001 From: Alex Humesky Date: Sun, 28 Feb 2021 21:57:13 -0500 Subject: [PATCH] Enable maven_install to use the Starlark version of aar_import to facilitate 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 --- WORKSPACE | 40 ++++++++++++++++++++++++++++++ coursier.bzl | 22 ++++++++++++++++ defs.bzl | 15 +++++++++-- private/dependency_tree_parser.bzl | 8 +++++- private/rules/jetifier.bzl | 7 ++++-- tests/unit/aar_import/BUILD | 15 +++++++++++ 6 files changed, 102 insertions(+), 5 deletions(-) create mode 100644 tests/unit/aar_import/BUILD diff --git a/WORKSPACE b/WORKSPACE index 37b0c3203..0a341d5dd 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -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", diff --git a/coursier.bzl b/coursier.bzl index b52068781..8c14e3183 100644 --- a/coursier.bzl +++ b/coursier.bzl @@ -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", @@ -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): @@ -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, ) @@ -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, ) @@ -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, ) @@ -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", diff --git a/defs.bzl b/defs.bzl index 3c15ef35c..d34493973 100644 --- a/defs.bzl +++ b/defs.bzl @@ -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") @@ -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 @@ -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): @@ -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: diff --git a/private/dependency_tree_parser.bzl b/private/dependency_tree_parser.bzl index 06e259567..9e8bbfb93 100644 --- a/private/dependency_tree_parser.bzl +++ b/private/dependency_tree_parser.bzl @@ -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 + "("] @@ -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. # diff --git a/private/rules/jetifier.bzl b/private/rules/jetifier.bzl index 6bb2794c5..4656ce275 100644 --- a/private/rules/jetifier.bzl +++ b/private/rules/jetifier.bzl @@ -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 diff --git a/tests/unit/aar_import/BUILD b/tests/unit/aar_import/BUILD new file mode 100644 index 000000000..eee520310 --- /dev/null +++ b/tests/unit/aar_import/BUILD @@ -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", + ], +)