Skip to content

Commit

Permalink
Update build config
Browse files Browse the repository at this point in the history
  • Loading branch information
syxc committed Jul 5, 2024
1 parent e83677f commit 6046b57
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 39 deletions.
1 change: 0 additions & 1 deletion androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {
alias(libs.plugins.android.application)
kotlin("android")
alias(libs.plugins.compose.compiler)
id("com.jithub.build.logic")
}

android {
Expand Down
6 changes: 5 additions & 1 deletion build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ dependencies {
gradlePlugin {
plugins {
register("build-logic") {
id = "com.jithub.build.logic"
id = "com.jithub.build-logic"
implementationClass = "BuildLogic"
}
register("build-support") {
id = "com.jithub.build-support"
implementationClass = "BuildSupportPlugin"
}
}
}
108 changes: 108 additions & 0 deletions build-logic/convention/src/main/kotlin/BuildSupportPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.Framework
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

class BuildSupportPlugin : BasePlugin() {

override fun apply(target: Project) {
log("apply target: ${target.displayName}")

target.group = "com.jithub.build-support"
target.version = "0.1"

target.configureCommonKotlin()
target.configureCommonCompose()
}

// https://github.com/cashapp/redwood/blob/trunk/build-support/src/main/kotlin/app/cash/redwood/buildsupport/RedwoodBuildPlugin.kt
private fun Project.configureCommonKotlin() {
tasks.withType(KotlinCompile::class.java).configureEach {
compilerOptions {
// Treat all Kotlin warnings as errors (disabled by default)
allWarningsAsErrors.set(properties["warningsAsErrors"] as? Boolean ?: false)

freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
// https://kotlinlang.org/docs/whatsnew13.html#progressive-mode
"-progressive",
// https://kotlinlang.org/docs/multiplatform-expect-actual.html#expected-and-actual-classes
"-Xexpect-actual-classes"
)
)
}
}

tasks.withType(KotlinJvmCompile::class.java).configureEach {
compilerOptions {
freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
"-Xjvm-default=all"
)
)
jvmTarget.set(Versions.jvmTarget)
}
}

// Kotlin requires the Java compatibility matches.
tasks.withType(JavaCompile::class.java).configureEach {
sourceCompatibility = Versions.javaVersion.toString()
targetCompatibility = Versions.javaVersion.toString()
}

plugins.withId("org.jetbrains.kotlin.multiplatform") {
val kotlin = extensions.getByName("kotlin") as KotlinMultiplatformExtension

// We set the JVM target (the bytecode version) above for all Kotlin-based Java bytecode
// compilations, but we also need to set the JDK API version for the Kotlin JVM targets to
// prevent linking against newer JDK APIs (the Android targets link against the android.jar).
kotlin.targets.withType(KotlinJvmTarget::class.java) {
compilations.configureEach {
compileTaskProvider.configure {
compilerOptions {
freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
"-Xjdk-release=${Versions.javaVersion}"
)
)
}
}
}
}

kotlin.targets.withType(KotlinNativeTarget::class.java) {
binaries.withType(Framework::class.java) {
linkerOpts += "-lsqlite3"
}
}
}
}

/**
* Force Android Compose UI and JetPack Compose UI usage to Compose compiler versions which are compatible
* with the project's Kotlin version.
*/
private fun Project.configureCommonCompose() {
tasks.withType<KotlinJsCompile>().configureEach {
compilerOptions {
freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
// https://github.com/JetBrains/compose-multiplatform/issues/3421
"-Xpartial-linkage=disable",
// https://github.com/JetBrains/compose-multiplatform/issues/3418
"-Xklib-enable-signature-clash-checks=false",
// Translate capturing lambdas into anonymous JS functions rather than hoisting parameters
// and creating a named sibling function. Only affects targets which produce actual JS.
"-Xir-generate-inline-anonymous-functions"
)
)
}
}
}
}
39 changes: 2 additions & 37 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ plugins {
// alias(libs.plugins.jetbrains.compose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.spotless) apply false
id("com.jithub.build.logic")
id("com.jithub.build-logic")
}

allprojects {
Expand All @@ -44,7 +44,7 @@ allprojects {
}
maven("https://jitpack.io")
}
configureCommonKotlin()
apply(plugin = "com.jithub.build-support")
}

subprojects {
Expand All @@ -69,38 +69,3 @@ gradle.taskGraph.whenReady {
tasks.register<Delete>("clean") {
delete(rootProject.layout.buildDirectory)
}

private fun Project.configureCommonKotlin() {
tasks.withType(KotlinCompile::class.java).configureEach {
compilerOptions {
// Treat all Kotlin warnings as errors (disabled by default)
allWarningsAsErrors.set(properties["warningsAsErrors"] as? Boolean ?: false)

freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
// https://kotlinlang.org/docs/whatsnew13.html#progressive-mode
"-progressive",
// https://kotlinlang.org/docs/multiplatform-expect-actual.html#expected-and-actual-classes
"-Xexpect-actual-classes"
)
)
}
}

tasks.withType(KotlinJvmCompile::class.java).configureEach {
compilerOptions {
freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
"-Xjvm-default=all"
)
)
jvmTarget = Versions.jvmTarget
}
}

// Kotlin requires the Java compatibility matches.
tasks.withType(JavaCompile::class.java).configureEach {
sourceCompatibility = Versions.javaVersion.toString()
targetCompatibility = Versions.javaVersion.toString()
}
}

0 comments on commit 6046b57

Please sign in to comment.