[Gradle] KotlinCompilerArgumentsProducer: Replace contribute(Type) with semantic methods
KTIJ-24976
This commit is contained in:
committed by
Space Team
parent
8156717e31
commit
133f2260e0
+5
-5
@@ -102,7 +102,7 @@ abstract class KaptGenerateStubsTask @Inject constructor(
|
||||
internal abstract val compileTaskCompilerOptions: Property<KotlinJvmCompilerOptions>
|
||||
|
||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JVMCompilerArguments> {
|
||||
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 }
|
||||
}
|
||||
}
|
||||
|
||||
+34
-8
@@ -38,8 +38,10 @@ interface KotlinCompilerArgumentsProducer {
|
||||
}
|
||||
|
||||
interface ContributeCompilerArgumentsContext<T : CommonToolArguments> {
|
||||
fun <T> tryLenient(action: () -> T): T?
|
||||
fun contribute(type: ArgumentType, contribution: (T) -> Unit)
|
||||
fun <T> 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<T : CommonToolArguments>(
|
||||
private val arguments: T
|
||||
private class ContributeCompilerArgumentsContextImpl<T : CommonToolArguments>(
|
||||
private val arguments: T,
|
||||
private val includedArgumentTypes: Set<KotlinCompilerArgumentsProducer.ArgumentType>,
|
||||
private val isLenient: Boolean
|
||||
) : ContributeCompilerArgumentsContext<T> {
|
||||
|
||||
override fun <T> tryLenient(action: () -> T): T? {
|
||||
private inline fun applyContribution(contribution: (args: T) -> Unit) {
|
||||
try {
|
||||
contribution(arguments)
|
||||
} catch (t: Throwable) {
|
||||
if (!isLenient) throw t
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T> 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -127,7 +127,7 @@ abstract class KotlinJsIrLink @Inject constructor(
|
||||
override fun contributeAdditionalCompilerArguments(context: ContributeCompilerArgumentsContext<K2JSCompilerArguments>) {
|
||||
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)
|
||||
|
||||
+7
-7
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -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
|
||||
|
||||
+5
-5
@@ -132,7 +132,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
|
||||
K2JSCompilerArguments()
|
||||
|
||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JSCompilerArguments> {
|
||||
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
|
||||
}
|
||||
|
||||
+5
-5
@@ -220,7 +220,7 @@ abstract class KotlinCompile @Inject constructor(
|
||||
override fun createCompilerArguments(
|
||||
context: KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext
|
||||
): K2JVMCompilerArguments = context.create<K2JVMCompilerArguments> {
|
||||
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 {
|
||||
|
||||
+6
-6
@@ -69,7 +69,7 @@ abstract class KotlinCompileCommon @Inject constructor(
|
||||
internal var executionTimeFreeCompilerArgs: List<String>? = null
|
||||
|
||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2MetadataCompilerArguments> {
|
||||
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()
|
||||
}
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ abstract class KotlinJsDce @Inject constructor(
|
||||
override val toolOptions: KotlinJsDceCompilerToolOptions = objectFactory.newInstance<KotlinJsDceCompilerToolOptionsDefault>()
|
||||
|
||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JSDceArguments> {
|
||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
||||
primitive { args ->
|
||||
KotlinJsDceCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args)
|
||||
args.declarationsToKeep = keep.toTypedArray()
|
||||
args.outputDirectory = destinationDirectory.get().asFile.path
|
||||
|
||||
Reference in New Issue
Block a user