Skip to content

Commit bbe5a05

Browse files
committed
[bazel] Re-enable building dist-zips
1 parent e6c7853 commit bbe5a05

File tree

2 files changed

+59
-75
lines changed

2 files changed

+59
-75
lines changed

java/private/common.bzl

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def _has_maven_deps_impl(target, ctx):
2323
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])
2424
all_deps = deps + exports + rt_deps
2525

26-
coordinates = read_coordinates(tags)
2726
if "maven:compile_only" in tags:
2827
return MavenInfo(
2928
coordinates = None,
@@ -43,16 +42,15 @@ def _has_maven_deps_impl(target, ctx):
4342
# it's enough to set set the transitive deps to just be the rule for
4443
# anything that depends upon it. Otherwise, gather them up, and carry on
4544
# as if nothing really mattered.
46-
47-
if len(coordinates) > 0:
48-
transitive_maven_deps = depset(coordinates)
45+
coordinate = read_coordinates(tags)
46+
if coordinate:
47+
transitive_maven_deps = depset([coordinate])
4948
else:
50-
transitive_maven_deps = depset(coordinates, transitive = [info.transitive_maven_deps for info in all_infos])
49+
transitive_maven_deps = depset(transitive = [info.transitive_maven_deps for info in all_infos])
5150
artifact_jars = depset(java_info.runtime_output_jars, transitive = [info.artifact_jars for info in all_infos if not info.coordinates])
5251
source_jars = depset(java_info.source_jars, transitive = [info.source_jars for info in all_infos if not info.coordinates])
5352

5453
infos = []
55-
coordinate = coordinates[0] if len(coordinates) > 0 else None
5654

5755
if JavaModuleInfo in target:
5856
info = MavenInfo(
@@ -123,4 +121,4 @@ def read_coordinates(tags):
123121
coordinates.append(tag[len(MAVEN_PREFIX):])
124122
if len(coordinates) > 1:
125123
fail("Zero or one set of coordinates should be defined: %s" % coordinates)
126-
return coordinates
124+
return coordinates[0] if len(coordinates) else None

java/private/dist_zip.bzl

Lines changed: 54 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,62 @@
1-
load("//java/private:common.bzl", "MAVEN_PREFIX", "MavenInfo", "explode_coordinates", "has_maven_deps")
2-
#load("//java/private:module.bzl", "GatheredJavaModuleInfo", "has_java_module_deps")
1+
load("//java/private:common.bzl", "MavenInfo", "has_maven_deps", "read_coordinates", "explode_coordinates")
2+
load("//java/private:module.bzl", "JavaModuleInfo")
33

44
DistZipInfo = provider(
55
fields = {
66
"dist_infos": "Transitive collection of structs containing base_name, binary_jar, and source_jar",
77
},
88
)
99

10+
_ATTR_ASPECTS = [
11+
"deps",
12+
"exports",
13+
"runtime_deps",
14+
]
15+
16+
def _name(coordinates, default):
17+
if not coordinates:
18+
return default
19+
exploded = explode_coordinates(coordinates)
20+
return exploded[1] + "-" + exploded[2]
21+
1022
def _dist_aspect_impl(target, ctx):
1123
deps = getattr(ctx.rule.attr, "deps", [])
1224
exports = getattr(ctx.rule.attr, "exports", [])
1325
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])
14-
tgt = getattr(ctx.rule.attr, "target", None)
1526

1627
all_deps = deps + exports + rt_deps
17-
if tgt:
18-
all_deps.append(tgt)
28+
transitive_infos = [d[DistZipInfo].dist_infos for d in all_deps]
1929

2030
name = None
21-
tags = getattr(ctx.rule.attr, "tags", [])
22-
for tag in tags:
23-
if tag.startswith(MAVEN_PREFIX):
24-
raw = tag[len(MAVEN_PREFIX):]
25-
coords = explode_coordinates(raw)
26-
if "jar" == coords[3]:
27-
name = "%s-%s" % (coords[1], coords[2])
28-
29-
transitive_infos = [d[DistZipInfo].dist_infos for d in all_deps if DistZipInfo in d]
30-
31-
if not name:
32-
# Return accumulated dist infos
33-
return [
34-
DistZipInfo(
35-
dist_infos = depset([], transitive = transitive_infos),
36-
),
37-
]
38-
39-
source_jars = depset()
40-
binary_jars = depset()
41-
42-
if "maven_artifacts" == ctx.rule.kind:
43-
binary_jars = target[OutputGroupInfo].binjar
44-
source_jars = target[OutputGroupInfo].srcjar
31+
binary_jars = []
32+
source_jars = []
33+
34+
if MavenInfo in target and target[MavenInfo].coordinates:
35+
name = _name(target[MavenInfo].coordinates, None)
36+
binary_jars = target[MavenInfo].artifact_jars
37+
source_jars = target[MavenInfo].source_jars
38+
elif JavaModuleInfo in target and target[JavaModuleInfo].name:
39+
coordinates = read_coordinates(ctx.rule.attr.tags)
40+
name = _name(coordinates, target[JavaModuleInfo].name)
41+
binary_jars = target[JavaInfo].runtime_output_jars
42+
source_jars = target[JavaInfo].source_jars
4543
elif JavaInfo in target:
46-
binary_jars = depset(target[JavaInfo].runtime_output_jars)
47-
# elif GatheredJavaModuleInfo in target:
48-
# binary_jars = depset(target[GatheredJavaModuleInfo].binary_jars)
49-
# source_jars = depset(target[GatheredJavaModuleInfo].source_jars)
50-
51-
binary_jar = None
52-
if len(binary_jars.to_list()) > 1:
53-
fail("Unable to process more than one binary jar")
54-
elif len(binary_jars.to_list()) == 1:
55-
binary_jar = binary_jars.to_list()[0]
56-
source_jar = None
57-
if len(source_jars.to_list()) > 1:
58-
fail("Unable to process more than one source jar")
59-
elif len(source_jars.to_list()) == 1:
60-
source_jar = source_jars.to_list()[0]
44+
coordinates = read_coordinates(ctx.rule.attr.tags)
45+
if coordinates:
46+
name = _name(coordinates, None)
47+
binary_jars = target[JavaInfo].runtime_output_jars
48+
source_jars = target[JavaInfo].source_jars
49+
50+
if len(binary_jars) > 1:
51+
fail("Unsure how to handle expanding binary jars for " + target)
52+
if len(source_jars) > 1:
53+
fail("Unsure how to handle expanding source jars for " + target)
6154

