build.gradle.kts 5.0 KB
Newer Older
1 2 3 4 5 6

import java.io.File
import proguard.gradle.ProGuardTask
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.file.DuplicatesStrategy

7 8
description = "Kotlin Compiler"

9 10 11 12 13 14 15 16 17 18 19
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath("com.github.jengelman.gradle.plugins:shadow:1.2.3")
        classpath("net.sf.proguard:proguard-gradle:5.3.1")
    }
}

20 21 22
plugins {
    `java-base`
}
23 24 25 26

// Set to false to disable proguard run on kotlin-compiler.jar. Speeds up the build
val shrink = true
val compilerManifestClassPath =
27
        "kotlin-runtime.jar kotlin-reflect.jar kotlin-script-runtime.jar"
28

29 30 31 32 33 34
val fatJarContents by configurations.creating
val fatSourcesJarContents by configurations.creating
val proguardLibraryJars by configurations.creating
val fatJar by configurations.creating
val compilerJar by configurations.creating
val archives by configurations
35 36 37 38 39 40 41

val compilerBaseName: String by rootProject.extra

val outputJar = File(buildDir, "libs", "$compilerBaseName.jar")

val javaHome = System.getProperty("java.home")

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
val compilerModules: Array<String> by rootProject.extra

val packagesToRelocate =
        listOf("com.intellij",
                "com.google",
                "com.sampullara",
                "org.apache",
                "org.jdom",
                "org.picocontainer",
                "jline",
                "gnu",
                "javax.inject",
                "org.fusesource")

fun firstFromJavaHomeThatExists(vararg paths: String): File =
        paths.mapNotNull { File(javaHome, it).takeIf { it.exists() } }.firstOrNull()
                ?: throw GradleException("Cannot find under '$javaHome' neither of: ${paths.joinToString()}")

compilerModules.forEach { evaluationDependsOn(it) }

val compiledModulesSources = compilerModules.map {
    project(it).the<JavaPluginConvention>().sourceSets.getByName("main").allSource
}
65 66

dependencies {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    compilerModules.forEach {
        fatJarContents(project(it)) { isTransitive = false }
    }
    compiledModulesSources.forEach {
        fatSourcesJarContents(it)
    }
//    buildVersion()

    fatJarContents(project(":core:builtins", configuration = "builtins"))
    fatJarContents(ideaSdkCoreDeps(*(rootProject.extra["ideaCoreSdkJars"] as Array<String>)))
    fatJarContents(ideaSdkDeps("jna-platform", "oromatcher"))
    fatJarContents(ideaSdkDeps("jps-model.jar", subdir = "jps"))
    fatJarContents(commonDep("javax.inject"))
    fatJarContents(commonDep("org.jline", "jline"))
    fatJarContents(protobufFull())
    fatJarContents(commonDep("com.github.spullara.cli-parser", "cli-parser"))
    fatJarContents(commonDep("com.google.code.findbugs", "jsr305"))
    fatJarContents(commonDep("io.javaslang", "javaslang"))
    fatJarContents(preloadedDeps("json-org"))

    proguardLibraryJars(files(firstFromJavaHomeThatExists("lib/rt.jar", "../Classes/classes.jar"),
            firstFromJavaHomeThatExists("lib/jsse.jar", "../Classes/jsse.jar"),
            firstFromJavaHomeThatExists("../lib/tools.jar", "../Classes/tools.jar")))
    proguardLibraryJars(project(":kotlin-stdlib", configuration = "mainJar"))
    proguardLibraryJars(project(":kotlin-script-runtime", configuration = "mainJar"))
    proguardLibraryJars(project(":kotlin-reflect", configuration = "mainJar"))
    proguardLibraryJars(preloadedDeps("kotlinx-coroutines-core"))

//    proguardLibraryJars(project(":prepare:runtime", configuration = "default").apply { isTransitive = false })
//    proguardLibraryJars(project(":prepare:reflect", configuration = "default").apply { isTransitive = false })
//    proguardLibraryJars(project(":core:script.runtime").apply { isTransitive = false })
98 99
}

100 101
val packCompiler by task<ShadowJar> {
    configurations = listOf(fatJar)
102 103
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    destinationDir = File(buildDir, "libs")
104
//    baseName = compilerBaseName
105
    dependsOn(protobufFullTask)
106 107 108

    setupPublicJar("before-proguard", "")
    from(fatJarContents)
109 110 111 112 113

    manifest.attributes.put("Class-Path", compilerManifestClassPath)
    manifest.attributes.put("Main-Class", "org.jetbrains.kotlin.cli.jvm.K2JVMCompiler")
}

114 115
val proguard by task<ProGuardTask> {
    dependsOn(packCompiler)
116 117
    configuration("$rootDir/compiler/compiler.pro")

118 119 120
    val outputJar = File(buildDir, "libs", "$compilerBaseName-after-proguard.jar")

    inputs.files(packCompiler.outputs.files.singleFile)
121 122 123 124
    outputs.file(outputJar)

    // TODO: remove after dropping compatibility with ant build
    doFirst {
125
        System.setProperty("kotlin-compiler-jar-before-shrink", packCompiler.outputs.files.singleFile.canonicalPath)
126 127 128
        System.setProperty("kotlin-compiler-jar", outputJar.canonicalPath)
    }

129
    libraryjars(proguardLibraryJars)
130 131 132 133 134
    printconfiguration("$buildDir/compiler.pro.dump")
}

dist {
    if (shrink) {
135
        from(proguard)
136
    } else {
137
        from(packCompiler)
138
    }
139
    rename(".*", compilerBaseName + ".jar")
140 141
}

142 143
runtimeJarArtifactBy(proguard, proguard.outputs.files.singleFile) {
    name = compilerBaseName
144 145
    classifier = ""
}
146 147 148 149
sourcesJar {
    from(fatSourcesJarContents)
}
javadocJar()
150

151
publish()