|
| 1 | +/* |
| 2 | + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license |
| 3 | + * that can be found in the license/LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +@file:JvmName("MPPTools") |
| 7 | + |
| 8 | +import groovy.lang.Closure |
| 9 | +import org.gradle.api.Project |
| 10 | +import org.gradle.api.Task |
| 11 | +import org.jetbrains.kotlin.gradle.plugin.KotlinTarget |
| 12 | +import org.jetbrains.kotlin.gradle.plugin.KotlinTargetPreset |
| 13 | +import java.nio.file.Paths |
| 14 | + |
| 15 | +/* |
| 16 | + * This file includes short-cuts that may potentially be implemented in Kotlin MPP Gradle plugin in the future. |
| 17 | + */ |
| 18 | + |
| 19 | +// Short-cuts for detecting the host OS. |
| 20 | +@get:JvmName("isMacos") |
| 21 | +val isMacos by lazy { hostOs == "Mac OS X" } |
| 22 | + |
| 23 | +@get:JvmName("isWindows") |
| 24 | +val isWindows by lazy { hostOs.startsWith("Windows") } |
| 25 | + |
| 26 | +@get:JvmName("isLinux") |
| 27 | +val isLinux by lazy { hostOs == "Linux" } |
| 28 | + |
| 29 | +// Short-cuts for mostly used paths. |
| 30 | +@get:JvmName("mingwPath") |
| 31 | +val mingwPath by lazy { System.getenv("MINGW64_DIR") ?: "c:/msys64/mingw64" } |
| 32 | + |
| 33 | +@get:JvmName("kotlinNativeDataPath") |
| 34 | +val kotlinNativeDataPath by lazy { |
| 35 | + System.getenv("KONAN_DATA_DIR") ?: Paths.get(userHome, ".konan").toString() |
| 36 | +} |
| 37 | + |
| 38 | +// A short-cut for evaluation of the default host Kotlin/Native preset. |
| 39 | +@JvmOverloads |
| 40 | +fun defaultHostPreset( |
| 41 | + subproject: Project, |
| 42 | + whitelist: List<KotlinTargetPreset<*>> = listOf(subproject.kotlin.presets.macosX64, subproject.kotlin.presets.linuxX64, subproject.kotlin.presets.mingwX64) |
| 43 | +): KotlinTargetPreset<*> { |
| 44 | + |
| 45 | + if (whitelist.isEmpty()) |
| 46 | + throw Exception("Preset whitelist must not be empty in Kotlin/Native ${subproject.displayName}.") |
| 47 | + |
| 48 | + val presetCandidate = when { |
| 49 | + isMacos -> subproject.kotlin.presets.macosX64 |
| 50 | + isLinux -> subproject.kotlin.presets.linuxX64 |
| 51 | + isWindows -> subproject.kotlin.presets.mingwX64 |
| 52 | + else -> null |
| 53 | + } |
| 54 | + |
| 55 | + val preset = if (presetCandidate != null && presetCandidate in whitelist) |
| 56 | + presetCandidate |
| 57 | + else |
| 58 | + throw Exception("Host OS '$hostOs' is not supported in Kotlin/Native ${subproject.displayName}.") |
| 59 | + |
| 60 | + subproject.ext.set("hostPreset", preset) |
| 61 | + |
| 62 | + return preset |
| 63 | +} |
| 64 | + |
| 65 | +// A short-cut to add a Kotlin/Native run task. |
| 66 | +@JvmOverloads |
| 67 | +fun createRunTask( |
| 68 | + subproject: Project, |
| 69 | + name: String, |
| 70 | + target: KotlinTarget, |
| 71 | + configureClosure: Closure<Any>? = null |
| 72 | +): Task { |
| 73 | + val task = subproject.tasks.create(name, RunKotlinNativeTask::class.java, target) |
| 74 | + task.configure(configureClosure ?: task.emptyConfigureClosure()) |
| 75 | + return task |
| 76 | +} |
0 commit comments