build.gradle.kts 7.7 KB

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