Update Kotlin2JsCompile task to use compiler options

^KT-27301 In Progress
This commit is contained in:
Yahor Berdnikau
2022-09-12 11:38:36 +02:00
parent 228fff555d
commit 6de55d5246
5 changed files with 68 additions and 59 deletions
@@ -235,7 +235,12 @@ internal class Kotlin2JsSourceSetProcessor(
override fun doRegisterTask(project: Project, taskName: String): TaskProvider<out Kotlin2JsCompile> {
val configAction = Kotlin2JsCompileConfig(kotlinCompilation)
applyStandardTaskConfiguration(configAction)
return tasksProvider.registerKotlinJSTask(project, taskName, kotlinCompilation.kotlinOptions, configAction)
return tasksProvider.registerKotlinJSTask(
project,
taskName,
kotlinCompilation.compilerOptions.options as CompilerJsOptions,
configAction
)
}
override fun doTargetSpecificProcessing() {
@@ -282,9 +287,11 @@ internal class Kotlin2JsSourceSetProcessor(
}
}
val subpluginEnvironment: SubpluginEnvironment = SubpluginEnvironment.loadSubplugins(project)
if (kotlinCompilation is KotlinCompilation<*>) { // FIXME support compiler plugins with PM20
subpluginEnvironment.addSubpluginOptions(project, kotlinCompilation)
project.whenEvaluated {
val subpluginEnvironment: SubpluginEnvironment = SubpluginEnvironment.loadSubplugins(project)
if (kotlinCompilation is KotlinCompilation<*>) { // FIXME support compiler plugins with PM20
subpluginEnvironment.addSubpluginOptions(project, kotlinCompilation)
}
}
}
}
@@ -300,7 +307,12 @@ internal class KotlinJsIrSourceSetProcessor(
override fun doRegisterTask(project: Project, taskName: String): TaskProvider<out Kotlin2JsCompile> {
val configAction = Kotlin2JsCompileConfig(kotlinCompilation)
applyStandardTaskConfiguration(configAction)
return tasksProvider.registerKotlinJSTask(project, taskName, kotlinCompilation.kotlinOptions, configAction)
return tasksProvider.registerKotlinJSTask(
project,
taskName,
kotlinCompilation.compilerOptions.options as CompilerJsOptions,
configAction
)
}
override fun doTargetSpecificProcessing() {
@@ -16,8 +16,7 @@ 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.gradle.dsl.KotlinJsOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJsOptionsImpl
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptionsDefault
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinCompilationData
@@ -41,7 +40,7 @@ abstract class KotlinJsIrLink @Inject constructor(
workerExecutor: WorkerExecutor,
private val projectLayout: ProjectLayout
) : Kotlin2JsCompile(
KotlinJsOptionsImpl(),
objectFactory.newInstance(CompilerJsOptionsDefault::class.java),
objectFactory,
workerExecutor
) {
@@ -105,7 +104,7 @@ abstract class KotlinJsIrLink @Inject constructor(
.apply {
set(
destinationDirectory.map { dir ->
if (kotlinOptions.outputFile != null) {
if (compilerOptions.outputFile.orNull != null) {
projectLayout.dir(outputFileProperty.map { it.parentFile }).get()
} else {
dir
@@ -163,52 +162,44 @@ abstract class KotlinJsIrLink @Inject constructor(
.filterNot { it.isEmpty() }
}
override fun setupCompilerArgs(args: K2JSCompilerArguments, defaultsOnly: Boolean, ignoreClasspathResolutionErrors: Boolean) {
override fun setupCompilerArgs(
args: K2JSCompilerArguments,
defaultsOnly: Boolean,
ignoreClasspathResolutionErrors: Boolean
) {
when (mode) {
PRODUCTION -> {
kotlinOptions.configureOptions(ENABLE_DCE, GENERATE_D_TS, MINIMIZED_MEMBER_NAMES)
args.configureOptions(ENABLE_DCE, GENERATE_D_TS, MINIMIZED_MEMBER_NAMES)
}
DEVELOPMENT -> {
kotlinOptions.configureOptions(GENERATE_D_TS)
args.configureOptions(GENERATE_D_TS)
}
}
val alreadyDefinedOutputMode = kotlinOptions.freeCompilerArgs
val alreadyDefinedOutputMode = compilerOptions.freeCompilerArgs.get()
.any { it.startsWith(PER_MODULE) }
if (!alreadyDefinedOutputMode) {
kotlinOptions.freeCompilerArgs += outputGranularity.toCompilerArgument()
args.freeArgs += outputGranularity.toCompilerArgument()
}
super.setupCompilerArgs(args, defaultsOnly, ignoreClasspathResolutionErrors)
}
private fun KotlinJsOptions.configureOptions(vararg additionalCompilerArgs: String) {
freeCompilerArgs += (additionalCompilerArgs.toList() + PRODUCE_JS + "$ENTRY_IR_MODULE=${entryModule.get().asFile.canonicalPath}")
.mapNotNull { arg ->
if (kotlinOptions.freeCompilerArgs
.any { it.startsWith(arg) }
) null else arg
}
private fun K2JSCompilerArguments.configureOptions(vararg additionalCompilerArgs: String) {
freeArgs = freeArgs + (
additionalCompilerArgs
.mapNotNull { arg ->
if (compilerOptions.freeCompilerArgs.get().any { it.startsWith(arg) }) {
null
} else {
arg
}
} +
PRODUCE_JS +
"$ENTRY_IR_MODULE=${entryModule.get().asFile.canonicalPath}"
)
if (platformType == KotlinPlatformType.wasm) {
freeCompilerArgs += WASM_BACKEND
freeArgs = freeArgs + WASM_BACKEND
}
}
@get:Input
override val filteredArgumentsMap: Map<String, String>
get() {
val superFiltered = super.filteredArgumentsMap
return superFiltered.mapValues { (key, value) ->
if (key != K2JSCompilerArguments::freeArgs.name) {
value
} else {
value
.removePrefix("[")
.removeSuffix("]")
.split(", ")
.filter { !it.contains(ENTRY_IR_MODULE) }
.joinToString()
}
}
}
}
@@ -904,14 +904,16 @@ abstract class KotlinCompile @Inject constructor(
@CacheableTask
abstract class Kotlin2JsCompile @Inject constructor(
override val kotlinOptions: KotlinJsOptions,
override val compilerOptions: CompilerJsOptions,
objectFactory: ObjectFactory,
workerExecutor: WorkerExecutor
) : AbstractKotlinCompile<K2JSCompilerArguments>(objectFactory, workerExecutor),
KotlinCompilationTask<CompilerJsOptions>,
KotlinJsCompile {
init {
incremental = true
compilerOptions.verbose.convention(logger.isDebugEnabled)
}
internal abstract class LibraryFilterCachingService : BuildService<BuildServiceParameters.None>, AutoCloseable {
@@ -928,6 +930,13 @@ abstract class Kotlin2JsCompile @Inject constructor(
}
}
@Suppress("DEPRECATION")
@Deprecated("Replaced by compilerOptions input", replaceWith = ReplaceWith("compilerOptions"))
override val kotlinOptions: KotlinJsOptions = object : KotlinJsOptions {
override val options: CompilerJsOptions
get() = compilerOptions
}
@get:Input
internal var incrementalJsKlib: Boolean = true
@@ -968,7 +977,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
K2JSCompilerArguments()
override fun setupCompilerArgs(args: K2JSCompilerArguments, defaultsOnly: Boolean, ignoreClasspathResolutionErrors: Boolean) {
args.apply { fillDefaultValues() }
(compilerOptions as CompilerJsOptionsDefault).fillDefaultValues(args)
super.setupCompilerArgs(args, defaultsOnly = defaultsOnly, ignoreClasspathResolutionErrors = ignoreClasspathResolutionErrors)
try {
@@ -982,7 +991,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
if (defaultsOnly) return
(kotlinOptions as KotlinJsOptionsImpl).updateArguments(args)
(compilerOptions as CompilerJsOptionsDefault).fillCompilerArguments(args)
}
@get:InputFiles
@@ -1001,14 +1010,10 @@ abstract class Kotlin2JsCompile @Inject constructor(
it.exists() && !it.name.endsWith(".jar") && libraryFilter(it)
}
@Suppress("unused")
@get:InputFiles
@get:IgnoreEmptyDirectories
@get:Optional
@get:NormalizeLineEndings
@get:PathSensitive(PathSensitivity.RELATIVE)
internal val sourceMapBaseDirs: FileCollection?
get() = (kotlinOptions as KotlinJsOptionsImpl).sourceMapBaseDirs
@get:Internal
internal val sourceMapBaseDir: Property<Directory> = objectFactory
.directoryProperty()
.value(project.layout.projectDirectory)
private fun isHybridKotlinJsLibrary(file: File): Boolean =
JsLibraryUtils.isKotlinJavascriptLibrary(file) && isKotlinLibrary(file)
@@ -1066,9 +1071,6 @@ abstract class Kotlin2JsCompile @Inject constructor(
libraryCache.get().getOrCompute(file.asLibraryFilterCacheKey, libraryFilterBody)
}
@get:Internal
internal val absolutePathProvider = project.projectDir.absolutePath
override val incrementalProps: List<FileCollection>
get() = super.incrementalProps + listOf(friendDependencies)
@@ -1102,8 +1104,8 @@ abstract class Kotlin2JsCompile @Inject constructor(
args.friendModules = friendDependencies.files.joinToString(File.pathSeparator) { it.absolutePath }
if (args.sourceMapBaseDirs == null && !args.sourceMapPrefix.isNullOrEmpty()) {
args.sourceMapBaseDirs = absolutePathProvider
if (!args.sourceMapPrefix.isNullOrEmpty()) {
args.sourceMapBaseDirs = sourceMapBaseDir.get().asFile.absolutePath
}
args.legacyDeprecatedNoWarn = jsLegacyNoWarn.get()
@@ -22,6 +22,7 @@ import org.gradle.api.UnknownTaskException
import org.gradle.api.tasks.TaskCollection
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.dsl.CompilerJsOptions
import org.jetbrains.kotlin.gradle.dsl.CompilerJvmOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrLink
@@ -110,12 +111,15 @@ internal open class KotlinTasksProvider {
}
fun registerKotlinJSTask(
project: Project, taskName: String, kotlinOptions: KotlinCommonOptions, configuration: Kotlin2JsCompileConfig
project: Project,
taskName: String,
compilerOptions: CompilerJsOptions,
configuration: Kotlin2JsCompileConfig
): TaskProvider<out Kotlin2JsCompile> {
return project.registerTask(
taskName,
Kotlin2JsCompile::class.java,
constructorArgs = listOf(kotlinOptions)
constructorArgs = listOf(compilerOptions)
).also {
configuration.execute(it)
}
@@ -29,7 +29,7 @@ internal open class BaseKotlin2JsCompileConfig<TASK : Kotlin2JsCompile>(
task.outputFileProperty.value(task.project.provider {
val extensionName = if (compilation.platformType == KotlinPlatformType.wasm) ".mjs" else ".js"
task.kotlinOptions.outputFile?.let(::File)
task.compilerOptions.outputFile.orNull?.let(::File)
?: task.destinationDirectory.locationOnly.get().asFile.resolve("${compilation.ownModuleName}$extensionName")
}).disallowChanges()