build.gradle.kts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Copyright 2019-2020 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 apply false
  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. }
  71. afterEvaluate {
  72. configureJvmTarget()
  73. configureMppShadow()
  74. configureEncoding()
  75. configureKotlinTestSettings()
  76. configureKotlinCompilerSettings()
  77. configureKotlinExperimentalUsages()
  78. // useIr()
  79. if (isKotlinJvmProject) {
  80. configureFlattenSourceSets()
  81. }
  82. configureDokka()
  83. }
  84. }
  85. fun Project.useIr() {
  86. kotlinCompilations?.forEach { kotlinCompilation ->
  87. kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
  88. }
  89. }
  90. fun Project.configureDokka() {
  91. apply(plugin = "org.jetbrains.dokka")
  92. tasks {
  93. val dokka by getting(DokkaTask::class) {
  94. outputFormat = "html"
  95. outputDirectory = "$buildDir/dokka"
  96. }
  97. val dokkaMarkdown by creating(DokkaTask::class) {
  98. outputFormat = "markdown"
  99. outputDirectory = "$buildDir/dokka-markdown"
  100. }
  101. val dokkaGfm by creating(DokkaTask::class) {
  102. outputFormat = "gfm"
  103. outputDirectory = "$buildDir/dokka-gfm"
  104. }
  105. }
  106. for (task in tasks.filterIsInstance<DokkaTask>()) {
  107. task.configuration {
  108. perPackageOption {
  109. prefix = "net.mamoe.mirai"
  110. skipDeprecated = true
  111. }
  112. for (suppressedPackage in arrayOf(
  113. "net.mamoe.mirai.internal",
  114. "net.mamoe.mirai.event.internal",
  115. "net.mamoe.mirai.utils.internal",
  116. "net.mamoe.mirai.internal"
  117. )) {
  118. perPackageOption {
  119. prefix = suppressedPackage
  120. suppress = true
  121. }
  122. }
  123. }
  124. }
  125. }
  126. @Suppress("NOTHING_TO_INLINE") // or error
  127. fun Project.configureJvmTarget() {
  128. tasks.withType(KotlinJvmCompile::class.java) {
  129. kotlinOptions.jvmTarget = "1.8"
  130. }
  131. kotlinTargets.orEmpty().filterIsInstance<KotlinJvmTarget>().forEach { target ->
  132. target.compilations.all {
  133. kotlinOptions.jvmTarget = "1.8"
  134. kotlinOptions.languageVersion = "1.4"
  135. }
  136. target.testRuns["test"].executionTask.configure { useJUnitPlatform() }
  137. }
  138. extensions.findByType(JavaPluginExtension::class.java)?.run {
  139. sourceCompatibility = JavaVersion.VERSION_1_8
  140. targetCompatibility = JavaVersion.VERSION_1_8
  141. }
  142. }
  143. fun Project.configureMppShadow() {
  144. val kotlin =
  145. runCatching {
  146. (this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
  147. }.getOrNull() ?: return
  148. val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
  149. group = "mirai"
  150. archiveClassifier.set("-all")
  151. val compilations =
  152. kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
  153. .map { it.compilations["main"] }
  154. compilations.forEach {
  155. dependsOn(it.compileKotlinTask)
  156. from(it.output)
  157. }
  158. println(project.configurations.joinToString())
  159. from(project.configurations.getByName("jvmRuntimeClasspath"))
  160. this.exclude { file ->
  161. file.name.endsWith(".sf", ignoreCase = true)
  162. }
  163. /*
  164. this.manifest {
  165. this.attributes(
  166. "Manifest-Version" to 1,
  167. "Implementation-Vendor" to "Mamoe Technologies",
  168. "Implementation-Title" to this.name.toString(),
  169. "Implementation-Version" to this.version.toString()
  170. )
  171. }*/
  172. }
  173. }
  174. fun Project.configureEncoding() {
  175. tasks.withType(JavaCompile::class.java) {
  176. options.encoding = "UTF8"
  177. }
  178. }
  179. fun Project.configureKotlinTestSettings() {
  180. tasks.withType(Test::class) {
  181. useJUnitPlatform()
  182. }
  183. when {
  184. isKotlinJvmProject -> {
  185. dependencies {
  186. testImplementation(kotlin("test-junit5"))
  187. testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
  188. testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  189. }
  190. }
  191. isKotlinMpp -> {
  192. kotlinSourceSets?.forEach { sourceSet ->
  193. if (sourceSet.name == "common") {
  194. sourceSet.dependencies {
  195. implementation(kotlin("test"))
  196. implementation(kotlin("test-annotations-common"))
  197. }
  198. } else {
  199. sourceSet.dependencies {
  200. implementation(kotlin("test-junit5"))
  201. implementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
  202. implementation("org.junit.jupiter:junit-jupiter-engine:5.2.0")
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. fun Project.configureKotlinCompilerSettings() {
  210. val kotlinCompilations = kotlinCompilations ?: return
  211. for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
  212. if (isKotlinJvmProject) {
  213. @Suppress("UNCHECKED_CAST")
  214. this as KotlinCompilation<KotlinJvmOptions>
  215. }
  216. kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
  217. }
  218. }
  219. val experimentalAnnotations = arrayOf(
  220. "kotlin.RequiresOptIn",
  221. "kotlin.contracts.ExperimentalContracts",
  222. "kotlin.experimental.ExperimentalTypeInference",
  223. "kotlin.ExperimentalUnsignedTypes",
  224. "kotlin.time.ExperimentalTime",
  225. "kotlinx.serialization.ExperimentalSerializationApi",
  226. "net.mamoe.mirai.utils.MiraiInternalApi",
  227. "net.mamoe.mirai.utils.MiraiExperimentalApi",
  228. "net.mamoe.mirai.LowLevelApi",
  229. "net.mamoe.mirai.utils.UnstableExternalImage",
  230. "net.mamoe.mirai.message.data.ExperimentalMessageKey"
  231. )
  232. fun Project.configureKotlinExperimentalUsages() {
  233. val sourceSets = kotlinSourceSets ?: return
  234. for (target in sourceSets) {
  235. target.languageSettings.progressiveMode = true
  236. target.languageSettings.enableLanguageFeature("InlineClasses")
  237. experimentalAnnotations.forEach { a ->
  238. target.languageSettings.useExperimentalAnnotation(a)
  239. }
  240. }
  241. }
  242. fun Project.configureFlattenSourceSets() {
  243. sourceSets {
  244. findByName("main")?.apply {
  245. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  246. java.setSrcDirs(listOf(projectDir.resolve("src")))
  247. }
  248. findByName("test")?.apply {
  249. resources.setSrcDirs(listOf(projectDir.resolve("resources")))
  250. java.setSrcDirs(listOf(projectDir.resolve("test")))
  251. }
  252. }
  253. }
  254. val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets
  255. val Project.kotlinTargets
  256. get() =
  257. extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
  258. ?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets
  259. val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
  260. val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension
  261. val Project.kotlinCompilations
  262. get() = kotlinTargets?.flatMap { it.compilations }