mirai/build.gradle.kts

307 lines
9.9 KiB
Plaintext
Raw Normal View History

2020-11-01 15:07:32 +08:00
/*
* Copyright 2019-2020 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-03-25 13:48:27 +08:00
@file:Suppress("UnstableApiUsage", "UNUSED_VARIABLE")
2020-03-25 12:18:58 +08:00
2020-09-22 13:23:08 +08:00
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.dokka.gradle.DokkaTask
2020-10-03 13:35:05 +08:00
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
2020-10-03 13:35:05 +08:00
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
2020-03-01 13:00:34 +08:00
buildscript {
repositories {
mavenLocal()
2020-05-04 15:10:31 +08:00
// maven(url = "https://mirrors.huaweicloud.com/repository/maven")
2020-10-03 13:35:05 +08:00
mavenCentral()
2020-03-01 13:00:34 +08:00
jcenter()
google()
2020-10-03 13:35:05 +08:00
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
maven(url = "https://kotlin.bintray.com/kotlinx")
2020-03-01 13:00:34 +08:00
}
dependencies {
2020-10-03 13:35:05 +08:00
classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.atomicFU}")
classpath("org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.binaryValidator}")
2020-03-01 13:00:34 +08:00
}
}
2020-03-23 19:31:22 +08:00
plugins {
2020-10-03 13:35:05 +08:00
kotlin("jvm") version Versions.kotlinCompiler
kotlin("plugin.serialization") version Versions.kotlinCompiler
id("org.jetbrains.dokka") version Versions.dokka apply false
2020-08-18 22:09:05 +08:00
id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge apply false
2020-10-03 13:35:05 +08:00
id("com.jfrog.bintray") version Versions.bintray
2020-03-23 19:31:22 +08:00
}
2020-05-09 15:37:02 +08:00
// https://github.com/kotlin/binary-compatibility-validator
2020-05-10 01:11:57 +08:00
//apply(plugin = "binary-compatibility-validator")
2020-05-09 15:37:02 +08:00
2020-04-10 16:13:03 +08:00
project.ext.set("isAndroidSDKAvailable", false)
// until
// https://youtrack.jetbrains.com/issue/KT-37152,
// are fixed.
/*
2020-03-01 13:00:34 +08:00
runCatching {
val keyProps = Properties().apply {
file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
}
if (keyProps.getProperty("sdk.dir", "").isNotEmpty()) {
project.ext.set("isAndroidSDKAvailable", true)
} else {
project.ext.set("isAndroidSDKAvailable", false)
}
2020-04-08 10:39:13 +08:00
}.exceptionOrNull()?.run {
project.ext.set("isAndroidSDKAvailable", false)
2020-04-10 16:13:03 +08:00
}*/
2020-03-01 13:00:34 +08:00
allprojects {
group = "net.mamoe"
2020-10-03 13:35:05 +08:00
version = Versions.project
2020-03-01 13:00:34 +08:00
repositories {
2020-12-23 18:38:46 +08:00
// mavenLocal() // cheching issue cause compiler exception
2020-05-04 15:10:31 +08:00
// maven(url = "https://mirrors.huaweicloud.com/repository/maven")
2020-12-22 20:56:19 +08:00
jcenter()
2020-03-24 10:01:03 +08:00
maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
maven(url = "https://kotlin.bintray.com/kotlinx")
2020-03-01 13:00:34 +08:00
google()
mavenCentral()
maven(url = "https://dl.bintray.com/karlatemp/misc")
2020-03-01 13:00:34 +08:00
}
2020-03-25 10:12:24 +08:00
2020-10-03 13:35:05 +08:00
afterEvaluate {
configureJvmTarget()
configureMppShadow()
configureEncoding()
configureKotlinTestSettings()
configureKotlinCompilerSettings()
configureKotlinExperimentalUsages()
2020-12-22 20:56:19 +08:00
// useIr()
2020-10-03 13:35:05 +08:00
if (isKotlinJvmProject) {
configureFlattenSourceSets()
}
configureDokka()
}
2020-10-03 13:35:05 +08:00
}
2020-09-22 13:23:08 +08:00
2020-12-22 20:56:19 +08:00
fun Project.useIr() {
kotlinCompilations?.forEach { kotlinCompilation ->
kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
}
}
2020-10-03 13:35:05 +08:00
fun Project.configureDokka() {
apply(plugin = "org.jetbrains.dokka")
tasks {
val dokka by getting(DokkaTask::class) {
outputFormat = "html"
outputDirectory = "$buildDir/dokka"
}
val dokkaMarkdown by creating(DokkaTask::class) {
outputFormat = "markdown"
outputDirectory = "$buildDir/dokka-markdown"
}
val dokkaGfm by creating(DokkaTask::class) {
outputFormat = "gfm"
outputDirectory = "$buildDir/dokka-gfm"
2020-09-22 18:56:27 +08:00
}
2020-10-03 13:35:05 +08:00
}
for (task in tasks.filterIsInstance<DokkaTask>()) {
task.configuration {
perPackageOption {
prefix = "net.mamoe.mirai"
skipDeprecated = true
}
for (suppressedPackage in arrayOf(
"net.mamoe.mirai.internal",
"net.mamoe.mirai.event.internal",
"net.mamoe.mirai.utils.internal",
"net.mamoe.mirai.internal"
)) {
perPackageOption {
prefix = suppressedPackage
suppress = true
}
}
}
}
}
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
@Suppress("NOTHING_TO_INLINE") // or error
fun Project.configureJvmTarget() {
tasks.withType(KotlinJvmCompile::class.java) {
2020-10-25 12:31:34 +08:00
kotlinOptions.jvmTarget = "1.8"
2020-10-03 13:35:05 +08:00
}
2020-03-25 10:12:24 +08:00
kotlinTargets.orEmpty().filterIsInstance<KotlinJvmTarget>().forEach { target ->
target.compilations.all {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.languageVersion = "1.4"
}
target.testRuns["test"].executionTask.configure { useJUnitPlatform() }
}
2020-10-03 13:35:05 +08:00
extensions.findByType(JavaPluginExtension::class.java)?.run {
2020-10-25 12:31:16 +08:00
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
2020-10-03 13:35:05 +08:00
}
}
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
fun Project.configureMppShadow() {
val kotlin =
runCatching {
(this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
}.getOrNull() ?: return
2020-03-25 10:12:24 +08:00
2020-10-03 13:35:05 +08:00
val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
group = "mirai"
archiveClassifier.set("-all")
2020-03-25 10:12:24 +08:00
2020-10-03 13:35:05 +08:00
val compilations =
kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
.map { it.compilations["main"] }
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
compilations.forEach {
dependsOn(it.compileKotlinTask)
from(it.output)
}
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
println(project.configurations.joinToString())
2020-03-26 21:17:30 +08:00
2020-10-03 13:35:05 +08:00
from(project.configurations.getByName("jvmRuntimeClasspath"))
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
this.exclude { file ->
file.name.endsWith(".sf", ignoreCase = true)
2020-03-25 10:12:24 +08:00
}
2020-10-02 23:17:56 +08:00
/*
2020-10-03 13:35:05 +08:00
this.manifest {
this.attributes(
"Manifest-Version" to 1,
"Implementation-Vendor" to "Mamoe Technologies",
"Implementation-Title" to this.name.toString(),
"Implementation-Version" to this.version.toString()
)
}*/
}
}
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
fun Project.configureEncoding() {
tasks.withType(JavaCompile::class.java) {
options.encoding = "UTF8"
}
}
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
fun Project.configureKotlinTestSettings() {
tasks.withType(Test::class) {
useJUnitPlatform()
}
when {
isKotlinJvmProject -> {
dependencies {
testImplementation(kotlin("test-junit5"))
2020-09-22 13:23:08 +08:00
2020-10-03 13:35:05 +08:00
testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
2020-09-22 13:23:08 +08:00
}
2020-10-03 13:35:05 +08:00
}
isKotlinMpp -> {
kotlinSourceSets?.forEach { sourceSet ->
if (sourceSet.name == "common") {
sourceSet.dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-annotations-common"))
2020-03-25 10:12:24 +08:00
}
2020-10-03 13:35:05 +08:00
} 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-09-21 22:59:00 +08:00
}
}
}
}
2020-10-03 13:35:05 +08:00
}
}
2020-10-03 13:35:05 +08:00
fun Project.configureKotlinCompilerSettings() {
val kotlinCompilations = kotlinCompilations ?: return
for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
if (isKotlinJvmProject) {
@Suppress("UNCHECKED_CAST")
this as KotlinCompilation<KotlinJvmOptions>
2020-05-24 16:42:42 +08:00
}
2020-10-03 13:35:05 +08:00
kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
}
}
2020-05-24 16:42:42 +08:00
2020-10-03 13:35:05 +08:00
val experimentalAnnotations = arrayOf(
"kotlin.RequiresOptIn",
"kotlin.contracts.ExperimentalContracts",
"kotlin.experimental.ExperimentalTypeInference",
2020-11-22 11:57:53 +08:00
"kotlin.ExperimentalUnsignedTypes",
"kotlin.time.ExperimentalTime",
"kotlinx.serialization.ExperimentalSerializationApi",
2020-11-01 15:07:32 +08:00
"net.mamoe.mirai.utils.MiraiInternalApi",
"net.mamoe.mirai.utils.MiraiExperimentalApi",
"net.mamoe.mirai.LowLevelApi",
"net.mamoe.mirai.utils.UnstableExternalImage",
"net.mamoe.mirai.message.data.ExperimentalMessageKey"
2020-10-03 13:35:05 +08:00
)
fun Project.configureKotlinExperimentalUsages() {
val sourceSets = kotlinSourceSets ?: return
for (target in sourceSets) {
2020-11-22 11:57:53 +08:00
target.languageSettings.progressiveMode = true
target.languageSettings.enableLanguageFeature("InlineClasses")
2020-10-03 13:35:05 +08:00
experimentalAnnotations.forEach { a ->
target.languageSettings.useExperimentalAnnotation(a)
2020-05-24 16:42:42 +08:00
}
2020-03-25 10:12:24 +08:00
}
2020-10-03 13:35:05 +08:00
}
2020-03-25 10:12:24 +08:00
2020-10-03 13:35:05 +08:00
fun Project.configureFlattenSourceSets() {
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")))
2020-05-24 16:42:42 +08:00
}
}
}
2020-10-03 13:35:05 +08:00
val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
2020-10-03 13:35:05 +08:00
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 }