[Gradle, JS] Add gradle property for per-module/whole-program

Merge-request: KT-MR-5200
This commit is contained in:
Ilya Goncharov
2021-12-15 09:08:45 +00:00
committed by Space
parent 7eb0798d64
commit db9640c3b3
6 changed files with 56 additions and 3 deletions
@@ -404,7 +404,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
fullJs = true,
dceJs = arguments.irDce,
multiModule = arguments.irPerModule,
relativeRequirePath = false,
relativeRequirePath = true,
)
transformer.generateModule(ir.allModules)
@@ -415,7 +415,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
fullJs = true,
dceJs = arguments.irDce,
multiModule = arguments.irPerModule,
relativeRequirePath = false,
relativeRequirePath = true,
)
transformer.generateModule(ir.allModules)
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMultiplatformPlugin
import org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatsService
import org.jetbrains.kotlin.gradle.targets.js.dukat.ExternalsOutputFormat
import org.jetbrains.kotlin.gradle.targets.js.dukat.ExternalsOutputFormat.Companion.externalsOutputFormatProperty
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrOutputGranularity
import org.jetbrains.kotlin.gradle.targets.js.webpack.WebpackMajorVersion
import org.jetbrains.kotlin.gradle.targets.native.DisabledNativeTargetsReporter
import org.jetbrains.kotlin.gradle.tasks.*
@@ -103,6 +104,10 @@ internal class PropertiesProvider private constructor(private val project: Proje
val incrementalJsIr: Boolean
get() = booleanProperty("kotlin.incremental.js.ir") ?: false
val jsIrOutputGranularity: KotlinJsIrOutputGranularity
get() = property("kotlin.js.ir.output.granularity")?.let { KotlinJsIrOutputGranularity.byArgument(it) }
?: KotlinJsIrOutputGranularity.PER_MODULE
val incrementalMultiplatform: Boolean?
get() = booleanProperty("kotlin.incremental.multiplatform")
@@ -24,6 +24,7 @@ internal const val PRODUCE_ZIPPED_KLIB = "-Xir-produce-klib-file"
internal const val MODULE_NAME = "-Xir-module-name"
internal const val PER_MODULE = "-Xir-per-module"
internal const val PER_MODULE_OUTPUT_NAME = "-Xir-per-module-output-name"
internal const val WASM_BACKEND = "-Xwasm"
@@ -17,6 +17,8 @@ import org.gradle.api.tasks.*
import org.gradle.work.InputChanges
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment
import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner
import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl
@@ -41,6 +43,7 @@ import org.jetbrains.kotlin.gradle.utils.getCacheDirectory
import org.jetbrains.kotlin.gradle.utils.getDependenciesCacheDirectories
import org.jetbrains.kotlin.gradle.utils.getValue
import org.jetbrains.kotlin.statistics.metrics.BooleanMetrics
import org.jetbrains.kotlin.statistics.metrics.StringMetrics
import java.io.File
import javax.inject.Inject
@@ -70,8 +73,15 @@ abstract class KotlinJsIrLink @Inject constructor(
@get:Internal
internal lateinit var compilation: KotlinCompilationData<*>
@Transient
@get:Internal
internal val propertiesProvider = PropertiesProvider(project)
@get:Input
internal val incrementalJsIr: Boolean = PropertiesProvider(project).incrementalJsIr
internal val incrementalJsIr: Boolean = propertiesProvider.incrementalJsIr
@get:Input
val outputGranularity: KotlinJsIrOutputGranularity = propertiesProvider.jsIrOutputGranularity
// Link tasks are not affected by compiler plugin
override val pluginClasspath: ConfigurableFileCollection = project.objects.fileCollection()
@@ -111,6 +121,15 @@ abstract class KotlinJsIrLink @Inject constructor(
) {
KotlinBuildStatsService.applyIfInitialised {
it.report(BooleanMetrics.JS_IR_INCREMENTAL, incrementalJsIr)
val newArgs = K2JSCompilerArguments()
parseCommandLineArguments(ArgumentUtils.convertArgumentsToStringList(args), newArgs)
it.report(
StringMetrics.JS_OUTPUT_GRANULARITY,
if (newArgs.irPerModule)
KotlinJsIrOutputGranularity.PER_MODULE.name.toLowerCase()
else
KotlinJsIrOutputGranularity.WHOLE_PROGRAM.name.toLowerCase()
)
}
if (incrementalJsIr && mode == DEVELOPMENT) {
val visitedCompilations = mutableSetOf<KotlinCompilation<*>>()
@@ -177,6 +196,11 @@ abstract class KotlinJsIrLink @Inject constructor(
kotlinOptions.configureOptions(GENERATE_D_TS)
}
}
val alreadyDefinedOutputMode = kotlinOptions.freeCompilerArgs
.any { it.startsWith(PER_MODULE) }
if (!alreadyDefinedOutputMode) {
kotlinOptions.freeCompilerArgs += outputGranularity.toCompilerArgument()
}
super.setupCompilerArgs(args, defaultsOnly, ignoreClasspathResolutionErrors)
}
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.gradle.targets.js.ir
enum class KotlinJsIrOutputGranularity {
WHOLE_PROGRAM,
PER_MODULE;
companion object {
fun byArgument(argument: String): KotlinJsIrOutputGranularity? =
values()
.firstOrNull { it.name.replace("_", "-").equals(argument, ignoreCase = true) }
}
}
fun KotlinJsIrOutputGranularity.toCompilerArgument(): String {
val perModule = this == KotlinJsIrOutputGranularity.PER_MODULE
return "$PER_MODULE=$perModule"
}
@@ -43,6 +43,7 @@ enum class StringMetrics(val type: StringOverridePolicy, val anonymization: Stri
KOTLIN_API_VERSION(OVERRIDE, COMPONENT_VERSION),
JS_GENERATE_EXECUTABLE_DEFAULT(CONCAT, SAFE),
JS_TARGET_MODE(CONCAT, SAFE),
JS_OUTPUT_GRANULARITY(OVERRIDE, SAFE),
// Compiler parameters
JVM_DEFAULTS(CONCAT, SAFE),