From dcdc7f0c71df012e5e8a793f3c35d184b62939c2 Mon Sep 17 00:00:00 2001 From: lihenggui Date: Wed, 7 Feb 2024 16:27:20 -0800 Subject: [PATCH] Add KMP convention plugin --- build-logic/convention/build.gradle.kts | 4 + .../main/kotlin/KmpLibraryConventionPlugin.kt | 32 ++++++++ .../apps/nowinandroid/KotlinMultiplatform.kt | 77 +++++++++++++++++++ gradle/libs.versions.toml | 1 + 4 files changed, 114 insertions(+) create mode 100644 build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt create mode 100644 build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/KotlinMultiplatform.kt diff --git a/build-logic/convention/build.gradle.kts b/build-logic/convention/build.gradle.kts index aa0e615ad..2c84a8003 100644 --- a/build-logic/convention/build.gradle.kts +++ b/build-logic/convention/build.gradle.kts @@ -110,5 +110,9 @@ gradlePlugin { id = "nowinandroid.jvm.library" implementationClass = "JvmLibraryConventionPlugin" } + register("kmpLibrary") { + id = "nowinandroid.kmp.library" + implementationClass = "KmpLibraryConventionPlugin" + } } } diff --git a/build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt b/build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt new file mode 100644 index 000000000..ed9a20f47 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.google.samples.apps.nowinandroid.configureKotlinMultiplatform +import org.gradle.api.Plugin +import org.gradle.api.Project + +/** + * A plugin that applies the Kotlin Multiplatform plugin and configures it for the project. + * https://github.com/cashapp/sqldelight/blob/master/buildLogic/multiplatform-convention/src/main/kotlin/app/cash/sqldelight/multiplatform/MultiplatformConventions.kt + */ +class KmpLibraryConventionPlugin: Plugin { + override fun apply(target: Project) { + with(target) { + plugins.apply("org.jetbrains.kotlin.multiplatform") + configureKotlinMultiplatform() + } + } +} diff --git a/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/KotlinMultiplatform.kt b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/KotlinMultiplatform.kt new file mode 100644 index 000000000..cd5ca7545 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/KotlinMultiplatform.kt @@ -0,0 +1,77 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.nowinandroid + +import org.gradle.api.Project +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.dsl.kotlinExtension +import org.jetbrains.kotlin.konan.target.HostManager + +internal fun Project.configureKotlinMultiplatform() { + (project.kotlinExtension as KotlinMultiplatformExtension).apply { + jvm() + + js { + browser { + testTask { + useKarma { + useChromeHeadless() + } + } + } + compilations.configureEach { + kotlinOptions { + moduleKind = "umd" + } + } + } + + // tier 1 + linuxX64() + macosX64() + macosArm64() + iosSimulatorArm64() + iosX64() + + // tier 2 + linuxArm64() + watchosSimulatorArm64() + watchosX64() + watchosArm32() + watchosArm64() + tvosSimulatorArm64() + tvosX64() + tvosArm64() + iosArm64() + + // tier 3 + androidNativeArm32() + androidNativeArm64() + androidNativeX86() + androidNativeX64() + mingwX64() + watchosDeviceArm64() + + // linking fails for the linux test build if not built on a linux host + // ensure the tests and linking for them is only done on linux hosts + project.tasks.named("linuxX64Test") { enabled = HostManager.hostIsLinux } + project.tasks.named("linkDebugTestLinuxX64") { enabled = HostManager.hostIsLinux } + + project.tasks.named("mingwX64Test") { enabled = HostManager.hostIsMingw } + project.tasks.named("linkDebugTestMingwX64") { enabled = HostManager.hostIsMingw } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index db9ca3ed0..117fefa4c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -202,3 +202,4 @@ nowinandroid-android-lint = { id = "nowinandroid.android.lint", version = "unspe nowinandroid-android-room = { id = "nowinandroid.android.room", version = "unspecified" } nowinandroid-android-test = { id = "nowinandroid.android.test", version = "unspecified" } nowinandroid-jvm-library = { id = "nowinandroid.jvm.library", version = "unspecified" } +nowinandroid-kmp-library = { id = "nowinandroid.kmp.library", version = "unspecified" }