[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>
|
internal abstract val compileTaskCompilerOptions: Property<KotlinJvmCompilerOptions>
|
||||||
|
|
||||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JVMCompilerArguments> {
|
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JVMCompilerArguments> {
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
args.allowNoSourceFiles = true
|
args.allowNoSourceFiles = true
|
||||||
KotlinJvmCompilerOptionsHelper.fillCompilerArguments(compileTaskCompilerOptions.get(), args)
|
KotlinJvmCompilerOptionsHelper.fillCompilerArguments(compileTaskCompilerOptions.get(), args)
|
||||||
|
|
||||||
@@ -126,18 +126,18 @@ abstract class KaptGenerateStubsTask @Inject constructor(
|
|||||||
args.destinationAsFile = destinationDirectory.get().asFile
|
args.destinationAsFile = destinationDirectory.get().asFile
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args ->
|
classpath { args ->
|
||||||
args.pluginClasspaths = tryLenient {
|
args.pluginClasspaths = runSafe {
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
||||||
).reduce(FileCollection::plus).toPathsArray()
|
).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()
|
args.friendPaths = friendPaths.toPathsArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args ->
|
sources{ args ->
|
||||||
args.freeArgs += (scriptSources.asFileTree.files + javaSources.files + sources.asFileTree.files).map { it.absolutePath }
|
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> {
|
interface ContributeCompilerArgumentsContext<T : CommonToolArguments> {
|
||||||
fun <T> tryLenient(action: () -> T): T?
|
fun <T> runSafe(action: () -> T): T?
|
||||||
fun contribute(type: ArgumentType, contribution: (T) -> Unit)
|
fun primitive(contribution: (args: T) -> Unit)
|
||||||
|
fun classpath(contribution: (args: T) -> Unit)
|
||||||
|
fun sources(contribution: (args: T) -> Unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createCompilerArguments(
|
fun createCompilerArguments(
|
||||||
@@ -65,15 +67,25 @@ private class CreateCompilerArgumentsContextImpl(
|
|||||||
val constructor = type.java.constructors.firstOrNull { it.parameters.isEmpty() }
|
val constructor = type.java.constructors.firstOrNull { it.parameters.isEmpty() }
|
||||||
?: throw IllegalArgumentException("'${type.qualifiedName}' does not have an empty constructor")
|
?: throw IllegalArgumentException("'${type.qualifiedName}' does not have an empty constructor")
|
||||||
val arguments = type.cast(constructor.newInstance())
|
val arguments = type.cast(constructor.newInstance())
|
||||||
ContributeCompilerArgumentsContextImpl(arguments).also(action)
|
ContributeCompilerArgumentsContextImpl(arguments, includeArgumentTypes, isLenient).also(action)
|
||||||
return arguments
|
return arguments
|
||||||
}
|
}
|
||||||
|
|
||||||
private inner class ContributeCompilerArgumentsContextImpl<T : CommonToolArguments>(
|
private class ContributeCompilerArgumentsContextImpl<T : CommonToolArguments>(
|
||||||
private val arguments: T
|
private val arguments: T,
|
||||||
|
private val includedArgumentTypes: Set<KotlinCompilerArgumentsProducer.ArgumentType>,
|
||||||
|
private val isLenient: Boolean
|
||||||
) : ContributeCompilerArgumentsContext<T> {
|
) : 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 {
|
return try {
|
||||||
action()
|
action()
|
||||||
} catch (t: Throwable) {
|
} catch (t: Throwable) {
|
||||||
@@ -81,8 +93,22 @@ private class CreateCompilerArgumentsContextImpl(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun contribute(type: KotlinCompilerArgumentsProducer.ArgumentType, contribution: (T) -> Unit) {
|
override fun primitive(contribution: (args: T) -> Unit) {
|
||||||
if (type in includeArgumentTypes) contribution(arguments)
|
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>) {
|
override fun contributeAdditionalCompilerArguments(context: ContributeCompilerArgumentsContext<K2JSCompilerArguments>) {
|
||||||
super.contributeAdditionalCompilerArguments(context)
|
super.contributeAdditionalCompilerArguments(context)
|
||||||
|
|
||||||
context.contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
context.primitive { args ->
|
||||||
// TODO Ilya Goncharov: This should not be part of creating compiler arguments;
|
// TODO Ilya Goncharov: This should not be part of creating compiler arguments;
|
||||||
KotlinBuildStatsService.applyIfInitialised {
|
KotlinBuildStatsService.applyIfInitialised {
|
||||||
it.report(BooleanMetrics.JS_IR_INCREMENTAL, this.incrementalJsIr)
|
it.report(BooleanMetrics.JS_IR_INCREMENTAL, this.incrementalJsIr)
|
||||||
|
|||||||
+7
-7
@@ -200,7 +200,7 @@ constructor(
|
|||||||
kotlinPluginData?.orNull?.let { CompilerPluginData(it.classpath, it.options) }
|
kotlinPluginData?.orNull?.let { CompilerPluginData(it.classpath, it.options) }
|
||||||
)
|
)
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
args.outputName = outputFile.get().absolutePath
|
args.outputName = outputFile.get().absolutePath
|
||||||
args.optimization = optimized
|
args.optimization = optimized
|
||||||
args.debug = debuggable
|
args.debug = debuggable
|
||||||
@@ -226,15 +226,15 @@ constructor(
|
|||||||
KotlinCommonCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args)
|
KotlinCommonCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args ->
|
classpath { args ->
|
||||||
args.pluginClasspaths = compilerPlugins.flatMap { classpath -> tryLenient { classpath.files } ?: emptySet() }.toPathsArray()
|
args.pluginClasspaths = compilerPlugins.flatMap { classpath -> runSafe { classpath.files } ?: emptySet() }.toPathsArray()
|
||||||
args.libraries = tryLenient { libraries.files.filterKlibsPassedToCompiler() }?.toPathsArray()
|
args.libraries = runSafe { libraries.files.filterKlibsPassedToCompiler() }?.toPathsArray()
|
||||||
args.exportedLibraries = tryLenient { exportLibraries.files.filterKlibsPassedToCompiler() }?.toPathsArray()
|
args.exportedLibraries = runSafe { exportLibraries.files.filterKlibsPassedToCompiler() }?.toPathsArray()
|
||||||
args.friendModules = tryLenient { friendModule.files.toList().takeIf { it.isNotEmpty() } }
|
args.friendModules = runSafe { friendModule.files.toList().takeIf { it.isNotEmpty() } }
|
||||||
?.joinToString(File.pathSeparator) { it.absolutePath }
|
?.joinToString(File.pathSeparator) { it.absolutePath }
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args ->
|
sources { args ->
|
||||||
args.includes = sources.asFileTree.files.toPathsArray()
|
args.includes = sources.asFileTree.files.toPathsArray()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -428,7 +428,7 @@ internal constructor(
|
|||||||
kotlinPluginData?.orNull?.let { CompilerPluginData(it.classpath, it.options) }
|
kotlinPluginData?.orNull?.let { CompilerPluginData(it.classpath, it.options) }
|
||||||
)
|
)
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
args.moduleName = compilerOptions.moduleName.get()
|
args.moduleName = compilerOptions.moduleName.get()
|
||||||
args.shortModuleName = shortModuleName
|
args.shortModuleName = shortModuleName
|
||||||
args.multiPlatform = true
|
args.multiPlatform = true
|
||||||
@@ -454,19 +454,19 @@ internal constructor(
|
|||||||
KotlinNativeCompilerOptionsHelper.fillCompilerArguments(compilerOptions, args)
|
KotlinNativeCompilerOptionsHelper.fillCompilerArguments(compilerOptions, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args ->
|
classpath { args ->
|
||||||
args.pluginClasspaths = compilerPlugins.flatMap { classpath -> tryLenient { classpath.files } ?: emptySet() }.toPathsArray()
|
args.pluginClasspaths = compilerPlugins.flatMap { classpath -> runSafe { classpath.files } ?: emptySet() }.toPathsArray()
|
||||||
|
|
||||||
args.libraries = tryLenient { libraries.files.filterKlibsPassedToCompiler().toPathsArray() }
|
args.libraries = runSafe { libraries.files.filterKlibsPassedToCompiler().toPathsArray() }
|
||||||
args.friendModules = tryLenient {
|
args.friendModules = runSafe {
|
||||||
friendModule.files.takeIf { it.isNotEmpty() }?.map { it.absolutePath }?.joinToString(File.pathSeparator)
|
friendModule.files.takeIf { it.isNotEmpty() }?.map { it.absolutePath }?.joinToString(File.pathSeparator)
|
||||||
}
|
}
|
||||||
args.refinesPaths = tryLenient {
|
args.refinesPaths = runSafe {
|
||||||
sharedCompilationData?.refinesPaths?.files?.takeIf { it.isNotEmpty() }?.toPathsArray()
|
sharedCompilationData?.refinesPaths?.files?.takeIf { it.isNotEmpty() }?.toPathsArray()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args ->
|
sources { args ->
|
||||||
if (compilerOptions.usesK2.get()) {
|
if (compilerOptions.usesK2.get()) {
|
||||||
/*
|
/*
|
||||||
For now, we only pass multiplatform structure to K2 for platform compilations
|
For now, we only pass multiplatform structure to K2 for platform compilations
|
||||||
|
|||||||
+5
-5
@@ -132,7 +132,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
|
|||||||
K2JSCompilerArguments()
|
K2JSCompilerArguments()
|
||||||
|
|
||||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JSCompilerArguments> {
|
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JSCompilerArguments> {
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
args.multiPlatform = multiPlatformEnabled.get()
|
args.multiPlatform = multiPlatformEnabled.get()
|
||||||
|
|
||||||
args.pluginOptions = (pluginOptions.toSingleCompilerPluginOptions() + kotlinPluginData?.orNull?.options)
|
args.pluginOptions = (pluginOptions.toSingleCompilerPluginOptions() + kotlinPluginData?.orNull?.options)
|
||||||
@@ -172,8 +172,8 @@ abstract class Kotlin2JsCompile @Inject constructor(
|
|||||||
args.freeArgs = executionTimeFreeCompilerArgs ?: enhancedFreeCompilerArgs.get()
|
args.freeArgs = executionTimeFreeCompilerArgs ?: enhancedFreeCompilerArgs.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args ->
|
classpath { args ->
|
||||||
args.pluginClasspaths = tryLenient {
|
args.pluginClasspaths = runSafe {
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
||||||
).reduce(FileCollection::plus).toPathsArray()
|
).reduce(FileCollection::plus).toPathsArray()
|
||||||
@@ -181,7 +181,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
|
|||||||
|
|
||||||
args.friendModules = friendDependencies.files.joinToString(File.pathSeparator) { it.absolutePath }
|
args.friendModules = friendDependencies.files.joinToString(File.pathSeparator) { it.absolutePath }
|
||||||
|
|
||||||
args.libraries = tryLenient {
|
args.libraries = runSafe {
|
||||||
libraries
|
libraries
|
||||||
.filter { it.exists() && libraryFilter(it) }
|
.filter { it.exists() && libraryFilter(it) }
|
||||||
.map { it.normalize().absolutePath }
|
.map { it.normalize().absolutePath }
|
||||||
@@ -191,7 +191,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args ->
|
sources { args ->
|
||||||
if (!args.sourceMapPrefix.isNullOrEmpty()) {
|
if (!args.sourceMapPrefix.isNullOrEmpty()) {
|
||||||
args.sourceMapBaseDirs = sourceMapBaseDir.get().asFile.absolutePath
|
args.sourceMapBaseDirs = sourceMapBaseDir.get().asFile.absolutePath
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -220,7 +220,7 @@ abstract class KotlinCompile @Inject constructor(
|
|||||||
override fun createCompilerArguments(
|
override fun createCompilerArguments(
|
||||||
context: KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext
|
context: KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext
|
||||||
): K2JVMCompilerArguments = context.create<K2JVMCompilerArguments> {
|
): K2JVMCompilerArguments = context.create<K2JVMCompilerArguments> {
|
||||||
contribute(ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
args.allowNoSourceFiles = true
|
args.allowNoSourceFiles = true
|
||||||
|
|
||||||
args.multiPlatform = multiPlatformEnabled.get()
|
args.multiPlatform = multiPlatformEnabled.get()
|
||||||
@@ -252,20 +252,20 @@ abstract class KotlinCompile @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(ArgumentType.Classpath) { args ->
|
classpath { args ->
|
||||||
args.pluginClasspaths = tryLenient {
|
args.pluginClasspaths = runSafe {
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
||||||
).reduce(FileCollection::plus).toPathsArray()
|
).reduce(FileCollection::plus).toPathsArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
args.friendPaths = friendPaths.toPathsArray()
|
args.friendPaths = friendPaths.toPathsArray()
|
||||||
args.classpathAsList = tryLenient {
|
args.classpathAsList = runSafe {
|
||||||
libraries.toList().filter { it.exists() }
|
libraries.toList().filter { it.exists() }
|
||||||
}.orEmpty()
|
}.orEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(ArgumentType.Sources) { args ->
|
sources { args ->
|
||||||
if (compilerOptions.usesK2.get()) {
|
if (compilerOptions.usesK2.get()) {
|
||||||
args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs
|
args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+6
-6
@@ -69,7 +69,7 @@ abstract class KotlinCompileCommon @Inject constructor(
|
|||||||
internal var executionTimeFreeCompilerArgs: List<String>? = null
|
internal var executionTimeFreeCompilerArgs: List<String>? = null
|
||||||
|
|
||||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2MetadataCompilerArguments> {
|
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2MetadataCompilerArguments> {
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
args.multiPlatform = multiPlatformEnabled.get()
|
args.multiPlatform = multiPlatformEnabled.get()
|
||||||
|
|
||||||
args.moduleName = this@KotlinCompileCommon.moduleName.get()
|
args.moduleName = this@KotlinCompileCommon.moduleName.get()
|
||||||
@@ -93,18 +93,18 @@ abstract class KotlinCompileCommon @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Classpath) { args ->
|
classpath { args ->
|
||||||
args.pluginClasspaths = tryLenient {
|
args.pluginClasspaths = runSafe {
|
||||||
listOfNotNull(
|
listOfNotNull(
|
||||||
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
pluginClasspath, kotlinPluginData?.orNull?.classpath
|
||||||
).reduce(FileCollection::plus).toPathsArray()
|
).reduce(FileCollection::plus).toPathsArray()
|
||||||
}
|
}
|
||||||
args.classpath = tryLenient { libraries.files.filter { it.exists() }.joinToString(File.pathSeparator) }
|
args.classpath = runSafe { libraries.files.filter { it.exists() }.joinToString(File.pathSeparator) }
|
||||||
args.friendPaths = tryLenient { this@KotlinCompileCommon.friendPaths.files.toPathsArray() }
|
args.friendPaths = runSafe { this@KotlinCompileCommon.friendPaths.files.toPathsArray() }
|
||||||
args.refinesPaths = refinesMetadataPaths.toPathsArray()
|
args.refinesPaths = refinesMetadataPaths.toPathsArray()
|
||||||
}
|
}
|
||||||
|
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Sources) { args ->
|
sources { args ->
|
||||||
args.freeArgs += sources.asFileTree.map { it.absolutePath }
|
args.freeArgs += sources.asFileTree.map { it.absolutePath }
|
||||||
args.commonSources = commonSourceSet.asFileTree.toPathsArray()
|
args.commonSources = commonSourceSet.asFileTree.toPathsArray()
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -59,7 +59,7 @@ abstract class KotlinJsDce @Inject constructor(
|
|||||||
override val toolOptions: KotlinJsDceCompilerToolOptions = objectFactory.newInstance<KotlinJsDceCompilerToolOptionsDefault>()
|
override val toolOptions: KotlinJsDceCompilerToolOptions = objectFactory.newInstance<KotlinJsDceCompilerToolOptionsDefault>()
|
||||||
|
|
||||||
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JSDceArguments> {
|
override fun createCompilerArguments(context: CreateCompilerArgumentsContext) = context.create<K2JSDceArguments> {
|
||||||
contribute(KotlinCompilerArgumentsProducer.ArgumentType.Primitive) { args ->
|
primitive { args ->
|
||||||
KotlinJsDceCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args)
|
KotlinJsDceCompilerToolOptionsHelper.fillCompilerArguments(toolOptions, args)
|
||||||
args.declarationsToKeep = keep.toTypedArray()
|
args.declarationsToKeep = keep.toTypedArray()
|
||||||
args.outputDirectory = destinationDirectory.get().asFile.path
|
args.outputDirectory = destinationDirectory.get().asFile.path
|
||||||
|
|||||||
Reference in New Issue
Block a user