2
0

build.gradle.kts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. analyzes.CompiledCodeVerify.run { registerAllVerifyTasks() }
  53. allprojects {
  54. group = "net.mamoe"
  55. version = Versions.project
  56. repositories {
  57. // mavenLocal() // cheching issue cause compiler exception
  58. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  59. mavenCentral()
  60. gradlePluginPortal()
  61. google()
  62. }
  63. afterEvaluate {
  64. configureJvmTarget()
  65. configureMppShadow()
  66. configureEncoding()
  67. configureKotlinTestSettings()
  68. configureKotlinExperimentalUsages()
  69. runCatching {
  70. blockingBridge {
  71. unitCoercion = net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY
  72. }
  73. }
  74. // useIr()
  75. if (isKotlinJvmProject) {
  76. configureFlattenSourceSets()
  77. }
  78. configureJarManifest()
  79. if (System.getenv("MIRAI_IS_SNAPSHOTS_PUBLISHING") != null) {
  80. project.tasks.filterIsInstance<ShadowJar>().forEach { shadow ->
  81. shadow.enabled = false // they are too big
  82. }
  83. logger.info("Disabled all shadow tasks.")
  84. }
  85. }
  86. }
  87. subprojects {
  88. afterEvaluate {
  89. if (project.name == "mirai-core-api") configureDokka()
  90. if (project.name == "mirai-console") configureDokka()
  91. }
  92. }
  93. tasks.register("cleanExceptIntellij") {
  94. group = "build"
  95. allprojects.forEach { proj ->
  96. if (proj.name != "mirai-console-intellij") {
  97. // Type mismatch
  98. // proj.tasks.findByName("clean")?.let(::dependsOn)
  99. proj.tasks.findByName("clean")?.let { dependsOn(it) }
  100. }
  101. }
  102. }
  103. extensions.findByName("buildScan")?.withGroovyBuilder {
  104. setProperty("termsOfServiceUrl", "https://gradle.com/terms-of-service")
  105. setProperty("termsOfServiceAgree", "yes")
  106. }
  107. fun Project.useIr() {
  108. kotlinCompilations?.forEach { kotlinCompilation ->
  109. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  110. }
  111. }
  112. fun Project.configureDokka() {
  113. // apply(plugin = "org.jetbrains.dokka")
  114. // tasks {
  115. // val dokkaHtml by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  116. // outputDirectory.set(buildDir.resolve("dokka"))
  117. // }
  118. // val dokkaGfm by getting(org.jetbrains.dokka.gradle.DokkaTask::class) {
  119. // outputDirectory.set(buildDir.resolve("dokka-gfm"))
  120. // }
  121. // }
  122. // tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
  123. // dokkaSourceSets.configureEach {
  124. // perPackageOption {
  125. // matchingRegex.set("net\\.mamoe\\.mirai\\.*")
  126. // skipDeprecated.set(true)
  127. // }
  128. //
  129. // for (suppressedPackage in arrayOf(
  130. // """net.mamoe.mirai.internal""",
  131. // """net.mamoe.mirai.internal.message""",
  132. // """net.mamoe.mirai.internal.network""",
  133. // """net.mamoe.mirai.console.internal""",
  134. // """net.mamoe.mirai.console.compiler.common"""
  135. // )) {
  136. // perPackageOption {
  137. // matchingRegex.set(suppressedPackage.replace(".", "\\."))
  138. // suppress.set(true)
  139. // }
  140. // }
  141. // }
  142. // }
  143. }
  144. fun Project.configureMppShadow() {
  145. val kotlin =
  146. runCatching {
  147. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  148. }.getOrNull() ?: return
  149. if (project.configurations.findByName("jvmRuntimeClasspath") != null) {
  150. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  151. group = "mirai"
  152. archiveClassifier.set("-all")
  153. val compilations =
  154. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  155. .map { it.compilations["main"] }
  156. compilations.forEach {
  157. dependsOn(it.compileKotlinTask)
  158. from(it.output)
  159. }
  160. from(project.configurations.findByName("jvmRuntimeClasspath"))
  161. this.exclude { file ->
  162. file.name.endsWith(".sf", ignoreCase = true)
  163. }
  164. /*
  165. this.manifest {
  166. this.attributes(
  167. "Manifest-Version" to 1,
  168. "Implementation-Vendor" to "Mamoe Technologies",
  169. "Implementation-Title" to this.name.toString(),
  170. "Implementation-Version" to this.version.toString()
  171. )
  172. }*/
  173. }
  174. }
  175. }