Browse Source
Sometimes we need to report warnings during the configuration phase. For example, when Androidx Compose Compiler is used with non-JVM targets (e.g. iOS/js), we want to warn users that using non-JB compiler with non-JVM targets is not supported. The default way of reporting warnings in Gradle is using a logger. For example we could write something like: ``` abstract class ComposePlugin : Plugin<Project> { override fun apply(project: Project) { if (project.hasNonJvmTargets() && project.usesNonJBComposeCompiler()) { project.logger.warn("...") } } } ``` This approach has a few issues: 1. When the Configuration Cache is enabled, project's configuration might get skipped altogether, and the warning won't be printed. 2. If a project contains multiple Gradle modules (subprojects), the warning might be printed multiple times. That might be OK for some warnings. But repeating exactly the same warning 10s or 100s is unnecessary. The only way to share the state between Gradle modules, while preserving compatibility with the Configuration Cache, is to define Gradle Build Service. In 1.5.0 we used Gradle Build Service mechanism for both warnings. However, I did not know that: * only the service's parameters are persisted in the Configuration Cache. The service itself is not persisted. * if a service instance is materialized during the configuration phase, then all changes made to its parameters will not be visible to that particular instance (but they will be visible to the next instance). So the only way to report diagnostics with configuration cache without repetition is to define a service that is not materialized during the configuration phase (i.e. serviceProvider.get() is not called), add to add warnings to a set property of the service. This change implements that. Resolves #3595pull/3628/head
Alexey Tsvetkov
1 year ago
committed by
GitHub
16 changed files with 332 additions and 167 deletions
@ -1,119 +0,0 @@ |
|||||||
package org.jetbrains.compose |
|
||||||
|
|
||||||
import org.gradle.api.Project |
|
||||||
import org.gradle.api.logging.Logging |
|
||||||
import org.gradle.api.provider.MapProperty |
|
||||||
import org.gradle.api.provider.Provider |
|
||||||
import org.gradle.api.provider.SetProperty |
|
||||||
import org.gradle.api.services.BuildService |
|
||||||
import org.gradle.api.services.BuildServiceParameters |
|
||||||
import org.gradle.tooling.events.FinishEvent |
|
||||||
import org.gradle.tooling.events.OperationCompletionListener |
|
||||||
import org.jetbrains.compose.experimental.internal.SUPPORTED_NATIVE_CACHE_KIND_PROPERTIES |
|
||||||
import org.jetbrains.compose.internal.utils.BuildEventsListenerRegistryProvider |
|
||||||
import org.jetbrains.compose.internal.utils.loadProperties |
|
||||||
import org.jetbrains.compose.internal.utils.localPropertiesFile |
|
||||||
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact |
|
||||||
|
|
||||||
// The service implements OperationCompletionListener just so Gradle would use the service |
|
||||||
// even if the service is not used by any task or transformation |
|
||||||
abstract class ComposeMultiplatformBuildService : BuildService<ComposeMultiplatformBuildService.Parameters>, |
|
||||||
OperationCompletionListener, AutoCloseable { |
|
||||||
|
|
||||||
abstract class Parameters : BuildServiceParameters { |
|
||||||
abstract val gradlePropertiesCacheKindSnapshot: MapProperty<String, String> |
|
||||||
abstract val localPropertiesCacheKindSnapshot: MapProperty<String, String> |
|
||||||
} |
|
||||||
|
|
||||||
private val log = Logging.getLogger(this.javaClass) |
|
||||||
|
|
||||||
internal abstract val unsupportedCompilerPlugins: SetProperty<Provider<SubpluginArtifact?>> |
|
||||||
internal abstract val delayedWarnings: SetProperty<String> |
|
||||||
internal val gradlePropertiesSnapshot: Map<String, String> = parameters.gradlePropertiesCacheKindSnapshot.get() |
|
||||||
internal val localPropertiesSnapshot: Map<String, String> = parameters.localPropertiesCacheKindSnapshot.get() |
|
||||||
|
|
||||||
fun warnOnceAfterBuild(message: String) { |
|
||||||
delayedWarnings.add(message) |
|
||||||
} |
|
||||||
|
|
||||||
override fun close() { |
|
||||||
notifyAboutUnsupportedCompilerPlugin() |
|
||||||
logDelayedWarnings() |
|
||||||
} |
|
||||||
|
|
||||||
private fun notifyAboutUnsupportedCompilerPlugin() { |
|
||||||
val unsupportedCompilerPlugin = unsupportedCompilerPlugins.orNull |
|
||||||
?.firstOrNull() |
|
||||||
?.orNull |
|
||||||
|
|
||||||
if (unsupportedCompilerPlugin != null) { |
|
||||||
log.error(createWarningAboutNonCompatibleCompiler(unsupportedCompilerPlugin.groupId)) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
private fun logDelayedWarnings() { |
|
||||||
for (warning in delayedWarnings.get()) { |
|
||||||
log.warn(warning) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
override fun onFinish(event: FinishEvent) {} |
|
||||||
|
|
||||||
companion object { |
|
||||||
private val COMPOSE_SERVICE_FQ_NAME = ComposeMultiplatformBuildService::class.java.canonicalName |
|
||||||
|
|
||||||
private fun findExistingComposeService(project: Project): ComposeMultiplatformBuildService? { |
|
||||||
val registration = project.gradle.sharedServices.registrations.findByName(COMPOSE_SERVICE_FQ_NAME) |
|
||||||
val service = registration?.service?.orNull |
|
||||||
if (service != null) { |
|
||||||
if (service !is ComposeMultiplatformBuildService) { |
|
||||||
// Compose Gradle plugin was probably loaded more than once |
|
||||||
// See https://github.com/JetBrains/compose-multiplatform/issues/3459 |
|
||||||
if (service.javaClass.canonicalName == ComposeMultiplatformBuildService::class.java.canonicalName) { |
|
||||||
val rootScript = project.rootProject.buildFile |
|
||||||
error(""" |
|
||||||
Compose Multiplatform Gradle plugin has been loaded in multiple classloaders. |
|
||||||
To avoid classloading issues, declare Compose Gradle Plugin in root build file $rootScript. |
|
||||||
""".trimIndent()) |
|
||||||
} else { |
|
||||||
error("Shared build service '$COMPOSE_SERVICE_FQ_NAME' has unexpected type: ${service.javaClass.canonicalName}") |
|
||||||
} |
|
||||||
} |
|
||||||
return service |
|
||||||
} |
|
||||||
|
|
||||||
return null |
|
||||||
} |
|
||||||
|
|
||||||
fun getInstance(project: Project): ComposeMultiplatformBuildService = |
|
||||||
findExistingComposeService(project) ?: error("ComposeMultiplatformBuildService was not initialized!") |
|
||||||
|
|
||||||
@Suppress("UnstableApiUsage") |
|
||||||
fun init(project: Project) { |
|
||||||
val existingService = findExistingComposeService(project) |
|
||||||
if (existingService != null) { |
|
||||||
return |
|
||||||
} |
|
||||||
|
|
||||||
val newService = project.gradle.sharedServices.registerIfAbsent(COMPOSE_SERVICE_FQ_NAME, ComposeMultiplatformBuildService::class.java) { |
|
||||||
it.parameters.initPropertiesSnapshots(project.rootProject) |
|
||||||
} |
|
||||||
// workaround to instanciate a service even if it not binded to a task |
|
||||||
BuildEventsListenerRegistryProvider.getInstance(project).onTaskCompletion(newService) |
|
||||||
} |
|
||||||
|
|
||||||
private fun Parameters.initPropertiesSnapshots(rootProject: Project) { |
|
||||||
// we want to record original properties (explicitly set by a user) |
|
||||||
// before we possibly change them in configureNativeCompilerCaching.kt |
|
||||||
val localProperties = loadProperties(rootProject.localPropertiesFile) |
|
||||||
for (cacheKindProperty in SUPPORTED_NATIVE_CACHE_KIND_PROPERTIES) { |
|
||||||
rootProject.findProperty(cacheKindProperty)?.toString()?.let { value -> |
|
||||||
gradlePropertiesCacheKindSnapshot.put(cacheKindProperty, value) |
|
||||||
} |
|
||||||
localProperties[cacheKindProperty]?.toString()?.let { value -> |
|
||||||
localPropertiesCacheKindSnapshot.put(cacheKindProperty, value) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,81 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2023 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.internal.service |
||||||
|
|
||||||
|
import org.gradle.api.Project |
||||||
|
import org.gradle.api.services.BuildService |
||||||
|
import org.gradle.api.services.BuildServiceParameters |
||||||
|
import org.gradle.api.services.BuildServiceRegistration |
||||||
|
import org.gradle.tooling.events.FinishEvent |
||||||
|
import org.gradle.tooling.events.OperationCompletionListener |
||||||
|
|
||||||
|
// The service implements OperationCompletionListener just so Gradle would materialize the service even if the service is not used by any task or transformation |
||||||
|
abstract class AbstractComposeMultiplatformBuildService<P : BuildServiceParameters> : BuildService<P> , OperationCompletionListener, AutoCloseable { |
||||||
|
override fun onFinish(event: FinishEvent) {} |
||||||
|
override fun close() {} |
||||||
|
} |
||||||
|
|
||||||
|
internal inline fun <reified Service : BuildService<*>> serviceName(instance: Service? = null): String = |
||||||
|
fqName(instance) |
||||||
|
|
||||||
|
internal inline fun <reified Service : AbstractComposeMultiplatformBuildService<Params>, reified Params : BuildServiceParameters> registerServiceIfAbsent( |
||||||
|
project: Project, |
||||||
|
crossinline initParams: Params.() -> Unit = {} |
||||||
|
): BuildServiceRegistration<Service, Params> { |
||||||
|
if (findRegistration<Service, Params>(project) == null) { |
||||||
|
val newService = project.gradle.sharedServices.registerIfAbsent(fqName<Service>(), Service::class.java) { |
||||||
|
it.parameters.initParams() |
||||||
|
} |
||||||
|
// Workaround to materialize a service even if it is not bound to a task |
||||||
|
BuildEventsListenerRegistryProvider.getInstance(project).onTaskCompletion(newService) |
||||||
|
} |
||||||
|
|
||||||
|
return getExistingServiceRegistration(project) |
||||||
|
} |
||||||
|
|
||||||
|
internal inline fun <reified Service : BuildService<Params>, reified Params : BuildServiceParameters> getExistingServiceRegistration( |
||||||
|
project: Project |
||||||
|
): BuildServiceRegistration<Service, Params> { |
||||||
|
val registration = findRegistration<Service, Params>(project) |
||||||
|
?: error("Service '${serviceName<Service>()}' was not initialized") |
||||||
|
return registration.verified(project) |
||||||
|
} |
||||||
|
|
||||||
|
private inline fun <reified Service : BuildService<Params>, reified Params : BuildServiceParameters> BuildServiceRegistration<*, *>.verified( |
||||||
|
project: Project |
||||||
|
): BuildServiceRegistration<Service, Params> { |
||||||
|
val parameters = parameters |
||||||
|
// We are checking the type of parameters instead of the type of service |
||||||
|
// to avoid materializing the service. |
||||||
|
// After a service instance is created all changes made to its parameters won't be visible to |
||||||
|
// that particular service instance. |
||||||
|
// This is undesirable in some cases. For example, when reporting configuration problems, |
||||||
|
// we want to collect all configuration issues from all projects first, then report issues all at once |
||||||
|
// in execution phase. |
||||||
|
if (parameters !is Params) { |
||||||
|
// Compose Gradle plugin was probably loaded more than once |
||||||
|
// See https://github.com/JetBrains/compose-multiplatform/issues/3459 |
||||||
|
if (fqName(parameters) == fqName<Params>()) { |
||||||
|
val rootScript = project.rootProject.buildFile |
||||||
|
error(""" |
||||||
|
Compose Multiplatform Gradle plugin has been loaded in multiple classloaders. |
||||||
|
To avoid classloading issues, declare Compose Gradle Plugin in root build file $rootScript. |
||||||
|
""".trimIndent()) |
||||||
|
} else { |
||||||
|
error("Shared build service '${serviceName<Service>()}' parameters have unexpected type: ${fqName(parameters)}") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Suppress("UNCHECKED_CAST") |
||||||
|
return this as BuildServiceRegistration<Service, Params> |
||||||
|
} |
||||||
|
|
||||||
|
private inline fun <reified S : BuildService<P>, reified P : BuildServiceParameters> findRegistration( |
||||||
|
project: Project |
||||||
|
): BuildServiceRegistration<*, *>? = |
||||||
|
project.gradle.sharedServices.registrations.findByName(fqName<S>()) |
||||||
|
|
||||||
|
private inline fun <reified T : Any> fqName(instance: T? = null) = T::class.java.canonicalName |
@ -0,0 +1,66 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2023 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.internal.service |
||||||
|
|
||||||
|
import org.gradle.api.Project |
||||||
|
import org.gradle.api.logging.Logging |
||||||
|
import org.gradle.api.provider.ListProperty |
||||||
|
import org.gradle.api.provider.Provider |
||||||
|
import org.gradle.api.provider.SetProperty |
||||||
|
import org.gradle.api.services.BuildServiceParameters |
||||||
|
import org.jetbrains.compose.createWarningAboutNonCompatibleCompiler |
||||||
|
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact |
||||||
|
|
||||||
|
abstract class ConfigurationProblemReporterService : AbstractComposeMultiplatformBuildService<ConfigurationProblemReporterService.Parameters>() { |
||||||
|
interface Parameters : BuildServiceParameters { |
||||||
|
val unsupportedPluginWarningProviders: ListProperty<Provider<String?>> |
||||||
|
val warnings: SetProperty<String> |
||||||
|
} |
||||||
|
|
||||||
|
private val log = Logging.getLogger(this.javaClass) |
||||||
|
|
||||||
|
override fun close() { |
||||||
|
warnAboutUnsupportedCompilerPlugin() |
||||||
|
logWarnings() |
||||||
|
} |
||||||
|
|
||||||
|
private fun warnAboutUnsupportedCompilerPlugin() { |
||||||
|
for (warningProvider in parameters.unsupportedPluginWarningProviders.get()) { |
||||||
|
val warning = warningProvider.orNull |
||||||
|
if (warning != null) { |
||||||
|
log.warn(warning) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private fun logWarnings() { |
||||||
|
for (warning in parameters.warnings.get()) { |
||||||
|
log.warn(warning) |
||||||
|
} |
||||||
|
} |
||||||
|
companion object { |
||||||
|
fun init(project: Project) { |
||||||
|
registerServiceIfAbsent<ConfigurationProblemReporterService, Parameters>(project) |
||||||
|
} |
||||||
|
|
||||||
|
private inline fun configureParameters(project: Project, fn: Parameters.() -> Unit) { |
||||||
|
getExistingServiceRegistration<ConfigurationProblemReporterService, Parameters>(project) |
||||||
|
.parameters.fn() |
||||||
|
} |
||||||
|
|
||||||
|
fun reportProblem(project: Project, message: String) { |
||||||
|
configureParameters(project) { warnings.add(message) } |
||||||
|
} |
||||||
|
|
||||||
|
fun registerUnsupportedPluginProvider(project: Project, unsupportedPlugin: Provider<SubpluginArtifact?>) { |
||||||
|
configureParameters(project) { |
||||||
|
unsupportedPluginWarningProviders.add(unsupportedPlugin.map { unsupportedCompiler -> |
||||||
|
unsupportedCompiler?.groupId?.let { createWarningAboutNonCompatibleCompiler(it) } |
||||||
|
}) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2020-2023 JetBrains s.r.o. and respective authors and developers. |
||||||
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.jetbrains.compose.internal.service |
||||||
|
|
||||||
|
import org.gradle.api.Project |
||||||
|
import org.gradle.api.provider.MapProperty |
||||||
|
import org.gradle.api.services.BuildServiceParameters |
||||||
|
import org.jetbrains.compose.experimental.internal.SUPPORTED_NATIVE_CACHE_KIND_PROPERTIES |
||||||
|
import org.jetbrains.compose.internal.utils.loadProperties |
||||||
|
import org.jetbrains.compose.internal.utils.localPropertiesFile |
||||||
|
|
||||||
|
internal abstract class GradlePropertySnapshotService : AbstractComposeMultiplatformBuildService<GradlePropertySnapshotService.Parameters>() { |
||||||
|
interface Parameters : BuildServiceParameters { |
||||||
|
val gradlePropertiesCacheKindSnapshot: MapProperty<String, String> |
||||||
|
val localPropertiesCacheKindSnapshot: MapProperty<String, String> |
||||||
|
} |
||||||
|
|
||||||
|
internal val gradleProperties: Map<String, String> = parameters.gradlePropertiesCacheKindSnapshot.get() |
||||||
|
internal val localProperties: Map<String, String> = parameters.localPropertiesCacheKindSnapshot.get() |
||||||
|
|
||||||
|
companion object { |
||||||
|
fun init(project: Project) { |
||||||
|
registerServiceIfAbsent<GradlePropertySnapshotService, Parameters>(project) { |
||||||
|
initParams(project) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fun getInstance(project: Project): GradlePropertySnapshotService = |
||||||
|
getExistingServiceRegistration<GradlePropertySnapshotService, Parameters>(project).service.get() |
||||||
|
|
||||||
|
private fun Parameters.initParams(project: Project) { |
||||||
|
// we want to record original properties (explicitly set by a user) |
||||||
|
// before we possibly change them in configureNativeCompilerCaching.kt |
||||||
|
val rootProject = project.rootProject |
||||||
|
val localProperties = loadProperties(rootProject.localPropertiesFile) |
||||||
|
for (cacheKindProperty in SUPPORTED_NATIVE_CACHE_KIND_PROPERTIES) { |
||||||
|
rootProject.findProperty(cacheKindProperty)?.toString()?.let { value -> |
||||||
|
gradlePropertiesCacheKindSnapshot.put(cacheKindProperty, value) |
||||||
|
} |
||||||
|
localProperties[cacheKindProperty]?.toString()?.let { value -> |
||||||
|
localPropertiesCacheKindSnapshot.put(cacheKindProperty, value) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,39 @@ |
|||||||
|
plugins { |
||||||
|
id "org.jetbrains.kotlin.multiplatform" |
||||||
|
id "org.jetbrains.compose" |
||||||
|
} |
||||||
|
|
||||||
|
repositories { |
||||||
|
google() |
||||||
|
jetbrainsCompose() |
||||||
|
} |
||||||
|
|
||||||
|
kotlin { |
||||||
|
def platforms = project.property("platforms").split(",") |
||||||
|
if (platforms.contains("jvm")) { |
||||||
|
jvm() |
||||||
|
} |
||||||
|
if (platforms.contains("js")) { |
||||||
|
js(IR) { |
||||||
|
browser() |
||||||
|
binaries.executable() |
||||||
|
} |
||||||
|
} |
||||||
|
if (platforms.contains("ios")) { |
||||||
|
ios() |
||||||
|
} |
||||||
|
|
||||||
|
sourceSets { |
||||||
|
commonMain { |
||||||
|
dependencies { |
||||||
|
implementation compose.runtime |
||||||
|
implementation compose.material |
||||||
|
implementation compose.foundation |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
compose { |
||||||
|
kotlinCompilerPlugin.set(COMPOSE_COMPILER_PLUGIN_PLACEHOLDER) |
||||||
|
} |
@ -0,0 +1,2 @@ |
|||||||
|
org.jetbrains.compose.experimental.jscanvas.enabled=true |
||||||
|
org.jetbrains.compose.experimental.uikit.enabled=true |
@ -0,0 +1,11 @@ |
|||||||
|
pluginManagement { |
||||||
|
plugins { |
||||||
|
id 'org.jetbrains.kotlin.multiplatform' version 'KOTLIN_VERSION_PLACEHOLDER' |
||||||
|
id 'org.jetbrains.compose' version 'COMPOSE_GRADLE_PLUGIN_VERSION_PLACEHOLDER' |
||||||
|
} |
||||||
|
repositories { |
||||||
|
mavenLocal() |
||||||
|
gradlePluginPortal() |
||||||
|
} |
||||||
|
} |
||||||
|
rootProject.name = "customCompilerUnsupportedPlatformsWarning" |
@ -0,0 +1,19 @@ |
|||||||
|
import androidx.compose.material.Button |
||||||
|
import androidx.compose.material.MaterialTheme |
||||||
|
import androidx.compose.material.Text |
||||||
|
import androidx.compose.runtime.Composable |
||||||
|
import androidx.compose.runtime.mutableStateOf |
||||||
|
import androidx.compose.runtime.remember |
||||||
|
|
||||||
|
@Composable |
||||||
|
fun App() { |
||||||
|
MaterialTheme { |
||||||
|
var message by remember { mutableStateOf("Press the button!") } |
||||||
|
|
||||||
|
Button( |
||||||
|
onClick = { message = "Welcome to Compose Multiplatform!" } |
||||||
|
) { |
||||||
|
Text(message) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue