build.gradle.kts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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", "UNUSED_VARIABLE", "NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
  10. import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
  11. import org.jetbrains.dokka.gradle.DokkaTask
  12. import org.jetbrains.kotlin.gradle.dsl.*
  13. import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
  14. import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
  15. import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
  16. import org.jetbrains.kotlin.utils.addToStdlib.safeAs
  17. buildscript {
  18. repositories {
  19. mavenLocal()
  20. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  21. mavenCentral()
  22. jcenter()
  23. google()
  24. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  25. maven(url = "https://kotlin.bintray.com/kotlinx")
  26. }
  27. dependencies {
  28. classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
  29. classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.atomicFU}")
  30. classpath("org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.binaryValidator}")
  31. }
  32. }
  33. plugins {
  34. kotlin("jvm") version Versions.kotlinCompiler
  35. kotlin("plugin.serialization") version Versions.kotlinCompiler
  36. id("org.jetbrains.dokka") version Versions.dokka
  37. id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
  38. id("com.jfrog.bintray") // version Versions.bintray
  39. }
  40. // https://github.com/kotlin/binary-compatibility-validator
  41. //apply(plugin = "binary-compatibility-validator")
  42. project.ext.set("isAndroidSDKAvailable", false)
  43. // until
  44. // https://youtrack.jetbrains.com/issue/KT-37152,
  45. // are fixed.
  46. /*
  47. runCatching {
  48. val keyProps = Properties().apply {
  49. file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
  50. }
  51. if (keyProps.getProperty("sdk.dir", "").isNotEmpty()) {
  52. project.ext.set("isAndroidSDKAvailable", true)
  53. } else {
  54. project.ext.set("isAndroidSDKAvailable", false)
  55. }
  56. }.exceptionOrNull()?.run {
  57. project.ext.set("isAndroidSDKAvailable", false)
  58. }*/
  59. allprojects {
  60. group = "net.mamoe"
  61. version = Versions.project
  62. repositories {
  63. // mavenLocal() // cheching issue cause compiler exception
  64. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  65. jcenter()
  66. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  67. maven(url = "https://kotlin.bintray.com/kotlinx")
  68. google()
  69. mavenCentral()
  70. maven(url = "https://dl.bintray.com/karlatemp/misc")
  71. }
  72. afterEvaluate {
  73. configureJvmTarget()
  74. configureMppShadow()
  75. configureEncoding()
  76. configureKotlinTestSettings()
  77. configureKotlinCompilerSettings()
  78. configureKotlinExperimentalUsages()
  79. // blockingBridge {
  80. // unitCoercion = COMPATIBILITY
  81. // }
  82. // useIr()
  83. if (isKotlinJvmProject) {
  84. configureFlattenSourceSets()
  85. }
  86. }
  87. }
  88. subprojects {
  89. afterEvaluate {
  90. if (project.name == "mirai-core-api") configureDokka()
  91. }
  92. }
  93. fun Project.useIr() {
  94. kotlinCompilations?.forEach { kotlinCompilation ->
  95. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  96. }
  97. }
  98. fun Project.configureDokka() {
  99. apply(plugin = "org.jetbrains.dokka")
  100. tasks {
  101. val dokkaHtml by getting(DokkaTask::class) {
  102. outputDirectory.set(buildDir.resolve("dokka"))
  103. }
  104. val dokkaGfm by getting(DokkaTask::class) {
  105. outputDirectory.set(buildDir.resolve("dokka-gfm"))
  106. }
  107. }
  108. tasks.withType<DokkaTask>().configureEach {
  109. dokkaSourceSets.configureEach {
  110. perPackageOption {
  111. matchingRegex.set("net\\.mamoe\\.mirai\\.*")
  112. skipDeprecated.set(true)
  113. }
  114. for (suppressedPackage in arrayOf(
  115. """net.mamoe.mirai.internal""",
  116. """net.mamoe.mirai.internal.message""",
  117. """net.mamoe.mirai.internal.network"""
  118. )) {
  119. perPackageOption {
  120. matchingRegex.set(suppressedPackage.replace(".", "\\."))
  121. suppress.set(true)
  122. }
  123. }
  124. }
  125. }
  126. }
  127. @Suppress("NOTHING_TO_INLINE") // or error
  128. fun Project.configureJvmTarget() {
  129. tasks.withType(KotlinJvmCompile::class.java) {
  130. kotlinOptions.jvmTarget = "1.8"
  131. }
  132. kotlinTargets.orEmpty().filterIsInstance<KotlinJvmTarget>().forEach { target ->
  133. target.compilations.all {
  134. kotlinOptions.jvmTarget = "1.8"
  135. kotlinOptions.languageVersion = "1.4"
  136. }
  137. target.testRuns["test"].executionTask.configure { useJUnitPlatform() }
  138. }
  139. extensions.findByType(JavaPluginExtension::class.java)?.run {
  140. sourceCompatibility = JavaVersion.VERSION_1_8
  141. targetCompatibility = JavaVersion.VERSION_1_8
  142. }
  143. }
  144. fun Project.configureMppShadow() {
  145. val kotlin =
  146. runCatching {
  147. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  148. }.getOrNull() ?: return
  149. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  150. group = "mirai"
  151. archiveClassifier.set("-all")
  152. val compilations =
  153. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  154. .map { it.compilations["main"] }
  155. compilations.forEach {
  156. dependsOn(it.compileKotlinTask)
  157. from(it.output)
  158. }
  159. println(project.configurations.joinToString())
  160. from(project.configurations.getByName("jvmRuntimeClasspath"))
  161. this.exclude { file ->
  162. file.name.endsWith(".sf", ignoreCase = true)
  163. }
  164. /*
  165. this.manifest {
  166. this.attributes(
  167. "Manifest-Version" to 1,
  168. "Implementation-Vendor" to "Mamoe Technologies",
  169. "Implementation-Title" to this.name.toString(),
  170. "Implementation-Version" to this.version.toString()
  171. )
  172. }*/
  173. }
  174. }
  175. fun Project.configureEncoding() {
  176. tasks.withType(JavaCompile::class.java) {
  177. options.encoding = "UTF8"
  178. }
  179. }
  180. fun Project.configureKotlinTestSettings() {
  181. tasks.withType(Test::class) {
  182. useJUnitPlatform()
  183. }
  184. when {
  185. isKotlinJvmProject -> {
  186. dependencies {
  187. testImplementation(kotlin("test-junit5"))
  188. testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
  189. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  190. }
  191. }
  192. isKotlinMpp -> {
  193. kotlinSourceSets?.forEach { sourceSet ->
  194. if (sourceSet.name == "common") {
  195. sourceSet.dependencies {
  196. implementation(kotlin("test"))
  197. implementation(kotlin("test-annotations-common"))
  198. }
  199. } else {
  200. sourceSet.dependencies {
  201. implementation(kotlin("test-junit5"))
  202. implementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
  203. implementation("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. fun Project.configureKotlinCompilerSettings() {
  211. val kotlinCompilations = kotlinCompilations ?: return
  212. for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
  213. if (isKotlinJvmProject) {
  214. @Suppress("UNCHECKED_CAST")
  215. this as KotlinCompilation<KotlinJvmOptions>
  216. }
  217. kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
  218. }
  219. }
  220. val experimentalAnnotations = arrayOf(
  221. "kotlin.RequiresOptIn",
  222. "kotlin.contracts.ExperimentalContracts",
  223. "kotlin.experimental.ExperimentalTypeInference",
  224. "kotlin.ExperimentalUnsignedTypes",
  225. "kotlin.time.ExperimentalTime",
  226. "kotlinx.serialization.ExperimentalSerializationApi",
  227. "net.mamoe.mirai.utils.MiraiInternalApi",
  228. "net.mamoe.mirai.utils.MiraiExperimentalApi",
  229. "net.mamoe.mirai.LowLevelApi",
  230. "net.mamoe.mirai.utils.UnstableExternalImage",
  231. "net.mamoe.mirai.message.data.ExperimentalMessageKey"
  232. )
  233. fun Project.configureKotlinExperimentalUsages() {
  234. val sourceSets = kotlinSourceSets ?: return
  235. for (target in sourceSets) {
  236. target.languageSettings.progressiveMode = true
  237. target.languageSettings.enableLanguageFeature("InlineClasses")
  238. experimentalAnnotations.forEach { a ->
  239. target.languageSettings.useExperimentalAnnotation(a)
  240. }
  241. }
  242. }
  243. fun Project.configureFlattenSourceSets() {
  244. sourceSets {
  245. findByName("main")?.apply {
  246. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  247. java.setSrcDirs(listOf(projectDir.resolve("src")))
  248. }
  249. findByName("test")?.apply {
  250. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  251. java.setSrcDirs(listOf(projectDir.resolve("test")))
  252. }
  253. }
  254. }
  255. val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
  256. val Project.kotlinTargets
  257. get() =
  258. extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
  259. ?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets
  260. val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
  261. val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension
  262. val Project.kotlinCompilations
  263. get() = kotlinTargets?.flatMap { it.compilations }