From 2d2d2b6ce80298d7e229cc36ce555a7c9c94fed5 Mon Sep 17 00:00:00 2001 From: thomas Date: Thu, 8 Aug 2024 16:40:41 +0200 Subject: [PATCH] CMP-5906 Add option to enable Class data sharing (CDS) --- .../dsl/JvmApplicationDistributions.kt | 2 ++ .../internal/configureJvmApplication.kt | 1 + .../application/tasks/AbstractJLinkTask.kt | 29 +++++++++++++++++-- .../README.md | 15 ++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/JvmApplicationDistributions.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/JvmApplicationDistributions.kt index f59b838b6a0..e063a8b39ed 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/JvmApplicationDistributions.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/dsl/JvmApplicationDistributions.kt @@ -19,6 +19,8 @@ abstract class JvmApplicationDistributions : AbstractDistributions() { } var includeAllModules: Boolean = false + var cds: Boolean = false + val linux: LinuxPlatformSettings = objects.newInstance(LinuxPlatformSettings::class.java) open fun linux(fn: Action) { fn.execute(linux) diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureJvmApplication.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureJvmApplication.kt index 83d51089ef6..784bbde26ab 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureJvmApplication.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/configureJvmApplication.kt @@ -110,6 +110,7 @@ private fun JvmApplicationContext.configureCommonJvmDesktopTasks(): CommonJvmDes modules.set(provider { app.nativeDistributions.modules }) includeAllModules.set(provider { app.nativeDistributions.includeAllModules }) javaRuntimePropertiesFile.set(checkRuntime.flatMap { it.javaRuntimePropertiesFile }) + cds.set(provider { app.nativeDistributions.cds }) destinationDir.set(appTmpDir.dir("runtime")) } diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJLinkTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJLinkTask.kt index b44237ef7b7..6dbb3be8b45 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJLinkTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractJLinkTask.kt @@ -11,8 +11,9 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Optional -import org.jetbrains.compose.desktop.application.internal.RuntimeCompressionLevel +import org.gradle.process.ExecResult import org.jetbrains.compose.desktop.application.internal.JvmRuntimeProperties +import org.jetbrains.compose.desktop.application.internal.RuntimeCompressionLevel import org.jetbrains.compose.desktop.application.internal.cliArg import org.jetbrains.compose.internal.utils.ioFile import org.jetbrains.compose.internal.utils.notNullProperty @@ -31,6 +32,9 @@ abstract class AbstractJLinkTask : AbstractJvmToolOperationTask("jlink") { @get:InputFile val javaRuntimePropertiesFile: RegularFileProperty = objects.fileProperty() + @get:Input + val cds: Property = objects.notNullProperty(false) + @get:Input internal val stripDebug: Property = objects.notNullProperty(true) @@ -56,12 +60,33 @@ abstract class AbstractJLinkTask : AbstractJvmToolOperationTask("jlink") { cliArg("--add-modules", m) } + val cds = cds.get() + + if (cds) { + cliArg("--generate-cds-archive", true) + } cliArg("--strip-debug", stripDebug) cliArg("--no-header-files", noHeaderFiles) cliArg("--no-man-pages", noManPages) - cliArg("--strip-native-commands", stripNativeCommands) + // Native commands cannot be stripped if CDS is enabled, because bin/java is used by the + // CDS option. The files will be stripped later. + if (!cds) { + cliArg("--strip-native-commands", stripNativeCommands) + } cliArg("--compress", compressionLevel.orNull?.id) cliArg("--output", destinationDir) } + + override fun checkResult(result: ExecResult) { + super.checkResult(result) + if (stripNativeCommands.get() && cds.get()) { + // Native files were not removed yet, so do it here. + destinationDir.get().dir("bin").asFile + .walkBottomUp() + // Windows JVM has its .ddl files in bin/ + .filter { it.isFile && it.extension != "dll" } + .forEach { it.delete() } + } + } } \ No newline at end of file diff --git a/tutorials/Native_distributions_and_local_execution/README.md b/tutorials/Native_distributions_and_local_execution/README.md index ffc91c4904d..e0fa7566b1a 100755 --- a/tutorials/Native_distributions_and_local_execution/README.md +++ b/tutorials/Native_distributions_and_local_execution/README.md @@ -280,6 +280,21 @@ compose.desktop { } ``` +## Enabling Class data sharing (CDS) + +This option requires JDK 18. Class data sharing (CDS) can be enabled which helps reduce the startup +time and memory footprint. + +``` kotlin +compose.desktop { + application { + nativeDistributions { + cds = true + } + } +} +``` + ## Packaging resources There are multiple ways to package and load resources with Compose for Desktop.