build.gradle.kts 10 KB

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