Skip to content

Commit

Permalink
cherry pick kt_jvm_import fixes and doc and kt_jvm_import the compile…
Browse files Browse the repository at this point in the history
…r jar to prevent ijarification (bazelbuild#143)
  • Loading branch information
hsyed authored Sep 14, 2018
1 parent 907768e commit 46b867e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 56 deletions.
84 changes: 49 additions & 35 deletions kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,63 +64,77 @@ def _write_launcher_action(ctx, rjars, main_class, jvm_flags, args = "", wrapper
)

def kt_jvm_import_impl(ctx):
jars = depset()
source_jars = depset()
runtime_jars = depset()
transitive_compile_time_jars = depset()
transitive_runtime_jars = depset()

jars = []
if ctx.file.srcjar:
source_jars += [ctx.file.srcjar]

if hasattr(ctx.attr, "runtime_deps"):
for jar in ctx.attr.runtime_deps:
transitive_runtime_jars += jar[JavaInfo].transitive_runtime_jars
source_jars = [ctx.file.srcjar]
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
source_jars += jar[JavaInfo].transitive_source_jars
transitive_compile_time_jars += jar[JavaInfo].transitive_compile_time_jars
transitive_runtime_jars += jar[JavaInfo].transitive_runtime_jars
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:
if file.basename.endswith("-sources.jar"):
source_jars += [file]
source_jars.append(file)
elif file.basename.endswith(".jar"):
jars += [file]
jars.append(file)
else:
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar")

runtime_jars += jars
transitive_compile_time_jars += jars
transitive_runtime_jars += jars

java_info = java_common.create_provider(
use_ijar = False,
source_jars = source_jars,
compile_time_jars = jars,
runtime_jars = runtime_jars,
transitive_compile_time_jars = transitive_compile_time_jars,
transitive_runtime_jars = transitive_runtime_jars,
)
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)

# This is needed for intellij plugin, try to pair up jars with their sources so that the sources are mounted
# 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.to_list():
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.to_list():
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)]

kotlin_info = _KtJvmInfo(outputs = struct(jars = artifacts))
default_info = DefaultInfo(files = depset(jars))
return struct(kt = kotlin_info, providers = [default_info, java_info, kotlin_info])
# Normalize to None if no source jars discovered
if len(source_jars) == 0:
source_jar = None
else:
source_jar = source_jars[0]

kt_info = _KtJvmInfo(
module_name = "",
outputs = struct(
jars = artifacts,
),
)
return struct(
kt = kt_info,
providers = [
DefaultInfo(files = depset(jars)),
JavaInfo(
output_jar = jars[0],
compile_jar = jars[0],
source_jar = source_jar,
runtime_deps = ctx.attr.runtime_deps,
neverlink = getattr(ctx.attr, "neverlink", False),
),
kt_info,
],
)

def kt_jvm_library_impl(ctx):
return _make_providers(
Expand Down
4 changes: 4 additions & 0 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,10 @@ kt_jvm_import = rule(
mandatory = False,
providers = [JavaInfo],
),
"neverlink": attr.bool(
doc = """If true only use this library for compilation and not at runtime.""",
default = False,
),
},
implementation = _kt_jvm_import_impl,
provides = [JavaInfo, _KtJvmInfo],
Expand Down
33 changes: 12 additions & 21 deletions kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,39 @@
# See the License for the specific language governing permissions and
# limitations under the License.
package(default_visibility = ["//visibility:public"])
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_import", "kt_js_import")

# Kotlin home filegroup containing everything that is needed.
filegroup(
name = "home",
srcs = glob(["**"]),
)

# Kotlin dependencies that are internal to this repo and are meant to be loaded at runtime.
# Kotlin dependencies that are internal to this repo and are meant to be loaded manually into a classloader.
[
java_import(
name = "%s" % art,
jars = ["lib/%s.jar" % art],
kt_jvm_import(
name = "kotlin-%s" % art,
jars = ["lib/kotlin-%s.jar" % art],
neverlink = 1,
)
for art in [
"kotlin-annotation-processing",
"kotlin-annotation-processing-runtime",
"kotlin-compiler",
"annotation-processing",
"annotation-processing-runtime",
"compiler",
]
]

# Kotlin dependencies that are internal to this repo and may be linked.
[
java_import(
name = "%s" % art,
jars = ["lib/%s.jar" % art],
name = "kotlin-%s" % art,
jars = ["lib/kotlin-%s.jar" % art],
)
for art in [
"kotlin-preloader",
"preloader",
]
]

# Kotlin annotations jar. Contains annotations like NotNull.
java_import(
name = "annotations",
jars = ["lib/annotations-13.0.jar"],
)

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

# The Kotlin standard libraries. These should be setup in a Toolchain.
[
kt_jvm_import(
Expand All @@ -71,9 +64,7 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_import")
]
]

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_js_import")

# The Kotlin standard libraries. These should be setup in a Toolchain.
# The Kotlin JS standard libraries. These should be setup in a Toolchain.
[
kt_js_import(
name = "kotlin-%s" % art,
Expand Down

0 comments on commit 46b867e

Please sign in to comment.