Skip to content

Commit

Permalink
Build: Introduce flag for disabling jar post processing
Browse files Browse the repository at this point in the history
 proguard, relocation, etc.
  • Loading branch information
4u7 committed Jun 27, 2019
1 parent 6e20e03 commit 2d6a3cb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
13 changes: 11 additions & 2 deletions buildSrc/src/main/kotlin/buildProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class KotlinBuildProperties(

private operator fun get(key: String): Any? = localProperties.getProperty(key) ?: propertiesProvider.getProperty(key)

private fun getBoolean(key: String): Boolean = this[key]?.toString()?.toBoolean() == true
private fun getBoolean(key: String, default: Boolean = false): Boolean =
(this[key]?.toString()?.toBoolean() ?: default) == true

val isJpsBuildEnabled: Boolean = getBoolean("jpsBuild")

Expand Down Expand Up @@ -70,9 +71,17 @@ class KotlinBuildProperties(
}
return kotlinUltimateExists && (explicitlyEnabled || isTeamcityBuild)
}

val postProcessing: Boolean get() = isTeamcityBuild || getBoolean("kotlin.build.postprocessing", true)

val relocation: Boolean get() = postProcessing

val proguard: Boolean get() = postProcessing && getBoolean("kotlin.build.proguard")

val jsIrDist: Boolean get() = getBoolean("kotlin.stdlib.js.ir.dist")
}

private const val extensionName = "kotlinBuildFlags"
private const val extensionName = "kotlinBuildProperties"

