Skip to content

Commit

Permalink
Add android compilation target
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Feb 1, 2021
1 parent 2a7f0a3 commit bacfef5
Show file tree
Hide file tree
Showing 14 changed files with 532 additions and 238 deletions.
206 changes: 26 additions & 180 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlin.utils.addToStdlib.safeAs

buildscript {
repositories {
Expand All @@ -36,7 +33,7 @@ buildscript {
}

plugins {
kotlin("jvm") version Versions.kotlinCompiler
kotlin("jvm") // version Versions.kotlinCompiler
kotlin("plugin.serialization") version Versions.kotlinCompiler
id("org.jetbrains.dokka") version Versions.dokka
id("net.mamoe.kotlin-jvm-blocking-bridge") version Versions.blockingBridge
Expand All @@ -63,8 +60,6 @@ configure<kotlinx.validation.ApiValidationExtension> {
nonPublicMarkers.add("net.mamoe.mirai.MiraiExperimentalApi")
}

project.ext.set("isAndroidSDKAvailable", false)

tasks.register("publishMiraiCoreArtifactsToMavenLocal") {
group = "mirai"
dependsOn(
Expand All @@ -74,24 +69,6 @@ tasks.register("publishMiraiCoreArtifactsToMavenLocal") {
)
}

// until
// https://youtrack.jetbrains.com/issue/KT-37152,
// are fixed.

/*
runCatching {
val keyProps = Properties().apply {
file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
}
if (keyProps.getProperty("sdk.dir", "").isNotEmpty()) {
project.ext.set("isAndroidSDKAvailable", true)
} else {
project.ext.set("isAndroidSDKAvailable", false)
}
}.exceptionOrNull()?.run {
project.ext.set("isAndroidSDKAvailable", false)
}*/

allprojects {
group = "net.mamoe"
version = Versions.project
Expand Down Expand Up @@ -136,12 +113,6 @@ subprojects {
}
}

fun Project.useIr() {
kotlinCompilations?.forEach { kotlinCompilation ->
kotlinCompilation.kotlinOptions.freeCompilerArgs += "-Xuse-ir"
}
}

fun Project.configureDokka() {
apply(plugin = "org.jetbrains.dokka")
tasks {
Expand Down Expand Up @@ -175,169 +146,44 @@ fun Project.configureDokka() {
}
}

@Suppress("NOTHING_TO_INLINE") // or error
fun Project.configureJvmTarget() {
tasks.withType(KotlinJvmCompile::class.java) {
kotlinOptions.jvmTarget = "1.8"
}

kotlinTargets.orEmpty().filterIsInstance<KotlinJvmTarget>().forEach { target ->
target.compilations.all {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.languageVersion = "1.4"
}
target.testRuns["test"].executionTask.configure { useJUnitPlatform() }
}

extensions.findByType(JavaPluginExtension::class.java)?.run {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}

fun Project.configureMppShadow() {
val kotlin =
runCatching {

(this as ExtensionAware).extensions.getByName("kotlin") as? KotlinMultiplatformExtension
}.getOrNull() ?: return

val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
group = "mirai"
archiveClassifier.set("-all")

val compilations =
kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
.map { it.compilations["main"] }

compilations.forEach {
dependsOn(it.compileKotlinTask)
from(it.output)
}

from(project.configurations.getByName("jvmRuntimeClasspath"))

this.exclude { file ->
file.name.endsWith(".sf", ignoreCase = true)
}

/*
this.manifest {
this.attributes(
"Manifest-Version" to 1,
"Implementation-Vendor" to "Mamoe Technologies",
"Implementation-Title" to this.name.toString(),
"Implementation-Version" to this.version.toString()
)
}*/
}
}

fun Project.configureEncoding() {
tasks.withType(JavaCompile::class.java) {
options.encoding = "UTF8"
}
}
if (project.configurations.findByName("jvmRuntimeClasspath") != null) {
val shadowJvmJar by tasks.creating(ShadowJar::class) sd@{
group = "mirai"
archiveClassifier.set("-all")

fun Project.configureKotlinTestSettings() {
tasks.withType(Test::class) {
useJUnitPlatform()
}
when {
isKotlinJvmProject -> {
dependencies {
testImplementation(kotlin("test-junit5"))
val compilations =
kotlin.targets.filter { it.platformType == KotlinPlatformType.jvm }
.map { it.compilations["main"] }

testApi("org.junit.jupiter:junit-jupiter-api:5.2.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.2.0")
compilations.forEach {
dependsOn(it.compileKotlinTask)
from(it.output)
}
}
isKotlinMpp -> {
kotlinSourceSets?.forEach { sourceSet ->
if (sourceSet.name == "common") {
sourceSet.dependencies {
implementation(kotlin("test"))
implementation(kotlin("test-annotations-common"))
}
} else {
sourceSet.dependencies {
implementation(kotlin("test-junit5"))

implementation("org.junit.jupiter:junit-jupiter-api:5.2.0")
implementation("org.junit.jupiter:junit-jupiter-engine:5.2.0")
}
}
from(project.configurations.findByName("jvmRuntimeClasspath"))

this.exclude { file ->
file.name.endsWith(".sf", ignoreCase = true)
}
}
}
}

fun Project.configureKotlinCompilerSettings() {
val kotlinCompilations = kotlinCompilations ?: return
for (kotlinCompilation in kotlinCompilations) with(kotlinCompilation) {
if (isKotlinJvmProject) {
@Suppress("UNCHECKED_CAST")
this as KotlinCompilation<KotlinJvmOptions>
/*
this.manifest {
this.attributes(
"Manifest-Version" to 1,
"Implementation-Vendor" to "Mamoe Technologies",
"Implementation-Title" to this.name.toString(),
"Implementation-Version" to this.version.toString()
)
}*/
}
kotlinOptions.freeCompilerArgs += "-Xjvm-default=all"
}
}

val experimentalAnnotations = arrayOf(
"kotlin.RequiresOptIn",
"kotlin.contracts.ExperimentalContracts",
"kotlin.experimental.ExperimentalTypeInference",
"kotlin.ExperimentalUnsignedTypes",
"kotlin.time.ExperimentalTime",
"kotlin.io.path.ExperimentalPathApi",
"io.ktor.util.KtorExperimentalAPI",

"kotlinx.serialization.ExperimentalSerializationApi",

"net.mamoe.mirai.utils.MiraiInternalApi",
"net.mamoe.mirai.utils.MiraiExperimentalApi",
"net.mamoe.mirai.LowLevelApi",
"net.mamoe.mirai.utils.UnstableExternalImage",

"net.mamoe.mirai.message.data.ExperimentalMessageKey",
"net.mamoe.mirai.console.ConsoleFrontEndImplementation",
"net.mamoe.mirai.console.util.ConsoleInternalApi",
"net.mamoe.mirai.console.util.ConsoleExperimentalApi"
)

fun Project.configureKotlinExperimentalUsages() {
val sourceSets = kotlinSourceSets ?: return

for (target in sourceSets) {
target.languageSettings.progressiveMode = true
target.languageSettings.enableLanguageFeature("InlineClasses")
experimentalAnnotations.forEach { a ->
target.languageSettings.useExperimentalAnnotation(a)
}
}
}

fun Project.configureFlattenSourceSets() {
sourceSets {
findByName("main")?.apply {
resources.setSrcDirs(listOf(projectDir.resolve("resources")))
java.setSrcDirs(listOf(projectDir.resolve("src")))
}
findByName("test")?.apply {
resources.setSrcDirs(listOf(projectDir.resolve("resources")))
java.setSrcDirs(listOf(projectDir.resolve("test")))
}
}
}

val Project.kotlinSourceSets get() = extensions.findByName("kotlin").safeAs<KotlinProjectExtension>()?.sourceSets

val Project.kotlinTargets
get() =
extensions.findByName("kotlin").safeAs<KotlinSingleTargetExtension>()?.target?.let { listOf(it) }
?: extensions.findByName("kotlin").safeAs<KotlinMultiplatformExtension>()?.targets

val Project.isKotlinJvmProject: Boolean get() = extensions.findByName("kotlin") is KotlinJvmProjectExtension
val Project.isKotlinMpp: Boolean get() = extensions.findByName("kotlin") is KotlinMultiplatformExtension

val Project.kotlinCompilations
get() = kotlinTargets?.flatMap { it.compilations }
5 changes: 5 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ plugins {
repositories {
mavenLocal()
jcenter()
google()
mavenCentral()
}

kotlin {
Expand Down Expand Up @@ -46,6 +48,9 @@ dependencies {

api("com.jfrog.bintray.gradle", "gradle-bintray-plugin", version("bintray"))
api("com.github.jengelman.gradle.plugins", "shadow", version("shadow"))
api("org.jetbrains.kotlin", "kotlin-gradle-plugin", version("kotlinCompiler"))
api("org.jetbrains.kotlin", "kotlin-compiler-embeddable", version("kotlinCompiler"))
api("com.android.tools.build", "gradle", version("androidGradlePlugin"))

api(gradleApi())
}
50 changes: 33 additions & 17 deletions buildSrc/src/main/kotlin/Mpp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.gradle.api.NamedDomainObjectCollection
import org.gradle.api.NamedDomainObjectProvider
import org.gradle.api.Project
import org.gradle.kotlin.dsl.provideDelegate
import java.util.*

/*
* Copyright 2020 Mamoe Technologies and contributors.
Expand All @@ -21,11 +21,31 @@ import org.gradle.kotlin.dsl.provideDelegate
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/

val Project.isAndroidSDKAvailable: Boolean
get() {
val isAndroidSDKAvailable: Boolean by this
return isAndroidSDKAvailable
private object ProjectAndroidSdkAvailability {
val map: MutableMap<String, Boolean> = mutableMapOf()

@Synchronized
operator fun get(project: Project): Boolean {
if (map[project.path] != null) return map[project.path]!!

val projectAvailable = project.runCatching {
val keyProps = Properties().apply {
file("local.properties").takeIf { it.exists() }?.inputStream()?.use { load(it) }
}
keyProps.getProperty("sdk.dir", "").isNotEmpty()
}.getOrElse { false }


fun impl(): Boolean {
if (project === project.rootProject) return projectAvailable
return projectAvailable || get(project.rootProject)
}
map[project.path] = impl()
return map[project.path]!!
}
}

val Project.isAndroidSDKAvailable: Boolean get() = ProjectAndroidSdkAvailability[this]

val <T> NamedDomainObjectCollection<T>.androidMain: NamedDomainObjectProvider<T>
get() = named("androidMain")
Expand All @@ -43,16 +63,12 @@ val <T> NamedDomainObjectCollection<T>.commonMain: NamedDomainObjectProvider<T>
get() = named("commonMain")

fun Project.printAndroidNotInstalled() {
// println(
// """Android SDK 可能未安装.
// $name 的 Android 目标编译将不会进行.
// 这不会影响 Android 以外的平台的编译.
// """.trimIndent()
// )
// println(
// """Android SDK might not be installed.
// Android target of $name will not be compiled.
// It does no influence on the compilation of other platforms.
// """.trimIndent()
// )
println(
"""Android SDK 可能未安装. $name 的 Android 目标编译将不会进行. 这不会影响 Android 以外的平台的编译.
""".trimIndent()
)
println(
"""Android SDK might not be installed. Android target of $name will not be compiled. It does no influence on the compilation of other platforms.
""".trimIndent()
)
}
Loading

0 comments on commit bacfef5

Please sign in to comment.