build.gradle.kts 9.8 KB

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