6255
current = struct(
6356
target = str(target.label),
64-
base_name = name,
65-
binary_jar = binary_jar,
66-
source_jar = source_jar,
57+
name = name,
58+
binary_jar = binary_jars[0] if len(binary_jars) else None,
59+
source_jar = source_jars[0] if len(source_jars) else None,
6760
)
6861

6962
return [
@@ -74,19 +67,13 @@ def _dist_aspect_impl(target, ctx):
7467

7568
_dist_aspect = aspect(
7669
_dist_aspect_impl,
77-
attr_aspects = [
78-
"deps",
79-
"exports",
80-
"runtime_deps",
81-
"target",
82-
],
70+
attr_aspects = _ATTR_ASPECTS,
8371
provides = [
8472
DistZipInfo,
8573
],
8674
required_aspect_providers = [
87-
[DistZipInfo],
88-
# [GatheredJavaModuleInfo],
8975
[JavaInfo],
76+
[JavaInfo, JavaModuleInfo],
9077
[MavenInfo],
9178
],
9279
)
@@ -98,10 +85,10 @@ def is_third_party(prefixes, target):
9885
return False
9986

10087
def _java_dist_zip_impl(ctx):
101-
out = ctx.actions.declare_file("%s-dist.zip" % ctx.attr.name)
88+
# out = ctx.actions.declare_file("%s-dist.zip" % ctx.attr.name)
10289

103-
args = ctx.actions.args()
104-
args.add_all(["c", out.path])
90+
# args = ctx.actions.args()
91+
# args.add_all(["c", out.path])
10592

10693
inputs = []
10794
files = []
@@ -116,22 +103,22 @@ def _java_dist_zip_impl(ctx):
116103

117104
for info in infos:
118105
for dist_info in info.dist_infos.to_list():
119-
if not dist_info.binary_jar:
106+
if not dist_info.name:
120107
continue
121108

122109
inputs.append(dist_info.binary_jar)
123110
if is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
124-
third_party.append("lib/%s.jar=%s" % (dist_info.base_name, dist_info.binary_jar.path))
111+
third_party.append("lib/%s.jar=%s" % (dist_info.name, dist_info.binary_jar.path))
125112
else:
126-
first_party.append("%s.jar=%s" % (dist_info.base_name, dist_info.binary_jar.path))
113+
first_party.append("%s.jar=%s" % (dist_info.name, dist_info.binary_jar.path))
127114

128-
if dist_info.source_jar:
115+
if dist_info.source_jar and not is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
129116
inputs.append(dist_info.source_jar)
130-
if is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
131-
third_party.append("lib/%s-sources.jar=%s" % (dist_info.base_name, dist_info.source_jar.path))
132-
else:
133-
first_party.append("%s-sources.jar=%s" % (dist_info.base_name, dist_info.source_jar.path))
117+
first_party.append("%s-sources.jar=%s" % (dist_info.name, dist_info.source_jar.path))
134118

119+
out = ctx.actions.declare_file("%s.zip" % ctx.attr.name)
120+
args = ctx.actions.args()
121+
args.add_all(["c", out])
135122
args.add_all(sorted(files))
136123
args.add_all(sorted(first_party))
137124
args.add_all(sorted(third_party))
@@ -155,12 +142,11 @@ java_dist_zip = rule(
155142
allow_files = True,
156143
),
157144
"deps": attr.label_list(
158-
allow_empty = False,
159145
providers = [
160146
[DistZipInfo],
161147
],
162148
aspects = [
163-
_dist_aspect,
149+
_dist_aspect, has_maven_deps,
164150
],
165151
),
166152
"third_party_prefixes": attr.string_list(

0 commit comments

Comments
 (0)