diff --git a/kotlin-native/platformLibs/build.gradle b/kotlin-native/platformLibs/build.gradle deleted file mode 100644 index 5bbefed7509..00000000000 --- a/kotlin-native/platformLibs/build.gradle +++ /dev/null @@ -1,148 +0,0 @@ -import org.jetbrains.kotlin.gradle.plugin.konan.tasks.KonanCacheTask -import org.jetbrains.kotlin.KlibInstall -import org.jetbrains.kotlin.konan.target.* -import org.jetbrains.kotlin.konan.util.* -import org.jetbrains.kotlin.UtilsKt - -import static org.jetbrains.kotlin.konan.util.VisibleNamedKt.getVisibleName - -buildscript { - repositories { - if (UtilsKt.getCacheRedirectorEnabled(project)) - maven { url 'https://cache-redirector.jetbrains.com/maven-central'} - else - mavenCentral() - maven { - url project.bootstrapKotlinRepo - } - } -} -// These properties are used by the 'konan' plugin, thus we set them before applying it. -ext.konanHome = distDir.absolutePath -def jvmArguments = [project.findProperty("platformLibsJvmArgs") ?: "-Xmx6G", *HostManager.defaultJvmArgs] -ext.jvmArgs = jvmArguments.join(" ") -ext.setProperty("org.jetbrains.kotlin.native.home", konanHome) -ext.setProperty("konan.jvmArgs", jvmArgs) - -apply plugin: 'konan' - -//#region Util functions. -private ArrayList targetDefFiles(KonanTarget target) { - file("src/platform/${getVisibleName(target.family)}") - .listFiles() - .findAll { it.name.endsWith(".def") } - // The libz.a/libz.so and zlib.h are missing in MIPS sysroots. - // Just workaround it until we have sysroots corrected. - .findAll { ! ((target in targetsWithoutZlib) && it.name == 'zlib.def') } - .collect { DefFileKt.DefFile(it, target) } -} - -private String defFileToLibName(String target, String name) { - return "$target-$name".toString() -} -//#endregion - -if (HostManager.host.name == "macos_arm64") { - JvmToolchain.configureJvmToolchain(project, JdkMajorVersion.JDK_17) -} - -// TODO: I think most for the non-DSL language below can either be incorporated into DSL -// or moved out of .gradle file. -rootProject.project("kotlin-native").ext.platformManager.enabled.each { target -> - - def targetName = target.visibleName - - ArrayList installTasks = [] - ArrayList cacheTasks = [] - - targetDefFiles(target).each { df -> - def libName = defFileToLibName(targetName, df.name) - def fileNamePrefix = PlatformLibsInfo.namePrefix - - konanArtifacts { - interop(libName, targets: [targetName]) { - defFile df.file - artifactName "${fileNamePrefix}${df.name}" - noDefaultLibs true - noEndorsedLibs true - libraries { - klibs df.config.depends.collect { - "${fileNamePrefix}${it}".toString() - } - } - extraOpts '-Xpurge-user-libs', "-Xshort-module-name", df.name - compilerOpts "-fmodules-cache-path=${project.buildDir}/clangModulesCache" - } - } - - def libTask = konanArtifacts."$libName"."$targetName" - libTask.configure { - it.dependsOn df.config.depends.collect { defFileToLibName(targetName, it) } - it.dependsOn ":kotlin-native:${targetName}CrossDist" - it.enableParallel = project.hasProperty("kotlin.native.platformLibs.parallel") - ? project.findProperty("kotlin.native.platformLibs.parallel")?.toString()?.toBoolean() - : HostManager.host != "macos_arm64" - } - - def klibInstallTask = tasks.register("$libName", KlibInstall) { - it.klib = project.provider { libTask.get().artifact } - it.repo = file("$konanHome/klib/platform/$targetName") - it.target = targetName - it.dependsOn libTask - } - installTasks.add(klibInstallTask) - - if (target in cacheableTargets) { - def cacheTask = tasks.register("${libName}Cache", KonanCacheTask) { - it.target = targetName - it.originalKlib = tasks[libName].installDir.get() - it.cacheRoot = file("$konanHome/klib/cache").absolutePath - - it.dependsOn ":kotlin-native:${targetName}StdlibCache" - it.dependsOn tasks[libName] - it.dependsOn df.config.depends.collect { - def depName = defFileToLibName(targetName, it) - "${depName}Cache" - } - } - cacheTasks.add(cacheTask) - } - } - - tasks.register("${targetName}Install") { - it.dependsOn installTasks - } - - if (target in cacheableTargets) { - tasks.register("${targetName}Cache") { - it.dependsOn cacheTasks - - it.group = BasePlugin.BUILD_GROUP - it.description = "Builds the compilation cache for platform: ${targetName}" - } - } -} - -// TODO: Don't install libraries here - copy them in the distPlatformLibs task -task hostInstall { - dependsOn tasks.withType(KlibInstall.class).matching { - it.target == HostManager.hostName - } -} - -task hostCache { - dependsOn tasks.withType(KonanCacheTask.class).matching { - it.target == HostManager.hostName - } -} - -task install { - dependsOn tasks.withType(KlibInstall.class) -} - -task cache { - dependsOn tasks.withType(KonanCacheTask.class) - - group = BasePlugin.BUILD_GROUP - description = "Builds all the compilation caches" -} diff --git a/kotlin-native/platformLibs/build.gradle.kts b/kotlin-native/platformLibs/build.gradle.kts new file mode 100644 index 00000000000..d5b5002bea7 --- /dev/null +++ b/kotlin-native/platformLibs/build.gradle.kts @@ -0,0 +1,146 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +import org.jetbrains.kotlin.KlibInstall +import org.jetbrains.kotlin.gradle.plugin.konan.tasks.KonanCacheTask +import org.jetbrains.kotlin.gradle.plugin.tasks.KonanInteropTask +import org.jetbrains.kotlin.konan.target.* +import org.jetbrains.kotlin.konan.util.* + +// These properties are used by the 'konan' plugin, thus we set them before applying it. +val distDir: File by project +val konanHome: String by extra(distDir.absolutePath) +val jvmArgs: String by extra( + mutableListOf().apply { + addAll(HostManager.defaultJvmArgs) + add(project.findProperty("platformLibsJvmArgs") as? String ?: "-Xmx6G") + }.joinToString(" ") +) + +extra["org.jetbrains.kotlin.native.home"] = konanHome +extra["konan.jvmArgs"] = jvmArgs + +plugins { + id("konan") +} + +val targetsWithoutZlib: List by project + +// region: Util functions. +fun targetDefFiles(target: KonanTarget) = + project.fileTree("src/platform/${target.family.visibleName}") + .filter { it.name.endsWith(".def") } + // The libz.a/libz.so and zlib.h are missing in MIPS sysroots. + // Just workaround it until we have sysroots corrected. + .filter { ! ((target in targetsWithoutZlib) && it.name == "zlib.def") } + .map { DefFile(it, target) } + + +fun defFileToLibName(target: String, name: String) = "$target-$name" + +// endregion + +if (HostManager.host == KonanTarget.MACOS_ARM64) { + project.configureJvmToolchain(JdkMajorVersion.JDK_17) +} + +val konanTargetList: List by project +val targetList: List by project +val cacheableTargets: List by project + +konanTargetList.forEach { target -> + val targetName = target.visibleName + val installTasks = mutableListOf>() + val cacheTasks = mutableListOf>() + + targetDefFiles(target).forEach { df -> + val libName = defFileToLibName(targetName, df.name) + val fileNamePrefix = PlatformLibsInfo.namePrefix + + konanArtifacts { + interop( + args = mapOf("targets" to listOf(targetName)), + name = libName + ) { + df.file?.let { defFile(it) } + artifactName("${fileNamePrefix}${df.name}") + noDefaultLibs(true) + noEndorsedLibs(true) + libraries { + klibs(df.config.depends.map { "${fileNamePrefix}${it}" }) + } + extraOpts("-Xpurge-user-libs", "-Xshort-module-name", df.name) + compilerOpts("-fmodules-cache-path=${project.buildDir}/clangModulesCache") + } + } + + @kotlin.Suppress("UNCHECKED_CAST") + val libTask = konanArtifacts.getByName(libName).getByTarget(targetName) as? TaskProvider + libTask?.configure { + dependsOn(df.config.depends.map { defFileToLibName(targetName, it) }) + dependsOn(":kotlin-native:${targetName}CrossDist") + + enableParallel = if (project.hasProperty("kotlin.native.platformLibs.parallel")) { + project.findProperty("kotlin.native.platformLibs.parallel")?.toString()?.toBoolean() ?: false + } else { + (HostManager.host != KonanTarget.MACOS_ARM64) + } + } + + val klibInstallTask = tasks.register(libName, KlibInstall::class.java) { + klib = project.provider { libTask?.get()?.artifact } + repo = file("$konanHome/klib/platform/$targetName") + this.target = targetName + dependsOn(libTask) + } + installTasks.add(klibInstallTask) + + if (target in cacheableTargets) { + val cacheTask = tasks.register("${libName}Cache", KonanCacheTask::class.java) { + this.target = targetName + originalKlib = klibInstallTask.get().installDir.get() + cacheRoot = file("$konanHome/klib/cache").absolutePath + + dependsOn(":kotlin-native:${targetName}StdlibCache") + dependsOn(tasks[libName]) + dependsOn(df.config.depends.map { + val depName = defFileToLibName(targetName, it) + "${depName}Cache" + }) + } + cacheTasks.add(cacheTask) + } + } + + tasks.register("${targetName}Install") { + dependsOn(installTasks) + } + + if (target in cacheableTargets) { + tasks.register("${targetName}Cache") { + dependsOn(cacheTasks) + + group = BasePlugin.BUILD_GROUP + description = "Builds the compilation cache for platform: $targetName" + } + } +} + +val hostName: String by project + +val hostInstall by tasks.registering { + dependsOn("${hostName}Install") +} + +val hostCache by tasks.registering { + dependsOn("${hostName}Cache") +} + +val cache by tasks.registering { + dependsOn(tasks.withType(KonanCacheTask::class.java)) + + group = BasePlugin.BUILD_GROUP + description = "Builds all the compilation caches" +}