build.gradle.kts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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.gradle.DokkaTask
  12. import org.jetbrains.kotlin.gradle.dsl.*
  13. import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
  14. import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
  15. import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
  16. import org.jetbrains.kotlin.utils.addToStdlib.safeAs
  17. buildscript {
  18. repositories {
  19. mavenLocal()
  20. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  21. mavenCentral()
  22. jcenter()
  23. google()
  24. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  25. maven(url = "https://kotlin.bintray.com/kotlinx")
  26. }
  27. dependencies {
  28. classpath("com.android.tools.build:gradle:${Versions.androidGradlePlugin}")
  29. classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${Versions.atomicFU}")
  30. classpath("org.jetbrains.kotlinx:binary-compatibility-validator:${Versions.binaryValidator}")
  31. }
  32. }
  33. plugins {
  34. kotlin("jvm") version Versions.kotlinCompiler
  35. kotlin("plugin.serialization") version Versions.kotlinCompiler
  36. id("org.jetbrains.dokka") version Versions.dokka
  37. id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
  38. id("com.jfrog.bintray") // version Versions.bintray
  39. id("com.gradle.plugin-publish") version "0.12.0" apply false
  40. }
  41. // https://github.com/kotlin/binary-compatibility-validator
  42. apply(plugin = "binary-compatibility-validator")
  43. configure<kotlinx.validation.ApiValidationExtension> {
  44. allprojects.forEach { subproject ->
  45. ignoredProjects.add(subproject.name)
  46. }
  47. ignoredProjects.remove("binary-compatibility-validator")
  48. // Enable validator for module `binary-compatibility-validator` only.
  49. ignoredPackages.add("net.mamoe.mirai.internal")
  50. nonPublicMarkers.add("net.mamoe.mirai.MiraiInternalApi")
  51. nonPublicMarkers.add("net.mamoe.mirai.MiraiExperimentalApi")
  52. }
  53. project.ext.set("isAndroidSDKAvailable", false)
  54. // until
  55. // https://youtrack.jetbrains.com/issue/KT-37152,
  56. // are fixed.
  57. /*
  58. runCatching {
  59. val keyProps = Properties().apply {
  60. file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
  61. }
  62. if (keyProps.getProperty("sdk.dir", "").isNotEmpty()) {
  63. project.ext.set("isAndroidSDKAvailable", true)
  64. } else {
  65. project.ext.set("isAndroidSDKAvailable", false)
  66. }
  67. }.exceptionOrNull()?.run {
  68. project.ext.set("isAndroidSDKAvailable", false)
  69. }*/
  70. allprojects {
  71. group = "net.mamoe"
  72. version = Versions.project
  73. repositories {
  74. // mavenLocal() // cheching issue cause compiler exception
  75. // maven(url = "https://mirrors.huaweicloud.com/repository/maven")
  76. jcenter()
  77. maven(url = "https://dl.bintray.com/kotlin/kotlin-eap")
  78. maven(url = "https://kotlin.bintray.com/kotlinx")
  79. google()
  80. mavenCentral()
  81. maven(url = "https://dl.bintray.com/karlatemp/misc")
  82. }
  83. afterEvaluate {
  84. configureJvmTarget()
  85. configureMppShadow()
  86. configureEncoding()
  87. configureKotlinTestSettings()
  88. configureKotlinCompilerSettings()
  89. configureKotlinExperimentalUsages()
  90. runCatching {
  91. blockingBridge {
  92. unitCoercion = net.mamoe.kjbb.compiler.UnitCoercion.COMPATIBILITY
  93. }
  94. }
  95. // useIr()
  96. if (isKotlinJvmProject) {
  97. configureFlattenSourceSets()
  98. }
  99. }
  100. }
  101. subprojects {
  102. afterEvaluate {
  103. if (project.name == "mirai-core-api") configureDokka()
  104. }
  105. }
  106. fun Project.useIr() {
  107. kotlinCompilations?.forEach { kotlinCompilation ->
  108. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  109. }
  110. }
  111. fun Project.configureDokka() {
  112. apply(plugin = "org.jetbrains.dokka")
  113. tasks {
  114. val dokkaHtml by getting(DokkaTask::class) {
  115. outputDirectory.set(buildDir.resolve("dokka"))
  116. }
  117. val dokkaGfm by getting(DokkaTask::class) {
  118. outputDirectory.set(buildDir.resolve("dokka-gfm"))
  119. }
  120. }
  121. tasks.withType<DokkaTask>().configureEach {
  122. dokkaSourceSets.configureEach {
  123. perPackageOption {
  124. matchingRegex.set("net\\.mamoe\\.mirai\\.*")
  125. skipDeprecated.set(true)
  126. }
  127. for (suppressedPackage in arrayOf(
  128. """net.mamoe.mirai.internal""",
  129. """net.mamoe.mirai.internal.message""",
  130. """net.mamoe.mirai.internal.network"""
  131. )) {
  132. perPackageOption {
  133. matchingRegex.set(suppressedPackage.replace(".", "\\."))
  134. suppress.set(true)
  135. }
  136. }
  137. }
  138. }
  139. }
  140. @Suppress("NOTHING_TO_INLINE") // or error
  141. fun Project.configureJvmTarget() {
  142. tasks.withType(KotlinJvmCompile::class.java) {
  143. kotlinOptions.jvmTarget = "1.8"
  144. }
  145. kotlinTargets.orEmpty().filterIsInstance<KotlinJvmTarget>().forEach { target ->
  146. target.compilations.all {
  147. kotlinOptions.jvmTarget = "1.8"
  148. kotlinOptions.languageVersion = "1.4"
  149. }
  150. target.testRuns["test"].executionTask.configure { useJUnitPlatform() }
  151. }
  152. extensions.findByType(JavaPluginExtension::class.java)?.run {
  153. sourceCompatibility = JavaVersion.VERSION_1_8
  154. targetCompatibility = JavaVersion.VERSION_1_8
  155. }
  156. }
  157. fun Project.configureMppShadow() {
  158. val kotlin =
  159. runCatching {
  160. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  161. }.getOrNull() ?: return
  162. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  163. group = "mirai"
  164. archiveClassifier.set("-all")
  165. val compilations =
  166. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  167. .map { it.compilations["main"] }
  168. compilations.forEach {
  169. dependsOn(it.compileKotlinTask)
  170. from(it.output)
  171. }
  172. println(project.configurations.joinToString())
  173. from(project.configurations.getByName("jvmRuntimeClasspath"))
  174. this.exclude { file ->
  175. file.name.endsWith(".sf", ignoreCase = true)
  176. }
  177. /*
  178. this.manifest {
  179. this.attributes(
  180. "Manifest-Version" to 1,
  181. "Implementation-Vendor" to "Mamoe Technologies",
  182. "Implementation-Title" to this.name.toString(),
  183. "Implementation-Version" to this.version.toString()
  184. )
  185. }*/
  186. }
  187. }
  188. fun Project.configureEncoding() {
  189. tasks.withType(JavaCompile::class.java) {
  190. options.encoding = "UTF8"
  191. }
  192. }
  193. fun Project.configureKotlinTestSettings() {
  194. tasks.withType(Test::class) {
  195. useJUnitPlatform()
  196. }
  197. when {
  198. isKotlinJvmProject -> {
  199. dependencies {
  200. testImplementation(kotlin("test-junit5"))
  201. testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
  202. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  203. }
  204. }
  205. isKotlinMpp -> {
  206. kotlinSourceSets?.forEach { sourceSet ->
  207. if (sourceSet.name == "common") {
  208. sourceSet.dependencies {
  209. implementation(kotlin("test"))
  210. implementation(kotlin("test-annotations-common"))
  211. }
  212. } else {
  213. sourceSet.dependencies {
  214. implementation(kotlin("test-junit5"))
  215. implementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
  216. implementation("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. fun Project.configureKotlinCompilerSettings() {
  224. val kotlinCompilations = kotlinCompilations ?: return
  225. for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
  226. if (isKotlinJvmProject) {
  227. @Suppress("UNCHECKED_CAST")
  228. this as KotlinCompilation<KotlinJvmOptions>
  229. }
  230. kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
  231. }
  232. }
  233. val experimentalAnnotations = arrayOf(
  234. "kotlin.RequiresOptIn",
  235. "kotlin.contracts.ExperimentalContracts",
  236. "kotlin.experimental.ExperimentalTypeInference",
  237. "kotlin.ExperimentalUnsignedTypes",
  238. "kotlin.time.ExperimentalTime",
  239. "kotlinx.serialization.ExperimentalSerializationApi",
  240. "net.mamoe.mirai.utils.MiraiInternalApi",
  241. "net.mamoe.mirai.utils.MiraiExperimentalApi",
  242. "net.mamoe.mirai.LowLevelApi",
  243. "net.mamoe.mirai.utils.UnstableExternalImage",
  244. "net.mamoe.mirai.message.data.ExperimentalMessageKey",
  245. "net.mamoe.mirai.console.ConsoleFrontEndImplementation",
  246. "net.mamoe.mirai.console.util.ConsoleInternalApi",
  247. "net.mamoe.mirai.console.util.ConsoleExperimentalApi"
  248. )
  249. fun Project.configureKotlinExperimentalUsages() {
  250. val sourceSets = kotlinSourceSets ?: return
  251. for (target in sourceSets) {
  252. target.languageSettings.progressiveMode = true
  253. target.languageSettings.enableLanguageFeature("InlineClasses")
  254. experimentalAnnotations.forEach { a ->
  255. target.languageSettings.useExperimentalAnnotation(a)
  256. }
  257. }
  258. }
  259. fun Project.configureFlattenSourceSets() {
  260. sourceSets {
  261. findByName("main")?.apply {
  262. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  263. java.setSrcDirs(listOf(projectDir.resolve("src")))
  264. }
  265. findByName("test")?.apply {
  266. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  267. java.setSrcDirs(listOf(projectDir.resolve("test")))
  268. }
  269. }
  270. }
  271. val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
  272. val Project.kotlinTargets
  273. get() =
  274. extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
  275. ?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets
  276. val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
  277. val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension
  278. val Project.kotlinCompilations
  279. get() = kotlinTargets?.flatMap { it.compilations }