From 816c609c171594ae72fe504b54d96cd86a0f14ab Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Thu, 19 Jan 2017 14:53:07 -0800 Subject: [PATCH] Add compiler version and flags in KotlinConfig. --- .../kotlin/com/beust/kobalt/BuildScript.kt | 19 ++++++++++++ .../com/beust/kobalt/internal/JvmCompiler.kt | 4 +-- .../kobalt/internal/KobaltSettingsXml.kt | 30 +++++++++++++++++-- .../beust/kobalt/plugin/java/JavaCompiler.kt | 3 +- .../kobalt/plugin/kotlin/KotlinCompiler.kt | 23 +++++++++----- .../kobalt/plugin/kotlin/KotlinPlugin.kt | 6 ++++ 6 files changed, 72 insertions(+), 13 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt index 7f646774..074469da 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/BuildScript.kt @@ -11,6 +11,25 @@ import org.eclipse.aether.repository.Proxy import java.io.File import java.net.InetSocketAddress +var BUILD_SCRIPT_CONFIG : BuildScriptConfig? = null + +class BuildScriptConfig { +// var repos = listOf() +// var plugins = listOf() + + // The following settings are used to modify the compiler used to + // compile the build file. Projects should use kotlinCompiler { compilerVersion } to configure + // the Kotin compiler for their source files. + var kobaltCompilerVersion : String? = null + var kobaltCompilerRepo: String? = null + var kobaltCompilerFlags: String? = null +} + +@Directive +fun buildScript(init: BuildScriptConfig.() -> Unit) { + BUILD_SCRIPT_CONFIG = BuildScriptConfig().apply { init() } +} + @Directive fun homeDir(vararg dirs: String) : String = SystemProperties.homeDir + File.separator + dirs.toMutableList().joinToString(File.separator) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt index 23f9f8dc..25294934 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompiler.kt @@ -34,7 +34,7 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) val addedFlags = contributorFlags + ArrayList(info.compilerArgs) validateClasspath(allDependencies.map { it.jarFile.get().absolutePath }) - return action.compile(project?.name, info.copy(dependencies = allDependencies, compilerArgs = addedFlags)) + return action.compile(project, info.copy(dependencies = allDependencies, compilerArgs = addedFlags)) } private fun validateClasspath(cp: List) { @@ -47,5 +47,5 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager) } interface ICompilerAction { - fun compile(projectName: String?, info: CompilerActionInfo): TaskResult + fun compile(project: Project?, info: CompilerActionInfo): TaskResult } \ No newline at end of file diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt index 78561eb2..5288932e 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltSettingsXml.kt @@ -1,5 +1,6 @@ package com.beust.kobalt.internal +import com.beust.kobalt.BUILD_SCRIPT_CONFIG import com.beust.kobalt.Constants import com.beust.kobalt.ProxyConfig import com.beust.kobalt.homeDir @@ -100,9 +101,32 @@ class KobaltSettings @Inject constructor(val xmlFile: KobaltSettingsXml) { } } - var kobaltCompilerVersion = xmlFile.kobaltCompilerVersion - var kobaltCompilerRepo = xmlFile.kobaltCompilerRepo - var kobaltCompilerFlags = xmlFile.kobaltCompilerFlags + val kobaltCompilerVersion : String? + get() { + return if (BUILD_SCRIPT_CONFIG != null && BUILD_SCRIPT_CONFIG?.kobaltCompilerVersion != null) { + BUILD_SCRIPT_CONFIG?.kobaltCompilerVersion + } else { + xmlFile.kobaltCompilerVersion + } + } + + val kobaltCompilerRepo : String? + get() { + return if (BUILD_SCRIPT_CONFIG != null && BUILD_SCRIPT_CONFIG?.kobaltCompilerRepo != null) { + BUILD_SCRIPT_CONFIG?.kobaltCompilerRepo + } else { + xmlFile.kobaltCompilerRepo + } + } + + val kobaltCompilerFlags : String? + get() { + return if (BUILD_SCRIPT_CONFIG != null && BUILD_SCRIPT_CONFIG?.kobaltCompilerFlags != null) { + BUILD_SCRIPT_CONFIG?.kobaltCompilerFlags + } else { + xmlFile.kobaltCompilerFlags + } + } companion object { val SETTINGS_FILE_PATH = KFiles.joinDir(KFiles.HOME_KOBALT_DIR.absolutePath, "settings.xml") diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt index 00bfab4a..6a47c897 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -23,7 +23,8 @@ import javax.tools.ToolProvider class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler, val kobaltLog: ParallelLogger, val compilerUtils: CompilerUtils) : ICompiler { fun compilerAction(executable: File) = object : ICompilerAction { - override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult { + override fun compile(project: Project?, info: CompilerActionInfo): TaskResult { + val projectName = project?.name if (info.sourceFiles.isEmpty()) { warn("No source files to compile") return TaskResult() diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt index 482204ce..c342ed74 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinCompiler.kt @@ -34,7 +34,8 @@ class KotlinCompiler @Inject constructor( val kobaltLog: ParallelLogger) { val compilerAction = object: ICompilerAction { - override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult { + override fun compile(project: Project?, info: CompilerActionInfo): TaskResult { + val projectName = project?.name val version = settings.kobaltCompilerVersion if (! info.outputDir.path.endsWith("ript.jar")) { // Don't display the message if compiling Build.kt @@ -72,28 +73,36 @@ class KotlinCompiler @Inject constructor( // If the Kotlin compiler version in settings.xml is different from the default, we // need to spawn a Kotlin compiler in a separate process. Otherwise, we can just invoke // the K2JVMCompiler class directly - if (settings.kobaltCompilerVersion == Constants.KOTLIN_COMPILER_VERSION) { + val actualVersion = kotlinConfig(project)?.version ?: settings.kobaltCompilerVersion + if (actualVersion == Constants.KOTLIN_COMPILER_VERSION) { return invokeCompilerDirectly(projectName ?: "kobalt-" + Random().nextInt(), outputDir, classpath, info.sourceFiles, info.friendPaths.toTypedArray()) } else { - return invokeCompilerInSeparateProcess(classpath, info) + return invokeCompilerInSeparateProcess(classpath, info, project) } } - private fun invokeCompilerInSeparateProcess(classpath: String, info: CompilerActionInfo): TaskResult { + fun kotlinConfig(project: Project?) + = (Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as KotlinPlugin).configurationFor(project) + + private fun invokeCompilerInSeparateProcess(classpath: String, info: CompilerActionInfo, + project: Project?): TaskResult { val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable val compilerClasspath = compilerDep.jarFile.get().path + File.pathSeparator + compilerEmbeddableDependencies(null).map { it.jarFile.get().path } .joinToString(File.pathSeparator) - val xFlags = settings.kobaltCompilerFlags?.split(" ")?.toTypedArray() ?: emptyArray() + val xFlagsString = kotlinConfig(project)?.flags + ?: settings.kobaltCompilerFlags + ?: "" + val xFlagsArray = xFlagsString.split(" ").toTypedArray() val newArgs = listOf( "-classpath", compilerClasspath, K2JVMCompiler::class.java.name, "-classpath", classpath, "-d", info.outputDir.absolutePath, - *xFlags, + *xFlagsArray, *info.sourceFiles.toTypedArray()) log(2, " Invoking separate kotlinc:\n " + java!!.absolutePath + " " + newArgs.joinToString()) @@ -133,7 +142,7 @@ class KotlinCompiler @Inject constructor( updateArgsWithCompilerFlags(args, settings) - fun logk(level: Int, message: CharSequence) = kobaltLog.log(projectName ?: "", level, message) + fun logk(level: Int, message: CharSequence) = kobaltLog.log(projectName, level, message) logk(2, " Invoking K2JVMCompiler with arguments:" + if (args.skipMetadataVersionCheck) " -Xskip-metadata-version-check" else "" diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt index 1ac3e828..8447b98d 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -147,6 +147,12 @@ fun kotlinProject(vararg projects: Project, init: Project.() -> Unit): Project { class KotlinConfig(val project: Project) { val compilerArgs = arrayListOf() fun args(vararg options: String) = compilerArgs.addAll(options) + + /** The version of the Kotlin compiler */ + var version: String? = null + + /** The flags to pass to the Kotlin compiler */ + var flags: String? = null } @Directive