class ProjectProperties(val project: Project) : PropertiesProvider {
override val rootProjectDir: File
Expand Down
31 changes: 18 additions & 13 deletions libraries/reflect/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ val core = "$rootDir/core"
val relocatedCoreSrc = "$buildDir/core-relocated"
val libsDir = property("libsDir")


val proguardDeps by configurations.creating
val proguardAdditionalInJars by configurations.creating

Expand Down Expand Up @@ -109,23 +108,25 @@ class KotlinModuleShadowTransformer(private val logger: Logger) : Transformer {
}

val reflectShadowJar by task<ShadowJar> {
classifier = "shadow"
version = null
archiveClassifier.set("shadow")
configurations = listOf(embedded)

callGroovy("manifestAttributes", manifest, project, "Main" /*true*/)

exclude("**/*.proto")

transform(KotlinModuleShadowTransformer(logger))

configurations = listOf(embedded)
relocate("org.jetbrains.kotlin", "kotlin.reflect.jvm.internal.impl")
relocate("javax.inject", "kotlin.reflect.jvm.internal.impl.javax.inject")
mergeServiceFiles()

if (kotlinBuildProperties.relocation) {
transform(KotlinModuleShadowTransformer(logger))
relocate("org.jetbrains.kotlin", "kotlin.reflect.jvm.internal.impl")
relocate("javax.inject", "kotlin.reflect.jvm.internal.impl.javax.inject")
}
}

val stripMetadata by tasks.creating {
dependsOn("reflectShadowJar")
val inputJar = reflectShadowJar.archivePath
dependsOn(reflectShadowJar)
val inputJar = reflectShadowJar.outputs.files.singleFile
val outputJar = File("$libsDir/kotlin-reflect-stripped.jar")
inputs.file(inputJar)
outputs.file(outputJar)
Expand Down Expand Up @@ -189,14 +190,18 @@ addArtifact("archives", sourcesJar)
addArtifact("sources", sourcesJar)

val result by task<Jar> {
dependsOn(proguard)
from(zipTree(file(proguardOutput)))
val task = if (kotlinBuildProperties.proguard) proguard else reflectShadowJar
dependsOn(task)
from {
zipTree(task.outputs.files.singleFile)
}

callGroovy("manifestAttributes", manifest, project, "Main")
}

val modularJar by task<Jar> {
dependsOn(proguard)
classifier = "modular"
archiveClassifier.set("modular")
from(zipTree(file(proguardOutput)))
from(zipTree(reflectShadowJar.archivePath)) {
include("META-INF/versions/**")
Expand Down
18 changes: 7 additions & 11 deletions libraries/tools/kotlin-main-kts/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ plugins {
id("jps-compatible")
}

// You can run Gradle with "-Pkotlin.build.proguard=true" to enable ProGuard run on the jar (on TeamCity, ProGuard always runs)
val shrink =
findProperty("kotlin.build.proguard")?.toString()?.toBoolean()
?: hasProperty("teamcity")

val jarBaseName = property("archivesBaseName") as String

val proguardLibraryJars by configurations.creating
Expand Down Expand Up @@ -58,18 +53,19 @@ val mainKtsRelocatedDepsRootPackage = "$mainKtsRootPackage.relocatedDeps"
val packJar by task<ShadowJar> {
configurations = emptyList()
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
destinationDir = File(buildDir, "libs")

setupPublicJar(project.the<BasePluginConvention>().archivesBaseName, "before-proguard")
destinationDirectory.set(File(buildDir, "libs"))
archiveClassifier.set("before-proguard")

from(mainSourceSet.output)
from(project.configurations.embedded)

// don't add this files to resources classpath to avoid IDE exceptions on kotlin project
from("jar-resources")

packagesToRelocate.forEach {
relocate(it, "$mainKtsRelocatedDepsRootPackage.$it")
if (kotlinBuildProperties.relocation) {
packagesToRelocate.forEach {
relocate(it, "$mainKtsRelocatedDepsRootPackage.$it")
}
}
}

Expand All @@ -90,7 +86,7 @@ val proguard by task<ProGuardTask> {
}

val resultJar = tasks.register<Jar>("resultJar") {
val pack = if (shrink) proguard else packJar
val pack = if (kotlinBuildProperties.proguard) proguard else packJar
dependsOn(pack)
setupPublicJar(jarBaseName)
from {
Expand Down
15 changes: 5 additions & 10 deletions prepare/compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ plugins {
java
}

// You can run Gradle with "-Pkotlin.build.proguard=true" to enable ProGuard run on kotlin-compiler.jar (on TeamCity, ProGuard always runs)
val shrink = findProperty("kotlin.build.proguard")?.toString()?.toBoolean() ?: hasProperty("teamcity")

val jsIrDist = findProperty("kotlin.stdlib.js.ir.dist")?.toString()?.toBoolean() == true

val fatJarContents by configurations.creating
val fatJarContentsStripMetadata by configurations.creating
val fatJarContentsStripServices by configurations.creating
Expand Down Expand Up @@ -85,10 +80,10 @@ val distLibraryProjects = listOfNotNull(
":kotlin-scripting-compiler",
":kotlin-scripting-compiler-impl",
":kotlin-scripting-jvm",
":kotlin-stdlib-js-ir".takeIf { jsIrDist },
":kotlin-stdlib-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-source-sections-compiler-plugin",
":kotlin-test:kotlin-test-js",
":kotlin-test:kotlin-test-js-ir".takeIf { jsIrDist },
":kotlin-test:kotlin-test-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-test:kotlin-test-junit",
":kotlin-test:kotlin-test-junit5",
":kotlin-test:kotlin-test-jvm",
Expand All @@ -109,9 +104,9 @@ val distCompilerPluginProjects = listOf(
val distSourcesProjects = listOfNotNull(
":kotlin-annotations-jvm",
":kotlin-script-runtime",
":kotlin-stdlib-js-ir".takeIf { jsIrDist },
":kotlin-stdlib-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-test:kotlin-test-js",
":kotlin-test:kotlin-test-js-ir".takeIf { jsIrDist },
":kotlin-test:kotlin-test-js-ir".takeIf { kotlinBuildProperties.jsIrDist },
":kotlin-test:kotlin-test-junit",
":kotlin-test:kotlin-test-junit5",
":kotlin-test:kotlin-test-jvm",
Expand Down Expand Up @@ -262,7 +257,7 @@ val proguard by task<ProGuardTask> {
}
}

val pack = if (shrink) proguard else packCompiler
val pack = if (kotlinBuildProperties.proguard) proguard else packCompiler
val distDir: String by rootProject.extra

val jar = runtimeJar {
Expand Down

0 comments on commit 2d6a3cb

Please sign in to comment.