cherry pick kt_jvm_import fixes and doc and kt_jvm_import the compiler jar to prevent ijarification (#143)
diff --git a/kotlin/internal/jvm/impl.bzl b/kotlin/internal/jvm/impl.bzl
index 22c7194..15658c6 100644
--- a/kotlin/internal/jvm/impl.bzl
+++ b/kotlin/internal/jvm/impl.bzl
@@ -64,63 +64,77 @@
)
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]
+ source_jars = [ctx.file.srcjar]
+ else:
+ source_jars = []
- if hasattr(ctx.attr, "runtime_deps"):
- for jar in ctx.attr.runtime_deps:
- transitive_runtime_jars += jar[JavaInfo].transitive_runtime_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
+ 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)
- 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,
- )
-
- # 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(
diff --git a/kotlin/internal/jvm/jvm.bzl b/kotlin/internal/jvm/jvm.bzl
index 2224653..f0af8c5 100644
--- a/kotlin/internal/jvm/jvm.bzl
+++ b/kotlin/internal/jvm/jvm.bzl
@@ -339,6 +339,10 @@
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],
diff --git a/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin b/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin
index 9d11d95..4c3dc5a 100644
--- a/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin
+++ b/kotlin/internal/repositories/BUILD.com_github_jetbrains_kotlin
@@ -12,6 +12,7 @@
# 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(
@@ -19,39 +20,31 @@
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(
@@ -71,9 +64,7 @@
]
]
-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,