Native: don't pack endorsed libraries (namely kotlinx.cli) into dist

The overall concept of "endorsed" libraries is currently unmaintained
and non-reviewed.
kotlinx-cli is in its alpha, and is available from Maven.

See also KT-54098.
This commit is contained in:
Svyatoslav Scherbina
2023-05-02 11:40:40 +02:00
committed by Space Team
parent 7b45f75213
commit 16acb72521
4 changed files with 5 additions and 260 deletions
@@ -1,97 +0,0 @@
import org.jetbrains.kotlin.EndorsedLibraryInfo
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.kotlinNativeDist
import org.jetbrains.kotlin.mergeManifestsByTargets
if (HostManager.host == KonanTarget.MACOS_ARM64) {
project.configureJvmToolchain(JdkMajorVersion.JDK_17_0)
}
plugins {
base
}
val endorsedLibraries = listOf(EndorsedLibraryInfo(project("kotlinx.cli"), "org.jetbrains.kotlinx.kotlinx-cli"))
extra["endorsedLibraries"] = endorsedLibraries.associateBy { it.project }
tasks.register("jvmJar") {
endorsedLibraries.forEach { library ->
dependsOn("${library.projectName}:jvmJar")
}
}
val targetList: List<String> by project
val cacheableTargetNames: List<String> by project
// Build all default libraries.
targetList.forEach { target ->
tasks.create("${target}EndorsedLibraries") {
endorsedLibraries.forEach { library ->
dependsOn(tasks.register("${target}${library.name}EndorsedLibraries", Copy::class.java) {
dependsOn("${library.projectName}:${target}${library.taskName}")
destinationDir = project.file("${project.buildDir}/${library.name}")
from(library.project.file("build/${target}${library.name}")) {
include("**")
eachFile {
if (name == "manifest") {
val existingManifest = file("$destinationDir/$path")
if (existingManifest.exists()) {
project.mergeManifestsByTargets(file, existingManifest)
exclude()
}
}
}
}
})
}
}
if (target in cacheableTargetNames) {
val cacheTask = tasks.register("${target}Cache", Copy::class.java) {
destinationDir = project.file("${project.buildDir}/cache/$target")
endorsedLibraries.forEach { library ->
from(library.project.file("build/cache/$target")) {
include("**")
}
}
}
endorsedLibraries.forEach { library ->
cacheTask.configure {
dependsOn("${library.projectName}:${target}${library.taskName}Cache")
}
}
}
}
endorsedLibraries.forEach { library ->
tasks.register("${library.taskName}CommonSources", Zip::class.java) {
destinationDirectory.set(file("${project.kotlinNativeDist}/sources"))
archiveFileName.set("${library.name}-common-sources.zip")
includeEmptyDirs = false
include("**/*.kt")
from(library.project.file("src/main/kotlin"))
}
tasks.register("${library.taskName}NativeSources", Zip::class.java) {
destinationDirectory.set(file("${project.kotlinNativeDist}/sources"))
archiveFileName.set("${library.name}-native-sources.zip")
includeEmptyDirs = false
include("**/*.kt")
from(library.project.file("src/main/kotlin-native"))
}
}
tasks.register("endorsedLibsSources") {
endorsedLibraries.forEach { library ->
dependsOn("${library.taskName}CommonSources")
dependsOn("${library.taskName}NativeSources")
}
}
@@ -1,18 +1,7 @@
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")
id("kotlin.native.build-tools-conventions")
id("konan")
}
kotlin {
@@ -54,88 +43,3 @@ kotlin {
}
}
}
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")
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)
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.taskName}", Copy::class.java) {
dependsOn(project.findKonanBuildTask(library.name, project.platformManager.hostPlatform.target))
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}")
klibUniqName = 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")
}
}
}