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.io.File
|
||||||
import java.net.InetSocketAddress
|
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
|
@Directive
|
||||||
fun homeDir(vararg dirs: String) : String = SystemProperties.homeDir +
|
fun homeDir(vararg dirs: String) : String = SystemProperties.homeDir +
|
||||||
File.separator + dirs.toMutableList().joinToString(File.separator)
|
File.separator + dirs.toMutableList().joinToString(File.separator)
|
||||||
|
|
|
@ -34,7 +34,7 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
|
||||||
val addedFlags = contributorFlags + ArrayList(info.compilerArgs)
|
val addedFlags = contributorFlags + ArrayList(info.compilerArgs)
|
||||||
|
|
||||||
validateClasspath(allDependencies.map { it.jarFile.get().absolutePath })
|
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>) {
|
private fun validateClasspath(cp: List<String>) {
|
||||||
|
@ -47,5 +47,5 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ICompilerAction {
|
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
|
package com.beust.kobalt.internal
|
||||||
|
|
||||||
|
import com.beust.kobalt.BUILD_SCRIPT_CONFIG
|
||||||
import com.beust.kobalt.Constants
|
import com.beust.kobalt.Constants
|
||||||
import com.beust.kobalt.ProxyConfig
|
import com.beust.kobalt.ProxyConfig
|
||||||
import com.beust.kobalt.homeDir
|
import com.beust.kobalt.homeDir
|
||||||
|
@ -100,9 +101,32 @@ class KobaltSettings @Inject constructor(val xmlFile: KobaltSettingsXml) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var kobaltCompilerVersion = xmlFile.kobaltCompilerVersion
|
val kobaltCompilerVersion : String?
|
||||||
var kobaltCompilerRepo = xmlFile.kobaltCompilerRepo
|
get() {
|
||||||
var kobaltCompilerFlags = xmlFile.kobaltCompilerFlags
|
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 {
|
companion object {
|
||||||
val SETTINGS_FILE_PATH = KFiles.joinDir(KFiles.HOME_KOBALT_DIR.absolutePath, "settings.xml")
|
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,
|
class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler, val kobaltLog: ParallelLogger,
|
||||||
val compilerUtils: CompilerUtils) : ICompiler {
|
val compilerUtils: CompilerUtils) : ICompiler {
|
||||||
fun compilerAction(executable: File) = object : ICompilerAction {
|
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()) {
|
if (info.sourceFiles.isEmpty()) {
|
||||||
warn("No source files to compile")
|
warn("No source files to compile")
|
||||||
return TaskResult()
|
return TaskResult()
|
||||||
|
|
|
@ -34,7 +34,8 @@ class KotlinCompiler @Inject constructor(
|
||||||
val kobaltLog: ParallelLogger) {
|
val kobaltLog: ParallelLogger) {
|
||||||
|
|
||||||
val compilerAction = object: ICompilerAction {
|
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
|
val version = settings.kobaltCompilerVersion
|
||||||
if (! info.outputDir.path.endsWith("ript.jar")) {
|
if (! info.outputDir.path.endsWith("ript.jar")) {
|
||||||
// Don't display the message if compiling Build.kt
|
// 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
|
// 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
|
// need to spawn a Kotlin compiler in a separate process. Otherwise, we can just invoke
|
||||||
// the K2JVMCompiler class directly
|
// 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,
|
return invokeCompilerDirectly(projectName ?: "kobalt-" + Random().nextInt(), outputDir,
|
||||||
classpath, info.sourceFiles, info.friendPaths.toTypedArray())
|
classpath, info.sourceFiles, info.friendPaths.toTypedArray())
|
||||||
|
|
||||||
} else {
|
} 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 java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable
|
||||||
|
|
||||||
val compilerClasspath = compilerDep.jarFile.get().path + File.pathSeparator +
|
val compilerClasspath = compilerDep.jarFile.get().path + File.pathSeparator +
|
||||||
compilerEmbeddableDependencies(null).map { it.jarFile.get().path }
|
compilerEmbeddableDependencies(null).map { it.jarFile.get().path }
|
||||||
.joinToString(File.pathSeparator)
|
.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(
|
val newArgs = listOf(
|
||||||
"-classpath", compilerClasspath,
|
"-classpath", compilerClasspath,
|
||||||
K2JVMCompiler::class.java.name,
|
K2JVMCompiler::class.java.name,
|
||||||
"-classpath", classpath,
|
"-classpath", classpath,
|
||||||
"-d", info.outputDir.absolutePath,
|
"-d", info.outputDir.absolutePath,
|
||||||
*xFlags,
|
*xFlagsArray,
|
||||||
*info.sourceFiles.toTypedArray())
|
*info.sourceFiles.toTypedArray())
|
||||||
|
|
||||||
log(2, " Invoking separate kotlinc:\n " + java!!.absolutePath + " " + newArgs.joinToString())
|
log(2, " Invoking separate kotlinc:\n " + java!!.absolutePath + " " + newArgs.joinToString())
|
||||||
|
@ -133,7 +142,7 @@ class KotlinCompiler @Inject constructor(
|
||||||
|
|
||||||
updateArgsWithCompilerFlags(args, settings)
|
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:"
|
logk(2, " Invoking K2JVMCompiler with arguments:"
|
||||||
+ if (args.skipMetadataVersionCheck) " -Xskip-metadata-version-check" else ""
|
+ 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) {
|
class KotlinConfig(val project: Project) {
|
||||||
val compilerArgs = arrayListOf<String>()
|
val compilerArgs = arrayListOf<String>()
|
||||||
fun args(vararg options: String) = compilerArgs.addAll(options)
|
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
|
@Directive
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue