build.gradle.kts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2019-2021 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/master/LICENSE
  8. */
  9. @file:Suppress("UnstableApiUsage")
  10. import org.jetbrains.kotlin.gradle.dsl.*
  11. import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
  12. import org.jetbrains.kotlin.utils.addToStdlib.safeAs
  13. plugins {
  14. kotlin("jvm") version Versions.kotlinCompiler
  15. kotlin("plugin.serialization") version Versions.kotlinCompiler
  16. id("com.jfrog.bintray") version Versions.bintray apply false
  17. id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge apply false
  18. id("com.gradle.plugin-publish") version "0.12.0" apply false
  19. //id("com.bmuschko.nexus") version "2.3.1" apply false
  20. //id("io.codearte.nexus-staging") version "0.11.0" apply false
  21. }
  22. tasks.withType(JavaCompile::class.java) {
  23. options.encoding = "UTF8"
  24. }
  25. allprojects {
  26. group = "net.mamoe"
  27. repositories {
  28. mavenLocal()
  29. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  30. jcenter()
  31. mavenCentral()
  32. }
  33. }
  34. subprojects {
  35. afterEvaluate {
  36. apply<MiraiConsoleBuildPlugin>()
  37. configureJvmTarget()
  38. configureEncoding()
  39. configureKotlinExperimentalUsages()
  40. configureKotlinCompilerSettings()
  41. configureKotlinTestSettings()
  42. configureSourceSets()
  43. }
  44. }
  45. extensions.findByName("buildScan")?.withGroovyBuilder {
  46. setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  47. setProperty("termsOfServiceAgree", "yes")
  48. }
  49. val experimentalAnnotations = arrayOf(
  50. "kotlin.Experimental",
  51. "kotlin.RequiresOptIn",
  52. "kotlin.ExperimentalUnsignedTypes",
  53. "kotlin.ExperimentalStdlibApi",
  54. "kotlin.contracts.ExperimentalContracts",
  55. "kotlin.time.ExperimentalTime",
  56. "kotlin.experimental.ExperimentalTypeInference",
  57. "kotlinx.coroutines.ExperimentalCoroutinesApi",
  58. "kotlinx.serialization.ExperimentalSerializationApi",
  59. "kotlin.io.path.ExperimentalPathApi",
  60. "io.ktor.util.KtorExperimentalAPI",
  61. "net.mamoe.mirai.utils.MiraiInternalApi",
  62. "net.mamoe.mirai.utils.MiraiExperimentalApi",
  63. "net.mamoe.mirai.console.ConsoleFrontEndImplementation",
  64. "net.mamoe.mirai.console.util.ConsoleExperimentalApi",
  65. "net.mamoe.mirai.console.util.ConsoleInternalApi"
  66. )
  67. fun Project.configureJvmTarget() {
  68. tasks.withType(KotlinJvmCompile::class.java) {
  69. kotlinOptions.jvmTarget = "1.8"
  70. }
  71. extensions.findByType(JavaPluginExtension::class.java)?.run {
  72. sourceCompatibility = JavaVersion.VERSION_1_8
  73. targetCompatibility = JavaVersion.VERSION_1_8
  74. }
  75. }
  76. fun Project.useIr() {
  77. tasks {
  78. withType(KotlinJvmCompile::class.java) {
  79. kotlinOptions.useIR = true
  80. }
  81. }
  82. }
  83. fun Project.configureKotlinTestSettings() {
  84. tasks.withType(Test::class) {
  85. useJUnitPlatform()
  86. }
  87. when {
  88. isKotlinJvmProject -> {
  89. dependencies {
  90. testImplementation(kotlin("test-junit5"))
  91. testApi("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
  92. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
  93. }
  94. }
  95. isKotlinMpp -> {
  96. kotlinSourceSets?.forEach { sourceSet ->
  97. if (sourceSet.name == "common") {
  98. sourceSet.dependencies {
  99. implementation(kotlin("test"))
  100. implementation(kotlin("test-annotations-common"))
  101. }
  102. } else {
  103. sourceSet.dependencies {
  104. implementation(kotlin("test-junit5"))
  105. implementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
  106. implementation("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. fun Project.configureKotlinCompilerSettings() {
  114. val kotlinCompilations = kotlinCompilations ?: return
  115. for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
  116. if (isKotlinJvmProject) {
  117. @Suppress("UNCHECKED_CAST")
  118. this as KotlinCompilation<KotlinJvmOptions>
  119. }
  120. kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
  121. }
  122. }
  123. fun Project.configureEncoding() {
  124. tasks.withType(JavaCompile::class.java) {
  125. options.encoding = "UTF8"
  126. }
  127. }
  128. fun Project.configureSourceSets() {
  129. val flatten = extra.runCatching { get("flatten.sourceset") }.getOrNull()?.toString()?.toBoolean() ?: true
  130. if (!flatten) return
  131. sourceSets {
  132. findByName("main")?.apply {
  133. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  134. java.setSrcDirs(listOf(projectDir.resolve("src")))
  135. }
  136. findByName("test")?.apply {
  137. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  138. java.setSrcDirs(listOf(projectDir.resolve("test")))
  139. }
  140. }
  141. }
  142. fun Project.configureKotlinExperimentalUsages() {
  143. val sourceSets = kotlinSourceSets ?: return
  144. for (target in sourceSets) target.languageSettings.run {
  145. enableLanguageFeature("InlineClasses")
  146. progressiveMode = true
  147. experimentalAnnotations.forEach { a ->
  148. useExperimentalAnnotation(a)
  149. }
  150. }
  151. }
  152. val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
  153. val Project.kotlinTargets
  154. get() =
  155. extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
  156. ?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets
  157. val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
  158. val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension
  159. val Project.kotlinCompilations
  160. get() = kotlinTargets?.flatMap { it.compilations }