build.gradle.kts 12 KB

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