Restructure build logic, prepare for native
diff --git a/build.gradle b/build.gradle
index 12af8d2..32632ff 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,5 +1,4 @@
 allprojects {
-    group = 'org.jetbrains.kotlinx'
     def deployVersion = properties['DeployVersion']
     if (deployVersion != null) version = deployVersion
 }
@@ -13,12 +12,14 @@
     }
     repositories {
         jcenter()
-        maven { url "http://kotlin.bintray.com/kotlinx" }
-        maven { url "http://kotlin.bintray.com/kotlin-dev" }
+        maven { url "https://kotlin.bintray.com/kotlinx" }
+        maven { url "https://kotlin.bintray.com/kotlin-dev" }
+        maven { url "https://jetbrains.bintray.com/kotlin-native-dependencies" }
         maven { url "https://plugins.gradle.org/m2/" }
     }
     dependencies {
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
+        classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:$kotlin_native_version"
         classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
         classpath "org.jetbrains.kotlinx:atomicfu-gradle-plugin:$atomicFU_version"
         classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$bintray_version"
@@ -36,6 +37,7 @@
 static def platformOf(project) {
     if (project.name.endsWith("-common")) return "common"
     if (project.name.endsWith("-js")) return "js"
+    if (project.name.endsWith("-native")) return "native"
     return "jvm"
 }
 
@@ -47,37 +49,16 @@
 configure(subprojects.findAll { !sourceless.contains(it.name)  }) {
     def platform = platformOf(it)
     apply from: rootProject.file("gradle/compile-${platform}.gradle")
-    apply from: rootProject.file("gradle/compile-all.gradle")
 }
 
 // --------------- Configure sub-projects that are part of the library ---------------
 
 def internal = sourceless + ['benchmarks', 'knit', 'js-stub']
 
-// configure atomicfu for JVM modules
-configure(subprojects.findAll { !internal.contains(it.name) && platformOf(it) == "jvm" }) {
-    apply plugin: 'kotlinx-atomicfu'
-
-    dependencies {
-        compileOnly "org.jetbrains.kotlinx:atomicfu:$atomicFU_version"
-        testCompile "org.jetbrains.kotlinx:atomicfu:$atomicFU_version"
-    }
-
-    atomicFU {
-        inputFiles = sourceSets.main.output.classesDirs
-        outputDir = file("$buildDir/classes-atomicfu/main")
-        classPath = sourceSets.main.runtimeClasspath
-    }
-
-    jar {
-        mainSpec.sourcePaths.clear() // hack to clear existing paths
-        from files(atomicFU.outputs, sourceSets.main.output.resourcesDir)
-    }
-
-    test {
-        classpath = files(configurations.testRuntime, atomicFU.outputs, sourceSets.test.output.classesDirs,
-                sourceSets.main.output.resourcesDir)
-    }
+// configure atomicfu
+configure(subprojects.findAll { !internal.contains(it.name) }) {
+    def platform = platformOf(it)
+    apply from: rootProject.file("gradle/atomicfu-${platform}.gradle")
 }
 
 // configure dependencies on core
@@ -100,7 +81,8 @@
 
 // --------------- Configure sub-projects that are published ---------------
 
-def unpublished = internal + ['kotlinx-coroutines-rx-example', 'example-frontend-js']
+// todo: native is not published yet
+def unpublished = internal + ['kotlinx-coroutines-rx-example', 'example-frontend-js', 'kotlinx-coroutines-core-native']
 
 def core_docs_url = "https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/"
 def core_docs_file = "$projectDir/core/kotlinx-coroutines-core/build/dokka/kotlinx-coroutines-core/package-list"
diff --git a/gradle.properties b/gradle.properties
index dde178f..16e5ff9 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -1,8 +1,10 @@
 version = 0.22.5-SNAPSHOT
+group = org.jetbrains.kotlinx
 
-kotlin_version = 1.2.30
+kotlin_version = 1.2.31
+kotlin_native_version = 0.7-dev-1436
 junit_version = 4.12
-atomicFU_version = 0.9.2
+atomicFU_version = 0.10.0-alpha
 html_version = 0.6.8
 lincheck_version=1.9
 dokka_version = 0.9.16-dev-mpp-hacks-1
diff --git a/gradle/atomicfu-common.gradle b/gradle/atomicfu-common.gradle
new file mode 100644
index 0000000..bf524c1
--- /dev/null
+++ b/gradle/atomicfu-common.gradle
@@ -0,0 +1,4 @@
+
+dependencies {
+    compile "org.jetbrains.kotlinx:atomicfu-common:$atomicFU_version"
+}
\ No newline at end of file
diff --git a/gradle/atomicfu-js.gradle b/gradle/atomicfu-js.gradle
new file mode 100644
index 0000000..05f09f4
--- /dev/null
+++ b/gradle/atomicfu-js.gradle
@@ -0,0 +1,4 @@
+
+dependencies {
+    compile "org.jetbrains.kotlinx:atomicfu-js:$atomicFU_version"
+}
diff --git a/gradle/atomicfu-jvm.gradle b/gradle/atomicfu-jvm.gradle
new file mode 100644
index 0000000..a812931
--- /dev/null
+++ b/gradle/atomicfu-jvm.gradle
@@ -0,0 +1,22 @@
+apply plugin: 'kotlinx-atomicfu'
+
+dependencies {
+    compileOnly "org.jetbrains.kotlinx:atomicfu:$atomicFU_version"
+    testCompile "org.jetbrains.kotlinx:atomicfu:$atomicFU_version"
+}
+
+atomicFU {
+    inputFiles = sourceSets.main.output.classesDirs
+    outputDir = file("$buildDir/classes-atomicfu/main")
+    classPath = sourceSets.main.runtimeClasspath
+}
+
+jar {
+    mainSpec.sourcePaths.clear() // hack to clear existing paths
+    from files(atomicFU.outputs, sourceSets.main.output.resourcesDir)
+}
+
+test {
+    classpath = files(configurations.testRuntime, atomicFU.outputs, sourceSets.test.output.classesDirs,
+            sourceSets.main.output.resourcesDir)
+}
diff --git a/gradle/atomicfu-native.gradle b/gradle/atomicfu-native.gradle
new file mode 100644
index 0000000..75f0128
--- /dev/null
+++ b/gradle/atomicfu-native.gradle
@@ -0,0 +1,6 @@
+
+// todo: this does not work in Kotlin/Native
+
+//dependencies {
+//    compile "org.jetbrains.kotlinx:atomicfu-native:$atomicFU_version"
+//}
diff --git a/gradle/compile-all.gradle b/gradle/compile-all.gradle
deleted file mode 100644
index e4c0c99..0000000
--- a/gradle/compile-all.gradle
+++ /dev/null
@@ -1,19 +0,0 @@
-
-// Shared configuration to compile all modules
-
-kotlin.experimental.coroutines "enable"
-
-repositories {
-    jcenter()
-    maven { url "http://kotlin.bintray.com/kotlinx" }
-    maven { url "https://dl.bintray.com/devexperts/Maven/" }
-}
-
-tasks.withType(Test) {
-    testLogging {
-        showStandardStreams = true
-        events "passed", "failed"
-    }
-    def stressTest = project.properties['stressTest']
-    if (stressTest != null) systemProperties['stressTest'] = stressTest
-}
diff --git a/gradle/compile-common.gradle b/gradle/compile-common.gradle
index 219a8a8..b09589c 100644
--- a/gradle/compile-common.gradle
+++ b/gradle/compile-common.gradle
@@ -3,9 +3,15 @@
 
 apply plugin: 'kotlin-platform-common'
 
+kotlin.experimental.coroutines "enable"
+
 dependencies {
     compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
     testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlin_version"
     testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlin_version"
 }
 
+repositories {
+    jcenter()
+    maven { url "https://kotlin.bintray.com/kotlinx" }
+}
diff --git a/gradle/compile-js.gradle b/gradle/compile-js.gradle
index 9b5755a..59c47d2 100644
--- a/gradle/compile-js.gradle
+++ b/gradle/compile-js.gradle
@@ -3,18 +3,31 @@
 
 apply plugin: 'kotlin-platform-js'
 
+kotlin.experimental.coroutines "enable"
+
+dependencies {
+    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
+    testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
+}
+
+repositories {
+    jcenter()
+    maven { url "https://kotlin.bintray.com/kotlinx" }
+}
+
 tasks.withType(compileKotlin2Js.getClass()) {
     kotlinOptions {
         moduleKind = "umd"
         sourceMap = true
         metaInfo = true
+    }
+}
+
+compileKotlin2Js {
+    kotlinOptions {
         // drop -js suffix from outputFile
         def baseName = project.name - "-js"
         outputFile = new File(outputFile.parent, baseName + ".js")
     }
 }
 
-dependencies {
-    compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
-    testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
-}
diff --git a/gradle/compile-jvm.gradle b/gradle/compile-jvm.gradle
index fe00c2f..74030e5 100644
--- a/gradle/compile-jvm.gradle
+++ b/gradle/compile-jvm.gradle
@@ -6,9 +6,26 @@
 sourceCompatibility = 1.6
 targetCompatibility = 1.6
 
+kotlin.experimental.coroutines "enable"
+
 dependencies {
     compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
     testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
     testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
     testCompile "junit:junit:$junit_version"
 }
+
+repositories {
+    jcenter()
+    maven { url "https://kotlin.bintray.com/kotlinx" }
+    maven { url "https://dl.bintray.com/devexperts/Maven/" }
+}
+
+tasks.withType(Test) {
+    testLogging {
+        showStandardStreams = true
+        events "passed", "failed"
+    }
+    def stressTest = project.properties['stressTest']
+    if (stressTest != null) systemProperties['stressTest'] = stressTest
+}
diff --git a/gradle/compile-native.gradle b/gradle/compile-native.gradle
new file mode 100644
index 0000000..6de11bb
--- /dev/null
+++ b/gradle/compile-native.gradle
@@ -0,0 +1,37 @@
+apply plugin: 'konan'
+
+repositories {
+    jcenter()
+    maven { url "https://kotlin.bintray.com/kotlinx" }
+}
+
+def libraryName = project.name
+def testProgramName = libraryName + "-test"
+
+konanArtifacts {
+    library(libraryName, targets: ["ios_arm64", "ios_x64", "macos_x64"]) {
+        artifactName libraryName.replace('-', '_')
+        enableMultiplatform true
+        dependencies {
+            "artifact$libraryName" "org.jetbrains.kotlinx:atomicfu-native:$atomicFU_version"
+        }
+    }
+    // TODO: THIS IS A WORKAROUND: Cannot do tests together with publishing in Kotlin/Native
+    if (!rootProject.properties["publish"]) {
+        program(testProgramName, targets: ["macos_x64"]) {
+            srcDir 'src/test/kotlin'
+            commonSourceSet 'test'
+            libraries {
+                artifact libraryName
+            }
+            extraOpts '-tr'
+        }
+    }
+}
+
+task test(dependsOn: run)
+
+// TODO: THIS IS A WORKAROUND: Cannot do tests together with publishing in Kotlin/Native
+if (rootProject.properties["publish"]) {
+    publishToMavenLocal.dependsOn(build)
+}
diff --git a/settings.gradle b/settings.gradle
index b7657f3..ebe9b16 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -1,48 +1,41 @@
 rootProject.name = 'kotlinx.coroutines'
 
-include 'kotlinx-coroutines-core-common'
+def module(String path) {
+    int i = path.lastIndexOf('/')
+    def name = path.substring(i + 1)
+    include(name)
+    project(":$name").projectDir = file(path)
+}
 
-include 'kotlinx-coroutines-core'
-include 'kotlinx-coroutines-io'
+// ---------------------------
 
-include 'kotlinx-coroutines-guava'
-include 'kotlinx-coroutines-jdk8'
-include 'kotlinx-coroutines-nio'
-include 'kotlinx-coroutines-quasar'
+include('benchmarks')
+include('knit')
+include('site')
 
-include 'kotlinx-coroutines-reactive'
-include 'kotlinx-coroutines-reactor'
-include 'kotlinx-coroutines-rx1'
-include 'kotlinx-coroutines-rx2'
-include 'kotlinx-coroutines-rx-example'
+module('common/kotlinx-coroutines-core-common')
 
-include 'kotlinx-coroutines-android'
-include 'kotlinx-coroutines-javafx'
-include 'kotlinx-coroutines-swing'
+module('core/kotlinx-coroutines-core')
+module('core/kotlinx-coroutines-io')
 
-include 'kotlinx-coroutines-core-js'
-include 'js-stub'
-include 'example-frontend-js'
+module('integration/kotlinx-coroutines-guava')
+module('integration/kotlinx-coroutines-jdk8')
+module('integration/kotlinx-coroutines-nio')
+module('integration/kotlinx-coroutines-quasar')
 
-include 'benchmarks'
-include 'knit'
-include 'site'
+module('reactive/kotlinx-coroutines-reactive')
+module('reactive/kotlinx-coroutines-reactor')
+module('reactive/kotlinx-coroutines-rx1')
+module('reactive/kotlinx-coroutines-rx2')
+module('reactive/kotlinx-coroutines-rx-example')
 
-project(':kotlinx-coroutines-core-common').projectDir = file('common/kotlinx-coroutines-core-common')
-project(':kotlinx-coroutines-core').projectDir = file('core/kotlinx-coroutines-core')
-project(':kotlinx-coroutines-io').projectDir = file('core/kotlinx-coroutines-io')
-project(':kotlinx-coroutines-guava').projectDir = file('integration/kotlinx-coroutines-guava')
-project(':kotlinx-coroutines-jdk8').projectDir = file('integration/kotlinx-coroutines-jdk8')
-project(':kotlinx-coroutines-nio').projectDir = file('integration/kotlinx-coroutines-nio')
-project(':kotlinx-coroutines-quasar').projectDir = file('integration/kotlinx-coroutines-quasar')
-project(':kotlinx-coroutines-reactive').projectDir = file('reactive/kotlinx-coroutines-reactive')
-project(':kotlinx-coroutines-reactor').projectDir = file('reactive/kotlinx-coroutines-reactor')
-project(':kotlinx-coroutines-rx1').projectDir = file('reactive/kotlinx-coroutines-rx1')
-project(':kotlinx-coroutines-rx2').projectDir = file('reactive/kotlinx-coroutines-rx2')
-project(':kotlinx-coroutines-rx-example').projectDir = file('reactive/kotlinx-coroutines-rx-example')
-project(':kotlinx-coroutines-android').projectDir = file('ui/kotlinx-coroutines-android')
-project(':kotlinx-coroutines-javafx').projectDir = file('ui/kotlinx-coroutines-javafx')
-project(':kotlinx-coroutines-swing').projectDir = file('ui/kotlinx-coroutines-swing')
-project(':kotlinx-coroutines-core-js').projectDir = file('js/kotlinx-coroutines-core-js')
-project(':js-stub').projectDir = file('js/js-stub')
-project(':example-frontend-js').projectDir = file('js/example-frontend-js')
+module('ui/kotlinx-coroutines-android')
+module('ui/kotlinx-coroutines-javafx')
+module('ui/kotlinx-coroutines-swing')
+
+module('js/kotlinx-coroutines-core-js')
+module('js/js-stub')
+module('js/example-frontend-js')
+
+//module('native/kotlinx-coroutines-core-native')
+