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