build.gradle.kts 6.9 KB

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