build.gradle.kts 11 KB

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