Skip to content

Commit

Permalink
WIP: Kotlin Builder reduced classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
jongerrish committed Jan 4, 2021
1 parent 52b8384 commit 68089d1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/android/bzl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ define_kt_toolchain(
api_version = "1.4",
experimental_use_abi_jars = True,
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 @@ -390,6 +390,7 @@ def _run_kt_builder_action(
# This flag is currently unused by the builder but required for the unused_deps tool
args.add_all("--direct_dependencies", _java_infos_to_compile_jars(compile_deps.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 @@ -79,6 +79,7 @@ def _kotlin_toolchain_impl(ctx):
js_stdlibs = ctx.attr.js_stdlibs,
experimental_use_abi_jars = ctx.attr.experimental_use_abi_jars,
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 @@ -197,6 +198,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 @@ -235,6 +244,7 @@ def define_kt_toolchain(
jvm_target = None,
experimental_use_abi_jars = False,
experimental_report_unused_deps = None,
experimental_reduce_classpath_mode = None,
javac_options = None,
kotlinc_options = None):
"""Define the Kotlin toolchain."""
Expand All @@ -260,6 +270,7 @@ def define_kt_toolchain(
"//conditions:default": experimental_use_abi_jars,
}),
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 @@ -115,7 +115,8 @@ class KotlinBuilder @Inject internal constructor(
GENERATED_JAVA_SRC_JAR("--generated_java_srcjar"),
GENERATED_JAVA_STUB_JAR("--kapt_generated_stub_jar"),
GENERATED_CLASS_JAR("--kapt_generated_class_jar"),
BUILD_KOTLIN("--build_kotlin");
BUILD_KOTLIN("--build_kotlin"),
REDUCED_CLASSPATH_MODE("--reduced_classpath_mode"),
}
}

Expand Down Expand Up @@ -272,6 +273,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 @@ -78,6 +78,8 @@ message CompilationTaskInfo {
// trace: enables trace logging.
// timings: causes timing information to be printed at the of an action.
repeated string debug = 9;
// Optimize classpath by removing dependencies not required for compilation
string reduced_classpath_mode = 10;
}

// Mested messages not marked with stable could be refactored.
Expand Down Expand Up @@ -150,6 +152,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 68089d1

Please sign in to comment.