mirai/build.gradle.kts

179 lines
5.8 KiB
Plaintext
Raw Normal View History

2021-02-15 10:42:10 +08:00
/*
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
2020-04-02 22:08:58 +08:00
@file:Suppress("UnstableApiUsage")
2020-10-30 10:28:18 +08:00
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
2020-06-28 11:45:21 +08:00
plugins {
2020-08-16 23:21:11 +08:00
kotlin("jvm") version Versions.kotlinCompiler
kotlin("plugin.serialization") version Versions.kotlinCompiler
2020-09-19 20:33:34 +08:00
id("com.jfrog.bintray") version Versions.bintray apply false
id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge apply false
id("com.gradle.plugin-publish") version "0.12.0" apply false
//id("com.bmuschko.nexus") version "2.3.1" apply false
//id("io.codearte.nexus-staging") version "0.11.0" apply false
2020-06-28 11:45:21 +08:00
}
2020-08-01 21:53:24 +08:00
tasks.withType(JavaCompile::class.java) {
options.encoding = "UTF8"
}
2020-04-02 21:53:44 +08:00
allprojects {
group = "net.mamoe"
repositories {
2020-06-21 00:44:02 +08:00
mavenLocal()
2020-04-02 21:53:44 +08:00
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
jcenter()
mavenCentral()
}
2020-04-02 22:08:58 +08:00
}
subprojects {
afterEvaluate {
2020-06-20 22:55:07 +08:00
apply<MiraiConsoleBuildPlugin>()
2020-06-21 00:44:02 +08:00
2020-10-30 10:28:18 +08:00
configureJvmTarget()
configureEncoding()
configureKotlinExperimentalUsages()
configureKotlinCompilerSettings()
configureKotlinTestSettings()
configureSourceSets()
}
}
val experimentalAnnotations = arrayOf(
2020-11-12 09:42:18 +08:00
"kotlin.Experimental",
2020-10-30 10:28:18 +08:00
"kotlin.RequiresOptIn",
"kotlin.ExperimentalUnsignedTypes",
2020-11-12 09:42:18 +08:00
"kotlin.ExperimentalStdlibApi",
2020-10-30 10:28:18 +08:00
"kotlin.contracts.ExperimentalContracts",
2020-11-12 09:42:18 +08:00
"kotlin.time.ExperimentalTime",
2020-10-30 10:28:18 +08:00
"kotlin.experimental.ExperimentalTypeInference",
2020-11-12 09:42:18 +08:00
"kotlinx.coroutines.ExperimentalCoroutinesApi",
"kotlinx.serialization.ExperimentalSerializationApi",
2020-11-25 17:19:52 +08:00
"kotlin.io.path.ExperimentalPathApi",
2020-10-30 10:28:18 +08:00
"io.ktor.util.KtorExperimentalAPI",
2020-11-12 09:42:18 +08:00
2020-12-18 16:36:49 +08:00
"net.mamoe.mirai.utils.MiraiInternalApi",
"net.mamoe.mirai.utils.MiraiExperimentalApi",
2020-11-12 09:42:18 +08:00
"net.mamoe.mirai.console.ConsoleFrontEndImplementation",
"net.mamoe.mirai.console.util.ConsoleExperimentalApi",
"net.mamoe.mirai.console.util.ConsoleInternalApi"
2020-10-30 10:28:18 +08:00
)
fun Project.configureJvmTarget() {
tasks.withType(KotlinJvmCompile::class.java) {
kotlinOptions.jvmTarget = "1.8"
}
extensions.findByType(JavaPluginExtension::class.java)?.run {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
fun Project.useIr() {
tasks {
withType(KotlinJvmCompile::class.java) {
kotlinOptions.useIR = true
}
}
}
fun Project.configureKotlinTestSettings() {
tasks.withType(Test::class) {
useJUnitPlatform()
}
when {
isKotlinJvmProject -> {
dependencies {
testImplementation(kotlin("test-junit5"))
testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
}
}
isKotlinMpp -> {
kotlinSourceSets?.forEach { sourceSet ->
if (sourceSet.name == "common") {
sourceSet.dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-annotations-common"))
}
} else {
sourceSet.dependencies {
implementation(kotlin("test-junit5"))
implementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
implementation("org.junit.jupiter:junit-jupiter-engine:5.2.0")
}
}
}
}
2020-04-02 22:08:58 +08:00
}
2020-10-30 10:28:18 +08:00
}
fun Project.configureKotlinCompilerSettings() {
val kotlinCompilations = kotlinCompilations ?: return
for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
if (isKotlinJvmProject) {
@Suppress("UNCHECKED_CAST")
this as KotlinCompilation<KotlinJvmOptions>
}
kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
}
}
fun Project.configureEncoding() {
tasks.withType(JavaCompile::class.java) {
options.encoding = "UTF8"
}
}
fun Project.configureSourceSets() {
sourceSets {
findByName("main")?.apply {
resources.setSrcDirs(listOf(projectDir.resolve("resources")))
java.setSrcDirs(listOf(projectDir.resolve("src")))
}
findByName("test")?.apply {
resources.setSrcDirs(listOf(projectDir.resolve("resources")))
java.setSrcDirs(listOf(projectDir.resolve("test")))
}
}
}
fun Project.configureKotlinExperimentalUsages() {
val sourceSets = kotlinSourceSets ?: return
2020-11-12 09:42:18 +08:00
for (target in sourceSets) target.languageSettings.run {
enableLanguageFeature("InlineClasses")
progressiveMode = true
2020-10-30 10:28:18 +08:00
experimentalAnnotations.forEach { a ->
2020-11-12 09:42:18 +08:00
useExperimentalAnnotation(a)
2020-10-30 10:28:18 +08:00
}
}
}
val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
val Project.kotlinTargets
get() =
extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets
val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension
val Project.kotlinCompilations
get() = kotlinTargets?.flatMap { it.compilations }