Common iOS: Store konanTarget in KotlinNativeCompilation

This commit is contained in:
Ilya Matveev
2019-08-28 13:04:01 +07:00
parent c185cecb40
commit 6b10336497
7 changed files with 21 additions and 19 deletions
@@ -20,10 +20,12 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.utils.SingleWarningPerBuild import org.jetbrains.kotlin.gradle.utils.SingleWarningPerBuild
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.konan.target.KonanTarget
import java.util.concurrent.Callable import java.util.concurrent.Callable
class KotlinNativeCompilation( class KotlinNativeCompilation(
override val target: KotlinNativeTarget, override val target: KotlinNativeTarget,
val konanTarget: KonanTarget,
name: String name: String
) : AbstractKotlinCompilation<KotlinCommonOptions>(target, name), KotlinCompilationWithResources<KotlinCommonOptions> { ) : AbstractKotlinCompilation<KotlinCommonOptions>(target, name), KotlinCompilationWithResources<KotlinCommonOptions> {
@@ -7,9 +7,6 @@
package org.jetbrains.kotlin.gradle.plugin.mpp package org.jetbrains.kotlin.gradle.plugin.mpp
import org.gradle.api.Project import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.whenEvaluated
import org.jetbrains.kotlin.gradle.targets.native.CompilationFreeArgsValidator
class KotlinNativeCompilationFactory( class KotlinNativeCompilationFactory(
val project: Project, val project: Project,
@@ -24,5 +21,5 @@ class KotlinNativeCompilationFactory(
// when the compilation and the link args are separated (see KT-33717). // when the compilation and the link args are separated (see KT-33717).
// Note: such validation should be done in the whenEvaluate block because // Note: such validation should be done in the whenEvaluate block because
// a user can change args during project configuration. // a user can change args during project configuration.
KotlinNativeCompilation(target, name) KotlinNativeCompilation(target, target.konanTarget, name)
} }
@@ -59,7 +59,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
producingTask: Task, producingTask: Task,
copy: Boolean = false copy: Boolean = false
) { ) {
if (!compilation.target.konanTarget.enabledOnCurrentHost) { if (!compilation.konanTarget.enabledOnCurrentHost) {
return return
} }
@@ -118,7 +118,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
this.binary = binary this.binary = binary
group = BasePlugin.BUILD_GROUP group = BasePlugin.BUILD_GROUP
description = "Links ${binary.outputKind.description} '${binary.name}' for a target '${target.name}'." description = "Links ${binary.outputKind.description} '${binary.name}' for a target '${target.name}'."
enabled = target.konanTarget.enabledOnCurrentHost enabled = binary.konanTarget.enabledOnCurrentHost
destinationDir = binary.outputDirectory destinationDir = binary.outputDirectory
addCompilerPlugins() addCompilerPlugins()
@@ -135,7 +135,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
group = RUN_GROUP group = RUN_GROUP
description = "Executes Kotlin/Native executable ${binary.name} for target ${binary.target.name}" description = "Executes Kotlin/Native executable ${binary.name} for target ${binary.target.name}"
enabled = binary.target.konanTarget.isCurrentHost enabled = binary.konanTarget.isCurrentHost
executable = binary.outputFile.absolutePath executable = binary.outputFile.absolutePath
workingDir = project.projectDir workingDir = project.projectDir
@@ -154,7 +154,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
group = BasePlugin.BUILD_GROUP group = BasePlugin.BUILD_GROUP
description = "Compiles a klibrary from the '${compilation.name}' " + description = "Compiles a klibrary from the '${compilation.name}' " +
"compilation for target '${compilation.platformType.name}'." "compilation for target '${compilation.platformType.name}'."
enabled = compilation.target.konanTarget.enabledOnCurrentHost enabled = compilation.konanTarget.enabledOnCurrentHost
destinationDir = klibOutputDirectory(compilation) destinationDir = klibOutputDirectory(compilation)
addCompilerPlugins() addCompilerPlugins()
@@ -185,7 +185,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
description = "Generates Kotlin/Native interop library '${interop.name}' " + description = "Generates Kotlin/Native interop library '${interop.name}' " +
"for compilation '${compilation.name}'" + "for compilation '${compilation.name}'" +
"of target '${konanTarget.name}'." "of target '${konanTarget.name}'."
enabled = compilation.target.konanTarget.enabledOnCurrentHost enabled = compilation.konanTarget.enabledOnCurrentHost
val interopOutput = project.files(outputFileProvider).builtBy(this) val interopOutput = project.files(outputFileProvider).builtBy(this)
with(compilation) { with(compilation) {
@@ -90,8 +90,8 @@ abstract class AbstractKotlinNativeTargetPreset<T : KotlinNativeTarget>(
// Allow IDE to resolve the libraries provided by the compiler by adding them into dependencies. // Allow IDE to resolve the libraries provided by the compiler by adding them into dependencies.
result.compilations.all { compilation -> result.compilations.all { compilation ->
val target = compilation.target.konanTarget val target = compilation.konanTarget
compilation.target.project.whenEvaluated { project.whenEvaluated {
// First, put common libs: // First, put common libs:
defaultLibs(!compilation.enableEndorsedLibs).forEach { defaultLibs(!compilation.enableEndorsedLibs).forEach {
project.dependencies.add(compilation.compileDependencyConfigurationName, it) project.dependencies.add(compilation.compileDependencyConfigurationName, it)
@@ -32,6 +32,9 @@ sealed class NativeBinary(
var compilation: KotlinNativeCompilation var compilation: KotlinNativeCompilation
) : Named { ) : Named {
internal val konanTarget: KonanTarget
get() = compilation.konanTarget
val target: KotlinNativeTarget val target: KotlinNativeTarget
get() = compilation.target get() = compilation.target
@@ -120,7 +123,7 @@ class Executable constructor(
* Returns null if the executables's target is not a host one (macosX64, linuxX64 or mingw64). * Returns null if the executables's target is not a host one (macosX64, linuxX64 or mingw64).
*/ */
val runTaskName: String? val runTaskName: String?
get() = if (target.konanTarget in listOf(KonanTarget.MACOS_X64, KonanTarget.LINUX_X64, KonanTarget.MINGW_X64)) { get() = if (konanTarget in listOf(KonanTarget.MACOS_X64, KonanTarget.LINUX_X64, KonanTarget.MINGW_X64)) {
lowerCamelCaseName("run", name, compilation.target.targetName) lowerCamelCaseName("run", name, compilation.target.targetName)
} else { } else {
null null
@@ -215,7 +218,7 @@ class Framework(
/** /**
* Embed bitcode for the framework or not. See [BitcodeEmbeddingMode]. * Embed bitcode for the framework or not. See [BitcodeEmbeddingMode].
*/ */
var embedBitcode: BitcodeEmbeddingMode = buildType.embedBitcode(target.konanTarget) var embedBitcode: BitcodeEmbeddingMode = buildType.embedBitcode(konanTarget)
/** /**
* Enable or disable embedding bitcode for the framework. See [BitcodeEmbeddingMode]. * Enable or disable embedding bitcode for the framework. See [BitcodeEmbeddingMode].
@@ -130,7 +130,7 @@ open class FatFrameworkTask: DefaultTask() {
*/ */
fun from(frameworks: Iterable<Framework>) { fun from(frameworks: Iterable<Framework>) {
frameworks.forEach { frameworks.forEach {
val konanTarget = it.target.konanTarget val konanTarget = it.konanTarget
val arch = konanTarget.architecture val arch = konanTarget.architecture
require(konanTarget.family == Family.IOS) { require(konanTarget.family == Family.IOS) {
"Cannot add a framework with target '${konanTarget.visibleName}' to the fat framework: " + "Cannot add a framework with target '${konanTarget.visibleName}' to the fat framework: " +
@@ -165,14 +165,14 @@ open class FatFrameworkTask: DefaultTask() {
} }
private val Framework.plistPlatform: String private val Framework.plistPlatform: String
get() = when(target.konanTarget) { get() = when(konanTarget) {
KonanTarget.IOS_ARM32, KonanTarget.IOS_ARM64 -> "iPhoneOS" KonanTarget.IOS_ARM32, KonanTarget.IOS_ARM64 -> "iPhoneOS"
KonanTarget.IOS_X64 -> "iPhoneSimulator" KonanTarget.IOS_X64 -> "iPhoneSimulator"
KonanTarget.TVOS_ARM64 -> "appletvos" KonanTarget.TVOS_ARM64 -> "appletvos"
KonanTarget.TVOS_X64 -> "appletvsimulator" KonanTarget.TVOS_X64 -> "appletvsimulator"
KonanTarget.WATCHOS_ARM32, KonanTarget.WATCHOS_ARM64 -> "watchos" KonanTarget.WATCHOS_ARM32, KonanTarget.WATCHOS_ARM64 -> "watchos"
KonanTarget.WATCHOS_X86, KonanTarget.WATCHOS_X64 -> "watchsimulator" KonanTarget.WATCHOS_X86, KonanTarget.WATCHOS_X64 -> "watchsimulator"
else -> error("Fat frameworks are not supported for target `${target.konanTarget.visibleName}`") else -> error("Fat frameworks are not supported for target `${konanTarget.visibleName}`")
} }
// Runs the PlistBuddy utility with the given commands to configure the given plist file. // Runs the PlistBuddy utility with the given commands to configure the given plist file.
@@ -118,7 +118,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions> : Abstra
} }
val target: String val target: String
@Input get() = compilation.target.konanTarget.name @Input get() = compilation.konanTarget.name
// region Compiler options. // region Compiler options.
@get:Internal @get:Internal
@@ -148,7 +148,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions> : Abstra
// OutputFile is located under the destinationDir, so there is no need to register it as a separate output. // OutputFile is located under the destinationDir, so there is no need to register it as a separate output.
@Internal @Internal
val outputFile: Provider<File> = project.provider { val outputFile: Provider<File> = project.provider {
val konanTarget = compilation.target.konanTarget val konanTarget = compilation.konanTarget
val prefix = outputKind.prefix(konanTarget) val prefix = outputKind.prefix(konanTarget)
val suffix = outputKind.suffix(konanTarget) val suffix = outputKind.suffix(konanTarget)
@@ -614,7 +614,7 @@ open class CInteropProcess : DefaultTask() {
lateinit var destinationDir: Provider<File> lateinit var destinationDir: Provider<File>
val konanTarget: KonanTarget val konanTarget: KonanTarget
@Internal get() = settings.compilation.target.konanTarget @Internal get() = settings.compilation.konanTarget
val interopName: String val interopName: String
@Internal get() = settings.name @Internal get() = settings.name