Skip to content

Commit

Permalink
Merge pull request #30 from cgruber/support_1_0
Browse files Browse the repository at this point in the history
Remove 1.0-incompatible structures
  • Loading branch information
cgruber authored Oct 2, 2019
2 parents 0d618d7 + b9c07be commit adf1ea5
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 58 deletions.
4 changes: 4 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

exports_files([
"scripts/noop.sh",
])

# The entire test suite excluding local tests.
test_suite(
name = "all_tests",
Expand Down
94 changes: 42 additions & 52 deletions kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,74 +68,64 @@ def _write_launcher_action(ctx, rjars, main_class, jvm_flags, args = "", wrapper
is_executable = True,
)

def kt_jvm_import_impl(ctx):
jars = []
if ctx.file.srcjar:
source_jars = [ctx.file.srcjar]
def _unify_jars(ctx):
if bool(ctx.attr.jar):
return struct(class_jar = ctx.file.jar, source_jar = ctx.file.srcjar, ijar = None)
else:
source_jars = []

# TODO after a while remove the for block, the checks after it,and simplify the source-jar to jar allignment.
# There must be a single jar jar and it can either be a filegroup or a JavaInfo.
for jar in ctx.attr.jars:
# If a JavaInfo is available it's because it was picked up from a `maven_jar` style attribute -- e.g.,
# @com_google_guava_guava//jar. so the transitive_compile_jars or the transitive_runtime_jars should not be
# visited -- descending into these results in ijars entering the graph.
if JavaInfo in jar:
jars += jar[JavaInfo].full_compile_jars.to_list()
source_jars += jar[JavaInfo].transitive_source_jars.to_list()
else:
# this branch occurs when the attr was a filegroup.
for file in jar.files.to_list():
if file.basename.endswith("-sources.jar"):
source_jars.append(file)
elif file.basename.endswith(".jar"):
jars.append(file)
else:
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar")
# Legacy handling.
jars = []
source_jars = [ctx.file.srcjar] if ctx.file.srcjar else []

if len(jars) > 1:
print("got more than one jar, this is an error create an issue")
print(jars)
if len(source_jars) > 1:
print("got more than one source jar, this is an error create an issue")
print(source_jars)
# TODO after a while remove the for block, the checks after it,and simplify the source-jar to jar allignment.
# There must be a single jar jar and it can either be a filegroup or a JavaInfo.
for jar in ctx.attr.jars:
# If a JavaInfo is available it's because it was picked up from a `maven_jar` style attribute -- e.g.,
# @com_google_guava_guava//jar. so the transitive_compile_jars or the transitive_runtime_jars should not be
# visited -- descending into these results in ijars entering the graph.
if JavaInfo in jar:
jars += jar[JavaInfo].full_compile_jars.to_list()
source_jars += jar[JavaInfo].transitive_source_jars.to_list()
else:
# this branch occurs when the attr was a filegroup.
for file in jar.files.to_list():
if file.basename.endswith("-sources.jar"):
source_jars.append(file)
elif file.basename.endswith(".jar"):
jars.append(file)
else:
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar")

# This was needed for intellij plugin, try to pair up jars with their sources so that the sources are mounted
# correctly.
source_tally = {}
for sj in source_jars:
if sj.basename.endswith("-sources.jar"):
source_tally[sj.basename.replace("-sources.jar", ".jar")] = sj
artifacts = []
for jar in jars:
if jar.basename in source_tally:
artifacts += [struct(class_jar = jar, source_jar = source_tally[jar.basename], ijar = None)]
else:
artifacts += [struct(class_jar = jar, ijar = None)]
if len(jars) > 1:
fail("got more than one jar, this is an error create an issue: %s" % jars)
if len(source_jars) > 1:
fail("got more than one source jar. " +
"Did you include both srcjar and a sources jar in the jars attribute?: " +
jars)
print(source_jars)
return struct(class_jar = jars[0], source_jar = source_jars[0] if len(source_jars) == 1 else None, ijar = None)

# Normalize to None if no source jars discovered
if len(source_jars) == 0:
source_jar = None
else:
source_jar = source_jars[0]
def kt_jvm_import_impl(ctx):
if bool(ctx.attr.jars) and bool(ctx.attr.jar):
fail("Cannot use both jars= and jar= attribute. Prefer jar=")

artifact = _unify_jars(ctx)
kt_info = _KtJvmInfo(
module_name = _utils.derive_module_name(ctx),
outputs = struct(
jars = artifacts,
jars = [artifact],
),
)
all_files = [artifact.class_jar] + ([artifact.source_jar] if bool(artifact.source_jar) else [])
return struct(
kt = kt_info,
providers = [
DefaultInfo(files = depset(jars)),
DefaultInfo(files = depset(all_files)),
JavaInfo(
output_jar = jars[0],
source_jars = [source_jar] if bool(source_jar) else [],
output_jar = artifact.class_jar,
compile_jar = artifact.class_jar,
source_jar = artifact.source_jar,
runtime_deps = [dep[JavaInfo] for dep in ctx.attr.runtime_deps if JavaInfo in dep],
exports = [d[JavaInfo] for d in getattr(ctx.attr, "exports", [])],
use_ijar = False,
neverlink = getattr(ctx.attr, "neverlink", False),
),
kt_info,
Expand Down
16 changes: 10 additions & 6 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,20 @@ kt_jvm_import = rule(
attrs = {
"jars": attr.label_list(
doc = """The jars listed here are equavalent to an export attribute. The label should be either to a single
class jar, or multiple filegroup labels. When the labels is a file_provider it should follow the conventions
used in repositories generated by the maven_jar rule --i.e., the rule expects a file_provider with a single
class jar and a single source jar. a source jar is recognized by the suffix `-sources.jar`.""",
class jar, or one or more filegroup labels. The filegroups, when resolved, must contain only one jar
containing classes, and (optionally) one peer file containing sources, named `<jarname>-sources.jar`.
DEPRECATED - please use `jar` and `srcjar` attributes.""",
allow_files = True,
mandatory = True,
cfg = "target",
),
"jar": attr.label(
doc = """The jar listed here is equivalent to an export attribute.""",
allow_single_file = True,
cfg = "target",
),
"srcjar": attr.label(
doc = """The sources for the class jar. This should generally be provided especially when importing a single
jar.""",
doc = """The sources for the class jar.""",
allow_single_file = True,
cfg = "target",
),
Expand Down
2 changes: 2 additions & 0 deletions scripts/noop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
# do nothing.
23 changes: 23 additions & 0 deletions third_party/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,42 @@ package(default_visibility = ["//visibility:public"])

load("//kotlin:kotlin.bzl", "kt_jvm_import")


java_binary(
name = "bazel_deps",
main_class = "com.github.johnynek.bazel_deps.ParseProject",
runtime_deps = ["@bazel_deps//jar"],
)

sh_test(
name = "force_import_load_test",
srcs = ["//:scripts/noop.sh"],
data = [":force_import_load_deploy.jar"],
)

# This binary isn't used except to test the two usages of kt_jvm_import below.
java_binary(
name = "force_import_load",
create_executable = False,
runtime_deps = [
":kotlinx_coroutines",
":kotlinx_coroutines_common",
]
)

kt_jvm_import(
name = "kotlinx_coroutines",
jars = [
"@io_bazel_rules_kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core//jar",
],
)

kt_jvm_import(
name = "kotlinx_coroutines_common",
jar = "@io_bazel_rules_kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core_common//jar:io_bazel_rules_kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core_common.jar",
srcjar = "@io_bazel_rules_kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core_common//jar:io_bazel_rules_kotlin_org_jetbrains_kotlinx_kotlinx_coroutines_core_common-sources.jar",
)

java_plugin(
name = "autovalue_plugin",
generates_api = 1,
Expand Down

0 comments on commit adf1ea5

Please sign in to comment.