[Gradle] Implement SyncLanguageSettingsWithKotlinExtensionSetupAction

KT-61634
This commit is contained in:
Sebastian Sellmair
2023-10-16 19:02:36 +02:00
committed by Space Team
parent 58385ef64f
commit 8ca1c06cf2
5 changed files with 54 additions and 43 deletions
@@ -155,7 +155,6 @@ internal abstract class AbstractKotlinPlugin(
fun configureProjectGlobalSettings(project: Project) {
customizeKotlinDependencies(project)
project.setupGeneralKotlinExtensionParameters()
}
fun configureTarget(
@@ -61,7 +61,6 @@ class KotlinMultiplatformPlugin : Plugin<Project> {
// propagate compiler plugin options to the source set language settings
setupAdditionalCompilerArguments(project)
project.setupGeneralKotlinExtensionParameters()
}
@@ -164,39 +163,3 @@ class KotlinMultiplatformPlugin : Plugin<Project> {
"kotlin.mpp.freeCompilerArgsForSourceSet.$sourceSetName"
}
}
internal fun Project.setupGeneralKotlinExtensionParameters() {
project.launch {
for (sourceSet in kotlinExtension.awaitSourceSets()) {
val languageSettings = sourceSet.languageSettings
if (languageSettings !is DefaultLanguageSettingsBuilder) continue
val isMainSourceSet = sourceSet
.internal
.awaitPlatformCompilations()
.any { KotlinSourceSetTree.orNull(it) == KotlinSourceSetTree.main }
if (isMainSourceSet) {
languageSettings.explicitApi = project.providers.provider {
project.kotlinExtension.explicitApiModeAsCompilerArg()
}
}
languageSettings.freeCompilerArgsProvider = project.provider {
val propertyValue = with(project.extensions.extraProperties) {
val sourceSetFreeCompilerArgsPropertyName = sourceSetFreeCompilerArgsPropertyName(sourceSet.name)
if (has(sourceSetFreeCompilerArgsPropertyName)) {
get(sourceSetFreeCompilerArgsPropertyName)
} else null
}
mutableListOf<String>().apply {
when (propertyValue) {
is String -> add(propertyValue)
is Iterable<*> -> addAll(propertyValue.map { it.toString() })
}
}
}
}
}
}
@@ -0,0 +1,49 @@
/*
* 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.mpp
import org.jetbrains.kotlin.gradle.dsl.explicitApiModeAsCompilerArg
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinProjectSetupCoroutine
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree
import org.jetbrains.kotlin.gradle.plugin.hierarchy.orNull
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
import org.jetbrains.kotlin.gradle.plugin.sources.awaitPlatformCompilations
import org.jetbrains.kotlin.gradle.plugin.sources.internal
internal val SyncLanguageSettingsWithKotlinExtensionSetupAction = KotlinProjectSetupCoroutine {
for (sourceSet in kotlinExtension.awaitSourceSets()) {
val languageSettings = sourceSet.languageSettings
if (languageSettings !is DefaultLanguageSettingsBuilder) continue
val isMainSourceSet = sourceSet
.internal
.awaitPlatformCompilations()
.any { KotlinSourceSetTree.orNull(it) == KotlinSourceSetTree.main }
if (isMainSourceSet) {
languageSettings.explicitApi = project.providers.provider {
project.kotlinExtension.explicitApiModeAsCompilerArg()
}
}
languageSettings.freeCompilerArgsProvider = project.provider {
val propertyValue = with(project.extensions.extraProperties) {
val sourceSetFreeCompilerArgsPropertyName = KotlinMultiplatformPlugin.sourceSetFreeCompilerArgsPropertyName(sourceSet.name)
if (has(sourceSetFreeCompilerArgsPropertyName)) {
get(sourceSetFreeCompilerArgsPropertyName)
} else null
}
mutableListOf<String>().apply {
when (propertyValue) {
is String -> add(propertyValue)
is Iterable<*> -> addAll(propertyValue.map { it.toString() })
}
}
}
}
}
@@ -32,8 +32,8 @@ import org.jetbrains.kotlin.gradle.plugin.diagnostics.checkers.WasmSourceSetsNot
import org.jetbrains.kotlin.gradle.plugin.ide.IdeMultiplatformImportSetupAction
import org.jetbrains.kotlin.gradle.plugin.ide.IdeResolveDependenciesTaskSetupAction
import org.jetbrains.kotlin.gradle.plugin.mpp.GlobalProjectStructureMetadataStorageSetupAction
import org.jetbrains.kotlin.gradle.plugin.mpp.SyncLanguageSettingsWithKotlinExtensionSetupAction
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.AddBuildListenerForXCodeSetupAction
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubpluginSetupAction
import org.jetbrains.kotlin.gradle.targets.js.npm.AddNpmDependencyExtensionProjectSetupAction
import org.jetbrains.kotlin.gradle.targets.native.CreateFatFrameworksSetupAction
@@ -48,6 +48,7 @@ internal fun Project.registerKotlinPluginExtensions() {
register(project, AddNpmDependencyExtensionProjectSetupAction)
register(project, RegisterBuildKotlinToolingMetadataTask)
register(project, KotlinToolingDiagnosticsSetupAction)
register(project, SyncLanguageSettingsWithKotlinExtensionSetupAction)
if (isJvm || isMultiplatform) {
register(project, ScriptingGradleSubpluginSetupAction)
@@ -94,4 +95,6 @@ internal fun Project.registerKotlinPluginExtensions() {
private val Project.isMultiplatform get() = multiplatformExtensionOrNull != null
private val Project.isJvm get() = kotlinJvmExtensionOrNull != null
private val Project.isJvm get() = kotlinJvmExtensionOrNull != null
private val Project.isJs get() = kotlinExtensionOrNull is KotlinJsProjectExtension
@@ -15,15 +15,12 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.MAIN_COMPI
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.TEST_COMPILATION_NAME
import org.jetbrains.kotlin.gradle.plugin.mpp.compilerOptions
import org.jetbrains.kotlin.gradle.plugin.mpp.internal
import org.jetbrains.kotlin.gradle.plugin.mpp.setupGeneralKotlinExtensionParameters
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrSingleTargetPreset
import org.jetbrains.kotlin.gradle.utils.*
open class KotlinJsPlugin: Plugin<Project> {
override fun apply(project: Project) {
project.setupGeneralKotlinExtensionParameters()
// TODO get rid of this plugin, too? Use the 'base' plugin instead?
// in fact, the attributes schema of the Java base plugin may be required to consume non-MPP Kotlin/JS libs,
// so investigation is needed