|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import aws.sdk.kotlin.gradle.dsl.configurePublishing |
| 6 | +import aws.sdk.kotlin.gradle.kmp.configureKmpTargets |
| 7 | + |
| 8 | +plugins { |
| 9 | + kotlin("multiplatform") |
| 10 | +} |
| 11 | + |
| 12 | +val sdkVersion: String by project |
| 13 | +group = "aws.sdk.kotlin.crt" |
| 14 | +version = sdkVersion |
| 15 | +description = "Kotlin Multiplatform bindings for AWS SDK Common Runtime" |
| 16 | + |
| 17 | +// See: https://kotlinlang.org/docs/reference/opt-in-requirements.html#opting-in-to-using-api |
| 18 | +val optinAnnotations = listOf("kotlin.RequiresOptIn") |
| 19 | + |
| 20 | +val ideaActive = System.getProperty("idea.active") == "true" |
| 21 | +extra["ideaActive"] = ideaActive |
| 22 | + |
| 23 | +// KMP configuration from build plugin |
| 24 | +configureKmpTargets() |
| 25 | + |
| 26 | +kotlin { |
| 27 | + explicitApi() |
| 28 | + |
| 29 | + jvm { |
| 30 | + attributes { |
| 31 | + attribute<org.gradle.api.attributes.java.TargetJvmEnvironment>( |
| 32 | + TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, |
| 33 | + objects.named(TargetJvmEnvironment.STANDARD_JVM), |
| 34 | + ) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // KMP doesn't support sharing source sets for multiple JVM targets OR JVM + Android targets. |
| 39 | + // We can manually declare a `jvmCommon` target and wire it up. It will compile fine but Intellij does |
| 40 | + // not support this and the developer experience is abysmal. Kotlin/Native suffers a similar problem and |
| 41 | + // we can use the same solution. Simply, if Intellij is running (i.e. the one invoking this script) then |
| 42 | + // assume we are only building for JVM. Otherwise declare the additional JVM target for Android and |
| 43 | + // set the sourceSet the same for both but with different runtime dependencies. |
| 44 | + // See: |
| 45 | + // * https://kotlinlang.org/docs/mpp-share-on-platforms.html#share-code-in-libraries |
| 46 | + // * https://kotlinlang.org/docs/mpp-set-up-targets.html#distinguish-several-targets-for-one-platform |
| 47 | + if (!ideaActive) { |
| 48 | + |
| 49 | + // NOTE: We don't actually need the Android plugin. All of the Android specifics are handled in aws-crt-java, |
| 50 | + // we just need a variant with a different dependency set + some distinguishing attributes. |
| 51 | + jvm("android") { |
| 52 | + attributes { |
| 53 | + attribute( |
| 54 | + org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.Companion.attribute, |
| 55 | + org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType.androidJvm, |
| 56 | + ) |
| 57 | + attribute( |
| 58 | + TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, |
| 59 | + objects.named(TargetJvmEnvironment.ANDROID), |
| 60 | + ) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + val kotlinVersion: String by project |
| 66 | + val coroutinesVersion: String by project |
| 67 | + val mockServerVersion: String by project |
| 68 | + |
| 69 | + sourceSets { |
| 70 | + val commonMain by getting { |
| 71 | + dependencies { |
| 72 | + implementation(kotlin("stdlib-common")) |
| 73 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion") |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + val jvmMain by getting { |
| 78 | + dependencies { |
| 79 | + val crtJavaVersion: String by project |
| 80 | + implementation("software.amazon.awssdk.crt:aws-crt:$crtJavaVersion") |
| 81 | + |
| 82 | + // FIXME - temporary integration with CompletableFuture while we work out a POC on the jvm target |
| 83 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutinesVersion") |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + val jvmTest by getting { |
| 88 | + dependencies { |
| 89 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion") |
| 90 | + implementation("org.mock-server:mockserver-netty:$mockServerVersion") |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (!ideaActive) { |
| 95 | + val androidMain by getting { |
| 96 | + // re-use the jvm (desktop) sourceSet. We only really care about declaring a variant with a different set |
| 97 | + // of runtime dependencies |
| 98 | + kotlin.srcDir("jvm/src") |
| 99 | + dependsOn(commonMain) |
| 100 | + dependencies { |
| 101 | + val crtJavaVersion: String by project |
| 102 | + // we need symbols we can resolve during compilation but at runtime (i.e. on device) we depend on the Android dependency |
| 103 | + compileOnly("software.amazon.awssdk.crt:aws-crt:$crtJavaVersion") |
| 104 | + implementation("software.amazon.awssdk.crt:aws-crt-android:$crtJavaVersion@aar") |
| 105 | + |
| 106 | + // FIXME - temporary integration with CompletableFuture while we work out a POC on the jvm target |
| 107 | + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:$coroutinesVersion") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // disable compilation of android test source set. It is the same as the jvmTest sourceSet/tests. This |
| 112 | + // sourceSet only exists to create a new variant that is the same in every way except the runtime |
| 113 | + // dependency on aws-crt-android. To test this we would need to run it on device/emulator. |
| 114 | + tasks.getByName("androidTest").enabled = false |
| 115 | + tasks.getByName("compileTestKotlinAndroid").enabled = false |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + sourceSets.all { |
| 120 | + optinAnnotations.forEach { languageSettings.optIn(it) } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// Publishing |
| 125 | +configurePublishing("aws-crt-kotlin") |
0 commit comments