build.gradle.kts 6.8 KB

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