Implement Gradle Kotlin DSL build

This commit is contained in:
Ilya Chernikov
2017-08-02 12:26:50 +02:00
parent f5d6e41993
commit 61dfb75e0e
93 changed files with 3737 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
import org.gradle.jvm.tasks.Jar
apply { plugin("java") }
val projectsToShadow = listOf(
":plugins:lint",
":plugins:uast-kotlin",
":plugins:uast-kotlin-idea")
tasks.withType<Jar> {
setupRuntimeJar("Kotlin Android Lint")
archiveName = "android-lint.jar"
projectsToShadow.forEach {
dependsOn("$it:classes")
project(it).let { p ->
p.pluginManager.withPlugin("java") {
from(p.the<JavaPluginConvention>().sourceSets.getByName("main").output)
}
}
}
}
configureKotlinProjectSources() // no sources
configureKotlinProjectNoTests()
val jar: Jar by tasks
ideaPlugin {
from(jar)
}
+28
View File
@@ -0,0 +1,28 @@
import java.io.File
val buildVersionFilePath = "${rootProject.extra["distDir"]}/build.txt"
val mainCfg = configurations.create("default")
artifacts.add(mainCfg.name, file(buildVersionFilePath))
val mainTask = task("prepare") {
val versionString = rootProject.extra["build.number"].toString()
val versionFile = File(buildVersionFilePath)
outputs.file(buildVersionFilePath)
outputs.upToDateWhen {
(versionFile.exists() && versionFile.readText().trim() == versionString).apply {
if (!this) {
println("!!! not up-to-date $versionFile: ${versionFile.takeIf { it.exists() }?.readText()?.trim()}")
}
}
}
doLast {
versionFile.parentFile.mkdirs()
versionFile.writeText(versionString)
}
}
defaultTasks(mainTask.name)
@@ -0,0 +1,59 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
}
}
val embedCfg = configurations.create("embed")
val mainCfg = configurations.create("default")
val embeddableCompilerBaseName: String by rootProject.extra
val kotlinEmbeddableRootPackage = "org.jetbrains.kotlin"
val packagesToRelocate =
listOf("com.intellij",
"com.google",
"com.sampullara",
"org.apache",
"org.jdom",
"org.picocontainer",
"jline",
"gnu",
"javax.inject",
"org.fusesource")
dependencies {
embedCfg(project(":prepare:compiler", configuration = "default"))
}
val embeddableTask = task<ShadowJar>("prepare") {
destinationDir = File(buildDir, "libs")
baseName = embeddableCompilerBaseName
configurations = listOf(mainCfg)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
dependsOn(":build-common:assemble", ":core:script.runtime:assemble")
from(embedCfg.files)
relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf")
packagesToRelocate.forEach {
relocate(it, "$kotlinEmbeddableRootPackage.$it")
}
relocate("org.fusesource", "$kotlinEmbeddableRootPackage.org.fusesource") {
// TODO: remove "it." after #KT-12848 get addressed
exclude("org.fusesource.jansi.internal.CLibrary")
}
}
defaultTasks(embeddableTask.name)
artifacts.add(mainCfg.name, embeddableTask.outputs.files.singleFile) {
builtBy(embeddableTask)
classifier = ""
}
+118
View File
@@ -0,0 +1,118 @@
import java.io.File
import proguard.gradle.ProGuardTask
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.file.DuplicatesStrategy
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
classpath("net.sf.proguard:proguard-gradle:5.3.1")
}
}
apply { plugin("maven") }
// Set to false to disable proguard run on kotlin-compiler.jar. Speeds up the build
val shrink = true
val bootstrapBuild = false
val compilerManifestClassPath =
if (bootstrapBuild) "kotlin-runtime-internal-bootstrap.jar kotlin-reflect-internal-bootstrap.jar kotlin-script-runtime-internal-bootstrap.jar"
else "kotlin-runtime.jar kotlin-reflect.jar kotlin-script-runtime.jar"
val ideaSdkCoreCfg = configurations.create("ideaSdk-core")
val otherDepsCfg = configurations.create("other-deps")
val proguardLibraryJarsCfg = configurations.create("library-jars")
val mainCfg = configurations.create("default_")
val packedCfg = configurations.create("packed")
//val withBootstrapRuntimeCfg = configurations.create("withBootstrapRuntime")
val compilerBaseName: String by rootProject.extra
val outputJar = File(buildDir, "libs", "$compilerBaseName.jar")
val javaHome = System.getProperty("java.home")
val compilerProject = project(":compiler")
dependencies {
ideaSdkCoreCfg(ideaSdkCoreDeps(*(rootProject.extra["ideaCoreSdkJars"] as Array<String>)))
ideaSdkCoreCfg(ideaSdkDeps("jna-platform", "oromatcher"))
ideaSdkCoreCfg(ideaSdkDeps("jps-model.jar", subdir = "jps"))
otherDepsCfg(commonDep("javax.inject"))
otherDepsCfg(commonDep("jline"))
otherDepsCfg(protobufFull())
otherDepsCfg(commonDep("com.github.spullara.cli-parser", "cli-parser"))
otherDepsCfg(commonDep("com.google.code.findbugs", "jsr305"))
otherDepsCfg(commonDep("io.javaslang","javaslang"))
otherDepsCfg(preloadedDeps("json-org"))
buildVersion()
proguardLibraryJarsCfg(files("$javaHome/lib/rt.jar".takeIf { File(it).exists() } ?: "$javaHome/../Classes/classes.jar",
"$javaHome/lib/jsse.jar".takeIf { File(it).exists() } ?: "$javaHome/../Classes/jsse.jar"))
proguardLibraryJarsCfg(kotlinDep("stdlib"))
proguardLibraryJarsCfg(kotlinDep("script-runtime"))
proguardLibraryJarsCfg(kotlinDep("reflect"))
proguardLibraryJarsCfg(files("${System.getProperty("java.home")}/../lib/tools.jar"))
// proguardLibraryJarsCfg(project(":prepare:runtime", configuration = "default").apply { isTransitive = false })
// proguardLibraryJarsCfg(project(":prepare:reflect", configuration = "default").apply { isTransitive = false })
// proguardLibraryJarsCfg(project(":core:script.runtime").apply { isTransitive = false })
}
val packCompilerTask = task<ShadowJar>("internal.pack-compiler") {
configurations = listOf(packedCfg)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
destinationDir = File(buildDir, "libs")
baseName = compilerBaseName + "-before-shrink"
dependsOn(protobufFullTask)
setupRuntimeJar("Kotlin Compiler")
(rootProject.extra["compilerModules"] as Array<String>).forEach {
dependsOn("$it:classes")
from(project(it).getCompiledClasses())
}
from(ideaSdkCoreCfg.files)
from(otherDepsCfg.files)
from(project(":core:builtins").getResourceFiles()) { include("kotlin/**") }
manifest.attributes.put("Class-Path", compilerManifestClassPath)
manifest.attributes.put("Main-Class", "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler")
}
val proguardTask = task<ProGuardTask>("internal.proguard-compiler") {
dependsOn(packCompilerTask)
configuration("$rootDir/compiler/compiler.pro")
inputs.files(packCompilerTask.outputs.files.singleFile)
outputs.file(outputJar)
// TODO: remove after dropping compatibility with ant build
doFirst {
System.setProperty("kotlin-compiler-jar-before-shrink", packCompilerTask.outputs.files.singleFile.canonicalPath)
System.setProperty("kotlin-compiler-jar", outputJar.canonicalPath)
}
proguardLibraryJarsCfg.files.forEach { jar ->
libraryjars(jar)
}
printconfiguration("$buildDir/compiler.pro.dump")
}
dist {
if (shrink) {
from(proguardTask)
} else {
from(packCompilerTask)
rename("-before-shrink", "")
}
}
artifacts.add(mainCfg.name, proguardTask.outputs.files.singleFile) {
builtBy(proguardTask)
classifier = ""
}
+20
View File
@@ -0,0 +1,20 @@
import org.gradle.jvm.tasks.Jar
apply { plugin("java") }
tasks.withType<Jar> {
setupRuntimeJar("Kotlin Formatter")
archiveName = "kotlin-formatter.jar"
dependsOn(":idea:formatter:classes")
project(":idea:formatter").let { p ->
p.pluginManager.withPlugin("java") {
from(p.the<JavaPluginConvention>().sourceSets.getByName("main").output)
}
}
from(fileTree("$rootDir/idea/formatter")) { include("src/**") } // Eclipse formatter sources navigation depends on this
}
configureKotlinProjectSources() // no sources
configureKotlinProjectNoTests()
@@ -0,0 +1,20 @@
import org.gradle.jvm.tasks.Jar
apply { plugin("java") }
tasks.withType<Jar> {
setupRuntimeJar("Kotlin IDE Lazy Resolver")
archiveName = "kotlin-ide-common.jar"
dependsOn(":idea:ide-common:classes")
project(":idea:ide-common").let { p ->
p.pluginManager.withPlugin("java") {
from(p.the<JavaPluginConvention>().sourceSets.getByName("main").output)
}
}
from(fileTree("$rootDir/idea/ide-common")) { include("src/**") } // Eclipse formatter sources navigation depends on this
}
configureKotlinProjectSources() // no sources
configureKotlinProjectNoTests()
+47
View File
@@ -0,0 +1,47 @@
import org.gradle.jvm.tasks.Jar
apply { plugin("java") }
val projectsToShadow = listOf(
":build-common",
":compiler:cli-common",
":compiler:compiler-runner",
":compiler:daemon-client",
":compiler:daemon-common",
":core",
":idea:idea-jps-common",
":jps-plugin",
":compiler:preloader",
":compiler:util",
":core:util.runtime",
":plugins:android-extensions-jps")
dependencies {}
val jar: Jar by tasks
jar.apply {
setupRuntimeJar("Kotlin JPS plugin")
manifest.attributes.put("Main-Class", "org.jetbrains.kotlin.runner.Main")
manifest.attributes.put("Class-Path", "kotlin-runtime.jar")
archiveName = "kotlin-jps-plugin.jar"
projectsToShadow.forEach {
dependsOn("$it:classes")
project(it).let { p ->
p.pluginManager.withPlugin("java") {
from(p.the<JavaPluginConvention>().sourceSets.getByName("main").output)
}
}
}
from(fileTree("$rootDir/jps-plugin/src")) { include("META-INF/**") }
from(files("$rootDir/resources/kotlinManifest.properties"))
from(zipTree("$rootDir/dependencies/native-platform-uberjar.jar"))
}
configureKotlinProjectSources() // no sources
configureKotlinProjectNoTests()
ideaPlugin("lib/jps") {
from(jar)
}
+86
View File
@@ -0,0 +1,86 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
}
}
val projectsToShadow = listOf(
":core:builtins",
":plugins:annotation-based-compiler-plugins-ide-support",
":compiler:backend",
":compiler:backend-common",
":build-common",
":compiler:cli-common",
":compiler:container",
":compiler:daemon-common",
":core",
":eval4j",
":idea:formatter",
":compiler:frontend",
":compiler:frontend.java",
":compiler:frontend.script",
":idea:ide-common",
":idea",
":idea:idea-android",
":idea:idea-android-output-parser",
":idea:idea-core",
":idea:idea-jps-common",
//":idea-ultimate",
":compiler:ir.psi2ir",
":compiler:ir.tree",
":j2k",
":js:js.ast",
":js:js.frontend",
":js:js.parser",
":js:js.serializer",
":compiler:light-classes",
":compiler:plugin-api",
":compiler:preloader",
":compiler:resolution",
":compiler:serialization",
":compiler:util",
":core:util.runtime")
val packedJars by configurations.creating
val sideJars by configurations.creating
dependencies {
packedJars(commonDep("com.github.spullara.cli-parser", "cli-parser"))
packedJars(preloadedDeps("protobuf-${rootProject.extra["versions.protobuf-java"]}"))
sideJars(project(":kotlin-script-runtime"))
sideJars(commonDep("io.javaslang", "javaslang"))
sideJars(commonDep("javax.inject"))
sideJars(preloadedDeps("markdown", "kotlinx-coroutines-core", "kotlinx-coroutines-jdk8", "uast-java"))
}
val targetJar = File(buildDir, "libs", "kotlin-plugin.jar")
val shadowTask = task<ShadowJar>("shadowJar") {
setupRuntimeJar("Kotlin IDEA plugin")
archiveName = targetJar.canonicalPath
projectsToShadow.forEach {
dependsOn("$it:classes")
project(it).let { p ->
p.pluginManager.withPlugin("java") {
from(p.the<JavaPluginConvention>().sourceSets.getByName("main").output)
}
}
}
from(files("$rootDir/resources/kotlinManifest.properties"))
from(packedJars.files)
}
ideaPlugin {
dependsOn(shadowTask)
from(targetJar)
dependsOn(":kotlin-script-runtime:jar")
from(sideJars)
}
@@ -0,0 +1,42 @@
import org.gradle.jvm.tasks.Jar
import org.gradle.api.internal.HasConvention
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
apply { plugin("kotlin") }
dependencies {
val compile by configurations
compile(project(":kotlin-stdlib"))
}
val jar: Jar by tasks
jar.apply {
setupRuntimeJar("Kotlin Mock Runtime for Tests")
from(fileTree("${rootProject.extra["distDir"]}/builtins")) { include("kotlin/**") }
archiveName = "kotlin-mock-runtime-for-test.jar"
}
configure<JavaPluginConvention> {
sourceSets["main"].apply {
(this as HasConvention).convention.getPlugin<KotlinSourceSet>().kotlin.apply {
srcDir(File(rootDir, "core", "runtime.jvm", "src"))
.include("kotlin/TypeAliases.kt",
"kotlin/text/TypeAliases.kt")
srcDir(File(rootDir, "libraries", "stdlib", "src"))
.include("kotlin/collections/TypeAliases.kt",
"kotlin/jvm/JvmVersion.kt",
"kotlin/util/Standard.kt",
"kotlin/internal/Annotations.kt")
}
}
}
configureKotlinProjectNoTests()
task<Copy>("dist") {
into(rootProject.extra["distDir"].toString())
from(jar)
}
+131
View File
@@ -0,0 +1,131 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.Project
import org.jetbrains.org.objectweb.asm.*
import org.gradle.jvm.tasks.Jar
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.util.jar.JarFile
import java.util.zip.ZipOutputStream
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
classpath(ideaSdkDeps("asm-all"))
}
}
apply { plugin("com.github.johnrengelman.shadow") }
// Set to false to prevent relocation and metadata stripping on kotlin-reflect.jar and reflection sources. Use to debug reflection
val obfuscateReflect = true
val classesFromProjectsCfg = configurations.create("classes-from-projects")
val otherDepsCfg = configurations.create("other-deps")
val mainCfg = configurations.create("default")
val outputReflectJarFileBase = "$buildDir/libs/kotlin-reflect"
val coreProjectName = ":core"
val reflectionProjectName = ":core:reflection.jvm"
artifacts.add(mainCfg.name, File(outputReflectJarFileBase + ".jar"))
dependencies {
classesFromProjectsCfg.name(projectDepIntransitive(coreProjectName))
classesFromProjectsCfg.name(projectDepIntransitive(":core:util.runtime"))
classesFromProjectsCfg.name(projectDepIntransitive(reflectionProjectName))
otherDepsCfg.name(protobufLite())
otherDepsCfg.name(commonDep("javax.inject"))
buildVersion()
}
val prePackReflectTask = task<ShadowJar>("pre-pack-reflect") {
classifier = if (obfuscateReflect) outputReflectJarFileBase + "_beforeStrip" else outputReflectJarFileBase
configurations = listOf(mainCfg)
setupRuntimeJar("Kotlin Reflect")
dependsOn("$coreProjectName:assemble", "$reflectionProjectName:assemble", protobufLiteTask)
from(project(reflectionProjectName).getCompiledClasses())
from(project(coreProjectName).getCompiledClasses())
from(project(":core:util.runtime").getCompiledClasses())
from(project(coreProjectName).file("descriptor.loader.java/src")) {
include("META-INF/services/**")
}
from(otherDepsCfg.files)
manifest.attributes.put("Class-Path", "kotlin-runtime.jar")
if (obfuscateReflect) {
relocate("org.jetbrains.kotlin", "kotlin.reflect.jvm.internal.impl")
relocate("javax.inject", "kotlin.reflect.jvm.internal.impl.javax.inject")
}
}
val mainTask = task("prepare") {
dependsOn(prePackReflectTask)
val inFile = File(outputReflectJarFileBase + "_beforeStrip.jar")
val outFile = File(outputReflectJarFileBase + ".jar")
inputs.file(inFile)
outputs.file(outFile)
val annotationRegex = "kotlin/Metadata".toRegex()
val classRegex = "kotlin/reflect/jvm/internal/impl/.*".toRegex()
doLast {
println("Stripping annotations from all classes in $inFile")
println("Input file size: ${inFile.length()} bytes")
fun transform(entryName: String, bytes: ByteArray): ByteArray {
if (!entryName.endsWith(".class")) return bytes
if (!classRegex.matches(entryName.removeSuffix(".class"))) return bytes
var changed = false
val classWriter = ClassWriter(0)
val classVisitor = object : ClassVisitor(Opcodes.ASM5, classWriter) {
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor? {
if (annotationRegex.matches(Type.getType(desc).internalName)) {
changed = true
return null
}
return super.visitAnnotation(desc, visible)
}
}
ClassReader(bytes).accept(classVisitor, 0)
if (!changed) return bytes
return classWriter.toByteArray()
}
ZipOutputStream(BufferedOutputStream(FileOutputStream(outFile))).use { outJar ->
val inJar = JarFile(inFile)
try {
for (entry in inJar.entries()) {
if (entry.isDirectory) continue
val inBytes = inJar.getInputStream(entry).readBytes()
val outBytes = transform(entry.name, inBytes)
if (inBytes.size < outBytes.size) {
error("Size increased for ${entry.name}: was ${inBytes.size} bytes, became ${outBytes.size} bytes")
}
entry.compressedSize = -1L
outJar.putNextEntry(entry)
outJar.write(outBytes)
outJar.closeEntry()
}
}
finally {
// Yes, JarFile does not extend Closeable on JDK 6 so we can't use "use" here
inJar.close()
}
}
println("Output written to $outFile")
println("Output file size: ${outFile.length()} bytes")
}
}
defaultTasks(mainTask.name)
+39
View File
@@ -0,0 +1,39 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import java.io.File
buildscript {
repositories {
jcenter()
}
dependencies {
classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
classpath(ideaSdkDeps("asm-all"))
}
}
apply { plugin("com.github.johnrengelman.shadow") }
val mainCfg = configurations.create("default")
val outputRuntimeJarFileBase = "$buildDir/libs/kotlin-runtime"
artifacts.add(mainCfg.name, File(outputRuntimeJarFileBase + ".jar"))
dependencies {
mainCfg.name(projectDepIntransitive(":core:builtins"))
mainCfg.name(projectDepIntransitive(":kotlin-stdlib"))
buildVersion()
}
val mainTask = task<ShadowJar>("prepare") {
classifier = outputRuntimeJarFileBase
configurations = listOf(mainCfg)
dependsOn(":core:builtins:assemble", ":kotlin-stdlib:assemble")
setupRuntimeJar("Kotlin Runtime")
from(mainCfg.files)
}
defaultTasks(mainTask.name)