Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 660c2d7 | 2020-02-14 13:18:37 +0300 | [diff] [blame] | 2 | * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Victor Turansky | fe2fedc | 2020-08-27 15:47:50 +0300 | [diff] [blame] | 5 | apply plugin: 'org.jetbrains.kotlin.multiplatform' |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 6 | apply from: rootProject.file("gradle/compile-jvm-multiplatform.gradle") |
| 7 | apply from: rootProject.file("gradle/compile-common.gradle") |
| 8 | apply from: rootProject.file("gradle/compile-js-multiplatform.gradle") |
| 9 | apply from: rootProject.file("gradle/compile-native-multiplatform.gradle") |
| 10 | apply from: rootProject.file('gradle/publish-npm-js.gradle') |
| 11 | |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 12 | /* ========================================================================== |
| 13 | Configure source sets structure for kotlinx-coroutines-core: |
| 14 | |
| 15 | TARGETS SOURCE SETS |
| 16 | ------- ---------------------------------------------- |
| 17 | |
| 18 | js -----------------------------------------------------+ |
| 19 | | |
| 20 | V |
| 21 | jvm -------------------------------> concurrent ---> common |
| 22 | ^ |
| 23 | ios \ | |
| 24 | macos | ---> nativeDarwin ---> native --+ |
| 25 | tvos | ^ |
| 26 | watchos / | |
| 27 | | |
| 28 | linux \ ---> nativeOther -------+ |
| 29 | mingw / |
| 30 | |
| 31 | ========================================================================== */ |
| 32 | |
| 33 | project.ext.sourceSetSuffixes = ["Main", "Test"] |
| 34 | |
| 35 | void defineSourceSet(newName, dependsOn, includedInPred) { |
| 36 | for (suffix in project.ext.sourceSetSuffixes) { |
| 37 | def newSS = kotlin.sourceSets.maybeCreate(newName + suffix) |
| 38 | for (dep in dependsOn) { |
| 39 | newSS.dependsOn(kotlin.sourceSets[dep + suffix]) |
| 40 | } |
| 41 | for (curSS in kotlin.sourceSets) { |
| 42 | def curName = curSS.name |
| 43 | if (curName.endsWith(suffix)) { |
| 44 | def prefix = curName.substring(0, curName.length() - suffix.length()) |
| 45 | if (includedInPred(prefix)) curSS.dependsOn(newSS) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static boolean isNativeDarwin(String name) { return ["ios", "macos", "tvos", "watchos"].any { name.startsWith(it) } } |
| 52 | static boolean isNativeOther(String name) { return ["linux", "mingw"].any { name.startsWith(it) } } |
| 53 | |
| 54 | defineSourceSet("concurrent", ["common"]) { it in ["jvm", "native"] } |
| 55 | defineSourceSet("nativeDarwin", ["native"]) { isNativeDarwin(it) } |
| 56 | defineSourceSet("nativeOther", ["native"]) { isNativeOther(it) } |
| 57 | |
| 58 | /* ========================================================================== */ |
| 59 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 60 | /* |
| 61 | * All platform plugins and configuration magic happens here instead of build.gradle |
| 62 | * because JMV-only projects depend on core, thus core should always be initialized before configuration. |
| 63 | */ |
| 64 | kotlin { |
| 65 | configure(sourceSets) { |
| 66 | def srcDir = name.endsWith('Main') ? 'src' : 'test' |
| 67 | def platform = name[0..-5] |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 68 | kotlin.srcDirs = ["$platform/$srcDir"] |
Vsevolod Tolstopyatov | 8273a75 | 2019-03-15 19:24:41 +0300 | [diff] [blame] | 69 | if (name == "jvmMain") { |
| 70 | resources.srcDirs = ["$platform/resources"] |
| 71 | } else if (name == "jvmTest") { |
| 72 | resources.srcDirs = ["$platform/test-resources"] |
| 73 | } |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 74 | languageSettings { |
| 75 | progressiveMode = true |
| 76 | experimentalAnnotations.each { useExperimentalAnnotation(it) } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | configure(targets) { |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 81 | // Configure additional binaries and test runs -- one for each OS |
| 82 | if (["macos", "linux", "mingw"].any { name.startsWith(it) }) { |
| 83 | binaries { |
| 84 | // Test for memory leaks using a special entry point that does not exit but returns from main |
| 85 | binaries.getTest("DEBUG").freeCompilerArgs += ["-e", "kotlinx.coroutines.mainNoExit"] |
| 86 | // Configure a separate test where code runs in background |
| 87 | test("background", [org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType.DEBUG]) { |
| 88 | freeCompilerArgs += ["-e", "kotlinx.coroutines.mainBackground"] |
| 89 | } |
| 90 | } |
| 91 | testRuns { |
| 92 | background { setExecutionSourceFrom(binaries.backgroundDebugTest) } |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Roman Elizarov | d2f4b2b | 2019-09-02 17:22:39 +0300 | [diff] [blame] | 98 | configurations { |
| 99 | configureKotlinJvmPlatform(kotlinCompilerPluginClasspath) |
| 100 | } |
| 101 | |
Roman Elizarov | 90a9faf | 2020-02-12 16:38:26 +0300 | [diff] [blame] | 102 | // Update module name for metadata artifact to avoid conflicts |
| 103 | // see https://github.com/Kotlin/kotlinx.coroutines/issues/1797 |
| 104 | compileKotlinMetadata { |
| 105 | kotlinOptions { |
| 106 | freeCompilerArgs += ["-module-name", "kotlinx-coroutines-core-common"] |
| 107 | } |
| 108 | } |
| 109 | |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 110 | // :KLUDGE: Idea.active: This is needed to workaround resolve problems after importing this project to IDEA |
| 111 | def configureNativeSourceSetPreset(name, preset) { |
| 112 | def hostMainCompilation = project.kotlin.targetFromPreset(preset).compilations.main |
| 113 | // Look for platform libraries in "implementation" for default source set |
| 114 | def implementationConfiguration = configurations[hostMainCompilation.defaultSourceSet.implementationMetadataConfigurationName] |
| 115 | // Now find the libraries: Finds platform libs & stdlib, but platform declarations are still not resolved due to IDE bugs |
| 116 | def hostNativePlatformLibs = files( |
| 117 | provider { |
| 118 | implementationConfiguration.findAll { |
| 119 | it.path.endsWith(".klib") || it.absolutePath.contains("klib${File.separator}platform") || it.absolutePath.contains("stdlib") |
| 120 | } |
| 121 | } |
| 122 | ) |
| 123 | // Add all those dependencies |
| 124 | for (suffix in sourceSetSuffixes) { |
| 125 | configure(kotlin.sourceSets[name + suffix]) { |
| 126 | dependencies.add(implementationMetadataConfigurationName, hostNativePlatformLibs) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // :KLUDGE: Idea.active: Configure platform libraries for native source sets when working in IDEA |
| 132 | if (Idea.active) { |
| 133 | def manager = project.ext.hostManager |
| 134 | def linuxPreset = kotlin.presets.linuxX64 |
| 135 | def macosPreset = kotlin.presets.macosX64 |
| 136 | // linux should be always available (cross-compilation capable) -- use it as default |
| 137 | assert manager.isEnabled(linuxPreset.konanTarget) |
| 138 | // use macOS libs for nativeDarwin if available |
| 139 | def macosAvailable = manager.isEnabled(macosPreset.konanTarget) |
| 140 | // configure source sets |
| 141 | configureNativeSourceSetPreset("native", linuxPreset) |
| 142 | configureNativeSourceSetPreset("nativeOther", linuxPreset) |
| 143 | configureNativeSourceSetPreset("nativeDarwin", macosAvailable ? macosPreset : linuxPreset) |
| 144 | } |
| 145 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 146 | kotlin.sourceSets { |
Roman Elizarov | 0bf172b | 2020-06-02 10:20:57 +0300 | [diff] [blame] | 147 | jvmMain.dependencies { |
Vsevolod Tolstopyatov | 559023d | 2020-05-14 16:24:18 +0300 | [diff] [blame] | 148 | compileOnly "com.google.android:annotations:4.1.1.4" |
| 149 | } |
| 150 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 151 | jvmTest.dependencies { |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 152 | api "org.jetbrains.kotlinx:lincheck:$lincheck_version" |
Roman Elizarov | 660c2d7 | 2020-02-14 13:18:37 +0300 | [diff] [blame] | 153 | api "org.jetbrains.kotlinx:kotlinx-knit-test:$knit_version" |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 154 | api "com.esotericsoftware:kryo:4.0.0" |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 155 | implementation project(":android-unit-tests") |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | task checkJdk16() { |
| 160 | // only fail w/o JDK_16 when actually trying to compile, not during project setup phase |
| 161 | doLast { |
| 162 | if (!System.env.JDK_16) { |
| 163 | throw new GradleException("JDK_16 environment variable is not defined. " + |
| 164 | "Can't build against JDK 1.6 runtime and run JDK 1.6 compatibility tests. " + |
| 165 | "Please ensure JDK 1.6 is installed and that JDK_16 points to it.") |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { |
| 171 | kotlinOptions.jdkHome = System.env.JDK_16 |
| 172 | // only fail when actually trying to compile, not during project setup phase |
| 173 | dependsOn(checkJdk16) |
| 174 | } |
| 175 | |
| 176 | jvmTest { |
| 177 | minHeapSize = '1g' |
| 178 | maxHeapSize = '1g' |
| 179 | enableAssertions = true |
| 180 | systemProperty 'java.security.manager', 'kotlinx.coroutines.TestSecurityManager' |
Vsevolod Tolstopyatov | a64e4da | 2019-10-10 13:03:06 +0300 | [diff] [blame] | 181 | // 'stress' is required to be able to run all subpackage tests like ":jvmTests --tests "*channels*" -Pstress=true" |
Roman Elizarov | 738f5a2 | 2020-10-12 19:03:46 +0300 | [diff] [blame] | 182 | if (!Idea.active && rootProject.properties['stress'] == null) { |
Vsevolod Tolstopyatov | a64e4da | 2019-10-10 13:03:06 +0300 | [diff] [blame] | 183 | exclude '**/*StressTest.*' |
| 184 | } |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 185 | systemProperty 'kotlinx.coroutines.scheduler.keep.alive.sec', '100000' // any unpark problem hangs test |
| 186 | } |
| 187 | |
Vsevolod Tolstopyatov | 70a7487 | 2020-04-24 14:59:28 +0300 | [diff] [blame] | 188 | jvmJar { |
| 189 | manifest { |
| 190 | attributes "Premain-Class": "kotlinx.coroutines.debug.AgentPremain" |
| 191 | attributes "Can-Retransform-Classes": "true" |
| 192 | } |
| 193 | } |
| 194 | |
Roman Elizarov | 216828a | 2019-09-28 00:42:51 +0300 | [diff] [blame] | 195 | task jvmStressTest(type: Test, dependsOn: compileTestKotlinJvm) { |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 196 | classpath = files { jvmTest.classpath } |
| 197 | testClassesDirs = files { jvmTest.testClassesDirs } |
Roman Elizarov | fd27d55 | 2019-09-25 23:49:34 +0300 | [diff] [blame] | 198 | minHeapSize = '1g' |
| 199 | maxHeapSize = '1g' |
| 200 | include '**/*StressTest.*' |
Roman Elizarov | 0b16abf | 2019-09-03 11:07:11 +0300 | [diff] [blame] | 201 | enableAssertions = true |
| 202 | testLogging.showStandardStreams = true |
Roman Elizarov | fd27d55 | 2019-09-25 23:49:34 +0300 | [diff] [blame] | 203 | systemProperty 'kotlinx.coroutines.scheduler.keep.alive.sec', '100000' // any unpark problem hangs test |
Nikita Koval | ac42be9 | 2020-04-23 20:01:05 +0300 | [diff] [blame] | 204 | systemProperty 'kotlinx.coroutines.semaphore.segmentSize', '2' |
| 205 | systemProperty 'kotlinx.coroutines.semaphore.maxSpinCycles', '10' |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | task jdk16Test(type: Test, dependsOn: [compileTestKotlinJvm, checkJdk16]) { |
| 209 | classpath = files { jvmTest.classpath } |
| 210 | testClassesDirs = files { jvmTest.testClassesDirs } |
| 211 | executable = "$System.env.JDK_16/bin/java" |
Roman Elizarov | 7672c52 | 2019-08-28 12:04:59 +0300 | [diff] [blame] | 212 | exclude '**/*LFStressTest.*' // lock-freedom tests use LockFreedomTestEnvironment which needs JDK8 |
SokolovaMaria | 0126dba | 2019-11-26 01:04:07 +0300 | [diff] [blame] | 213 | exclude '**/*LCStressTest.*' // lin-check tests use LinChecker which needs JDK8 |
Roman Elizarov | 7672c52 | 2019-08-28 12:04:59 +0300 | [diff] [blame] | 214 | exclude '**/exceptions/**' // exceptions tests check suppressed exception which needs JDK8 |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 215 | exclude '**/ExceptionsGuideTest.*' |
Roman Elizarov | e60bcbd | 2020-07-17 12:29:59 +0300 | [diff] [blame] | 216 | exclude '**/RunInterruptibleStressTest.*' // fails on JDK 1.6 due to JDK bug |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // Run these tests only during nightly stress test |
| 220 | jdk16Test.onlyIf { project.properties['stressTest'] != null } |
| 221 | |
| 222 | // Always run those tests |
Roman Elizarov | 216828a | 2019-09-28 00:42:51 +0300 | [diff] [blame] | 223 | task moreTest(dependsOn: [jvmStressTest, jdk16Test]) |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 224 | build.dependsOn moreTest |
| 225 | |
| 226 | task testsJar(type: Jar, dependsOn: jvmTestClasses) { |
| 227 | classifier = 'tests' |
| 228 | from compileTestKotlinJvm.destinationDir |
| 229 | } |
| 230 | |
| 231 | artifacts { |
| 232 | archives testsJar |
| 233 | } |