diff options
author | Jingwen Chen <jingwen@google.com> | 2018-06-08 13:09:31 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-06-08 13:10:44 -0700 |
commit | c34d6ee17570afc6e8b3942ab170d70758251bfd (patch) | |
tree | 2d007328e8b45f57b1a5c9ce44cea56eb6a17bec /tools | |
parent | 45b308a62f42c2c0bcfe79dcd4046c4025a31059 (diff) |
Add support for aar_import_external and aar_maven_import_external
Usage example:
```python
# In WORKSPACE
load("@bazel_tools//tools/build_defs/repo:android.bzl", "aar_import_external", "aar_maven_import_external")
# Specify the URL directly:
aar_import_external(
name = "com_android_support_preference_v14_25_1_0", # required
licenses = ["notice"], # required
aar_urls = [ # required
"https://dl.google.com/dl/android/maven2/com/android/support/preference-v14/25.1.0/preference-v14-25.1.0.aar"
],
aar_sha256 = "442473fe5c395ebef26c14eb01d17ceda33ad207a4cc23a32a2ad95b87edfabb", # optional or empty string
deps = [ # optional or empty list
"@com_android_support_recyclerview_v7_25_1_0//aar",
"@com_android_support_appcompat_v7_25_1_0//aar",
"@com_android_support_preference_v7_25_1_0//aar",
"@com_android_support_support_v4_25_1_0//aar",
],
)
# Or, specify the artifact coordinate:
aar_maven_import_external(
name = "com_android_support_preference_v14_25_1_0", # required
artifact = "com.android.support.test:preference-v14:25.1.0", # required
sha256 = "442473fe5c395ebef26c14eb01d17ceda33ad207a4cc23a32a2ad95b87edfabb" # optional or empty string
licenses = ["notice"], # required
server_urls = ["https://maven.google.com"], # required
deps = [ # optional or empty list
"@com_android_support_recyclerview_v7_25_1_0//aar",
"@com_android_support_appcompat_v7_25_1_0//aar",
"@com_android_support_preference_v7_25_1_0//aar",
"@com_android_support_support_v4_25_1_0//aar",
],
)
# In BUILD.bazel
android_library(
name = "foo",
srcs = [...],
deps = [
"@com_android_support_preference_v14_25_1_0//aar",
],
)
```
To test this out with gmaven_rules, change the `load` statement in https://github.com/bazelbuild/gmaven_rules/blob/master/gmaven.bzl to
```
load('@bazel_tools//tools/build_defs/repo:android.bzl', 'aar_import_external')
load('@bazel_tools//tools/build_defs/repo:java.bzl', 'java_import_external')
```
Fixes https://github.com/bazelbuild/bazel/issues/4654
RELNOTES: New rules for importing Android dependencies: `aar_import_external` and `aar_maven_import_external`. `aar_import_external` enables specifying external AAR dependencies using a list of HTTP URLs for the artifact. `aar_maven_import_external` enables specifying external AAR dependencies using the artifact coordinate and a list of server URLs.
Closes #5319.
Change-Id: I9517e68ab78f2e30fb6ceabfe3b35061c585d607
PiperOrigin-RevId: 199839047
Diffstat (limited to 'tools')
-rw-r--r-- | tools/build_defs/repo/android.bzl | 85 | ||||
-rw-r--r-- | tools/build_defs/repo/java.bzl | 5 | ||||
-rw-r--r-- | tools/build_defs/repo/jvm.bzl | 84 |
3 files changed, 142 insertions, 32 deletions
diff --git a/tools/build_defs/repo/android.bzl b/tools/build_defs/repo/android.bzl new file mode 100644 index 0000000000..1e0e9caf0a --- /dev/null +++ b/tools/build_defs/repo/android.bzl @@ -0,0 +1,85 @@ +# Copyright 2018 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Rules for importing external Android Archives (AARs). + +Usage: + + # In WORKSPACE + load("@bazel_tools//tools/build_defs/repo:android.bzl", "aar_import_external", "aar_maven_import_external") + + # Specify the URL directly: + aar_import_external( + name = "com_android_support_preference_v14_25_1_0", # required + licenses = ["notice"], # required + aar_urls = [ # required + "https://dl.google.com/dl/android/maven2/com/android/support/preference-v14/25.1.0/preference-v14-25.1.0.aar" + ], + aar_sha256 = "442473fe5c395ebef26c14eb01d17ceda33ad207a4cc23a32a2ad95b87edfabb", # optional or empty string + deps = [ # optional or empty list + "@com_android_support_recyclerview_v7_25_1_0//aar", + "@com_android_support_appcompat_v7_25_1_0//aar", + "@com_android_support_preference_v7_25_1_0//aar", + "@com_android_support_support_v4_25_1_0//aar", + ], + ) + + # Or, specify the artifact coordinate: + aar_maven_import_external( + name = "com_android_support_preference_v14_25_1_0", # required + artifact = "com.android.support.test:preference-v14:25.1.0", # required + sha256 = "442473fe5c395ebef26c14eb01d17ceda33ad207a4cc23a32a2ad95b87edfabb" # optional or empty string + licenses = ["notice"], # required + server_urls = ["https://maven.google.com"], # required + deps = [ # optional or empty list + "@com_android_support_recyclerview_v7_25_1_0//aar", + "@com_android_support_appcompat_v7_25_1_0//aar", + "@com_android_support_preference_v7_25_1_0//aar", + "@com_android_support_support_v4_25_1_0//aar", + ], + ) + + # In BUILD.bazel + android_library( + name = "foo", + srcs = [...], + deps = [ + "@com_android_support_preference_v14_25_1_0//aar", + ], + ) +""" + +load(":jvm.bzl", "convert_artifact_coordinate_to_urls", "jvm_import_external") + +def aar_import_external(aar_sha256, aar_urls, **kwargs): + jvm_import_external( + rule_name = "aar_import", + rule_metadata = { + "extension": "aar", + "import_attr": "aar = %s", + }, + artifact_sha256 = aar_sha256, + artifact_urls = aar_urls, + **kwargs + ) + +def aar_maven_import_external(artifact, server_urls, aar_sha256 = "", **kwargs): + aar_import_external( + aar_sha256 = aar_sha256, + aar_urls = convert_artifact_coordinate_to_urls( + artifact, + server_urls, + "aar", + ), + **kwargs + ) diff --git a/tools/build_defs/repo/java.bzl b/tools/build_defs/repo/java.bzl index 1e40ba4a9d..4e6ce5f03c 100644 --- a/tools/build_defs/repo/java.bzl +++ b/tools/build_defs/repo/java.bzl @@ -171,9 +171,10 @@ reasonably expected to already be provided. load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_import_external") -def java_import_external(jar_sha256, **kwargs): +def java_import_external(jar_sha256, jar_urls, **kwargs): jvm_import_external( rule_name = "java_import", - jar_sha256 = jar_sha256, + artifact_sha256 = jar_sha256, + artifact_urls = jar_urls, **kwargs ) diff --git a/tools/build_defs/repo/jvm.bzl b/tools/build_defs/repo/jvm.bzl index b0acd2417f..1538ea1b7e 100644 --- a/tools/build_defs/repo/jvm.bzl +++ b/tools/build_defs/repo/jvm.bzl @@ -33,6 +33,7 @@ the following macros are defined below that utilize jvm_import_external: """ _HEADER = "# DO NOT EDIT: generated by jvm_import_external()" + _PASS_PROPS = ( "neverlink", "testonly_", @@ -49,18 +50,20 @@ def _jvm_import_external(repository_ctx): not repository_ctx.attr.neverlink): fail("Only use generated_linkable_rule_name if neverlink is set") name = repository_ctx.attr.generated_rule_name or repository_ctx.name - urls = repository_ctx.attr.jar_urls - sha = repository_ctx.attr.jar_sha256 - path = repository_ctx.name + ".jar" + urls = repository_ctx.attr.artifact_urls + sha = repository_ctx.attr.artifact_sha256 + extension = repository_ctx.attr.rule_metadata["extension"] + file_extension = "." + extension + path = repository_ctx.name + file_extension for url in urls: - if url.endswith(".jar"): + if url.endswith(file_extension): path = url[url.rindex("/") + 1:] break srcurls = repository_ctx.attr.srcjar_urls srcsha = repository_ctx.attr.srcjar_sha256 srcpath = repository_ctx.name + "-src.jar" if srcurls else "" for url in srcurls: - if url.endswith(".jar"): + if url.endswith(file_extension): srcpath = url[url.rindex("/") + 1:].replace("-sources.jar", "-src.jar") break lines = [_HEADER, ""] @@ -75,24 +78,26 @@ def _jvm_import_external(repository_ctx): lines.append("licenses(%s)" % repr(repository_ctx.attr.licenses)) lines.append("") lines.extend(_serialize_given_rule_import( - repository_ctx.attr.rule_name, - name, - path, - srcpath, - repository_ctx.attr, - _PASS_PROPS, - repository_ctx.attr.additional_rule_attrs, + name = name, + additional_rule_attrs = repository_ctx.attr.additional_rule_attrs, + attrs = repository_ctx.attr, + import_attr = repository_ctx.attr.rule_metadata["import_attr"], + path = path, + props = _PASS_PROPS, + rule_name = repository_ctx.attr.rule_name, + srcpath = srcpath, )) if (repository_ctx.attr.neverlink and repository_ctx.attr.generated_linkable_rule_name): lines.extend(_serialize_given_rule_import( - repository_ctx.attr.rule_name, - repository_ctx.attr.generated_linkable_rule_name, - path, - srcpath, - repository_ctx.attr, - [p for p in _PASS_PROPS if p != "neverlink"], - repository_ctx.attr.additional_rule_attrs, + name = repository_ctx.attr.generated_linkable_rule_name, + additonal_rule_attrs = repository_ctx.attr.additional_rule_attrs, + attrs = repository_ctx.attr, + import_attr = repository_ctx.attr.rule_metadata["import_attr"], + path = path, + props = [p for p in _PASS_PROPS if p != "neverlink"], + rule_name = repository_ctx.attr.rule_name, + srcpath = srcpath, )) extra = repository_ctx.attr.extra_build_file_content if extra: @@ -103,7 +108,7 @@ def _jvm_import_external(repository_ctx): if srcurls: repository_ctx.download(srcurls, srcpath, srcsha) repository_ctx.file("BUILD", "\n".join(lines)) - repository_ctx.file("jar/BUILD", "\n".join([ + repository_ctx.file("%s/BUILD" % extension, "\n".join([ _HEADER, "", "package(default_visibility = %r)" % ( @@ -112,18 +117,20 @@ def _jvm_import_external(repository_ctx): ), "", "alias(", - " name = \"jar\",", + " name = \"%s\"," % extension, " actual = \"@%s\"," % repository_ctx.name, ")", "", ])) -def _convert_to_url(artifact, server_urls): +# This method is public for usage in android.bzl macros +def convert_artifact_coordinate_to_urls(artifact, server_urls, packaging): + """This function converts a Maven artifact coordinate into URLs.""" + parts = artifact.split(":") group_id_part = parts[0].replace(".", "/") artifact_id = parts[1] version = parts[2] - packaging = "jar" classifier_part = "" if len(parts) == 4: packaging = parts[2] @@ -146,12 +153,13 @@ def _concat_with_needed_slash(server_url, url_suffix): else: return server_url + "/" + url_suffix -def _serialize_given_rule_import(rule_name, name, path, srcpath, attrs, props, additional_rule_attrs): +def _serialize_given_rule_import(rule_name, import_attr, name, path, srcpath, attrs, props, additional_rule_attrs): lines = [ "%s(" % rule_name, " name = %s," % repr(name), - " jars = [%s]," % repr(path), + " " + import_attr % repr(path) + ",", ] + if srcpath: lines.append(" srcjar = %s," % repr(srcpath)) for prop in props: @@ -167,12 +175,23 @@ def _serialize_given_rule_import(rule_name, name, path, srcpath, attrs, props, a return lines jvm_import_external = repository_rule( - implementation = _jvm_import_external, attrs = { "rule_name": attr.string(mandatory = True), - "licenses": attr.string_list(mandatory = True, allow_empty = False), - "jar_urls": attr.string_list(mandatory = True, allow_empty = False), - "jar_sha256": attr.string(), + "licenses": attr.string_list( + mandatory = True, + allow_empty = False, + ), + "artifact_urls": attr.string_list( + mandatory = True, + allow_empty = False, + ), + "artifact_sha256": attr.string(), + "rule_metadata": attr.string_dict( + default = { + "extension": "jar", + "import_attr": "jars = [%s]", + }, + ), "rule_load": attr.string(), "additional_rule_attrs": attr.string_dict(), "srcjar_urls": attr.string_list(), @@ -187,10 +206,15 @@ jvm_import_external = repository_rule( "default_visibility": attr.string_list(default = ["//visibility:public"]), "extra_build_file_content": attr.string(), }, + implementation = _jvm_import_external, ) def jvm_maven_import_external(artifact, server_urls, **kwargs): jvm_import_external( - jar_urls = _convert_to_url(artifact, server_urls), + artifact_urls = convert_artifact_coordinate_to_urls( + artifact, + server_urls, + "jar", + ), **kwargs ) |