Skip to content

Commit d06c617

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Fix maven apk source collection.
Use an aspect to ensure collected source is only from axt. PiperOrigin-RevId: 542923868
1 parent 0950bf0 commit d06c617

File tree

6 files changed

+83
-31
lines changed

6 files changed

+83
-31
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ on:
99
jobs:
1010
build:
1111
runs-on: ubuntu-latest
12-
timeout-minutes: 15
12+
timeout-minutes: 20
1313
steps:
1414
- name: Check out repository code
1515
uses: actions/checkout@v3

build_extensions/maven/axt_maven_apk.bzl

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,20 @@
11
"""Generate AXT android archive (aar)."""
22

33
load("//build_extensions/maven:combine_jars.bzl", "combine_jars")
4-
load("//build_extensions/maven:maven_info.bzl", "MavenFilesInfo", "MavenInfo")
4+
load("//build_extensions/maven:maven_info.bzl", "MavenFilesInfo", "MavenInfo", "collect_maven_apk_info")
55

66
def _axt_maven_apk_impl(ctx):
77
# produce src jar
8-
# hack - exclude source jars from external maven artifacts
9-
# TODO(b/283992063): use an aspect to gather this info
10-
axt_jars = [jar for jar in ctx.attr.included_dep[JavaInfo].transitive_source_jars.to_list() if "maven.org" not in jar.path]
118
combine_jars(
129
ctx = ctx,
13-
input_jars_deps = axt_jars,
10+
input_jars_deps = ctx.attr.included_dep[MavenInfo].transitive_included_src_jars,
1411
output = ctx.outputs.src_jar,
1512
)
1613

17-
maven_info = MavenInfo(
18-
artifact = ctx.attr.artifact,
19-
is_compileOnly = False,
20-
is_shaded = False,
21-
transitive_included_runtime_jars = depset(),
22-
transitive_included_src_jars = depset(),
23-
transitive_maven_direct_deps = depset(ctx.attr.maven_deps),
24-
)
25-
26-
return [maven_info, MavenFilesInfo(runtime = ctx.attr.included_dep[ApkInfo].signed_apk, src_jar = ctx.outputs.src_jar, validation = None)]
14+
return [
15+
ctx.attr.included_dep[MavenInfo],
16+
MavenFilesInfo(runtime = ctx.attr.included_dep[ApkInfo].signed_apk, src_jar = ctx.outputs.src_jar, validation = None),
17+
]
2718

