From 431d47a605f9eb06f7c816b58194fc2372c4eb83 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Sat, 14 Oct 2017 10:22:02 +0200 Subject: [PATCH] Fix dependencies rewriting and gradle integration tests after applying rewriting --- buildSrc/src/main/kotlin/embeddable.kt | 84 ++++++++++++++++--- .../build.gradle.kts | 22 +++-- .../build.gradle | 2 + .../tools/kotlin-gradle-plugin/build.gradle | 10 ++- prepare/compiler-embeddable/build.gradle.kts | 11 ++- 5 files changed, 106 insertions(+), 23 deletions(-) diff --git a/buildSrc/src/main/kotlin/embeddable.kt b/buildSrc/src/main/kotlin/embeddable.kt index 5a089d2e96f..2b435b58e9d 100644 --- a/buildSrc/src/main/kotlin/embeddable.kt +++ b/buildSrc/src/main/kotlin/embeddable.kt @@ -20,26 +20,87 @@ val packagesToRelocate = "org.picocontainer", "jline", "gnu", - "javax.inject", "org.fusesource") -fun Project.embeddableCompiler(taskName: String = "embeddable", body: Jar.() -> Unit = {}): Jar { +// The shaded compiler "dummy" is used to rewrite dependencies in projects that are used with the embeddable compiler +// on the runtime and use some shaded dependencies from the compiler +// To speed-up rewriting process we want to have this dummy as small as possible. +// But due to the shadow plugin bug (https://github.com/johnrengelman/shadow/issues/262) it is not possible to use +// packagesToRelocate list to for the include list. Therefore the exclude list has to be created. +val packagesToExcludeFromDummy = + listOf("org/jetbrains/kotlin/**", + "org/intellij/lang/annotations/**", + "org/jetbrains/jps/**", + "META-INF/**", + "com/sun/jna/**", + "com/thoughtworks/xstream/**", + "javaslang/**", + "*.proto", + "messages/**", + "net/sf/cglib/**", + "one/util/streamex/**", + "org/iq80/snappy/**", + "org/jline/**", + "org/json/**", + "org/xmlpull/**", + "*.txt") - val compilerJar = configurations.create("compilerJar") +private fun ShadowJar.configureEmbeddableCompilerRelocation(withJavaxInject: Boolean = true) { + relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf") + packagesToRelocate.forEach { + relocate(it, "$kotlinEmbeddableRootPackage.$it") + } + if (withJavaxInject) { + relocate("javax.inject", "$kotlinEmbeddableRootPackage.javax.inject") + } + relocate("org.fusesource", "$kotlinEmbeddableRootPackage.org.fusesource") { + // TODO: remove "it." after #KT-12848 get addressed + exclude("org.fusesource.jansi.internal.CLibrary") + } +} + +private fun Project.compilerShadowJar(taskName: String, body: ShadowJar.() -> Unit): Jar { + + val compilerJar = configurations.getOrCreate("compilerJar") dependencies.add(compilerJar.name, dependencies.project(":kotlin-compiler", configuration = "runtimeJar")) return task(taskName) { destinationDir = File(buildDir, "libs") duplicatesStrategy = DuplicatesStrategy.EXCLUDE from(compilerJar) - relocate("com.google.protobuf", "org.jetbrains.kotlin.protobuf") - packagesToRelocate.forEach { - relocate(it, "$kotlinEmbeddableRootPackage.$it") + body() + } +} + +fun Project.embeddableCompiler(taskName: String = "embeddable", body: ShadowJar.() -> Unit = {}): Jar = + compilerShadowJar(taskName) { + configureEmbeddableCompilerRelocation() + body() } - relocate("org.fusesource", "$kotlinEmbeddableRootPackage.org.fusesource") { - // TODO: remove "it." after #KT-12848 get addressed - exclude("org.fusesource.jansi.internal.CLibrary") + +fun Project.compilerDummyForDependenciesRewriting(taskName: String = "compilerDummy", body: ShadowJar.() -> Unit = {}): Jar = + compilerShadowJar(taskName) { + exclude(packagesToExcludeFromDummy) + body() } + +const val COMPILER_DUMMY_JAR_CONFIGURATION_NAME = "compilerDummyJar" + +fun Project.compilerDummyJar(task: Jar, body: Jar.() -> Unit = {}) { + task.body() + addArtifact(COMPILER_DUMMY_JAR_CONFIGURATION_NAME, task, task) +} + +fun Project.embeddableCompilerDummyForDependenciesRewriting(taskName: String = "embeddable", body: Jar.() -> Unit = {}): Jar { + val compilerDummyJar = configurations.getOrCreate("compilerDummyJar") + dependencies.add(compilerDummyJar.name, + dependencies.project(":kotlin-compiler-embeddable", configuration = COMPILER_DUMMY_JAR_CONFIGURATION_NAME)) + + return task(taskName) { + destinationDir = File(buildDir, "libs") + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + from(compilerDummyJar) + configureEmbeddableCompilerRelocation(withJavaxInject = false) body() } } @@ -56,7 +117,7 @@ fun Project.rewriteDepsToShadedJar(originalJarTask: Jar, shadowJarTask: Zip, bod } shadowJarTask.apply { dependsOn(originalJarTask) - from(originalJarTask) + from(originalJarTask)// { include("**") } classifier = "shadow" } dependsOn(shadowJarTask) @@ -65,4 +126,5 @@ fun Project.rewriteDepsToShadedJar(originalJarTask: Jar, shadowJarTask: Zip, bod } } -fun Project.rewriteDepsToShadedCompiler(originalJarTask: Jar, body: Jar.() -> Unit = {}): Jar = rewriteDepsToShadedJar(originalJarTask, embeddableCompiler(), body) +fun Project.rewriteDepsToShadedCompiler(originalJarTask: Jar, body: Jar.() -> Unit = {}): Jar = + rewriteDepsToShadedJar(originalJarTask, embeddableCompilerDummyForDependenciesRewriting(), body) diff --git a/libraries/tools/kotlin-annotation-processing/build.gradle.kts b/libraries/tools/kotlin-annotation-processing/build.gradle.kts index fae5c3924f8..0942422aad4 100644 --- a/libraries/tools/kotlin-annotation-processing/build.gradle.kts +++ b/libraries/tools/kotlin-annotation-processing/build.gradle.kts @@ -1,4 +1,6 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.gradle.api.internal.artifacts.publish.ArchivePublishArtifact +import org.gradle.jvm.tasks.Jar description = "Annotation Processor wrapper for Kotlin" @@ -22,12 +24,20 @@ projectTest { workingDir = projectDir } -val originalJar by task { - from(packedJars) - from(the().sourceSets.getByName("main").output) -} - -runtimeJar(rewriteDepsToShadedCompiler(originalJar)) +//noDefaultJar() +//tasks.remove(tasks.findByName("jar")) +// +//runtimeJar(task("jar")) { +// from(packedJars) +// from(the().sourceSets.getByName("main").output) +// configureRelocation() +//} +runtimeJar(rewriteDepsToShadedCompiler( + task("shadowJar") { + from(packedJars) + from(the().sourceSets.getByName("main").output) + } +)) sourcesJar() javadocJar() diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/build.gradle b/libraries/tools/kotlin-gradle-plugin-integration-tests/build.gradle index b447e5c7fc9..233a98a5449 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/build.gradle +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/build.gradle @@ -24,6 +24,8 @@ dependencies { testCompile project(path: ':compiler:incremental-compilation-impl', configuration: 'tests-jar') testCompile 'org.jetbrains.kotlin:gradle-api:2.2' + + testRuntime project(path: ':kotlin-android-extensions', configuration: 'runtimeJar') } test.dependsOn(":kotlin-allopen:install", diff --git a/libraries/tools/kotlin-gradle-plugin/build.gradle b/libraries/tools/kotlin-gradle-plugin/build.gradle index c3a8c8d4f60..2cafd06e688 100644 --- a/libraries/tools/kotlin-gradle-plugin/build.gradle +++ b/libraries/tools/kotlin-gradle-plugin/build.gradle @@ -32,9 +32,9 @@ dependencies { compileOnly project(':compiler:incremental-compilation-impl') compile project(':kotlin-stdlib') - compile project(':kotlin-android-extensions') - compile project(':kotlin-build-common') - compile project(':kotlin-compiler-runner') + compileOnly project(':kotlin-android-extensions') + compileOnly project(':kotlin-build-common') + compileOnly project(':kotlin-compiler-runner') compileOnly project(':kotlin-annotation-processing') compileOnly project(':kotlin-annotation-processing-gradle') @@ -44,6 +44,8 @@ dependencies { runtime project(path: ':kotlin-compiler-embeddable', configuration: "runtimeJar") runtime project(path: ':kotlin-annotation-processing-gradle', configuration: "runtimeJar") + runtime project(path: ':kotlin-android-extensions', configuration: 'runtimeJar') + runtime project(path: ':kotlin-compiler-runner', configuration: 'runtimeJar') agp25CompileOnly 'com.android.tools.build:gradle:3.0.0-alpha1' agp25CompileOnly 'org.codehaus.groovy:groovy-all:2.3.9' @@ -53,6 +55,8 @@ dependencies { testCompileOnly project(':compiler') testCompile project (path: ':kotlin-build-common', configuration: 'tests-jar') + testCompile project(':kotlin-android-extensions') + testCompile project(':kotlin-compiler-runner') testCompile project(':kotlin-test::kotlin-test-junit') testCompile "junit:junit:4.12" testCompileOnly project(':kotlin-annotation-processing') diff --git a/prepare/compiler-embeddable/build.gradle.kts b/prepare/compiler-embeddable/build.gradle.kts index 001cba75d6d..35f91d26420 100644 --- a/prepare/compiler-embeddable/build.gradle.kts +++ b/prepare/compiler-embeddable/build.gradle.kts @@ -6,13 +6,18 @@ plugins { } dependencies { - compile(project(":kotlin-stdlib")) - compile(project(":kotlin-script-runtime")) - compile(project(":kotlin-reflect")) + runtime(project(":kotlin-stdlib")) + runtime(project(":kotlin-script-runtime")) + runtime(project(":kotlin-reflect")) } noDefaultJar() +// dummy is used for rewriting dependencies to the shaded packages in the embeddable compiler +compilerDummyJar(compilerDummyForDependenciesRewriting("compilerDummy") { + classifier = "dummy" +}) + runtimeJar(embeddableCompiler()) sourcesJar()