[K/N][build] Rewrite kotlinx.cli build to kts

Rewrite to kts and use konan plugin instead of simple JavaExec task.
Produce cache in the project instead of the upper one.
This commit is contained in:
Pavel Punegov
2022-02-08 20:53:36 +03:00
parent fd6500c167
commit 361d922119
3 changed files with 148 additions and 126 deletions
+3 -19
View File
@@ -44,10 +44,10 @@ targetList.each { target ->
task("${target}EndorsedLibraries") {
endorsedLibrariesList.each { library ->
dependsOn task("${target}${library.name}EndorsedLibraries", type: Copy) {
dependsOn "$library.projectName:${target}${library.taskName}"
dependsOn "$library.projectName:${target}${library.name}"
destinationDir project.file("${project.buildDir}/${library.name}")
from(library.project.file("build/${target}${library.taskName}")) {
from(library.project.file("build/${target}${library.name}")) {
include('**')
eachFile {
if (name == 'manifest') {
@@ -75,23 +75,7 @@ targetList.each { target ->
}
endorsedLibrariesList.each { library ->
def dist = UtilsKt.getKotlinNativeDist(project)
task("${target}${library.taskName}Cache", type: KonanCacheTask) {
it.target = target
originalKlib = library.project.file("build/${target}${library.taskName}")
cacheRoot = library.project.file("build/cache/$target").path
compilerDistributionPath.set(distDir)
// TODO: should depend on stdlib producer task and its properties
def clibs = [:]
clibs.put(project.file("$dist/klib/common/stdlib"), project.file("$dist/klib/cache/${target}-g$cacheKind/stdlib-cache"))
cachedLibraries = clibs
dependsOn "$library.projectName:${target}${library.taskName}"
dependsOn ":kotlin-native:${target}CrossDistRuntime"
dependsOn ":kotlin-native:${target}StdlibCache"
cacheTask.dependsOn it
}
cacheTask.dependsOn "${library.projectName}:${target}${library.taskName}Cache"
}
}
}
@@ -1,107 +0,0 @@
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.util.PlatformLibsInfo
import org.jetbrains.kotlin.*
buildscript {
repositories {
if (UtilsKt.getCacheRedirectorEnabled(project))
maven { url 'https://cache-redirector.jetbrains.com/jcenter' }
else
jcenter()
maven {
url project.bootstrapKotlinRepo
}
}
}
apply plugin: 'kotlin-multiplatform'
repositories {
if (UtilsKt.getCacheRedirectorEnabled(project))
maven { url 'https://cache-redirector.jetbrains.com/jcenter' }
else
jcenter()
}
kotlin {
sourceSets {
commonMain {
dependencies {
implementation UtilsKt.projectOrFiles(project, ':kotlin-stdlib-common')
}
kotlin.srcDir 'src/main/kotlin'
}
commonTest {
dependencies {
implementation UtilsKt.projectOrFiles(project, ":kotlin-test:kotlin-test-common")
implementation UtilsKt.projectOrFiles(project, ":kotlin-test:kotlin-test-annotations-common")
}
kotlin.srcDir 'src/tests'
}
jvm().compilations.main.defaultSourceSet {
dependencies {
implementation UtilsKt.projectOrFiles(project, ":kotlin-stdlib-jdk8")
}
kotlin.srcDir 'src/main/kotlin-jvm'
}
// JVM-specific tests and their dependencies:
jvm().compilations.test.defaultSourceSet {
dependencies {
implementation UtilsKt.projectOrFiles(project, ":kotlin-test:kotlin-test-junit")
}
}
jvm().compilations.all {
kotlinOptions {
freeCompilerArgs = ["-opt-in=kotlinx.cli.ExperimentalCli", "-opt-in=kotlin.RequiresOptIn"]
suppressWarnings = true
}
}
}
}
def commonSrc = new File("$projectDir/src/main/kotlin")
def nativeSrc = new File("$projectDir/src/main/kotlin-native")
targetList.each { target ->
def konanJvmArgs = [*HostManager.regularJvmArgs]
def defaultArgs = ['-nopack', '-no-default-libs', '-no-endorsed-libs']
if (target != "wasm32") defaultArgs += '-g'
def konanArgs = [*defaultArgs,
'-target', target,
"-Xruntime=${project(':kotlin-native:runtime').file('build/bitcode/main/' + target + '/runtime.bc')}",
*project.globalBuildArgs]
task("${target}KotlinxCli", type: JavaExec) {
def outputFile = project.file("build/${target}KotlinxCli")
// See :endorsedLibraries.ext for full endorsedLibraries list.
def moduleName = endorsedLibraries[project].name
dependsOn ":kotlin-native:distCompiler"
dependsOn ":kotlin-native:${target}CrossDistRuntime"
def kotlinNativeDist = UtilsKt.getKotlinNativeDist(project)
systemProperties "konan.home": kotlinNativeDist
main = 'org.jetbrains.kotlin.cli.bc.K2NativeKt'
// This task depends on distCompiler, so the compiler jar is already in the dist directory.
classpath = fileTree("$kotlinNativeDist/konan/lib") {
include "*.jar"
}
jvmArgs = konanJvmArgs
args = [*konanArgs,
'-output', outputFile,
'-produce', 'library', '-module-name', moduleName,
'-Xmulti-platform', '-opt-in=kotlinx.cli.ExperimentalCli',
'-opt-in=kotlin.ExperimentalMultiplatform',
'-Werror', '-opt-in=kotlin.RequiresOptIn',
commonSrc.absolutePath,
"-Xcommon-sources=${commonSrc.absolutePath}",
nativeSrc]
inputs.dir(nativeSrc)
inputs.dir(commonSrc)
outputs.dir(outputFile)
}
}
@@ -0,0 +1,145 @@
import org.jetbrains.kotlin.*
import org.jetbrains.kotlin.gradle.plugin.konan.tasks.KonanCacheTask
import org.jetbrains.kotlin.konan.properties.loadProperties
import org.jetbrains.kotlin.konan.properties.saveProperties
import org.jetbrains.kotlin.library.KLIB_PROPERTY_NATIVE_TARGETS
import org.jetbrains.kotlin.konan.file.File as KFile
val distDir: File by project
val konanHome: String by extra(distDir.absolutePath)
extra["org.jetbrains.kotlin.native.home"] = konanHome
plugins {
kotlin("multiplatform")
konan
}
kotlin {
sourceSets {
commonMain {
dependencies {
implementation(project(":kotlin-stdlib-common"))
}
kotlin.srcDir("src/main/kotlin")
}
commonTest {
dependencies {
// projectOrFiles is required for the performance project that includes kotlinx.cli compositely
projectOrFiles(project, ":kotlin-test:kotlin-test-common")?.let { implementation(it) }
projectOrFiles(project, ":kotlin-test:kotlin-test-annotations-common")?.let { implementation(it) }
}
kotlin.srcDir("src/tests")
}
jvm {
compilations["main"].defaultSourceSet {
dependencies {
implementation(project(":kotlin-stdlib-jdk8"))
}
kotlin.srcDir("src/main/kotlin-jvm")
}
// JVM-specific tests and their dependencies:
compilations["test"].defaultSourceSet {
dependencies {
implementation(project(":kotlin-test:kotlin-test-junit"))
}
}
compilations.all {
kotlinOptions {
freeCompilerArgs = listOf("-opt-in=kotlinx.cli.ExperimentalCli", "-opt-in=kotlin.RequiresOptIn")
suppressWarnings = true
}
}
}
}
}
val commonSrc = project.file("src/main/kotlin")
val nativeSrc = project.file("src/main/kotlin-native")
val targetList: List<String> by project
val endorsedLibraries: Map<Project, EndorsedLibraryInfo> by project(":kotlin-native:endorsedLibraries").ext
val library = endorsedLibraries[project] ?: throw IllegalStateException("Library for $project is not set")
lateinit var buildTask: TaskProvider<Task>
konanArtifacts {
library(library.name) {
baseDir(project.buildDir.resolve(library.name))
noPack(true)
noDefaultLibs(true)
noEndorsedLibs(true)
enableMultiplatform(true)
// See :endorsedLibraries.ext for full endorsedLibraries list.
val moduleName = endorsedLibraries[project]?.name.toString()
extraOpts(project.globalBuildArgs)
extraOpts(
"-Werror",
"-module-name", moduleName,
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlin.ExperimentalMultiplatform",
"-opt-in=kotlinx.cli.ExperimentalCli"
)
commonSrcDir(commonSrc)
srcDir(nativeSrc)
buildTask = project.findKonanBuildTask(library.name, project.platformManager.hostPlatform.target).apply {
configure {
dependsOn(":kotlin-native:distCompiler")
dependsOn(":kotlin-native:distRuntime")
}
}
}
}
val hostName: String by project
val cacheableTargetNames: List<String> by project
targetList.forEach { targetName ->
val copyTask = tasks.register("${targetName}${library.name}", Copy::class.java) {
require(::buildTask.isInitialized)
dependsOn(buildTask)
destinationDir = buildDir.resolve("$targetName${library.name}")
// We build a single klib file for host platform and then copy it to other platforms
// creating target dirs and adding targets to manifest file
val libFile = buildDir.resolve("${library.name}/${hostName}/${library.name}")
from(libFile) {
exclude("default/targets/$hostName")
}
from(libFile.resolve("default/targets/$hostName")) {
into("default/targets/$targetName")
}
if (targetName != hostName) {
doLast {
// Change target in manifest file
with(KFile(destinationDir.resolve("default/manifest").absolutePath)) {
val props = loadProperties()
props[KLIB_PROPERTY_NATIVE_TARGETS] = targetName
saveProperties(props)
}
}
}
}
if (targetName in cacheableTargetNames) {
tasks.register("${targetName}${library.taskName}Cache", KonanCacheTask::class.java) {
target = targetName
originalKlib = project.buildDir.resolve("$targetName${library.name}")
cacheRoot = project.buildDir.resolve("cache/$targetName").absolutePath
cachedLibraries = mapOf(distDir.resolve("klib/common/stdlib") to
distDir.resolve("klib/cache/${target}-g$cacheKind/stdlib-cache"))
dependsOn(copyTask)
dependsOn(":kotlin-native:${targetName}CrossDistStdlib")
dependsOn(":kotlin-native:${targetName}StdlibCache")
}
}
}