Skip to content

Commit

Permalink
Add option to enable Class data sharing (CDS)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Vos committed May 28, 2022
1 parent bb32831 commit b43a7c0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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<LinuxPlatformSettings>) {
fn.execute(linux)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ internal fun Project.configurePackagingTasks(
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(project.layout.buildDirectory.dir("compose/tmp/${app.name}/runtime"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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.gradle.process.ExecResult
import org.jetbrains.compose.desktop.application.internal.RuntimeCompressionLevel
import org.jetbrains.compose.desktop.application.internal.*
import org.jetbrains.compose.desktop.application.internal.JavaRuntimeProperties
Expand All @@ -31,6 +32,9 @@ abstract class AbstractJLinkTask : AbstractJvmToolOperationTask("jlink") {
@get:InputFile
val javaRuntimePropertiesFile: RegularFileProperty = objects.fileProperty()

@get:Input
val cds: Property<Boolean> = objects.notNullProperty(false)

@get:Input
internal val stripDebug: Property<Boolean> = objects.notNullProperty(true)

Expand All @@ -56,12 +60,30 @@ abstract class AbstractJLinkTask : AbstractJvmToolOperationTask("jlink") {
cliArg("--add-modules", m)
}

val cds = cds.get()

cliArg("--generate-cds-archive", cds)
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.
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().asFile.walk().forEach { file ->
if (file.isDirectory && file.name == "bin") {
file.deleteRecursively()
}
}
}
}
}
13 changes: 13 additions & 0 deletions tutorials/Native_distributions_and_local_execution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ 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.
Expand Down

0 comments on commit b43a7c0

Please sign in to comment.