build.gradle.kts 12 KB

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