From 133f2260e0dbdf1276ec7e0bd1cbdf1b62010384 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Tue, 4 Apr 2023 21:44:59 +0200 Subject: [PATCH] [Gradle] KotlinCompilerArgumentsProducer: Replace contribute(Type) with semantic methods KTIJ-24976 --- .../internal/kapt/KaptGenerateStubsTask.kt | 10 ++--- .../plugin/KotlinCompilerArgumentsProducer.kt | 42 +++++++++++++++---- .../gradle/targets/js/ir/KotlinJsIrLink.kt | 2 +- .../targets/native/tasks/KotlinNativeLink.kt | 14 +++---- .../targets/native/tasks/KotlinNativeTasks.kt | 14 +++---- .../kotlin/gradle/tasks/Kotlin2JsCompile.kt | 10 ++--- .../kotlin/gradle/tasks/KotlinCompile.kt | 10 ++--- .../gradle/tasks/KotlinCompileCommon.kt | 12 +++--- .../kotlin/gradle/tasks/KotlinJsDce.kt | 2 +- 9 files changed, 71 insertions(+), 45 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt index 3c0cdce897d..d32857b4a7e 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/kapt/KaptGenerateStubsTask.kt @@ -102,7 +102,7 @@ abstract class KaptGenerateStubsTask @Inject constructor( internal abstract val compileTaskCompilerOptions: Property override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create { - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + primitive { args -> args.allowNoSourceFiles = true KotlinJvmCompilerOptionsHelper.fillCompilerArguments(compileTaskCompilerOptions.get(), args) @@ -126,18 +126,18 @@ abstract class KaptGenerateStubsTask @Inject constructor( args.destinationAsFile = destinationDirectory.get().asFile } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args -> - args.pluginClasspaths = tryLenient { + classpath { args -> + args.pluginClasspaths = runSafe { listOfNotNull( pluginClasspath, kotlinPluginData?.orNull?.classpath ).reduce(FileCollection::plus).toPathsArray() } - args.classpathAsList = tryLenient { libraries.toList().filter { it.exists() } }.orEmpty() + args.classpathAsList = runSafe { libraries.toList().filter { it.exists() } }.orEmpty() args.friendPaths = friendPaths.toPathsArray() } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args -> + sources{ args -> args.freeArgs += (scriptSources.asFileTree.files + javaSources.files + sources.asFileTree.files).map { it.absolutePath } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt index 397e69ca34a..2b97320b3f8 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt @@ -38,8 +38,10 @@ interface KotlinCompilerArgumentsProducer { } interface ContributeCompilerArgumentsContext { - fun tryLenient(action: () -> T): T? - fun contribute(type: ArgumentType, contribution: (T) -> Unit) + fun runSafe(action: () -> T): T? + fun primitive(contribution: (args: T) -> Unit) + fun classpath(contribution: (args: T) -> Unit) + fun sources(contribution: (args: T) -> Unit) } fun createCompilerArguments( @@ -65,15 +67,25 @@ private class CreateCompilerArgumentsContextImpl( val constructor = type.java.constructors.firstOrNull { it.parameters.isEmpty() } ?: throw IllegalArgumentException("'${type.qualifiedName}' does not have an empty constructor") val arguments = type.cast(constructor.newInstance()) - ContributeCompilerArgumentsContextImpl(arguments).also(action) + ContributeCompilerArgumentsContextImpl(arguments, includeArgumentTypes, isLenient).also(action) return arguments } - private inner class ContributeCompilerArgumentsContextImpl( - private val arguments: T + private class ContributeCompilerArgumentsContextImpl( + private val arguments: T, + private val includedArgumentTypes: Set, + private val isLenient: Boolean ) : ContributeCompilerArgumentsContext { - override fun tryLenient(action: () -> T): T? { + private inline fun applyContribution(contribution: (args: T) -> Unit) { + try { + contribution(arguments) + } catch (t: Throwable) { + if (!isLenient) throw t + } + } + + override fun runSafe(action: () -> T): T? { return try { action() } catch (t: Throwable) { @@ -81,8 +93,22 @@ private class CreateCompilerArgumentsContextImpl( } } - override fun contribute(type: KotlinCompilerArgumentsProducer.ArgumentType, contribution: (T) -> Unit) { - if (type in includeArgumentTypes) contribution(arguments) + override fun primitive(contribution: (args: T) -> Unit) { + if (KotlinCompilerArgumentsProducer.ArgumentType.Primitive in includedArgumentTypes) { + applyContribution(contribution) + } + } + + override fun classpath(contribution: (args: T) -> Unit) { + if (KotlinCompilerArgumentsProducer.ArgumentType.Classpath in includedArgumentTypes) { + applyContribution(contribution) + } + } + + override fun sources(contribution: (args: T) -> Unit) { + if (KotlinCompilerArgumentsProducer.ArgumentType.Sources in includedArgumentTypes) { + applyContribution(contribution) + } } } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrLink.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrLink.kt index 00e15f2318f..e82212f7962 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrLink.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/js/ir/KotlinJsIrLink.kt @@ -127,7 +127,7 @@ abstract class KotlinJsIrLink @Inject constructor( override fun contributeAdditionalCompilerArguments(context: ContributeCompilerArgumentsContext) { super.contributeAdditionalCompilerArguments(context) - context.contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + context.primitive { args -> // TODO Ilya Goncharov: This should not be part of creating compiler arguments; KotlinBuildStatsService.applyIfInitialised { it.report(BooleanMetrics.JS_IR_INCREMENTAL, this.incrementalJsIr) diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt index d19dbf6574f..d7c0099a05a 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeLink.kt @@ -200,7 +200,7 @@ constructor( kotlinPluginData?.orNull?.let { CompilerPluginData(it.classpath, it.options) } ) - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + primitive { args -> args.outputName = outputFile.get().absolutePath args.optimization = optimized args.debug = debuggable @@ -226,15 +226,15 @@ constructor( KotlinCommonCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args) } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args -> - args.pluginClasspaths = compilerPlugins.flatMap { classpath -> tryLenient { classpath.files } ?: emptySet() }.toPathsArray() - args.libraries = tryLenient { libraries.files.filterKlibsPassedToCompiler() }?.toPathsArray() - args.exportedLibraries = tryLenient { exportLibraries.files.filterKlibsPassedToCompiler() }?.toPathsArray() - args.friendModules = tryLenient { friendModule.files.toList().takeIf { it.isNotEmpty() } } + classpath { args -> + args.pluginClasspaths = compilerPlugins.flatMap { classpath -> runSafe { classpath.files } ?: emptySet() }.toPathsArray() + args.libraries = runSafe { libraries.files.filterKlibsPassedToCompiler() }?.toPathsArray() + args.exportedLibraries = runSafe { exportLibraries.files.filterKlibsPassedToCompiler() }?.toPathsArray() + args.friendModules = runSafe { friendModule.files.toList().takeIf { it.isNotEmpty() } } ?.joinToString(File.pathSeparator) { it.absolutePath } } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args -> + sources { args -> args.includes = sources.asFileTree.files.toPathsArray() } } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt index eb4b2c7c5cc..a21854f350d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/targets/native/tasks/KotlinNativeTasks.kt @@ -428,7 +428,7 @@ internal constructor( kotlinPluginData?.orNull?.let { CompilerPluginData(it.classpath, it.options) } ) - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + primitive { args -> args.moduleName = compilerOptions.moduleName.get() args.shortModuleName = shortModuleName args.multiPlatform = true @@ -454,19 +454,19 @@ internal constructor( KotlinNativeCompilerOptionsHelper.fillCompilerArguments(compilerOptions, args) } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args -> - args.pluginClasspaths = compilerPlugins.flatMap { classpath -> tryLenient { classpath.files } ?: emptySet() }.toPathsArray() + classpath { args -> + args.pluginClasspaths = compilerPlugins.flatMap { classpath -> runSafe { classpath.files } ?: emptySet() }.toPathsArray() - args.libraries = tryLenient { libraries.files.filterKlibsPassedToCompiler().toPathsArray() } - args.friendModules = tryLenient { + args.libraries = runSafe { libraries.files.filterKlibsPassedToCompiler().toPathsArray() } + args.friendModules = runSafe { friendModule.files.takeIf { it.isNotEmpty() }?.map { it.absolutePath }?.joinToString(File.pathSeparator) } - args.refinesPaths = tryLenient { + args.refinesPaths = runSafe { sharedCompilationData?.refinesPaths?.files?.takeIf { it.isNotEmpty() }?.toPathsArray() } } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args -> + sources { args -> if (compilerOptions.usesK2.get()) { /* For now, we only pass multiplatform structure to K2 for platform compilations diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/Kotlin2JsCompile.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/Kotlin2JsCompile.kt index f0484a47707..73dc9a50446 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/Kotlin2JsCompile.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/Kotlin2JsCompile.kt @@ -132,7 +132,7 @@ abstract class Kotlin2JsCompile @Inject constructor( K2JSCompilerArguments() override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create { - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + primitive { args -> args.multiPlatform = multiPlatformEnabled.get() args.pluginOptions = (pluginOptions.toSingleCompilerPluginOptions() + kotlinPluginData?.orNull?.options) @@ -172,8 +172,8 @@ abstract class Kotlin2JsCompile @Inject constructor( args.freeArgs = executionTimeFreeCompilerArgs ?: enhancedFreeCompilerArgs.get() } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args -> - args.pluginClasspaths = tryLenient { + classpath { args -> + args.pluginClasspaths = runSafe { listOfNotNull( pluginClasspath, kotlinPluginData?.orNull?.classpath ).reduce(FileCollection::plus).toPathsArray() @@ -181,7 +181,7 @@ abstract class Kotlin2JsCompile @Inject constructor( args.friendModules = friendDependencies.files.joinToString(File.pathSeparator) { it.absolutePath } - args.libraries = tryLenient { + args.libraries = runSafe { libraries .filter { it.exists() && libraryFilter(it) } .map { it.normalize().absolutePath } @@ -191,7 +191,7 @@ abstract class Kotlin2JsCompile @Inject constructor( } } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args -> + sources { args -> if (!args.sourceMapPrefix.isNullOrEmpty()) { args.sourceMapBaseDirs = sourceMapBaseDir.get().asFile.absolutePath } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompile.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompile.kt index 6b9117910db..4c4674674da 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompile.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompile.kt @@ -220,7 +220,7 @@ abstract class KotlinCompile @Inject constructor( override fun createCompilerArguments( context: KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext ): K2JVMCompilerArguments = context.create { - contribute(ArgumentType.Primitive) { args -> + primitive { args -> args.allowNoSourceFiles = true args.multiPlatform = multiPlatformEnabled.get() @@ -252,20 +252,20 @@ abstract class KotlinCompile @Inject constructor( } } - contribute(ArgumentType.Classpath) { args -> - args.pluginClasspaths = tryLenient { + classpath { args -> + args.pluginClasspaths = runSafe { listOfNotNull( pluginClasspath, kotlinPluginData?.orNull?.classpath ).reduce(FileCollection::plus).toPathsArray() } args.friendPaths = friendPaths.toPathsArray() - args.classpathAsList = tryLenient { + args.classpathAsList = runSafe { libraries.toList().filter { it.exists() } }.orEmpty() } - contribute(ArgumentType.Sources) { args -> + sources { args -> if (compilerOptions.usesK2.get()) { args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs } else { diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt index f69a31789de..2a4e2e5d5da 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinCompileCommon.kt @@ -69,7 +69,7 @@ abstract class KotlinCompileCommon @Inject constructor( internal var executionTimeFreeCompilerArgs: List? = null override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create { - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + primitive { args -> args.multiPlatform = multiPlatformEnabled.get() args.moduleName = this@KotlinCompileCommon.moduleName.get() @@ -93,18 +93,18 @@ abstract class KotlinCompileCommon @Inject constructor( } } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args -> - args.pluginClasspaths = tryLenient { + classpath { args -> + args.pluginClasspaths = runSafe { listOfNotNull( pluginClasspath, kotlinPluginData?.orNull?.classpath ).reduce(FileCollection::plus).toPathsArray() } - args.classpath = tryLenient { libraries.files.filter { it.exists() }.joinToString(File.pathSeparator) } - args.friendPaths = tryLenient { this@KotlinCompileCommon.friendPaths.files.toPathsArray() } + args.classpath = runSafe { libraries.files.filter { it.exists() }.joinToString(File.pathSeparator) } + args.friendPaths = runSafe { this@KotlinCompileCommon.friendPaths.files.toPathsArray() } args.refinesPaths = refinesMetadataPaths.toPathsArray() } - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args -> + sources { args -> args.freeArgs += sources.asFileTree.map { it.absolutePath } args.commonSources = commonSourceSet.asFileTree.toPathsArray() } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt index 80ac7c56d52..be29b0543d4 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt @@ -59,7 +59,7 @@ abstract class KotlinJsDce @Inject constructor( override val toolOptions: KotlinJsDceCompilerToolOptions = objectFactory.newInstance() override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create { - contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args -> + primitive { args -> KotlinJsDceCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args) args.declarationsToKeep = keep.toTypedArray() args.outputDirectory = destinationDirectory.get().asFile.path