build.gradle.kts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.kotlin.gradle.dsl.KotlinMultiplatformExtension
  12. import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
  13. buildscript {
  14. repositories {
  15. // mavenLocal()
  16. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  17. mavenCentral()
  18. gradlePluginPortal()
  19. google()
  20. }
  21. dependencies {
  22. classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
  23. classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.atomicFU}")
  24. classpath("org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.binaryValidator}")
  25. }
  26. }
  27. plugins {
  28. kotlin("jvm") // version Versions.kotlinCompiler
  29. kotlin("plugin.serialization") version Versions.kotlinCompiler
  30. // id("org.jetbrains.dokka") version Versions.dokka
  31. id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
  32. id("com.gradle.plugin-publish") version "0.12.0" apply false
  33. }
  34. // https://github.com/kotlin/binary-compatibility-validator
  35. apply(plugin = "binary-compatibility-validator")
  36. configure<kotlinx.validation.ApiValidationExtension> {
  37. allprojects.forEach { subproject ->
  38. ignoredProjects.add(subproject.name)
  39. }
  40. ignoredProjects.remove("binary-compatibility-validator")
  41. ignoredProjects.remove("binary-compatibility-validator-android")
  42. // Enable validator for module `binary-compatibility-validator` and `-android` only.
  43. ignoredPackages.add("net.mamoe.mirai.internal")
  44. ignoredPackages.add("net.mamoe.mirai.console.internal")
  45. nonPublicMarkers.add("net.mamoe.mirai.utils.MiraiInternalApi")
  46. nonPublicMarkers.add("net.mamoe.mirai.utils.MiraiInternalFile")
  47. nonPublicMarkers.add("net.mamoe.mirai.console.utils.ConsoleInternalApi")
  48. nonPublicMarkers.add("net.mamoe.mirai.console.utils.ConsoleExperimentalApi")
  49. nonPublicMarkers.add("net.mamoe.mirai.utils.MiraiExperimentalApi")
  50. }
  51. GpgSigner.setup(project)
  52. tasks.register("publishMiraiCoreArtifactsToMavenLocal") {
  53. group = "mirai"
  54. dependsOn(
  55. project(":mirai-core-api").tasks.getByName("publishToMavenLocal"),
  56. project(":mirai-core-utils").tasks.getByName("publishToMavenLocal"),
  57. project(":mirai-core").tasks.getByName("publishToMavenLocal")
  58. )
  59. }
  60. analyzes.CompiledCodeVerify.run { registerAllVerifyTasks() }
  61. allprojects {
  62. group = "net.mamoe"
  63. version = Versions.project
  64. repositories {
  65. // mavenLocal() // cheching issue cause compiler exception
  66. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  67. mavenCentral()
  68. gradlePluginPortal()
  69. google()
  70. }
  71. afterEvaluate {
  72. configureJvmTarget()
  73. configureMppShadow()
  74. configureEncoding()
  75. configureKotlinTestSettings()
  76. configureKotlinExperimentalUsages()
  77. runCatching {
  78. blockingBridge {
  79. unitCoercion = net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY
  80. }
  81. }
  82. // useIr()
  83. if (isKotlinJvmProject) {
  84. configureFlattenSourceSets()
  85. }
  86. configureJarManifest()
  87. }
  88. }
  89. subprojects {
  90. afterEvaluate {
  91. if (project.name == "mirai-core-api") configureDokka()
  92. if (project.name == "mirai-console") configureDokka()
  93. }
  94. }
  95. tasks.register("cleanExceptIntellij") {
  96. group = "build"
  97. allprojects.forEach { proj ->
  98. if (proj.name != "mirai-console-intellij") {
  99. // Type mismatch
  100. // proj.tasks.findByName("clean")?.let(::dependsOn)
  101. proj.tasks.findByName("clean")?.let { dependsOn(it) }
  102. }
  103. }
  104. }
  105. extensions.findByName("buildScan")?.withGroovyBuilder {
  106. setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  107. setProperty("termsOfServiceAgree", "yes")
  108. }
  109. fun Project.useIr() {
  110. kotlinCompilations?.forEach { kotlinCompilation ->
  111. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  112. }
  113. }
  114. fun Project.configureDokka() {
  115. // apply(plugin = "org.jetbrains.dokka")
  116. // tasks {
  117. // val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  118. // outputDirectory.set(buildDir.resolve("dokka"))
  119. // }
  120. // val dokkaGfm by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  121. // outputDirectory.set(buildDir.resolve("dokka-gfm"))
  122. // }
  123. // }
  124. // tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
  125. // dokkaSourceSets.configureEach {
  126. // perPackageOption {
  127. // matchingRegex.set("net\\.mamoe\\.mirai\\.*")
  128. // skipDeprecated.set(true)
  129. // }
  130. //
  131. // for (suppressedPackage in arrayOf(
  132. // """net.mamoe.mirai.internal""",
  133. // """net.mamoe.mirai.internal.message""",
  134. // """net.mamoe.mirai.internal.network""",
  135. // """net.mamoe.mirai.console.internal""",
  136. // """net.mamoe.mirai.console.compiler.common"""
  137. // )) {
  138. // perPackageOption {
  139. // matchingRegex.set(suppressedPackage.replace(".", "\\."))
  140. // suppress.set(true)
  141. // }
  142. // }
  143. // }
  144. // }
  145. }
  146. fun Project.configureMppShadow() {
  147. val kotlin =
  148. runCatching {
  149. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  150. }.getOrNull() ?: return
  151. if (project.configurations.findByName("jvmRuntimeClasspath") != null) {
  152. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  153. group = "mirai"
  154. archiveClassifier.set("-all")
  155. val compilations =
  156. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  157. .map { it.compilations["main"] }
  158. compilations.forEach {
  159. dependsOn(it.compileKotlinTask)
  160. from(it.output)
  161. }
  162. from(project.configurations.findByName("jvmRuntimeClasspath"))
  163. this.exclude { file ->
  164. file.name.endsWith(".sf", ignoreCase = true)
  165. }
  166. /*
  167. this.manifest {
  168. this.attributes(
  169. "Manifest-Version" to 1,
  170. "Implementation-Vendor" to "Mamoe Technologies",
  171. "Implementation-Title" to this.name.toString(),
  172. "Implementation-Version" to this.version.toString()
  173. )
  174. }*/
  175. }
  176. }
  177. }