Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2026 thisisthepy 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.desktop.application.internal

import org.jetbrains.compose.internal.utils.OS
import java.io.File

/**
* The shared libraries GraalVM writes beside a native image that the image does not load.
*
* native-image copies the JDK libraries the image would have used: the `java` and `jvm` shims,
* which a static image never opens, and the libraries whose code the plugin links in from NIK's
* static archives instead (AWT's, FreeType, JAWT). The application ships the executable alone,
* so whatever is left beside it is at best dead weight and at worst a second copy of a library
* that a later `loadLibrary` would pick up instead of the linked one.
*/
internal object NativeImageLeftovers {
/** File names, per host, of what the executable does not load. macOS writes none of them. */
fun names(os: OS): List<String> = when (os) {
OS.Linux -> listOf("libjava.so", "libjvm.so", "libawt_headless.so", "libfreetype.so")
// freetype.lib and jawt.lib are linked in (the Windows static JDK archives); the Java
// Access Bridge's library is copied with java.desktop but nothing registers or loads it.
OS.Windows -> listOf("java.dll", "jvm.dll", "freetype.dll", "jawt.dll", "javaaccessbridge.dll")
OS.MacOS -> emptyList()
}

/** Deletes [names] from [directory] and answers what it deleted. */
fun remove(os: OS, directory: File): List<File> =
names(os).map { directory.resolve(it) }.filter { it.isFile && it.delete() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.gradle.api.tasks.TaskAction
import org.gradle.work.DisableCachingByDefault
import org.jetbrains.compose.desktop.application.dsl.ApplicationWindowing
import org.jetbrains.compose.desktop.application.internal.NativeImageMainThread
import org.jetbrains.compose.desktop.application.internal.NativeImageLeftovers
import org.jetbrains.compose.desktop.application.internal.NativeImageNoAwt
import org.jetbrains.compose.desktop.tasks.AbstractComposeDesktopTask
import org.jetbrains.compose.internal.utils.OS
Expand Down Expand Up @@ -198,7 +199,9 @@ abstract class AbstractNativeImageTask : AbstractComposeDesktopTask() {
argFile.writeText(args.drop(1).joinToString("\n") { quoteArgument(it) })
logger.info("native-image arguments:\n" + args.drop(1).joinToString("\n"))
run(listOf(args.first(), "@${argFile.absolutePath}"), workDir.resolve("native-image.log"))
if (platform == Platform.LinuxX64 || platform == Platform.LinuxArm64) removeUnneededLinuxLibraries(output)
for (file in NativeImageLeftovers.remove(currentOS, output)) {
logger.info("native-image: removed $file, which the executable does not load")
}
logger.lifecycle("The executable is written to ${output.resolve(imageName.get())}")
}

Expand Down Expand Up @@ -247,7 +250,7 @@ abstract class AbstractNativeImageTask : AbstractComposeDesktopTask() {
*
* GraalVM still writes the shared libraries it would have used beside the executable
* (the `java` and `jvm` shims, `awt_headless`, `freetype`); the executable needs none of
* them, and [removeUnneededLinuxLibraries] takes them away.
* them, and [NativeImageLeftovers] takes them away.
*/
private fun linuxLink(graalvm: File, staticJdk: File, skikoArchive: File, skiaArchives: List<File>, awt: Boolean): Pair<List<String>, List<String>> {
val onLoad = compileC(graalvm, resourceText("static_onload.c"), "static_onload")
Expand Down Expand Up @@ -290,13 +293,6 @@ abstract class AbstractNativeImageTask : AbstractComposeDesktopTask() {
return linked to link
}

private fun removeUnneededLinuxLibraries(output: File) {
for (library in listOf("libjava.so", "libjvm.so", "libawt_headless.so", "libfreetype.so")) {
val file = output.resolve(library)
if (file.delete()) logger.info("native-image: removed $file, which the executable does not load")
}
}

/**
* Windows: the JDK's static libraries are built against the C runtime DLL (/MD), and
* JetBrains' Skia against the static one (/MT). The MSVC linker refuses to mix them on a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2026 thisisthepy 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.test.tests.unit

import org.jetbrains.compose.desktop.application.internal.NativeImageLeftovers
import org.jetbrains.compose.internal.utils.OS
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File

/**
* GraalVM writes the shared libraries an image would have loaded beside the executable, and the
* static image loads none of them; only the executable and what the application put there stay.
*/
class NativeImageLeftoversTest {
@TempDir
lateinit var dir: File

private fun files(vararg names: String) = names.forEach { dir.resolve(it).writeText(it) }
private fun left() = dir.list()!!.sorted()

@Test
fun windowsKeepsTheExecutableAlone() {
// What packageNativeImage left beside gemstone.exe (plugin 1.11.1-ext.3, Gemstone #255).
files("gemstone.exe", "freetype.dll", "java.dll", "javaaccessbridge.dll", "jawt.dll", "jvm.dll")
val removed = NativeImageLeftovers.remove(OS.Windows, dir)
assertEquals(listOf("gemstone.exe"), left())
assertEquals(
listOf("freetype.dll", "java.dll", "javaaccessbridge.dll", "jawt.dll", "jvm.dll"),
removed.map { it.name }.sorted(),
)
}

@Test
fun windowsKeepsLibrariesItDoesNotKnow() {
files("app.exe", "jvm.dll", "native-helper.dll", "app.pdb")
NativeImageLeftovers.remove(OS.Windows, dir)
assertEquals(listOf("app.exe", "app.pdb", "native-helper.dll"), left())
}

@Test
fun linuxRemovesWhatItAlwaysDid() {
files("app", "libjava.so", "libjvm.so", "libawt_headless.so", "libfreetype.so", "libother.so")
NativeImageLeftovers.remove(OS.Linux, dir)
assertEquals(listOf("app", "libother.so"), left())
}

@Test
fun macosRemovesNothing() {
files("app", "libjava.dylib")
assertEquals(emptyList<File>(), NativeImageLeftovers.remove(OS.MacOS, dir))
assertEquals(listOf("app", "libjava.dylib"), left())
}
}
Loading