[Gradle, JS] Fix bridge between Gradle and Js IR CLI

This commit is contained in:
Roman Artemev
2020-12-07 08:18:39 +03:00
parent 350ff8033d
commit 75016bf54d
8 changed files with 77 additions and 29 deletions
@@ -39,9 +39,7 @@ import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsBinaryMode
import org.jetbrains.kotlin.gradle.targets.js.ir.JsIrBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrCompilation
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink
import org.jetbrains.kotlin.gradle.targets.js.ir.*
import org.jetbrains.kotlin.gradle.targets.js.jsPluginDeprecationMessage
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlin.gradle.tasks.*
@@ -341,8 +339,12 @@ internal class KotlinJsIrSourceSetProcessor(
// outputFile can be set later during the configuration phase, get it only after the phase:
project.runOnceAfterEvaluated("KotlinJsIrSourceSetProcessor.doTargetSpecificProcessing", kotlinTask) {
val kotlinTaskInstance = kotlinTask.get()
kotlinTaskInstance.kotlinOptions.outputFile = kotlinTaskInstance.outputFile.absolutePath
val outputDir = kotlinTaskInstance.outputFile.parentFile
val kotlinOptions = kotlinTaskInstance.kotlinOptions
kotlinOptions.outputFile = kotlinTaskInstance.outputFile.absolutePath
val outputDir = if (kotlinOptions.isProduceUnzippedKlib())
kotlinTaskInstance.outputFile
else
kotlinTaskInstance.outputFile.parentFile
if (outputDir.isParentOf(project.rootDir))
throw InvalidUserDataException(
@@ -5,6 +5,8 @@
package org.jetbrains.kotlin.gradle.targets.js.ir
import org.jetbrains.kotlin.gradle.dsl.KotlinJsOptions
/**
* @see [compiler/testData/cli/js/jsExtraHelp.out]
*/
@@ -18,5 +20,9 @@ internal const val GENERATE_D_TS = "-Xgenerate-dts"
internal const val PRODUCE_JS = "-Xir-produce-js"
internal const val PRODUCE_UNZIPPED_KLIB = "-Xir-produce-klib-dir"
internal const val PRODUCE_ZIPPED_KLIB = "-Xir-produce-klib-file"
internal const val MODULE_NAME = "-Xir-module-name"
internal const val MODULE_NAME = "-Xir-module-name"
fun KotlinJsOptions.isProduceUnzippedKlib() = PRODUCE_UNZIPPED_KLIB in freeCompilerArgs
fun KotlinJsOptions.isProduceZippedKlib() = PRODUCE_ZIPPED_KLIB in freeCompilerArgs
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider
import org.jetbrains.kotlin.gradle.testing.internal.kotlinTestRegistry
import org.jetbrains.kotlin.gradle.testing.testTaskName
import org.jetbrains.kotlin.gradle.utils.klibModuleName
import java.io.File
open class KotlinJsIrTargetConfigurator(kotlinPluginVersion: String) :
KotlinOnlyTargetConfigurator<KotlinJsIrCompilation, KotlinJsIrTarget>(true, true, kotlinPluginVersion),
@@ -68,18 +69,33 @@ open class KotlinJsIrTargetConfigurator(kotlinPluginVersion: String) :
compilation.kotlinOptions {
configureOptions()
freeCompilerArgs += listOf(
DISABLE_PRE_IR,
PRODUCE_UNZIPPED_KLIB
)
var produceUnzippedKlib = isProduceUnzippedKlib()
val produceZippedKlib = isProduceZippedKlib()
freeCompilerArgs = freeCompilerArgs + DISABLE_PRE_IR
val isMainCompilation = compilation.isMain()
if (!produceUnzippedKlib && !produceZippedKlib) {
freeCompilerArgs = freeCompilerArgs + PRODUCE_UNZIPPED_KLIB
produceUnzippedKlib = true
}
// Configure FQ module name to avoid cyclic dependencies in klib manifests (see KT-36721).
val baseName = if (compilation.isMain()) {
val baseName = if (isMainCompilation) {
target.project.name
} else {
"${target.project.name}_${compilation.name}"
}
freeCompilerArgs += listOf("$MODULE_NAME=${target.project.klibModuleName(baseName)}")
val destinationDir = compilation.compileKotlinTask.destinationDir
outputFile = if (produceUnzippedKlib)
destinationDir.absoluteFile.normalize().absolutePath
else
File(destinationDir, "$baseName.$KLIB_TYPE").absoluteFile.normalize().absolutePath
val klibModuleName = target.project.klibModuleName(baseName)
freeCompilerArgs = freeCompilerArgs + "$MODULE_NAME=$klibModuleName"
}
compilation.binaries
@@ -44,9 +44,6 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.associateWithTransitiveClosure
import org.jetbrains.kotlin.gradle.plugin.mpp.ownModuleName
import org.jetbrains.kotlin.gradle.report.ReportingSettings
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.gradle.utils.isParentOf
import org.jetbrains.kotlin.gradle.utils.pathsAsStringRelativeTo
import org.jetbrains.kotlin.gradle.utils.toSortedPathsArray
import org.jetbrains.kotlin.incremental.ChangedFiles
import org.jetbrains.kotlin.library.impl.isKotlinLibrary
import org.jetbrains.kotlin.utils.JsLibraryUtils
@@ -631,15 +628,29 @@ open class Kotlin2JsCompile : AbstractKotlinCompile<K2JSCompilerArguments>(), Ko
override fun isIncrementalCompilationEnabled(): Boolean =
when {
"-Xir-produce-js" in kotlinOptions.freeCompilerArgs -> false
"-Xir-produce-klib-dir" in kotlinOptions.freeCompilerArgs -> incrementalJsKlib
"-Xir-produce-klib-dir" in kotlinOptions.freeCompilerArgs -> false // TODO: it's not supported yet
"-Xir-produce-klib-file" in kotlinOptions.freeCompilerArgs -> incrementalJsKlib
else -> incremental
}
@Suppress("unused")
@get:OutputFile
@get:Internal
val outputFile: File
get() = kotlinOptions.outputFile?.let(::File) ?: defaultOutputFile
get() = outputFilePath?.let(::File) ?: defaultOutputFile
@get:OutputFile
@get:Optional
val outputFileOrNull: File?
get() = outputFile.let { file ->
if (file.isFile) {
file
} else {
null
}
}
@get:Input
val outputFilePath: String?
get() = kotlinOptions.outputFile
override fun findKotlinCompilerClasspath(project: Project): List<File> =
findKotlinJsCompilerClasspath(project)
@@ -651,7 +662,14 @@ open class Kotlin2JsCompile : AbstractKotlinCompile<K2JSCompilerArguments>(), Ko
args.apply { fillDefaultValues() }
super.setupCompilerArgs(args, defaultsOnly = defaultsOnly, ignoreClasspathResolutionErrors = ignoreClasspathResolutionErrors)
args.outputFile = outputFile.canonicalPath
try {
outputFile.canonicalPath
} catch (ex: Throwable) {
logger.warn("IO EXCEPTION: outputFile: ${outputFile.path}")
throw ex
}
args.outputFile = outputFile.absoluteFile.normalize().absolutePath
if (defaultsOnly) return