Skip to content

Commit

Permalink
Kotlin Builder reduced classpath - experimental feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jongerrish committed Jan 8, 2021
1 parent a43314c commit f9a8e8a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/android/bzl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ define_kt_toolchain(
experimental_use_abi_jars = True,
experimental_strict_kotlin_deps = "warn",
experimental_report_unused_deps = "warn",
experimental_reduce_classpath_mode = "KOTLINBUILDER_REDUCED",
javac_options = ":default_javac_options",
kotlinc_options = ":default_kotlinc_options",
language_version = "1.4",
Expand Down
1 change: 1 addition & 0 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ def _run_kt_builder_action(
args.add_all("--direct_dependencies", _java_infos_to_compile_jars(compile_deps.deps))
args.add("--strict_kotlin_deps", toolchains.kt.experimental_strict_kotlin_deps)
args.add_all("--classpath", compile_deps.compile_jars)
args.add("--reduced_classpath_mode", toolchains.kt.experimental_reduce_classpath_mode)
args.add_all("--sources", srcs.all_srcs, omit_if_empty = True)
args.add_all("--source_jars", srcs.src_jars + generated_src_jars, omit_if_empty = True)
args.add_all("--deps_artifacts", deps_artifacts, omit_if_empty = True)
Expand Down
11 changes: 11 additions & 0 deletions kotlin/internal/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def _kotlin_toolchain_impl(ctx):
experimental_use_abi_jars = ctx.attr.experimental_use_abi_jars,
experimental_strict_kotlin_deps = ctx.attr.experimental_strict_kotlin_deps,
experimental_report_unused_deps = ctx.attr.experimental_report_unused_deps,
experimental_reduce_classpath_mode = ctx.attr.experimental_reduce_classpath_mode,
javac_options = ctx.attr.javac_options[JavacOptions] if ctx.attr.javac_options else None,
kotlinc_options = ctx.attr.kotlinc_options[KotlincOptions] if ctx.attr.kotlinc_options else None,
empty_jar = ctx.file._empty_jar,
Expand Down Expand Up @@ -209,6 +210,14 @@ _kt_toolchain = rule(
"error",
],
),
"experimental_reduce_classpath_mode": attr.string(
doc = "Removes unneeded dependencies from the classpath",
default = "NONE",
values = [
"NONE",
"KOTLINBUILDER_REDUCED",
],
),
"javac_options": attr.label(
doc = "Compiler options for javac",
providers = [JavacOptions],
Expand Down Expand Up @@ -248,6 +257,7 @@ def define_kt_toolchain(
experimental_use_abi_jars = False,
experimental_strict_kotlin_deps = None,
experimental_report_unused_deps = None,
experimental_reduce_classpath_mode = None,
javac_options = None,
kotlinc_options = None):
"""Define the Kotlin toolchain."""
Expand All @@ -274,6 +284,7 @@ def define_kt_toolchain(
}),
experimental_strict_kotlin_deps = experimental_strict_kotlin_deps,
experimental_report_unused_deps = experimental_report_unused_deps,
experimental_reduce_classpath_mode = experimental_reduce_classpath_mode,
javac_options = javac_options,
kotlinc_options = kotlinc_options,
visibility = ["//visibility:public"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class KotlinBuilder @Inject internal constructor(
GENERATED_CLASS_JAR("--kapt_generated_class_jar"),
BUILD_KOTLIN("--build_kotlin"),
STRICT_KOTLIN_DEPS("--strict_kotlin_deps"),
REDUCED_CLASSPATH_MODE("--reduced_classpath_mode"),
}
}

Expand Down Expand Up @@ -274,6 +275,9 @@ class KotlinBuilder @Inject internal constructor(

with(root.inputsBuilder) {
addAllClasspath(argMap.mandatory(JavaBuilderFlags.CLASSPATH))
addAllDepsArtifacts(
argMap.optional(JavaBuilderFlags.DEPS_ARTIFACTS) ?: emptyList()
)
addAllDirectDependencies(argMap.mandatory(JavaBuilderFlags.DIRECT_DEPENDENCIES))

addAllProcessors(argMap.optional(JavaBuilderFlags.PROCESSORS) ?: emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// Provides extensions for the JvmCompilationTask protocol buffer.
package io.bazel.kotlin.builder.tasks.jvm

import com.google.devtools.build.lib.view.proto.Deps
import io.bazel.kotlin.builder.toolchain.CompilationTaskContext
import io.bazel.kotlin.builder.toolchain.KotlinToolchain
import io.bazel.kotlin.builder.utils.IS_JVM_SOURCE_FILE
Expand All @@ -27,6 +28,7 @@ import io.bazel.kotlin.builder.utils.jars.SourceJarExtractor
import io.bazel.kotlin.builder.utils.partitionJvmSources
import io.bazel.kotlin.model.JvmCompilationTask
import io.bazel.kotlin.model.JvmCompilationTask.Directories
import java.io.BufferedInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.ObjectOutputStream
Expand All @@ -51,18 +53,38 @@ fun JvmCompilationTask.codeGenArgs(): CompilationArgs = CompilationArgs()
.flag("-d", directories.classes)
.values(info.passthroughFlagsList)

fun JvmCompilationTask.baseArgs(): CompilationArgs = CompilationArgs()
.flag("-cp")
.paths(
inputs.classpathList + directories.generatedClasses
) {
it.map(Path::toString)
.joinToString(File.pathSeparator)
}
.flag("-api-version", info.toolchainInfo.common.apiVersion)
.flag("-language-version", info.toolchainInfo.common.languageVersion)
.flag("-jvm-target", info.toolchainInfo.jvm.jvmTarget)
.flag("-module-name", info.moduleName)
fun JvmCompilationTask.baseArgs(): CompilationArgs {
val classpath = when (info.reducedClasspathMode) {
"KOTLINBUILDER_REDUCED" -> {
val transitiveDepsForCompile = mutableSetOf<String>()
inputs.depsArtifactsList.forEach { jdepsPath ->
BufferedInputStream(Paths.get(jdepsPath).toFile().inputStream()).use {
val deps = Deps.Dependencies.parseFrom(it)
deps.dependencyList.forEach { dep ->
if (dep.kind == Deps.Dependency.Kind.EXPLICIT) {
transitiveDepsForCompile.add(dep.path)
}
}
}
}
inputs.directDependenciesList + transitiveDepsForCompile
}
else -> inputs.classpathList
} as List<String>

return CompilationArgs()
.flag("-cp")
.paths(
classpath + directories.generatedClasses
) {
it.map(Path::toString)
.joinToString(File.pathSeparator)
}
.flag("-api-version", info.toolchainInfo.common.apiVersion)
.flag("-language-version", info.toolchainInfo.common.languageVersion)
.flag("-jvm-target", info.toolchainInfo.jvm.jvmTarget)
.flag("-module-name", info.moduleName)
}

internal fun JvmCompilationTask.plugins(
options: List<String>,
Expand Down
4 changes: 4 additions & 0 deletions src/main/protobuf/kotlin_model.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ message CompilationTaskInfo {
repeated string debug = 9;
// Enable strict dependency checking for Kotlin
string strict_kotlin_deps = 10;
// Optimize classpath by removing dependencies not required for compilation
string reduced_classpath_mode = 11;
}

// Mested messages not marked with stable could be refactored.
Expand Down Expand Up @@ -152,6 +154,8 @@ message JvmCompilationTask {
repeated string compiler_plugin_classpath = 14;
// Java opts to be passed to java compiler
repeated string javac_flags = 15;
// JDeps dependency artifacts
repeated string deps_artifacts = 16;
}

CompilationTaskInfo info = 1;
Expand Down

0 comments on commit f9a8e8a

Please sign in to comment.