Skip to content

Commit

Permalink
[bazel] Re-enable building dist-zips
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed Jan 3, 2020
1 parent e6c7853 commit bbe5a05
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 75 deletions.
12 changes: 5 additions & 7 deletions java/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def _has_maven_deps_impl(target, ctx):
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])
all_deps = deps + exports + rt_deps

coordinates = read_coordinates(tags)
if "maven:compile_only" in tags:
return MavenInfo(
coordinates = None,
Expand All @@ -43,16 +42,15 @@ def _has_maven_deps_impl(target, ctx):
# it's enough to set set the transitive deps to just be the rule for
# anything that depends upon it. Otherwise, gather them up, and carry on
# as if nothing really mattered.

if len(coordinates) > 0:
transitive_maven_deps = depset(coordinates)
coordinate = read_coordinates(tags)
if coordinate:
transitive_maven_deps = depset([coordinate])
else:
transitive_maven_deps = depset(coordinates, transitive = [info.transitive_maven_deps for info in all_infos])
transitive_maven_deps = depset(transitive = [info.transitive_maven_deps for info in all_infos])
artifact_jars = depset(java_info.runtime_output_jars, transitive = [info.artifact_jars for info in all_infos if not info.coordinates])
source_jars = depset(java_info.source_jars, transitive = [info.source_jars for info in all_infos if not info.coordinates])

infos = []
coordinate = coordinates[0] if len(coordinates) > 0 else None

if JavaModuleInfo in target:
info = MavenInfo(
Expand Down Expand Up @@ -123,4 +121,4 @@ def read_coordinates(tags):
coordinates.append(tag[len(MAVEN_PREFIX):])
if len(coordinates) > 1:
fail("Zero or one set of coordinates should be defined: %s" % coordinates)
return coordinates
return coordinates[0] if len(coordinates) else None
122 changes: 54 additions & 68 deletions java/private/dist_zip.bzl
Original file line number Diff line number Diff line change
@@ -1,69 +1,62 @@
load("//java/private:common.bzl", "MAVEN_PREFIX", "MavenInfo", "explode_coordinates", "has_maven_deps")
#load("//java/private:module.bzl", "GatheredJavaModuleInfo", "has_java_module_deps")
load("//java/private:common.bzl", "MavenInfo", "has_maven_deps", "read_coordinates", "explode_coordinates")
load("//java/private:module.bzl", "JavaModuleInfo")

DistZipInfo = provider(
fields = {
"dist_infos": "Transitive collection of structs containing base_name, binary_jar, and source_jar",
},
)

_ATTR_ASPECTS = [
"deps",
"exports",
"runtime_deps",
]

def _name(coordinates, default):
if not coordinates:
return default
exploded = explode_coordinates(coordinates)
return exploded[1] + "-" + exploded[2]

def _dist_aspect_impl(target, ctx):
deps = getattr(ctx.rule.attr, "deps", [])
exports = getattr(ctx.rule.attr, "exports", [])
rt_deps = getattr(ctx.rule.attr, "runtime_deps", [])
tgt = getattr(ctx.rule.attr, "target", None)

all_deps = deps + exports + rt_deps
if tgt:
all_deps.append(tgt)
transitive_infos = [d[DistZipInfo].dist_infos for d in all_deps]

name = None
tags = getattr(ctx.rule.attr, "tags", [])
for tag in tags:
if tag.startswith(MAVEN_PREFIX):
raw = tag[len(MAVEN_PREFIX):]
coords = explode_coordinates(raw)
if "jar" == coords[3]:
name = "%s-%s" % (coords[1], coords[2])

transitive_infos = [d[DistZipInfo].dist_infos for d in all_deps if DistZipInfo in d]

if not name:
# Return accumulated dist infos
return [
DistZipInfo(
dist_infos = depset([], transitive = transitive_infos),
),
]

source_jars = depset()
binary_jars = depset()

if "maven_artifacts" == ctx.rule.kind:
binary_jars = target[OutputGroupInfo].binjar
source_jars = target[OutputGroupInfo].srcjar
binary_jars = []
source_jars = []

if MavenInfo in target and target[MavenInfo].coordinates:
name = _name(target[MavenInfo].coordinates, None)
binary_jars = target[MavenInfo].artifact_jars
source_jars = target[MavenInfo].source_jars
elif JavaModuleInfo in target and target[JavaModuleInfo].name:
coordinates = read_coordinates(ctx.rule.attr.tags)
name = _name(coordinates, target[JavaModuleInfo].name)
binary_jars = target[JavaInfo].runtime_output_jars
source_jars = target[JavaInfo].source_jars
elif JavaInfo in target:
binary_jars = depset(target[JavaInfo].runtime_output_jars)
# elif GatheredJavaModuleInfo in target:
# binary_jars = depset(target[GatheredJavaModuleInfo].binary_jars)
# source_jars = depset(target[GatheredJavaModuleInfo].source_jars)

binary_jar = None
if len(binary_jars.to_list()) > 1:
fail("Unable to process more than one binary jar")
elif len(binary_jars.to_list()) == 1:
binary_jar = binary_jars.to_list()[0]
source_jar = None
if len(source_jars.to_list()) > 1:
fail("Unable to process more than one source jar")
elif len(source_jars.to_list()) == 1:
source_jar = source_jars.to_list()[0]
coordinates = read_coordinates(ctx.rule.attr.tags)
if coordinates:
name = _name(coordinates, None)
binary_jars = target[JavaInfo].runtime_output_jars
source_jars = target[JavaInfo].source_jars

if len(binary_jars) > 1:
fail("Unsure how to handle expanding binary jars for " + target)
if len(source_jars) > 1:
fail("Unsure how to handle expanding source jars for " + target)

current = struct(
target = str(target.label),
base_name = name,
binary_jar = binary_jar,
source_jar = source_jar,
name = name,
binary_jar = binary_jars[0] if len(binary_jars) else None,
source_jar = source_jars[0] if len(source_jars) else None,
)

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

_dist_aspect = aspect(
_dist_aspect_impl,
attr_aspects = [
"deps",
"exports",
"runtime_deps",
"target",
],
attr_aspects = _ATTR_ASPECTS,
provides = [
DistZipInfo,
],
required_aspect_providers = [
[DistZipInfo],
# [GatheredJavaModuleInfo],
[JavaInfo],
[JavaInfo, JavaModuleInfo],
[MavenInfo],
],
)
Expand All @@ -98,10 +85,10 @@ def is_third_party(prefixes, target):
return False

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

args = ctx.actions.args()
args.add_all(["c", out.path])
# args = ctx.actions.args()
# args.add_all(["c", out.path])

inputs = []
files = []
Expand All @@ -116,22 +103,22 @@ def _java_dist_zip_impl(ctx):

for info in infos:
for dist_info in info.dist_infos.to_list():
if not dist_info.binary_jar:
if not dist_info.name:
continue

inputs.append(dist_info.binary_jar)
if is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
third_party.append("lib/%s.jar=%s" % (dist_info.base_name, dist_info.binary_jar.path))
third_party.append("lib/%s.jar=%s" % (dist_info.name, dist_info.binary_jar.path))
else:
first_party.append("%s.jar=%s" % (dist_info.base_name, dist_info.binary_jar.path))
first_party.append("%s.jar=%s" % (dist_info.name, dist_info.binary_jar.path))

if dist_info.source_jar:
if dist_info.source_jar and not is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
inputs.append(dist_info.source_jar)
if is_third_party(ctx.attr.third_party_prefixes, dist_info.target):
third_party.append("lib/%s-sources.jar=%s" % (dist_info.base_name, dist_info.source_jar.path))
else:
first_party.append("%s-sources.jar=%s" % (dist_info.base_name, dist_info.source_jar.path))
first_party.append("%s-sources.jar=%s" % (dist_info.name, dist_info.source_jar.path))

out = ctx.actions.declare_file("%s.zip" % ctx.attr.name)
args = ctx.actions.args()
args.add_all(["c", out])
args.add_all(sorted(files))
args.add_all(sorted(first_party))
args.add_all(sorted(third_party))
Expand All @@ -155,12 +142,11 @@ java_dist_zip = rule(
allow_files = True,
),
"deps": attr.label_list(
allow_empty = False,
providers = [
[DistZipInfo],
],
aspects = [
_dist_aspect,
_dist_aspect, has_maven_deps,
],
),
"third_party_prefixes": attr.string_list(
Expand Down

0 comments on commit bbe5a05

Please sign in to comment.