diff --git a/build-common/src/org/jetbrains/kotlin/incremental/buildUtil.kt b/build-common/src/org/jetbrains/kotlin/incremental/buildUtil.kt index 3acefade596..9fd25304bb9 100644 --- a/build-common/src/org/jetbrains/kotlin/incremental/buildUtil.kt +++ b/build-common/src/org/jetbrains/kotlin/incremental/buildUtil.kt @@ -46,6 +46,26 @@ fun Iterable.javaSourceRoots(roots: Iterable): Iterable = .map { findSrcDirRoot(it, roots) } .filterNotNull() +fun makeModuleFile(name: String, isTest: Boolean, outputDir: File, sourcesToCompile: List, javaSourceRoots: Iterable, classpath: Iterable, friendDirs: Iterable): File { + val builder = KotlinModuleXmlBuilder() + builder.addModule( + name, + outputDir.absolutePath, + sourcesToCompile, + javaSourceRoots.map { JvmSourceRoot(it) }, + classpath, + "java-production", + isTest, + // this excludes the output directories from the class path, to be removed for true incremental compilation + setOf(outputDir), + friendDirs + ) + + val scriptFile = File.createTempFile("kjps", StringUtil.sanitizeJavaIdentifier(name) + ".script.xml") + FileUtil.writeToFile(scriptFile, builder.asText().toString()) + return scriptFile +} + fun makeCompileServices( incrementalCaches: Map, lookupTracker: LookupTracker, diff --git a/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt b/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt index b78a63c76c2..f20b8a7f1be 100644 --- a/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt +++ b/libraries/tools/kotlin-gradle-plugin-core/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt @@ -467,58 +467,59 @@ open class KotlinCompile() : AbstractKotlinCompile() { private data class CompileChangedResults(val exitCode: ExitCode, val generatedFiles: List>) - private fun compileChanged(targets: List, - sourcesToCompile: List, - outputDir: File, - args: K2JVMCompilerArguments, - getIncrementalCache: (TargetId) -> GradleIncrementalCacheImpl, - lookupTracker: LookupTracker) - : CompileChangedResults - { - // show kotlin compiler where to look for java source files - args.freeArgs = (sourcesToCompile.map { it.absolutePath } + getJavaSourceRoots().map { it.absolutePath }).distinct() - args.destination = outputDir.absolutePath - - logger.kotlinDebug("compiling with args ${ArgumentUtils.convertArgumentsToStringList(args)}") - + private fun compileChanged( + targets: List, + sourcesToCompile: List, + outputDir: File, + args: K2JVMCompilerArguments, + getIncrementalCache: (TargetId)->GradleIncrementalCacheImpl, + lookupTracker: LookupTracker + ): CompileChangedResults { + val moduleFile = makeModuleFile(args.moduleName, isTest = false, outputDir = outputDir, sourcesToCompile = sourcesToCompile, javaSourceRoots = getJavaSourceRoots(), classpath = classpath, friendDirs = listOf()) + args.module = moduleFile.absolutePath val outputItemCollector = OutputItemsCollectorImpl() - val messageCollector = GradleMessageCollector(logger, outputItemCollector) - val incrementalCaches = makeIncrementalCachesMap(targets, { listOf() }, getIncrementalCache, { this }) + try { + val incrementalCaches = makeIncrementalCachesMap(targets, { listOf() }, getIncrementalCache, { this }) + val compilationCanceledStatus = object : CompilationCanceledStatus { + override fun checkCanceled() { + } + } - val compilationCanceledStatus = object : CompilationCanceledStatus { - override fun checkCanceled() {} + logger.kotlinDebug("compiling with args ${ArgumentUtils.convertArgumentsToStringList(args)}") + val exitCode = compiler.exec(messageCollector, makeCompileServices(incrementalCaches, lookupTracker, compilationCanceledStatus), args) + return CompileChangedResults( + exitCode, + outputItemCollector.generatedFiles( + targets = targets, + representativeTarget = targets.first(), + getSources = { sourcesToCompile }, + getOutputDir = { outputDir })) + } + finally { + moduleFile.delete() } - - logger.kotlinDebug("compiling with args ${ArgumentUtils.convertArgumentsToStringList(args)}") - - val exitCode = compiler.exec(messageCollector, makeCompileServices(incrementalCaches, lookupTracker, compilationCanceledStatus), args) - - return CompileChangedResults( - exitCode, - outputItemCollector.generatedFiles( - targets = targets, - representativeTarget = targets.first(), - getSources = { sourcesToCompile }, - getOutputDir = { outputDir })) } - private fun compileNotIncremental(sourcesToCompile: List, - outputDir: File, - args: K2JVMCompilerArguments) - : ExitCode - { - // show kotlin compiler where to look for java source files - args.freeArgs = (sourcesToCompile.map { it.absolutePath } + getJavaSourceRoots().map { it.absolutePath }).distinct() - args.destination = outputDir.absolutePath + private fun compileNotIncremental( + sourcesToCompile: List, + outputDir: File, + args: K2JVMCompilerArguments + ): ExitCode { + val moduleFile = makeModuleFile(args.moduleName, isTest = false, outputDir = outputDir, sourcesToCompile = sourcesToCompile, javaSourceRoots = getJavaSourceRoots(), classpath = classpath, friendDirs = listOf()) + args.module = moduleFile.absolutePath + val messageCollector = GradleMessageCollector(logger) - logger.kotlinDebug("compiling with args ${ArgumentUtils.convertArgumentsToStringList(args)}") - - return compiler.exec(GradleMessageCollector(logger), Services.EMPTY, args) + try { + logger.kotlinDebug("compiling with args ${ArgumentUtils.convertArgumentsToStringList(args)}") + return compiler.exec(messageCollector, Services.EMPTY, args) + } + finally { + moduleFile.delete() + } } - private fun handleKaptProperties(extraProperties: ExtraPropertiesExtension, pluginOptions: MutableList) { val kaptAnnotationsFile = extraProperties.getOrNull("kaptAnnotationsFile") if (kaptAnnotationsFile != null) {