mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Add compiler version and flags in KotlinConfig.
This commit is contained in:
parent
48bbdfd1ff
commit
816c609c17
6 changed files with 72 additions and 13 deletions
|
@ -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<String>()
|
||||
// var plugins = listOf<String>()
|
||||
|
||||
// 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)
|
||||
|
|
|
@ -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<String>) {
|
||||
|
@ -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
|
||||
}
|
|
@ -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")
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 ""
|
||||
|
|
|
@ -147,6 +147,12 @@ fun kotlinProject(vararg projects: Project, init: Project.() -> Unit): Project {
|
|||
class KotlinConfig(val project: Project) {
|
||||
val compilerArgs = arrayListOf<String>()
|
||||
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue