build.gradle.kts 12 KB

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