Skip to content

Commit

Permalink
Java builder flow fixes (#408)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jongerrish authored Nov 24, 2020
1 parent 408a1ed commit f5c7c0e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/android/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_reg

kotlin_repositories()

kt_register_toolchains()
register_toolchains("//bzl:experimental_toolchain")

http_archive(
name = "rules_pkg",
Expand Down
21 changes: 21 additions & 0 deletions examples/android/bzl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "define_kt_toolchain")

load("@io_bazel_rules_kotlin//kotlin/internal:opts.bzl", "kt_javac_options", "kt_kotlinc_options")

kt_kotlinc_options(
name = "default_kotlinc_options",
)

kt_javac_options(
name = "default_javac_options",
)

define_kt_toolchain(
name = "experimental_toolchain",
api_version = "1.4",
experimental_use_abi_jars = True,
experimental_use_java_builder = True,
language_version = "1.4",
javac_options = ":default_javac_options",
kotlinc_options = ":default_kotlinc_options"
)
14 changes: 9 additions & 5 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def _run_kt_builder_action(
)

ctx.actions.run(
mnemonic = "KotlinCompile",
mnemonic = mnemonic,
inputs = depset(
srcs.all_srcs + srcs.src_jars,
transitive = [compile_deps.compile_jars, transitive_runtime_jars] + [p.classpath for p in compiler_plugins],
Expand Down Expand Up @@ -490,10 +490,10 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
transitive_runtime_jars = _plugin_mappers.targets_to_transitive_runtime_jars(ctx.attr.plugins + ctx.attr.deps)
plugins = ctx.attr.plugins + _exported_plugins(deps = ctx.attr.deps)

output_jars = []
generated_src_jars = []
if toolchains.kt.experimental_use_java_builder:
_run_kt_java_builder_actions(
compile_jar = ctx.actions.declare_file(ctx.label.name + ".abi.jar")
output_jars = _run_kt_java_builder_actions(
ctx = ctx,
rule_kind = rule_kind,
toolchains = toolchains,
Expand All @@ -504,6 +504,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
annotation_processors = annotation_processors,
transitive_runtime_jars = transitive_runtime_jars,
plugins = plugins,
compile_jar = compile_jar
)

else:
Expand All @@ -524,7 +525,7 @@ def kt_jvm_produce_jar_actions(ctx, rule_kind):
},
)
compile_jar = kt_java_output_jar
output_jars.append(kt_java_output_jar)
output_jars = [kt_java_output_jar]

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

"""Runs the necessary KotlinBuilder and JavaBuilder actions to compile a jar
"""
def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src_jars, friend, compile_deps, annotation_processors, transitive_runtime_jars, plugins):
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):
compile_jars = []
output_jars = []
kt_stubs_for_java = []

# Run KAPT
Expand Down Expand Up @@ -718,6 +720,8 @@ def _run_kt_java_builder_actions(ctx, rule_kind, toolchains, srcs, generated_src
input_jars = compile_jars,
)

return output_jars

def export_only_providers(ctx, actions, attr, outputs):
"""_export_only_providers creates a series of forwarding providers without compilation overhead.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class KotlinJvmTaskExecutor @Inject internal constructor(
}
.given(outputs.abijar).notEmpty {
plugin(plugins.jvmAbiGen) {
flag("outputDir", directories.classes)
flag("outputDir", directories.abiClasses)
}
given(outputs.jar).empty {
plugin(plugins.skipCodeGen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ internal fun JvmCompilationTask.kaptArgs(
aptMode: String
): CompilationArgs {
val javacArgs = mapOf<String, String>(
"-target" to info.toolchainInfo.jvm.jvmTarget
"-target" to info.toolchainInfo.jvm.jvmTarget,
"-source" to info.toolchainInfo.jvm.jvmTarget
)
return CompilationArgs()
.xFlag("plugin", plugins.kapt.jarPath)
Expand Down Expand Up @@ -195,7 +196,7 @@ internal fun JvmCompilationTask.createAbiJar() =
normalize = true,
verbose = false
).also {
it.addDirectory(Paths.get(directories.classes))
it.addDirectory(Paths.get(directories.abiClasses))
it.addDirectory(Paths.get(directories.generatedClasses))
it.setJarOwner(info.label, info.bazelRuleKind)
it.execute()
Expand Down

0 comments on commit f5c7c0e

Please sign in to comment.