[JS IR] Support generating both IR and pre-IR libraries

Remove all previous -Xir options

Add:
 -Xir-produce-klib-dir
 -Xir-produce-klib-file
 -Xir-produce-js
 -Xir-only
This commit is contained in:
Svyatoslav Kuzmich
2019-10-17 14:12:02 +03:00
parent 62d204f4d6
commit 1b8df45bfe
7 changed files with 70 additions and 42 deletions
@@ -106,22 +106,24 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
// Advanced options
@Argument(value = "-Xir", description = "Use IR backend")
var irBackend: Boolean by FreezableVar(false)
@Argument(
value = "-Xir-produce-klib-dir",
description = "Generate unpacked KLIB into parent directory of output JS file.\n" +
"In combination with -meta-info generates both IR and pre-IR versions of library."
)
var irProduceKlibDir: Boolean by FreezableVar(false)
@Argument(
value = "-Xir-produce-only",
valueDescription = "{ klib, js }",
description = "Type of output to produce. Overrides -meta-info argument."
value = "-Xir-produce-klib-file",
description = "Generate packed klib into file specified by -output. Disables pre-IR backend"
)
var irProduceOnly: String? by NullableStringFreezableVar(null)
var irProduceKlibFile: Boolean by FreezableVar(false)
@Argument(
value = "-Xir-legacy-gradle-plugin-compatibility",
description = "Make KLIB generation compatible with legacy gradle plugin"
)
var irLegacyGradlePluginCompatibility: Boolean by FreezableVar(false)
@Argument(value = "-Xir-produce-js", description = "Generates JS file using IR backend. Also disables pre-IR backend")
var irProduceJs: Boolean by FreezableVar(false)
@Argument(value = "-Xir-only", description = "Disables pre-IR backend")
var irOnly: Boolean by FreezableVar(false)
@GradleOption(DefaultValues.BooleanTrueDefault::class)
@Argument(value = "-Xtyped-arrays", description = "Translate primitive arrays to JS typed arrays")
@@ -144,3 +146,9 @@ class K2JSCompilerArguments : CommonCompilerArguments() {
@Argument(value = "-Xenable-js-scripting", description = "Enable experimental support of .kts files using K/JS (with -Xir only)")
var enableJsScripting: Boolean by FreezableVar(false)
}
fun K2JSCompilerArguments.isPreIrBackendDisabled(): Boolean =
irOnly || irProduceJs || irProduceKlibFile
fun K2JSCompilerArguments.isIrBackendEnabled(): Boolean =
irProduceKlibDir || irProduceJs || irProduceKlibFile
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys;
import org.jetbrains.kotlin.cli.common.CommonCompilerPerformanceManager;
import org.jetbrains.kotlin.cli.common.ExitCode;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArgumentsKt;
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants;
import org.jetbrains.kotlin.cli.common.config.ContentRootsKt;
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport;
@@ -186,8 +187,13 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
) {
MessageCollector messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
if (arguments.getIrBackend()) {
return getIrCompiler().doExecute(arguments, configuration, rootDisposable, paths);
ExitCode exitCode = OK;
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments)) {
exitCode = getIrCompiler().doExecute(arguments, configuration.copy(), rootDisposable, paths);
}
if (K2JSCompilerArgumentsKt.isPreIrBackendDisabled(arguments)) {
return exitCode;
}
if (arguments.getFreeArgs().isEmpty() && !IncrementalCompilation.isEnabledForJs()) {
@@ -388,8 +394,10 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
@NotNull CompilerConfiguration configuration, @NotNull K2JSCompilerArguments arguments,
@NotNull Services services
) {
if (arguments.getIrBackend()) {
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments)) {
getIrCompiler().setupPlatformSpecificArgumentsAndServices(configuration, arguments, services);
}
if (K2JSCompilerArgumentsKt.isPreIrBackendDisabled(arguments)) {
return;
}
@@ -175,17 +175,12 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
it.libraryFile.absolutePath in friendAbsolutePaths
}
val produceKind = produceMap[arguments.irProduceOnly]
if (produceKind == null) {
messageCollector.report(ERROR, "Unknown produce kind: ${arguments.irProduceOnly}. Valid values are: js, klib")
}
if (produceKind == ProduceKind.KLIB || (produceKind == ProduceKind.DEFAULT && arguments.metaInfo)) {
if (arguments.irProduceKlibDir || arguments.irProduceKlibFile) {
val outputKlibPath =
if (arguments.irLegacyGradlePluginCompatibility)
if (arguments.irProduceKlibDir)
File(outputFilePath).parent
else
"$outputFilePath.klib"
outputFilePath
generateKLib(
project = config.project,
@@ -194,11 +189,11 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
allDependencies = resolvedLibraries,
friendDependencies = friendDependencies,
outputKlibPath = outputKlibPath,
nopack = arguments.irLegacyGradlePluginCompatibility
nopack = arguments.irProduceKlibDir
)
}
if (produceKind == ProduceKind.JS || produceKind == ProduceKind.DEFAULT) {
if (arguments.irProduceJs) {
val phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
val compiledModule = compile(
@@ -10,10 +10,8 @@ abstract class AbstractIncrementalJsKlibCompilerRunnerTest : AbstractIncremental
libraries = "build/js-ir-runtime/full-runtime.klib"
outputFile = File(destinationDir, "${testDir.name}.klib").path
sourceMap = true
irBackend = true
irProduceOnly = "klib"
// Don't zip klib content since date on files affect the md5 checksum we compute to check whether output files identical
irLegacyGradlePluginCompatibility = true
irProduceKlibDir = true
}
override val buildLogFinder: BuildLogFinder
+5 -4
View File
@@ -3,10 +3,11 @@ where advanced options include:
-Xenable-js-scripting Enable experimental support of .kts files using K/JS (with -Xir only)
-Xfriend-modules=<path> Paths to friend modules
-Xfriend-modules-disabled Disable internal declaration export
-Xir Use IR backend
-Xir-legacy-gradle-plugin-compatibility
Make KLIB generation compatible with legacy gradle plugin
-Xir-produce-only={ klib, js } Type of output to produce. Overrides -meta-info argument.
-Xir-only Disables pre-IR backend
-Xir-produce-js Generates JS file using IR backend. Also disables pre-IR backend
-Xir-produce-klib-dir Generate unpacked KLIB into parent directory of output JS file.
In combination with -meta-info generates both IR and pre-IR versions of library.
-Xir-produce-klib-file Generate packed klib into file specified by -output. Disables pre-IR backend
-Xmetadata-only Generate *.meta.js and *.kjsm files only
-Xtyped-arrays Translate primitive arrays to JS typed arrays
-Xallow-kotlin-package Allow compiling code in package 'kotlin' and allow not requiring kotlin.stdlib in module-info
@@ -16,10 +16,7 @@ import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.build.DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.compilerRunner.*
import org.jetbrains.kotlin.daemon.common.MultiModuleICSettings
import org.jetbrains.kotlin.gradle.dsl.*
@@ -553,9 +550,30 @@ open class Kotlin2JsCompile : AbstractKotlinCompile<K2JSCompilerArguments>(), Ko
return friendPaths.filter { filter(File(it)) }
}
private fun isHybridKotlinJsLibrary(file: File): Boolean =
LibraryUtils.isKotlinJavascriptLibrary(file) && LibraryUtils.isKotlinJavascriptIrLibrary(file)
private fun KotlinJsOptions.isPreIrBackendDisabled(): Boolean =
listOf(
"-Xir-only",
"-Xir-produce-js",
"-Xir-produce-klib-file"
).any(freeCompilerArgs::contains)
private fun KotlinJsOptions.isIrBackendEnabled(): Boolean =
listOf(
"-Xir-produce-klib-dir",
"-Xir-produce-js",
"-Xir-produce-klib-file"
).any(freeCompilerArgs::contains)
private val libraryFilter: (File) -> Boolean
get() = if ("-Xir" in kotlinOptions.freeCompilerArgs) {
LibraryUtils::isKotlinJavascriptIrLibrary
get() = if (kotlinOptions.isIrBackendEnabled()) {
if (kotlinOptions.isPreIrBackendDisabled()) {
LibraryUtils::isKotlinJavascriptIrLibrary
} else {
::isHybridKotlinJsLibrary
}
} else {
LibraryUtils::isKotlinJavascriptLibrary
}
@@ -566,10 +584,9 @@ open class Kotlin2JsCompile : AbstractKotlinCompile<K2JSCompilerArguments>(), Ko
logger.debug("Calling compiler")
destinationDir.mkdirs()
if ("-Xir" in args.freeArgs) {
if (args.isIrBackendEnabled()) {
logger.kotlinDebug("Using JS IR backend")
incremental = false
args.freeArgs += "-Xir-legacy-gradle-plugin-compatibility"
}
val dependencies = compileClasspath
@@ -105,8 +105,9 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
arguments.setMetaInfo(metaInfo);
arguments.setModuleKind(moduleKind);
arguments.setMain(main);
arguments.setIrBackend(useIrBackend);
arguments.setIrLegacyGradlePluginCompatibility(true);
arguments.setIrOnly(useIrBackend);
arguments.setIrProduceJs(useIrBackend);
arguments.setIrProduceKlibDir(useIrBackend);
List<String> libraries;
try {