build.gradle.kts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge apply false
  17. id("com.gradle.plugin-publish") version "0.12.0" apply false
  18. //id("com.bmuschko.nexus") version "2.3.1" apply false
  19. //id("io.codearte.nexus-staging") version "0.11.0" apply false
  20. }
  21. tasks.withType(JavaCompile::class.java) {
  22. options.encoding = "UTF8"
  23. }
  24. allprojects {
  25. group = "net.mamoe"
  26. repositories {
  27. mavenLocal()
  28. jcenter()
  29. mavenCentral()
  30. }
  31. }
  32. subprojects {
  33. afterEvaluate {
  34. apply<MiraiConsoleBuildPlugin>()
  35. configureJvmTarget()
  36. configureEncoding()
  37. configureKotlinExperimentalUsages()
  38. configureKotlinCompilerSettings()
  39. configureKotlinTestSettings()
  40. configureSourceSets()
  41. }
  42. }
  43. extensions.findByName("buildScan")?.withGroovyBuilder {
  44. setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  45. setProperty("termsOfServiceAgree", "yes")
  46. }
  47. val experimentalAnnotations = arrayOf(
  48. "kotlin.Experimental",
  49. "kotlin.RequiresOptIn",
  50. "kotlin.ExperimentalUnsignedTypes",
  51. "kotlin.ExperimentalStdlibApi",
  52. "kotlin.contracts.ExperimentalContracts",
  53. "kotlin.time.ExperimentalTime",
  54. "kotlin.experimental.ExperimentalTypeInference",
  55. "kotlinx.coroutines.ExperimentalCoroutinesApi",
  56. "kotlinx.serialization.ExperimentalSerializationApi",
  57. "kotlin.io.path.ExperimentalPathApi",
  58. "io.ktor.util.KtorExperimentalAPI",
  59. "net.mamoe.mirai.utils.MiraiInternalApi",
  60. "net.mamoe.mirai.utils.MiraiExperimentalApi",
  61. "net.mamoe.mirai.console.ConsoleFrontEndImplementation",
  62. "net.mamoe.mirai.console.util.ConsoleExperimentalApi",
  63. "net.mamoe.mirai.console.util.ConsoleInternalApi"
  64. )
  65. fun Project.configureJvmTarget() {
  66. tasks.withType(KotlinJvmCompile::class.java) {
  67. kotlinOptions.jvmTarget = "1.8"
  68. }
  69. extensions.findByType(JavaPluginExtension::class.java)?.run {
  70. sourceCompatibility = JavaVersion.VERSION_1_8
  71. targetCompatibility = JavaVersion.VERSION_1_8
  72. }
  73. }
  74. fun Project.useIr() {
  75. tasks {
  76. withType(KotlinJvmCompile::class.java) {
  77. kotlinOptions.useIR = true
  78. }
  79. }
  80. }
  81. fun Project.configureKotlinTestSettings() {
  82. tasks.withType(Test::class) {
  83. useJUnitPlatform()
  84. }
  85. when {
  86. isKotlinJvmProject -> {
  87. dependencies {
  88. testImplementation(kotlin("test-junit5"))
  89. testApi("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
  90. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
  91. }
  92. }
  93. isKotlinMpp -> {
  94. kotlinSourceSets?.forEach { sourceSet ->
  95. if (sourceSet.name == "common") {
  96. sourceSet.dependencies {
  97. implementation(kotlin("test"))
  98. implementation(kotlin("test-annotations-common"))
  99. }
  100. } else {
  101. sourceSet.dependencies {
  102. implementation(kotlin("test-junit5"))
  103. implementation("org.junit.jupiter:junit-jupiter-api:${Versions.junit}")
  104. implementation("org.junit.jupiter:junit-jupiter-engine:${Versions.junit}")
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. fun Project.configureKotlinCompilerSettings() {
  112. val kotlinCompilations = kotlinCompilations ?: return
  113. for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
  114. if (isKotlinJvmProject) {
  115. @Suppress("UNCHECKED_CAST")
  116. this as KotlinCompilation<KotlinJvmOptions>
  117. }
  118. kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
  119. }
  120. }
  121. fun Project.configureEncoding() {
  122. tasks.withType(JavaCompile::class.java) {
  123. options.encoding = "UTF8"
  124. }
  125. }
  126. fun Project.configureSourceSets() {
  127. val flatten = extra.runCatching { get("flatten.sourceset") }.getOrNull()?.toString()?.toBoolean() ?: true
  128. if (!flatten) return
  129. sourceSets {
  130. findByName("main")?.apply {
  131. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  132. java.setSrcDirs(listOf(projectDir.resolve("src")))
  133. }
  134. findByName("test")?.apply {
  135. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  136. java.setSrcDirs(listOf(projectDir.resolve("test")))
  137. }
  138. }
  139. }
  140. fun Project.configureKotlinExperimentalUsages() {
  141. val sourceSets = kotlinSourceSets ?: return
  142. for (target in sourceSets) target.languageSettings.run {
  143. enableLanguageFeature("InlineClasses")
  144. progressiveMode = true
  145. experimentalAnnotations.forEach { a ->
  146. useExperimentalAnnotation(a)
  147. }
  148. }
  149. }
  150. val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
  151. val Project.kotlinTargets
  152. get() =
  153. extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
  154. ?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets
  155. val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
  156. val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension
  157. val Project.kotlinCompilations
  158. get() = kotlinTargets?.flatMap { it.compilations }