Extract kotlin-reflect-api module out of kotlin-reflect

This is needed only for faster compilation of the Kotlin project itself
and has no effect on the public artifact
org.jetbrains.kotlin:kotlin-reflect.

The problem this is solving is the rebuild of the project once anything
has been changed in modules in 'core' (even inside function bodies, i.e.
a non-API change). Previously, changes in 'core' led to the compilation
of kotlin-reflect, which led to the rebuild of all modules depending on
kotlin-reflect directly or indirectly (which is almost all modules in
the project) because kotlin-reflect's artifacts are custom-built and the
changes can not be picked up incrementally. But 99.9% of the time the
initial changes in 'core' could not have any effect on the usages of
kotlin-reflect, because classes from those modules are moved to an
internal package in kotlin-reflect and thus are an internal
implementation detail.

Now, changes in 'core' still lead to the compilation of kotlin-reflect
and to the process of building the custom jar. But if a module depends
on kotlin-reflect-api, not kotlin-reflect, then the incremental
difference checker will detect that the module does not have to be
recompiled if there hasn't been any changes to the API of
kotlin-reflect-api. Which means that the module will not be rebuilt on
every change in 'core'.

This commit only introduces the new module. The dependencies
(kotlin-reflect -> kotlin-reflect-api) are replaced in the next commit.
This commit is contained in:
Alexander Udalov
2017-11-09 14:29:24 +01:00
parent 3e8b39af90
commit 329fbd8fa8
9 changed files with 3041 additions and 32 deletions
+2
View File
@@ -3,6 +3,7 @@ buildscript {
val buildSrcKotlinVersion: String by extra(findProperty("buildSrc.kotlin.version")?.toString() ?: embeddedKotlinVersion)
val buildSrcKotlinRepo: String? by extra(findProperty("buildSrc.kotlin.repo") as String?)
extra["versions.shadow"] = "2.0.1"
extra["versions.protobuf-java"] = "2.6.1"
repositories {
buildSrcKotlinRepo?.let {
@@ -46,6 +47,7 @@ dependencies {
// compile("org.jetbrains.kotlin:kotlin-gradle-plugin:${rootProject.extra["bootstrap_kotlin_version"]}")
compile("com.github.jengelman.gradle.plugins:shadow:${property("versions.shadow")}")
compile("org.ow2.asm:asm-all:6.0_BETA")
compile("com.google.protobuf:protobuf-java:${property("versions.protobuf-java")}")
}
samWithReceiver {
@@ -0,0 +1,62 @@
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
import org.gradle.api.file.FileTreeElement
import org.gradle.api.logging.Logger
import org.jetbrains.kotlin.serialization.jvm.JvmPackageTable
import shadow.org.apache.tools.zip.ZipEntry
import shadow.org.apache.tools.zip.ZipOutputStream
import java.io.ByteArrayOutputStream
import java.io.DataInputStream
import java.io.DataOutputStream
class KotlinModuleShadowTransformer(private val logger: Logger) : Transformer {
private data class Entry(val path: String, val bytes: ByteArray)
private val data = mutableListOf<Entry>()
override fun canTransformResource(element: FileTreeElement): Boolean =
element.path.substringAfterLast(".") == KOTLIN_MODULE
override fun transform(context: TransformerContext) {
fun relocate(content: String): String =
context.relocators.fold(content) { acc, relocator -> relocator.applyToSourceContent(acc) }
val input = DataInputStream(context.`is`)
val version = IntArray(input.readInt()) { input.readInt() }
logger.info("Transforming ${context.path} with version ${version.toList()}")
val table = JvmPackageTable.PackageTable.parseFrom(context.`is`).toBuilder()
val newTable = JvmPackageTable.PackageTable.newBuilder().apply {
for (packageParts in table.packagePartsList + table.metadataPartsList) {
addPackageParts(JvmPackageTable.PackageParts.newBuilder(packageParts).apply {
packageFqName = relocate(packageFqName)
})
}
addAllJvmPackageName(table.jvmPackageNameList.map(::relocate))
}
val baos = ByteArrayOutputStream()
val output = DataOutputStream(baos)
output.writeInt(version.size)
version.forEach(output::writeInt)
newTable.build().writeTo(output)
output.flush()
data += Entry(context.path, baos.toByteArray())
}
override fun hasTransformedResource(): Boolean =
data.isNotEmpty()
override fun modifyOutputStream(os: ZipOutputStream) {
for ((path, bytes) in data) {
os.putNextEntry(ZipEntry(path))
os.write(bytes)
}
data.clear()
}
companion object {
const val KOTLIN_MODULE = "kotlin_module"
}
}