Stop using -Xbuild-file in Gradle Plugin

#KT-27640 fixed
    #KT-27778 fixed
    #KT-27638 fixed
This commit is contained in:
Alexey Tsvetkov
2018-11-02 05:08:48 +03:00
parent 9b5b0447fa
commit 5f54f67f70
12 changed files with 94 additions and 101 deletions
@@ -149,8 +149,12 @@ class GenerationState private constructor(
init {
val icComponents = configuration.get(JVMConfigurationKeys.INCREMENTAL_COMPILATION_COMPONENTS)
if (icComponents != null) {
incrementalCacheForThisTarget =
icComponents.getIncrementalCache(targetId ?: error("Target ID should be specified for incremental compilation"))
val targetId = targetId
?: moduleName?.let {
// hack for Gradle IC, Gradle does not use build.xml file, so there is no way to pass target id
TargetId(it, "java-production")
} ?: error("Target ID should be specified for incremental compilation")
incrementalCacheForThisTarget = icComponents.getIncrementalCache(targetId)
packagesWithObsoleteParts = incrementalCacheForThisTarget.getObsoletePackageParts().map {
JvmClassName.byInternalName(it).packageFqName
}.toSet()
@@ -191,6 +191,20 @@ class K2JVMCompilerArguments : CommonCompilerArguments() {
)
var javacArguments: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xjava-source-roots",
valueDescription = "<path>",
description = "Paths to output directories for friend modules (whose internals should be visible)"
)
var javaSourceRoots: Array<String>? by FreezableVar(null)
@Argument(
value = "-Xjava-package-prefix",
description = "Package prefix for Java files"
)
var javaPackagePrefix: String? by FreezableVar(null)
@Argument(
value = "-Xjsr305",
deprecatedName = "-Xjsr305-annotations",
@@ -85,6 +85,10 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
}
}
}
for (path in arguments.javaSourceRoots ?: emptyArray()) {
configuration.addJavaSourceRoot(File(path), arguments.javaPackagePrefix)
}
}
configuration.put(CommonConfigurationKeys.MODULE_NAME, arguments.moduleName ?: JvmAbi.DEFAULT_MODULE_NAME)
@@ -108,11 +112,17 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
val destination = arguments.destination
if (arguments.buildFile != null) {
fun strongWarning(message: String) {
messageCollector.report(STRONG_WARNING, message)
}
if (destination != null) {
messageCollector.report(
STRONG_WARNING,
"The '-d' option with a directory destination is ignored because '-Xbuild-file' is specified"
)
strongWarning("The '-d' option with a directory destination is ignored because '-Xbuild-file' is specified")
}
if (arguments.javaSourceRoots != null) {
strongWarning("The '-Xjava-source-roots' option is ignored because '-Xbuild-file' is specified")
}
if (arguments.javaPackagePrefix != null) {
strongWarning("The '-Xjava-package-prefix' option is ignored because '-Xbuild-file' is specified")
}
val sanitizedCollector = FilteringMessageCollector(messageCollector, VERBOSE::contains)
@@ -420,9 +420,11 @@ object KotlinToJVMBytecodeCompiler {
private fun GenerationState.Builder.withModule(module: Module?) =
apply {
targetId(module?.let { TargetId(it) })
moduleName(module?.getModuleName())
outDirectory(module?.let { File(it.getOutputDirectory()) })
if (module != null) {
targetId(TargetId(module))
moduleName(module.getModuleName())
outDirectory(File(module.getOutputDirectory()))
}
}
private fun generate(
@@ -28,7 +28,8 @@ open class CompilationOptions(
/** @See [ReportSeverity] */
val reportSeverity: Int,
/** @See [CompilationResultCategory]] */
val requestedCompilationResults: Array<Int>
val requestedCompilationResults: Array<Int>,
val kotlinScriptExtensions: Array<String>? = null
) : Serializable {
companion object {
const val serialVersionUID: Long = 0
@@ -41,6 +42,7 @@ open class CompilationOptions(
"reportCategories=${Arrays.toString(reportCategories)}, " +
"reportSeverity=$reportSeverity, " +
"requestedCompilationResults=${Arrays.toString(requestedCompilationResults)}" +
"kotlinScriptExtensions=${Arrays.toString(kotlinScriptExtensions)}" +
")"
}
}
@@ -65,8 +67,16 @@ class IncrementalCompilationOptions(
val outputFiles: List<File>,
val multiModuleICSettings: MultiModuleICSettings,
val modulesInfo: IncrementalModuleInfo,
val classpathFqNamesHistory: File? = null
) : CompilationOptions(compilerMode, targetPlatform, reportCategories, reportSeverity, requestedCompilationResults) {
val classpathFqNamesHistory: File? = null,
kotlinScriptExtensions: Array<String>? = null
) : CompilationOptions(
compilerMode,
targetPlatform,
reportCategories,
reportSeverity,
requestedCompilationResults,
kotlinScriptExtensions
) {
companion object {
const val serialVersionUID: Long = 0
}
@@ -454,7 +454,7 @@ class CompileServiceImpl(
doCompile(sessionId, daemonReporter, tracer = null) { _, _ ->
execIncrementalCompiler(
k2jvmArgs, gradleIncrementalArgs, gradleIncrementalServicesFacade, compilationResults!!,
messageCollector, daemonReporter
messageCollector
)
}
}
@@ -525,44 +525,28 @@ class CompileServiceImpl(
}
}
// todo: non-IC non-KTS scripts?
private fun execIncrementalCompiler(
k2jvmArgs: K2JVMCompilerArguments,
incrementalCompilationOptions: IncrementalCompilationOptions,
servicesFacade: IncrementalCompilerServicesFacade,
compilationResults: CompilationResults,
compilerMessageCollector: MessageCollector,
daemonMessageReporter: DaemonMessageReporter
compilerMessageCollector: MessageCollector
): ExitCode {
val moduleFile = k2jvmArgs.buildFile?.let(::File)
assert(moduleFile?.exists() ?: false) { "Module does not exist ${k2jvmArgs.buildFile}" }
// todo: pass javaSourceRoots and allKotlinFiles using IncrementalCompilationOptions
val parsedModule = run {
val bytesOut = ByteArrayOutputStream()
val printStream = PrintStream(bytesOut)
val mc = PrintingMessageCollector(printStream, MessageRenderer.PLAIN_FULL_PATHS, false)
val parsedModule = ModuleXmlParser.parseModuleScript(k2jvmArgs.buildFile!!, mc)
if (mc.hasErrors()) {
daemonMessageReporter.report(ReportSeverity.ERROR, bytesOut.toString("UTF8"))
val allKotlinExtensions = (DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS +
(incrementalCompilationOptions.kotlinScriptExtensions ?: emptyArray())).distinct()
val dotExtensions = allKotlinExtensions.map { ".$it" }
val freeArgs = arrayListOf<String>()
val allKotlinFiles = arrayListOf<File>()
for (arg in k2jvmArgs.freeArgs) {
val file = File(arg)
if (file.isFile && dotExtensions.any { ext -> file.path.endsWith(ext, ignoreCase = true) }) {
allKotlinFiles.add(file)
} else {
freeArgs.add(arg)
}
parsedModule
}
val javaSourceRoots = parsedModule.modules.flatMapTo(HashSet()) {
it.getJavaSourceRoots().map { JvmSourceRoot(File(it.path), it.packagePrefix) }
}
k2jvmArgs.commonSources = parsedModule.modules.flatMap { it.getCommonSourceFiles() }.toTypedArray().takeUnless { it.isEmpty() }
val allKotlinFiles = parsedModule.modules.flatMap { it.getSourceFiles().map(::File) }
val allKotlinExtensions = (
DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS +
allKotlinFiles.asSequence()
.map { it.extension }
.filter { !it.equals("java", ignoreCase = true) }
.asIterable()
).distinct()
k2jvmArgs.friendPaths = parsedModule.modules.flatMap(Module::getFriendPaths).toTypedArray()
k2jvmArgs.freeArgs = freeArgs
val changedFiles = if (incrementalCompilationOptions.areFileChangesKnown) {
ChangedFiles.Known(incrementalCompilationOptions.modifiedFiles!!, incrementalCompilationOptions.deletedFiles!!)
@@ -588,7 +572,6 @@ class CompileServiceImpl(
val compiler = IncrementalJvmCompilerRunner(
workingDir,
javaSourceRoots,
reporter,
buildHistoryFile = incrementalCompilationOptions.multiModuleICSettings.buildHistoryFile,
outputFiles = outputFiles,
@@ -62,11 +62,11 @@ fun makeIncrementally(
val files = rootsWalk.filter(File::isFile)
val sourceFiles = files.filter { it.extension.toLowerCase() in allExtensions }.toList()
val buildHistoryFile = File(cachesDir, "build-history.bin")
args.javaSourceRoots = sourceRoots.map { it.absolutePath }.toTypedArray()
withIC {
val compiler = IncrementalJvmCompilerRunner(
cachesDir,
sourceRoots.map { JvmSourceRoot(it, null) }.toSet(),
reporter,
// Use precise setting in case of non-Gradle build
usePreciseJavaTracking = true,
@@ -104,7 +104,6 @@ inline fun <R> withIC(enabled: Boolean = true, fn: ()->R): R {
class IncrementalJvmCompilerRunner(
workingDir: File,
private val javaSourceRoots: Set<JvmSourceRoot>,
reporter: ICReporter,
private val usePreciseJavaTracking: Boolean,
buildHistoryFile: File,
@@ -367,28 +366,11 @@ class IncrementalJvmCompilerRunner(
messageCollector: MessageCollector
): ExitCode {
val compiler = K2JVMCompiler()
val outputDir = args.destinationAsFile
val classpath = args.classpathAsList
val moduleFile = makeModuleFile(
args.moduleName!!,
isTest = false,
outputDir = outputDir,
sourcesToCompile = sourcesToCompile,
commonSources = args.commonSources?.map(::File).orEmpty(),
javaSourceRoots = javaSourceRoots,
classpath = classpath,
friendDirs = listOf()
)
val destination = args.destination
args.destination = null
args.buildFile = moduleFile.absolutePath
return try {
compiler.exec(messageCollector, services, args)
} finally {
args.destination = destination
moduleFile.delete()
}
val freeArgsBackup = args.freeArgs.toList()
args.freeArgs += sourcesToCompile.map { it.absolutePath }
val exitCode = compiler.exec(messageCollector, services, args)
args.freeArgs = freeArgsBackup
return exitCode
}
}
@@ -18,7 +18,8 @@ internal class GradleCompilerEnvironment(
outputItemsCollector: OutputItemsCollector,
val outputFiles: FileCollection,
val buildReportMode: BuildReportMode?,
val incrementalCompilationEnvironment: IncrementalCompilationEnvironment? = null
val incrementalCompilationEnvironment: IncrementalCompilationEnvironment? = null,
val kotlinScriptExtensions: Array<String> = emptyArray()
) : CompilerEnvironment(Services.EMPTY, messageCollector, outputItemsCollector) {
val toolsJar: File? by lazy { findToolsJar() }
@@ -22,7 +22,6 @@ import org.gradle.api.invocation.Gradle
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.bundling.AbstractArchiveTask
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.build.JvmSourceRoot
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
@@ -73,23 +72,16 @@ internal open class GradleCompilerRunner(protected val task: Task) {
args: K2JVMCompilerArguments,
environment: GradleCompilerEnvironment
) {
val buildFile = makeModuleFile(
args.moduleName!!,
isTest = false,
outputDir = args.destinationAsFile,
sourcesToCompile = sourcesToCompile,
commonSources = commonSources,
javaSourceRoots = javaSourceRoots.map { JvmSourceRoot(it, javaPackagePrefix) },
classpath = args.classpathAsList,
friendDirs = args.friendPaths?.map(::File).orEmpty()
)
args.buildFile = buildFile.absolutePath
args.freeArgs += sourcesToCompile.map { it.absolutePath }
args.commonSources = commonSources.map { it.absolutePath }.toTypedArray()
args.javaSourceRoots = javaSourceRoots.map { it.absolutePath }.toTypedArray()
args.javaPackagePrefix = javaPackagePrefix
if (environment.incrementalCompilationEnvironment == null || kotlinCompilerExecutionStrategy() != DAEMON_EXECUTION_STRATEGY) {
args.destination = null
}
runCompilerAsync(KotlinCompilerClass.JVM, args, environment, buildFile = buildFile)
runCompilerAsync(KotlinCompilerClass.JVM, args, environment)
}
/**
@@ -123,8 +115,7 @@ internal open class GradleCompilerRunner(protected val task: Task) {
private fun runCompilerAsync(
compilerClassName: String,
compilerArgs: CommonCompilerArguments,
environment: GradleCompilerEnvironment,
buildFile: File? = null
environment: GradleCompilerEnvironment
) {
if (compilerArgs.version) {
task.logger.lifecycle(
@@ -144,10 +135,10 @@ internal open class GradleCompilerRunner(protected val task: Task) {
isVerbose = compilerArgs.verbose,
incrementalCompilationEnvironment = incrementalCompilationEnvironment,
incrementalModuleInfo = modulesInfo,
buildFile = buildFile,
outputFiles = environment.outputFiles.toList(),
taskPath = task.path,
buildReportMode = environment.buildReportMode
buildReportMode = environment.buildReportMode,
kotlinScriptExtensions = environment.kotlinScriptExtensions
)
TaskLoggers.put(task.path, task.logger)
runCompilerAsync(workArgs)
@@ -19,7 +19,6 @@ import org.jetbrains.kotlin.gradle.tasks.clearLocalState
import org.jetbrains.kotlin.gradle.tasks.throwGradleExceptionIfError
import org.jetbrains.kotlin.gradle.utils.stackTraceAsString
import org.jetbrains.kotlin.incremental.ChangedFiles
import org.jetbrains.kotlin.incremental.DELETE_MODULE_FILE_PROPERTY
import org.slf4j.LoggerFactory
import java.io.*
import java.net.URLClassLoader
@@ -53,10 +52,10 @@ internal class GradleKotlinCompilerWorkArguments(
val isVerbose: Boolean,
val incrementalCompilationEnvironment: IncrementalCompilationEnvironment?,
val incrementalModuleInfo: IncrementalModuleInfo?,
val buildFile: File?,
val outputFiles: List<File>,
val taskPath: String,
val buildReportMode: BuildReportMode?
val buildReportMode: BuildReportMode?,
val kotlinScriptExtensions: Array<String>
) : Serializable {
companion object {
const val serialVersionUID: Long = 0
@@ -91,10 +90,10 @@ internal class GradleKotlinCompilerWork @Inject constructor(
private val isVerbose = config.isVerbose
private val incrementalCompilationEnvironment = config.incrementalCompilationEnvironment
private val incrementalModuleInfo = config.incrementalModuleInfo
private val buildFile = config.buildFile
private val outputFiles = config.outputFiles
private val taskPath = config.taskPath
private val buildReportMode = config.buildReportMode
private val kotlinScriptExtensions = config.kotlinScriptExtensions
private val log: KotlinLogger =
TaskLoggers.get(taskPath)?.let { GradleKotlinLogger(it).apply { debug("Using '$taskPath' logger") } }
@@ -114,14 +113,7 @@ internal class GradleKotlinCompilerWork @Inject constructor(
override fun run() {
val messageCollector = GradlePrintingMessageCollector(log)
val exitCode = try {
compileWithDaemonOrFallbackImpl(messageCollector)
} finally {
if (buildFile != null && System.getProperty(DELETE_MODULE_FILE_PROPERTY) != "false") {
buildFile.delete()
}
}
val exitCode = compileWithDaemonOrFallbackImpl(messageCollector)
if (incrementalCompilationEnvironment?.disableMultiModuleIC == true) {
incrementalCompilationEnvironment.multiModuleICSettings.buildHistoryFile.delete()
}
@@ -230,7 +222,8 @@ internal class GradleKotlinCompilerWork @Inject constructor(
targetPlatform = targetPlatform,
reportCategories = reportCategories(isVerbose),
reportSeverity = reportSeverity(isVerbose),
requestedCompilationResults = emptyArray()
requestedCompilationResults = emptyArray(),
kotlinScriptExtensions = kotlinScriptExtensions
)
val servicesFacade = GradleCompilerServicesFacadeImpl(log, bufferingMessageCollector)
return try {
@@ -275,7 +268,8 @@ internal class GradleKotlinCompilerWork @Inject constructor(
outputFiles = outputFiles,
multiModuleICSettings = icEnv.multiModuleICSettings,
modulesInfo = incrementalModuleInfo!!,
classpathFqNamesHistory = icEnv.classpathFqNamesHistory
classpathFqNamesHistory = icEnv.classpathFqNamesHistory,
kotlinScriptExtensions = kotlinScriptExtensions
)
log.info("Options for KOTLIN DAEMON: $compilationOptions")
@@ -55,6 +55,7 @@ internal object CompilerArgumentsGradleInput {
K2JVMCompilerArguments::buildFile, // in Gradle build, these XMLs are transient and provide no useful info
K2JVMCompilerArguments::pluginOptions, // handled specially in the task
K2JVMCompilerArguments::pluginClasspaths, // handled in the task as classpath
K2JVMCompilerArguments::javaSourceRoots, // handled in inputs
K2JSCompilerArguments::outputFile, // already handled by Gradle task property
K2JSCompilerArguments::libraries, // defined by by classpath and friendDependency of the Gradle task
@@ -439,7 +439,8 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
computedCompilerClasspath, messageCollector, outputItemCollector,
outputFiles = allOutputFiles(),
buildReportMode = buildReportMode,
incrementalCompilationEnvironment = icEnv
incrementalCompilationEnvironment = icEnv,
kotlinScriptExtensions = sourceFilesExtensions.toTypedArray()
)
compilerRunner.runJvmCompilerAsync(
sourceRoots.kotlinSourceFiles,