[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 // Advanced options
@Argument(
@Argument(value = "-Xir", description = "Use IR backend") value = "-Xir-produce-klib-dir",
var irBackend: Boolean by FreezableVar(false) 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( @Argument(
value = "-Xir-produce-only", value = "-Xir-produce-klib-file",
valueDescription = "{ klib, js }", description = "Generate packed klib into file specified by -output. Disables pre-IR backend"
description = "Type of output to produce. Overrides -meta-info argument."
) )
var irProduceOnly: String? by NullableStringFreezableVar(null) var irProduceKlibFile: Boolean by FreezableVar(false)
@Argument( @Argument(value = "-Xir-produce-js", description = "Generates JS file using IR backend. Also disables pre-IR backend")
value = "-Xir-legacy-gradle-plugin-compatibility", var irProduceJs: Boolean by FreezableVar(false)
description = "Make KLIB generation compatible with legacy gradle plugin"
) @Argument(value = "-Xir-only", description = "Disables pre-IR backend")
var irLegacyGradlePluginCompatibility: Boolean by FreezableVar(false) var irOnly: Boolean by FreezableVar(false)
@GradleOption(DefaultValues.BooleanTrueDefault::class) @GradleOption(DefaultValues.BooleanTrueDefault::class)
@Argument(value = "-Xtyped-arrays", description = "Translate primitive arrays to JS typed arrays") @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)") @Argument(value = "-Xenable-js-scripting", description = "Enable experimental support of .kts files using K/JS (with -Xir only)")
var enableJsScripting: Boolean by FreezableVar(false) 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.CommonCompilerPerformanceManager;
import org.jetbrains.kotlin.cli.common.ExitCode; import org.jetbrains.kotlin.cli.common.ExitCode;
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments; 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.arguments.K2JsArgumentConstants;
import org.jetbrains.kotlin.cli.common.config.ContentRootsKt; import org.jetbrains.kotlin.cli.common.config.ContentRootsKt;
import org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport; 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); MessageCollector messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY);
if (arguments.getIrBackend()) { ExitCode exitCode = OK;
return getIrCompiler().doExecute(arguments, configuration, rootDisposable, paths);
if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments)) {
exitCode = getIrCompiler().doExecute(arguments, configuration.copy(), rootDisposable, paths);
}
if (K2JSCompilerArgumentsKt.isPreIrBackendDisabled(arguments)) {
return exitCode;
} }
if (arguments.getFreeArgs().isEmpty() && !IncrementalCompilation.isEnabledForJs()) { if (arguments.getFreeArgs().isEmpty() && !IncrementalCompilation.isEnabledForJs()) {
@@ -388,8 +394,10 @@ public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
@NotNull CompilerConfiguration configuration, @NotNull K2JSCompilerArguments arguments, @NotNull CompilerConfiguration configuration, @NotNull K2JSCompilerArguments arguments,
@NotNull Services services @NotNull Services services
) { ) {
if (arguments.getIrBackend()) { if (K2JSCompilerArgumentsKt.isIrBackendEnabled(arguments)) {
getIrCompiler().setupPlatformSpecificArgumentsAndServices(configuration, arguments, services); getIrCompiler().setupPlatformSpecificArgumentsAndServices(configuration, arguments, services);
}
if (K2JSCompilerArgumentsKt.isPreIrBackendDisabled(arguments)) {
return; return;
} }
@@ -175,17 +175,12 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
it.libraryFile.absolutePath in friendAbsolutePaths it.libraryFile.absolutePath in friendAbsolutePaths
} }
val produceKind = produceMap[arguments.irProduceOnly] if (arguments.irProduceKlibDir || arguments.irProduceKlibFile) {
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)) {
val outputKlibPath = val outputKlibPath =
if (arguments.irLegacyGradlePluginCompatibility) if (arguments.irProduceKlibDir)
File(outputFilePath).parent File(outputFilePath).parent
else else
"$outputFilePath.klib" outputFilePath
generateKLib( generateKLib(
project = config.project, project = config.project,
@@ -194,11 +189,11 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
allDependencies = resolvedLibraries, allDependencies = resolvedLibraries,
friendDependencies = friendDependencies, friendDependencies = friendDependencies,
outputKlibPath = outputKlibPath, 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 phaseConfig = createPhaseConfig(jsPhases, arguments, messageCollector)
val compiledModule = compile( val compiledModule = compile(
@@ -10,10 +10,8 @@ abstract class AbstractIncrementalJsKlibCompilerRunnerTest : AbstractIncremental
libraries = "build/js-ir-runtime/full-runtime.klib" libraries = "build/js-ir-runtime/full-runtime.klib"
outputFile = File(destinationDir, "${testDir.name}.klib").path outputFile = File(destinationDir, "${testDir.name}.klib").path
sourceMap = true 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 // 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 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) -Xenable-js-scripting Enable experimental support of .kts files using K/JS (with -Xir only)
-Xfriend-modules=<path> Paths to friend modules -Xfriend-modules=<path> Paths to friend modules
-Xfriend-modules-disabled Disable internal declaration export -Xfriend-modules-disabled Disable internal declaration export
-Xir Use IR backend -Xir-only Disables pre-IR backend
-Xir-legacy-gradle-plugin-compatibility -Xir-produce-js Generates JS file using IR backend. Also disables pre-IR backend
Make KLIB generation compatible with legacy gradle plugin -Xir-produce-klib-dir Generate unpacked KLIB into parent directory of output JS file.
-Xir-produce-only={ klib, js } Type of output to produce. Overrides -meta-info argument. 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 -Xmetadata-only Generate *.meta.js and *.kjsm files only
-Xtyped-arrays Translate primitive arrays to JS typed arrays -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 -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.api.tasks.incremental.IncrementalTaskInputs
import org.gradle.workers.WorkerExecutor import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.build.DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS 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.*
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.compilerRunner.* import org.jetbrains.kotlin.compilerRunner.*
import org.jetbrains.kotlin.daemon.common.MultiModuleICSettings import org.jetbrains.kotlin.daemon.common.MultiModuleICSettings
import org.jetbrains.kotlin.gradle.dsl.* import org.jetbrains.kotlin.gradle.dsl.*
@@ -553,9 +550,30 @@ open class Kotlin2JsCompile : AbstractKotlinCompile<K2JSCompilerArguments>(), Ko
return friendPaths.filter { filter(File(it)) } 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 private val libraryFilter: (File) -> Boolean
get() = if ("-Xir" in kotlinOptions.freeCompilerArgs) { get() = if (kotlinOptions.isIrBackendEnabled()) {
LibraryUtils::isKotlinJavascriptIrLibrary if (kotlinOptions.isPreIrBackendDisabled()) {
LibraryUtils::isKotlinJavascriptIrLibrary
} else {
::isHybridKotlinJsLibrary
}
} else { } else {
LibraryUtils::isKotlinJavascriptLibrary LibraryUtils::isKotlinJavascriptLibrary
} }
@@ -566,10 +584,9 @@ open class Kotlin2JsCompile : AbstractKotlinCompile<K2JSCompilerArguments>(), Ko
logger.debug("Calling compiler") logger.debug("Calling compiler")
destinationDir.mkdirs() destinationDir.mkdirs()
if ("-Xir" in args.freeArgs) { if (args.isIrBackendEnabled()) {
logger.kotlinDebug("Using JS IR backend") logger.kotlinDebug("Using JS IR backend")
incremental = false incremental = false
args.freeArgs += "-Xir-legacy-gradle-plugin-compatibility"
} }
val dependencies = compileClasspath val dependencies = compileClasspath
@@ -105,8 +105,9 @@ public class K2JSCompilerMojo extends KotlinCompileMojoBase<K2JSCompilerArgument
arguments.setMetaInfo(metaInfo); arguments.setMetaInfo(metaInfo);
arguments.setModuleKind(moduleKind); arguments.setModuleKind(moduleKind);
arguments.setMain(main); arguments.setMain(main);
arguments.setIrBackend(useIrBackend); arguments.setIrOnly(useIrBackend);
arguments.setIrLegacyGradlePluginCompatibility(true); arguments.setIrProduceJs(useIrBackend);
arguments.setIrProduceKlibDir(useIrBackend);
List<String> libraries; List<String> libraries;
try { try {