2819
axt_maven_apk = rule(
2920
implementation = _axt_maven_apk_impl,
@@ -32,13 +23,7 @@ axt_maven_apk = rule(
3223
doc = "The android_binary to publish",
3324
mandatory = True,
3425
providers = [JavaInfo, ApkInfo],
35-
),
36-
"artifact": attr.string(
37-
doc = "the maven coordinates of the apk",
38-
mandatory = True,
39-
),
40-
"maven_deps": attr.string_list(
41-
doc = "the maven coordinates of the runtime dependencies of the apk",
26+
aspects = [collect_maven_apk_info],
4227
),
4328
"_combine_jars_java": attr.label(
4429
executable = True,

build_extensions/maven/maven_info.bzl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
load("//build_extensions/maven:kotlin_info.bzl", "is_kotlin")
1919
load("//build_extensions:axt_versions.bzl", "KOTLIN_VERSION")
20-
load("//build_extensions/maven:maven_registry.bzl", "get_artifact_from_label", "is_shaded_from_label")
20+
load("//build_extensions/maven:maven_registry.bzl", "get_artifact_from_label", "get_maven_apk_deps", "is_axt_label", "is_shaded_from_label")
2121

2222
# logic here largely inspired from https://github.com/google/dagger/blob/master/tools/maven_info.bzl
2323

@@ -163,3 +163,47 @@ collect_maven_info = aspect(
163163
""",
164164
implementation = _collect_maven_info_impl,
165165
)
166+
167+
def _collect_maven_apk_info_impl(target, ctx):
168+
neverlink = getattr(ctx.rule.attr, "neverlink", False)
169+
deps = getattr(ctx.rule.attr, "deps", [])
170+
exports = getattr(ctx.rule.attr, "exports", [])
171+
172+
# ignore non-runtime or non-java dependencies eg proto_library or non-axt-labels
173+
if neverlink or JavaInfo not in target or not is_axt_label(target.label):
174+
return MavenInfo(
175+
artifact = None,
176+
is_compileOnly = True,
177+
is_shaded = False,
178+
transitive_included_runtime_jars = depset(),
179+
transitive_included_src_jars = depset(),
180+
transitive_maven_direct_deps = depset(),
181+
)
182+
183+
artifact = get_artifact_from_label(target.label)
184+
included_src_jars = target[JavaInfo].source_jars
185+
transitive_included_src_jars = []
186+
187+
for dep in (deps + exports):
188+
transitive_included_src_jars.append(dep[MavenInfo].transitive_included_src_jars)
189+
190+
maven_deps = get_maven_apk_deps(artifact)
191+
return [MavenInfo(
192+
artifact = artifact,
193+
is_compileOnly = False,
194+
is_shaded = False,
195+
transitive_included_runtime_jars = depset(),
196+
transitive_included_src_jars = depset(included_src_jars, transitive = transitive_included_src_jars),
197+
transitive_maven_direct_deps = depset(maven_deps),
198+
)]
199+
200+
collect_maven_apk_info = aspect(
201+
attr_aspects = [
202+
"deps",
203+
"exports",
204+
],
205+
doc = """
206+
Collects the Maven apk information for targets, their dependencies, and their transitive exports.
207+
""",
208+
implementation = _collect_maven_apk_info_impl,
209+
)

build_extensions/maven/maven_registry.bzl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ _TARGET_TO_MAVEN_ARTIFACT = {
4545

4646
# services/events/java gets built into both androidx.test.runner as well as orchestrator v2
4747
"//services/events/java/": "androidx.test:runner:%s" % RUNNER_VERSION,
48+
"//services:test_services": "androidx.test.services:test-services:%s" % SERVICES_VERSION,
49+
"//runner/android_test_orchestrator/stubapp:stubapp": "androidx.test:orchestrator:%s" % ORCHESTRATOR_VERSION,
4850
}
4951

5052
_SHADED_TARGETS = [
@@ -74,6 +76,34 @@ def get_artifact_from_label(label):
7476

7577
return result
7678

79+
def is_axt_label(label):
80+
"""Determine if given target label is from androidx_test.
81+
82+
Args:
83+
label: the target label
84+
85+
Returns:
86+
True if the label is in a recognized androidx_test maven artifact
87+
"""
88+
label_string = str(label)
89+
for path in _TARGET_TO_MAVEN_ARTIFACT.keys():
90+
if path in label_string:
91+
return True
92+
93+
# special case the apk source dirs
94+
if "//services" in label_string:
95+
return True
96+
if "//runner/android_test_orchestrator" in label_string:
97+
return True
98+
return False
99+
100+
def get_maven_apk_deps(artifact):
101+
# TODO: don't hardcode this, instead try to obtain from build rule
102+
if artifact == ORCHESTRATOR_ARTIFACT:
103+
return [SERVICES_APK_ARTIFACT]
104+
else:
105+
return []
106+
77107
def is_shaded_from_label(label):
78108
"""Returns true if given target should be shaded.
79109

runner/android_test_orchestrator/stubapp/BUILD

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
22
load("//build_extensions/maven:maven_artifact.bzl", "maven_artifact")
33
load("//build_extensions/maven:axt_maven_apk.bzl", "axt_maven_apk")
4-
load("//build_extensions:axt_versions.bzl", "ORCHESTRATOR_ARTIFACT", "SERVICES_APK_ARTIFACT")
54

65
licenses(["notice"])
76

@@ -33,11 +32,7 @@ android_binary(
3332

3433
axt_maven_apk(
3534
name = "orchestrator_release_apk",
36-
artifact = ORCHESTRATOR_ARTIFACT,
3735
included_dep = ":stubapp",
38-
maven_deps = [
39-
SERVICES_APK_ARTIFACT,
40-
],
4136
)
4237

4338
maven_artifact(

services/BUILD

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
load("@rules_license//rules:license.bzl", "license")
22
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")
33
load("//build_extensions/maven:maven_artifact.bzl", "maven_artifact")
4-
load("//build_extensions:axt_versions.bzl", "SERVICES_APK_ARTIFACT")
54
load("//build_extensions/maven:axt_maven_apk.bzl", "axt_maven_apk")
65

76
# Description:
@@ -45,7 +44,6 @@ android_binary(
4544

4645
axt_maven_apk(
4746
name = "test_services_release_apk",
48-
artifact = SERVICES_APK_ARTIFACT,
4947
included_dep = ":test_services",
5048
)
5149

0 commit comments

Comments
 (0)