build.gradle.kts 6.9 KB

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