diff --git a/kotlin-native/endorsedLibraries/build.gradle b/kotlin-native/endorsedLibraries/build.gradle index 15de72a643f..26d230826b8 100644 --- a/kotlin-native/endorsedLibraries/build.gradle +++ b/kotlin-native/endorsedLibraries/build.gradle @@ -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" } } } diff --git a/kotlin-native/endorsedLibraries/kotlinx.cli/build.gradle b/kotlin-native/endorsedLibraries/kotlinx.cli/build.gradle deleted file mode 100644 index 9c8fc66ed33..00000000000 --- a/kotlin-native/endorsedLibraries/kotlinx.cli/build.gradle +++ /dev/null @@ -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) - } -} diff --git a/kotlin-native/endorsedLibraries/kotlinx.cli/build.gradle.kts b/kotlin-native/endorsedLibraries/kotlinx.cli/build.gradle.kts new file mode 100644 index 00000000000..a57934ef6f9 --- /dev/null +++ b/kotlin-native/endorsedLibraries/kotlinx.cli/build.gradle.kts @@ -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 by project +val endorsedLibraries: Map by project(":kotlin-native:endorsedLibraries").ext + +val library = endorsedLibraries[project] ?: throw IllegalStateException("Library for $project is not set") +lateinit var buildTask: TaskProvider + +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 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") + } + } +} \ No newline at end of file