Skip to content

Commit f5c7c0e

Browse files
authored
Java builder flow fixes (#408)
* Fix breakages in experimental_use_java_builder flow. Make examples/android use custom toolchain with both experimental_use_abi_jars and experimental_use_java_builder to validate this flow. * Pass Java source version for KAPT. This is required such that annotation processors generate source that matches the target version. For example see: google/dagger#1339 * ABI classes should be written to a separate folder to avoid polluting the ABI jar with non-ABI classes * Maintain mnemonic to distinguish between KotlinKapt + KotlinCompile (Both belong to the same worker group) * Add default java + kotlin compiler options
1 parent 408a1ed commit f5c7c0e

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

examples/android/WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_reg
6666

6767
kotlin_repositories()
6868

69-
kt_register_toolchains()
69+
register_toolchains("//bzl:experimental_toolchain")
7070

7171
http_archive(
7272
name = "rules_pkg",

examples/android/bzl/BUILD.bazel

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "define_kt_toolchain")
2+
3+
load("@io_bazel_rules_kotlin//kotlin/internal:opts.bzl", "kt_javac_options", "kt_kotlinc_options")
4+
5+
kt_kotlinc_options(
6+
name = "default_kotlinc_options",
7+
)
8+
9+
kt_javac_options(
10+
name = "default_javac_options",
11+
)
12+
13+
define_kt_toolchain(
14+
name = "experimental_toolchain",
15+
api_version = "1.4",
16+
experimental_use_abi_jars = True,
17+
experimental_use_java_builder = True,
18+
language_version = "1.4",
19+
javac_options = ":default_javac_options",
20+
kotlinc_options = ":default_kotlinc_options"
21+
)

kotlin/internal/jvm/compile.bzl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ def _run_kt_builder_action(
448448
)
449449

450450
ctx.actions.run(
451-
mnemonic = "KotlinCompile",
451+
mnemonic = mnemonic,
452452
inputs = depset(
453453
srcs.all_srcs + srcs.src_jars,
454454
transitive = [compile_deps.compile_jars, transitive_runtime_jars] + [p.classpath for p in compiler_plugins],
@@ -490,10 +490,10 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
490490
transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps)
491491
plugins = ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps)
492492

493-
output_jars = []
494493
generated_src_jars = []
495494
if toolchains.kt.experimental_use_java_builder:
496-
_run_kt_java_builder_actions(
495+
compile_jar = ctx.actions.declare_file(ctx.label.name + ".abi.jar")
496+
output_jars = _run_kt_java_builder_actions(
497497
ctx = ctx,
498498
rule_kind = rule_kind,
499499
toolchains = toolchains,
@@ -504,6 +504,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
504504
annotation_processors = annotation_processors,
505505
transitive_runtime_jars = transitive_runtime_jars,
506506
plugins = plugins,
507+
compile_jar = compile_jar
507508
)
508509

509510
else:
@@ -524,7 +525,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
524525
},
525526
)
526527
compile_jar = kt_java_output_jar
527-
output_jars.append(kt_java_output_jar)
528+
output_jars = [kt_java_output_jar]
528529

529530
# If this rule has any resources declared setup a zipper action to turn them into a jar.
530531
if len(ctx.files.resources) > 0:
@@ -593,8 +594,9 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
593594

594595
"""Runs the necessary KotlinBuilder and JavaBuilder actions to compile a jar
595596
"""
596-
def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src_jars, friend, compile_deps, annotation_processors, transitive_runtime_jars, plugins):
597+
def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src_jars, friend, compile_deps, annotation_processors, transitive_runtime_jars, plugins, compile_jar):
597598
compile_jars = []
599+
output_jars = []
598600
kt_stubs_for_java = []
599601

600602
# Run KAPT
@@ -718,6 +720,8 @@ def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src
718720
input_jars = compile_jars,
719721
)
720722

723+
return output_jars
724+
721725
def export_only_providers(ctx, actions, attr, outputs):
722726
"""_export_only_providers creates a series of forwarding providers without compilation overhead.
723727

src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class KotlinJvmTaskExecutor @Inject internal constructor(
6868
}
6969
.given(outputs.abijar).notEmpty {
7070
plugin(plugins.jvmAbiGen) {
71-
flag("outputDir", directories.classes)
71+
flag("outputDir", directories.abiClasses)
7272
}
7373
given(outputs.jar).empty {
7474
plugin(plugins.skipCodeGen)

src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/compilation_task.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ internal fun JvmCompilationTask.kaptArgs(
112112
aptMode: String
113113
): CompilationArgs {
114114
val javacArgs = mapOf<String, String>(
115-
"-target" to info.toolchainInfo.jvm.jvmTarget
115+
"-target" to info.toolchainInfo.jvm.jvmTarget,
116+
"-source" to info.toolchainInfo.jvm.jvmTarget
116117
)
117118
return CompilationArgs()
118119
.xFlag("plugin", plugins.kapt.jarPath)
@@ -195,7 +196,7 @@ internal fun JvmCompilationTask.createAbiJar() =
195196
normalize = true,
196197
verbose = false
197198
).also {
198-
it.addDirectory(Paths.get(directories.classes))
199+
it.addDirectory(Paths.get(directories.abiClasses))
199200
it.addDirectory(Paths.get(directories.generatedClasses))
200201
it.setJarOwner(info.label, info.bazelRuleKind)
201202
it.execute()

0 commit comments

Comments
 (0)