New flag for KotlinNativeCompilation to enable endorsed libraries (#2618)
This commit is contained in:
+19
@@ -691,6 +691,25 @@ class NewMultiplatformIT : BaseGradleIT() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testEndorsedLibsController() {
|
||||
with(
|
||||
transformProjectWithPluginsDsl("new-mpp-native-endorsed", gradleVersion)
|
||||
) {
|
||||
setupWorkingDir()
|
||||
|
||||
build("build") {
|
||||
assertSuccessful()
|
||||
}
|
||||
gradleBuildScript().modify {
|
||||
it.replace("enableEndorsedLibs = true", "")
|
||||
}
|
||||
build("build") {
|
||||
assertFailed()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDependenciesOnMppLibraryPartsWithNoMetadata() {
|
||||
val repoDir = with(Project("sample-lib", gradleVersion, "new-mpp-lib-and-app")) {
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
plugins {
|
||||
id("org.jetbrains.kotlin.multiplatform").version("<pluginMarkerVersion>")
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
maven { setUrl("https://dl.bintray.com/kotlin/kotlinx.html/") }
|
||||
}
|
||||
|
||||
kotlin {
|
||||
|
||||
val macos = macosX64("macos64")
|
||||
val linux = linuxX64("linux64")
|
||||
val windows = mingwX64("mingw64")
|
||||
|
||||
sourceSets {
|
||||
val commonNative by creating {}
|
||||
|
||||
windows.compilations["main"].defaultSourceSet {
|
||||
dependsOn(commonNative)
|
||||
}
|
||||
linux.compilations["main"].defaultSourceSet {
|
||||
dependsOn(commonNative)
|
||||
}
|
||||
macos.compilations["main"].defaultSourceSet {
|
||||
dependsOn(commonNative)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
configure(listOf(macos, linux, windows)) {
|
||||
compilations.all {
|
||||
kotlinOptions.verbose = true
|
||||
enableEndorsedLibs = true
|
||||
}
|
||||
binaries {
|
||||
executable()
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
}
|
||||
|
||||
enableFeaturePreview('GRADLE_METADATA')
|
||||
|
||||
rootProject.name = "native-endorsed"
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
import kotlinx.cli.*
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val argParser = ArgParser("test")
|
||||
val mode by argParser.option(
|
||||
ArgType.Choice(listOf("video", "audio", "both")), shortName = "m", description = "Play mode")
|
||||
.default("both")
|
||||
val size by argParser.option(ArgType.Int, shortName = "s", description = "Required size of videoplayer window")
|
||||
.delimiter(",")
|
||||
val fileName by argParser.argument(ArgType.String, description = "File to play")
|
||||
argParser.parse(args)
|
||||
}
|
||||
+3
@@ -86,6 +86,9 @@ class KotlinNativeCompilation(
|
||||
DefaultCInteropSettings(project, cinteropName, this)
|
||||
}
|
||||
|
||||
// Endorsed library controller.
|
||||
var enableEndorsedLibs = false
|
||||
|
||||
fun cinterops(action: Closure<Unit>) = cinterops(ConfigureUtil.configureUsing(action))
|
||||
fun cinterops(action: Action<NamedDomainObjectContainer<DefaultCInteropSettings>>) = action.execute(cinterops)
|
||||
|
||||
|
||||
+29
-17
@@ -4,14 +4,13 @@
|
||||
*/
|
||||
|
||||
@file:Suppress("PackageDirectoryMismatch") // Old package for compatibility
|
||||
|
||||
package org.jetbrains.kotlin.gradle.plugin.mpp
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.artifacts.Dependency
|
||||
import org.jetbrains.kotlin.compilerRunner.konanHome
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinNativeTargetConfigurator
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset
|
||||
import org.jetbrains.kotlin.gradle.plugin.*
|
||||
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
|
||||
import org.jetbrains.kotlin.gradle.targets.native.DisabledNativeTargetsReporter
|
||||
import org.jetbrains.kotlin.gradle.utils.NativeCompilerDownloader
|
||||
@@ -49,15 +48,25 @@ class KotlinNativeTargetPreset(
|
||||
}
|
||||
}
|
||||
|
||||
// We declare default K/N dependencies as files to avoid searching them in remote repos (see KT-28128).
|
||||
private fun defaultLibs(target: KonanTarget? = null): List<Dependency> = with(project) {
|
||||
|
||||
val relPath = if (target != null) "platform/${target.name}" else "common"
|
||||
|
||||
file("$konanHome/klib/$relPath")
|
||||
private fun nativeLibrariesList(directory: String) = with(project) {
|
||||
file("$konanHome/klib/$directory")
|
||||
.listFiles { file -> file.isDirectory }
|
||||
?.sortedBy { dir -> dir.name.toLowerCase() }
|
||||
?.map { dir -> dependencies.create(files(dir)) } ?: emptyList()
|
||||
}
|
||||
|
||||
// We declare default K/N dependencies (default and platform libraries) as files to avoid searching them in remote repos (see KT-28128).
|
||||
private fun defaultLibs(stdlibOnly: Boolean = false): List<Dependency> = with(project) {
|
||||
var filesList = nativeLibrariesList("common")
|
||||
if (stdlibOnly) {
|
||||
filesList = filesList?.filter { dir -> dir.name == "stdlib" }
|
||||
}
|
||||
|
||||
filesList?.map { dir -> dependencies.create(files(dir)) } ?: emptyList()
|
||||
}
|
||||
|
||||
private fun platformLibs(target: KonanTarget): List<Dependency> = with(project) {
|
||||
val filesList = nativeLibrariesList("platform/${target.name}")
|
||||
filesList?.map { dir -> dependencies.create(files(dir)) } ?: emptyList()
|
||||
}
|
||||
|
||||
override fun createTarget(name: String): KotlinNativeTarget {
|
||||
@@ -75,15 +84,18 @@ class KotlinNativeTargetPreset(
|
||||
KotlinNativeTargetConfigurator(kotlinPluginVersion).configureTarget(result)
|
||||
|
||||
// Allow IDE to resolve the libraries provided by the compiler by adding them into dependencies.
|
||||
|
||||
result.compilations.all { compilation ->
|
||||
val target = compilation.target.konanTarget
|
||||
// First, put common libs:
|
||||
defaultLibs().forEach {
|
||||
project.dependencies.add(compilation.compileDependencyConfigurationName, it)
|
||||
}
|
||||
// Then, platform-specific libs:
|
||||
defaultLibs(target).forEach {
|
||||
project.dependencies.add(compilation.compileDependencyConfigurationName, it)
|
||||
compilation.target.project.whenEvaluated {
|
||||
// First, put common libs:
|
||||
defaultLibs(!compilation.enableEndorsedLibs).forEach {
|
||||
project.dependencies.add(compilation.compileDependencyConfigurationName, it)
|
||||
}
|
||||
// Then, platform-specific libs:
|
||||
platformLibs(target).forEach {
|
||||
project.dependencies.add(compilation.compileDependencyConfigurationName, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -139,6 +139,10 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions> : Abstra
|
||||
get() = languageSettings.progressiveMode
|
||||
// endregion.
|
||||
|
||||
@get:Input
|
||||
val enableEndorsedLibs: Boolean
|
||||
get() = compilation.enableEndorsedLibs
|
||||
|
||||
val kotlinNativeVersion: String
|
||||
@Input get() = project.konanVersion.toString()
|
||||
|
||||
@@ -184,8 +188,10 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions> : Abstra
|
||||
// Args used by both the compiler and IDEA.
|
||||
protected open fun buildCommonArgs(defaultsOnly: Boolean = false): List<String> = mutableListOf<String>().apply {
|
||||
add("-Xmulti-platform")
|
||||
// Endorsed libs are disabled in plugin.
|
||||
add("-no-endorsed-libs")
|
||||
|
||||
if (!enableEndorsedLibs) {
|
||||
add("-no-endorsed-libs")
|
||||
}
|
||||
|
||||
// Compiler plugins.
|
||||
compilerPluginClasspath?.let { pluginClasspath ->
|
||||
|
||||
Reference in New Issue
Block a user