From 0a3eb6893437149fc0518ac521d9f3a87723fa96 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Tue, 21 Mar 2023 09:49:30 +0100 Subject: [PATCH] [Gradle] Implement IdeCompilerArgumentsResolver service KTIJ-24976 --- .../ide/IdeCompilerArgumentsResolver.kt | 31 ++++++++++++ .../ide/IdeCompilerArgumentsResolverImpl.kt | 48 +++++++++++++++++++ .../gradle/utils/androidExtensionUitls.kt | 4 +- 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolver.kt create mode 100644 libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolverImpl.kt diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolver.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolver.kt new file mode 100644 index 00000000000..164e0efb0ff --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolver.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.gradle.plugin.ide + +import org.gradle.api.Project +import org.gradle.api.logging.Logger +import org.gradle.api.logging.Logging +import org.jetbrains.kotlin.gradle.dsl.kotlinExtension +import org.jetbrains.kotlin.gradle.plugin.extraProperties +import org.jetbrains.kotlin.gradle.utils.getOrPut + +interface IdeCompilerArgumentsResolver { + fun resolveCompilerArguments(any: Any): List? + + companion object { + internal val logger: Logger = Logging.getLogger(IdeMultiplatformImport::class.java) + + @JvmStatic + fun instance(project: Project): IdeCompilerArgumentsResolver { + return project.extraProperties.getOrPut(IdeCompilerArgumentsResolver::class.java.name) { + IdeCompilerArgumentsResolverImpl(project.kotlinExtension) + } + } + } +} + +internal val Project.kotlinIdeCompilerArgumentsResolver: IdeCompilerArgumentsResolver get() = IdeCompilerArgumentsResolver.instance(project) + diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolverImpl.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolverImpl.kt new file mode 100644 index 00000000000..42375da260d --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/ide/IdeCompilerArgumentsResolverImpl.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.gradle.plugin.ide + +import org.jetbrains.kotlin.compilerRunner.ArgumentUtils +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension +import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension +import org.jetbrains.kotlin.gradle.plugin.CreateCompilerArgumentsContext +import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation +import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.utils.hasAndroidPlugin + +internal class IdeCompilerArgumentsResolverImpl( + private val extension: KotlinProjectExtension +) : IdeCompilerArgumentsResolver { + + override fun resolveCompilerArguments(any: Any): List? { + if (any is KotlinCompilerArgumentsProducer) { + return resolveCompilerArguments(any) + } + + if (any is KotlinCompilation<*>) { + return resolveCompilerArguments(any.compileTaskProvider.orNull ?: return null) + } + + return null + } + + private fun resolveCompilerArguments(producer: KotlinCompilerArgumentsProducer): List { + val compilerArguments = producer.createCompilerArguments( + CreateCompilerArgumentsContext( + isLenient = true, + includeArgumentTypes = setOfNotNull( + KotlinCompilerArgumentsProducer.ArgumentType.Primitive, + KotlinCompilerArgumentsProducer.ArgumentType.DependencyClasspath + .takeIf { extension !is KotlinMultiplatformExtension } + .takeIf { producer is KotlinCompile } + .takeIf { !extension.project.hasAndroidPlugin } + ), + ) + ) + return ArgumentUtils.convertArgumentsToStringList(compilerArguments) + } +} \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/utils/androidExtensionUitls.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/utils/androidExtensionUitls.kt index a797643cfa0..49db06c11f6 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/utils/androidExtensionUitls.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/utils/androidExtensionUitls.kt @@ -10,4 +10,6 @@ import org.gradle.api.Project internal val Project.androidExtensionOrNull: BaseExtension? get() = extensions.findByName("android")?.let { it as? BaseExtension } -internal val Project.androidExtension: BaseExtension get() = extensions.getByName("android") as BaseExtension \ No newline at end of file +internal val Project.androidExtension: BaseExtension get() = extensions.getByName("android") as BaseExtension + +internal val Project.hasAndroidPlugin : Boolean get() = extensions.findByName("android") != null \ No newline at end of file