/*
 * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

buildscript {
    if (project.hasProperty("bootstrap")) {
        ext.kotlin_version = property('kotlin.version.snapshot')
        ext["kotlin.native.home"] = System.getenv("KONAN_LOCAL_DIST")
    } else {
        ext.kotlin_version = property('kotlin.version')
    }
    if (project.hasProperty("library.version")) {
        ext.overriden_version = property('library.version')
    }
    ext.experimentalsEnabled = ["-progressive", "-Xuse-experimental=kotlin.Experimental",
                                "-Xuse-experimental=kotlin.ExperimentalMultiplatform",
                                "-Xuse-experimental=kotlinx.serialization.InternalSerializationApi"
    ]

    ext.experimentalsInTestEnabled = ["-progressive", "-Xuse-experimental=kotlin.Experimental",
                                      "-Xuse-experimental=kotlin.ExperimentalMultiplatform",
                                      "-Xuse-experimental=kotlinx.serialization.ImplicitReflectionSerializer",
                                      "-Xuse-experimental=kotlinx.serialization.UnstableDefault",
                                      "-Xuse-experimental=kotlinx.serialization.InternalSerializationApi"
    ]

    /*
    * This property group is used to build kotlinx.serialization against Kotlin compiler snapshot.
    * When build_snapshot_train is set to true, kotlin_version property is overridden with kotlin_snapshot_version.
    * DO NOT change the name of these properties without adapting kotlinx.train build chain.
    */
    def prop = rootProject.properties['build_snapshot_train']
    ext.build_snapshot_train = prop != null && prop != ""
    if (build_snapshot_train) {
        ext.kotlin_version = rootProject.properties['kotlin_snapshot_version']
        if (kotlin_version == null) {
            throw new IllegalArgumentException("'kotlin_snapshot_version' should be defined when building with snapshot compiler")
        }
        repositories {
            mavenLocal()
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
            maven { url "https://bintray.com/jetbrains/kotlin-native-dependencies" }
        }
    }

    repositories {
        mavenLocal()
        maven { url 'https://kotlin.bintray.com/kotlin-dev' }
        maven { url 'https://kotlin.bintray.com/kotlin-eap' }
        maven { url 'https://kotlin.bintray.com/kotlinx' }
        maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
        jcenter()
        gradlePluginPortal()
    }

    configurations.classpath {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.jetbrains.kotlin') {
                details.useVersion kotlin_version
            }
        }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
        classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$bintray_version"

        // Protobuf is udes in JVM tests
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.8'

        // Various benchmarking stuff
        classpath "com.github.jengelman.gradle.plugins:shadow:4.0.2"
        classpath "me.champeau.gradle:jmh-gradle-plugin:0.4.8"
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.21"
    }
}

// To make it visible for compilerVersion.gradle
ext.compilerVersion = org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION


allprojects {
    group 'org.jetbrains.kotlinx'

    if (ext.has('overriden_version')) {
        version = overriden_version
    }

    if (project.hasProperty("bootstrap")) {
        version = version + '-SNAPSHOT'
    }

    // the only place where HostManager could be instantiated
    project.ext.hostManager = new org.jetbrains.kotlin.konan.target.HostManager()

    if (build_snapshot_train) {
        // Snapshot-specific
        def deployVersion = properties['DeployVersion']
        if (deployVersion != null) version = deployVersion
        repositories {
            mavenLocal()
            maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
        }
    }

    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
            if (details.requested.group == 'org.jetbrains.kotlin') {
                details.useVersion kotlin_version
            }
        }
    }

    repositories {
        mavenLocal()
        jcenter()
        maven { url "https://dl.bintray.com/kotlin/kotlinx" }
        maven { url 'https://dl.bintray.com/kotlin/kotlin-dev' }
        maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
    }
}

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile).all { task ->
        if (task.name.contains("Test") || task.name.contains("Jmh")) {
            task.kotlinOptions.freeCompilerArgs += experimentalsInTestEnabled
        } else {
            task.kotlinOptions.freeCompilerArgs += experimentalsEnabled
        }
    }

    apply from: rootProject.file('gradle/teamcity.gradle')
    // Configure publishing for some artifacts
    if (project.name.contains("benchmark")) return
    apply from: rootProject.file('gradle/publishing.gradle')
}

apply from: rootProject.file('gradle/compilerVersion.gradle')
