[Gradle] Add compatibility fix to explicit api import in MPP projects

MPP projects import relies that `-Xexplicit-api` flag is present in
DefaultLanguageSettingsBuilder.freeCompilerArgs property to enable
related inspections.

^KT-59063 Fixed
This commit is contained in:
Yahor Berdnikau
2023-06-02 14:57:33 +02:00
committed by Space Team
parent 58dfdd2f77
commit b2212b9275
4 changed files with 37 additions and 5 deletions
@@ -14,6 +14,7 @@ import org.gradle.api.tasks.TaskProvider
import org.gradle.jvm.tasks.Jar
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.explicitApiModeAsCompilerArg
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.internal.customizeKotlinDependencies
@@ -319,9 +320,28 @@ internal fun sourcesJarTaskNamed(
}
internal fun Project.setupGeneralKotlinExtensionParameters() {
val sourceSetsInMainCompilation by lazy {
kotlinExtension.sourceSets.filter { sourceSet ->
sourceSet.internal.compilations.any {
// kotlin main compilation
it.isMain()
// android compilation which is NOT in tested variant
|| (it as? KotlinJvmAndroidCompilation)?.let { getTestedVariantData(it.androidVariant) == null } == true
}
}
}
kotlinExtension.sourceSets.all { sourceSet ->
(sourceSet.languageSettings as? DefaultLanguageSettingsBuilder)?.run {
explicitApi = project.providers.provider {
val explicitApiFlag = project.kotlinExtension.explicitApiModeAsCompilerArg()
if (explicitApiFlag != null && sourceSet in sourceSetsInMainCompilation) {
explicitApiFlag
} else {
null
}
}
// Set ad-hoc free compiler args from the internal project property
freeCompilerArgsProvider = project.provider {
val propertyValue = with(project.extensions.extraProperties) {
@@ -11,8 +11,10 @@ import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.dsl.toCompilerValue
import org.jetbrains.kotlin.gradle.plugin.LanguageSettingsBuilder
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompileTool
@@ -99,8 +101,18 @@ internal class DefaultLanguageSettingsBuilder : LanguageSettingsBuilder {
var freeCompilerArgsProvider: Provider<List<String>>? = null
val freeCompilerArgs: List<String>
// Kept here for compatibility with IDEA Kotlin import. It relies on explicit api argument in `freeCompilerArgs` to enable related
// inspections
internal var explicitApi: Provider<String>? = null
internal val freeCompilerArgsForNonImport: List<String>
get() = freeCompilerArgsProvider?.get().orEmpty()
val freeCompilerArgs: List<String>
get() = freeCompilerArgsProvider?.get()
.orEmpty()
.plus(explicitApi?.orNull)
.filterNotNull()
}
internal fun applyLanguageSettingsToCompilerOptions(
@@ -124,7 +136,7 @@ internal fun applyLanguageSettingsToCompilerOptions(
languageSettingsBuilder.enabledLanguageFeatures.forEach { featureName ->
freeArgs.add("-XXLanguage:+$featureName")
}
freeArgs.addAll(languageSettingsBuilderDefault.freeCompilerArgs)
freeArgs.addAll(languageSettingsBuilderDefault.freeCompilerArgsForNonImport)
if (freeArgs.isNotEmpty()) {
if (addFreeCompilerArgsAsConvention) {
@@ -93,9 +93,9 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
tasks.named(binary.linkTaskName, KotlinNativeLink::class.java).configure {
// We propagate compilation free args to the link task for now (see KT-33717).
val defaultLanguageSettings = binary.compilation.defaultSourceSet.languageSettings as? DefaultLanguageSettingsBuilder
if (defaultLanguageSettings != null && defaultLanguageSettings.freeCompilerArgs.isNotEmpty()) {
if (defaultLanguageSettings != null && defaultLanguageSettings.freeCompilerArgsForNonImport.isNotEmpty()) {
it.toolOptions.freeCompilerArgs.addAll(
defaultLanguageSettings.freeCompilerArgs
defaultLanguageSettings.freeCompilerArgsForNonImport
)
}
}
@@ -416,7 +416,7 @@ internal constructor(
override val additionalCompilerOptions: Provider<Collection<String>>
get() = compilerOptions
.freeCompilerArgs
.map { it + (languageSettings as DefaultLanguageSettingsBuilder).freeCompilerArgs }
.map { it + (languageSettings as DefaultLanguageSettingsBuilder).freeCompilerArgsForNonImport }
private val runnerSettings = KotlinNativeCompilerRunner.Settings.fromProject(project)
private val isAllowCommonizer: Boolean by lazy { project.isAllowCommonizer() }