<commit_before>package org.jetbrains.kotlin.gradle.plugin

import org.gradle.api.Project
import org.gradle.api.file.FileCollection

// Consider using JavaExecSPec
internal interface KonanToolRunner {
    val mainClass: String
    val classpath: FileCollection
    val jvmArgs: List<String>
    val environment: Map<String, Any>

    fun run(args: List<String>)
    fun run(vararg args: String) = run(args.toList())
}

internal abstract class KonanBaseRunner(val project: Project): KonanToolRunner {
    abstract val toolName: String

    override val classpath: FileCollection
        get() = project.fileTree("${project.konanHome}/konan/lib/").apply { include("*.jar")  }

    override val jvmArgs: List<String>
        get() = listOf("-Dkonan.home=${project.konanHome}", "-Djava.library.path=${project.konanHome}/konan/nativelib")

    override val environment: Map<String, Any>
        get() = emptyMap()

    override fun run(args: List<String>) {
        if (classpath.isEmpty) {
            throw IllegalStateException("Classpath if the tool is empty: $toolName\n" +
                    "Probably the 'konan.home' project property contains an incorrect path.\n" +
                    "Please change it to the compiler root directory and rerun the build.")
        }

        project.javaexec {
            it.main = mainClass
            it.classpath = classpath
            it.jvmArgs(jvmArgs)
            it.args(args.apply {
                project.logger.info("Run tool: $toolName with args: ${this.joinToString(separator = " ")}")
            })
            it.environment(environment)
        }
    }
}

internal class KonanInteropRunner(project: Project) : KonanBaseRunner(project){
    internal companion object {
        const val INTEROP_MAIN = "org.jetbrains.kotlin.native.interop.gen.jvm.MainKt"
    }

    override val toolName   get() = "Kotlin/Native cinterop tool"
    override val mainClass  get() = INTEROP_MAIN

    override val environment = mutableMapOf("LIBCLANG_DISABLE_CRASH_RECOVERY" to "1").apply {
        if (project.host == "mingw") {
            put("PATH", "${project.konanHome}\\dependencies" +
                    "\\msys2-mingw-w64-x86_64-gcc-6.3.0-clang-llvm-3.9.1-windows-x86-64" +
                    "\\bin;${System.getenv("PATH")}")
        }<commit_msg>gradle-plugin: Set LIBCLANG_DISABLE_CRASH_RECOVERY for compiler<commit_after>package org.jetbrains.kotlin.gradle.plugin

import org.gradle.api.Project
import org.gradle.api.file.FileCollection

internal interface KonanToolRunner {
    val mainClass: String
    val classpath: FileCollection
    val jvmArgs: List<String>
    val environment: Map<String, Any>

    fun run(args: List<String>)
    fun run(vararg args: String) = run(args.toList())
}

internal abstract class KonanBaseRunner(val project: Project): KonanToolRunner {
    abstract val toolName: String

    override val classpath: FileCollection =
            project.fileTree("${project.konanHome}/konan/lib/")
            .apply { include("*.jar")  }

    override val jvmArgs = mutableListOf("-Dkonan.home=${project.konanHome}",
            "-Djava.library.path=${project.konanHome}/konan/nativelib")

    override val environment = mutableMapOf("LIBCLANG_DISABLE_CRASH_RECOVERY" to "1")

    override fun run(args: List<String>) {
        if (classpath.isEmpty) {
            throw IllegalStateException("Classpath if the tool is empty: $toolName\n" +
                    "Probably the 'konan.home' project property contains an incorrect path.\n" +
                    "Please change it to the compiler root directory and rerun the build.")
        }

        project.javaexec {
            it.main = mainClass
            it.classpath = classpath
            it.jvmArgs(jvmArgs)
            it.args(args.apply {
                project.logger.info("Run tool: $toolName with args: ${this.joinToString(separator = " ")}")
            })
            it.environment(environment)
        }
    }
}

internal class KonanInteropRunner(project: Project) : KonanBaseRunner(project){
    internal companion object {
        const val INTEROP_MAIN = "org.jetbrains.kotlin.native.interop.gen.jvm.MainKt"
    }

    override val toolName   get() = "Kotlin/Native cinterop tool"
    override val mainClass  get() = INTEROP_MAIN

    init {
        if (project.host == "mingw") {
            environment.put("PATH", "${project.konanHome}\\dependencies" +
                    "\\msys2-mingw-w64-x86_64-gcc-6.3.0-clang-llvm-3.9.1-windows-x86-64" +
                    "\\bin;${System.getenv("PATH")}")
        }
{'fraction_non_alphanumeric': 0.1162280701754386, 'fraction_numerical': 0.007894736842105263, 'mean_word_length': 2.6458832933653076, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3962482', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1340, 'n_words': 295}