diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/NativeImageLeftovers.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/NativeImageLeftovers.kt new file mode 100644 index 0000000000..1165bbe4a0 --- /dev/null +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/internal/NativeImageLeftovers.kt @@ -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 = 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 = + names(os).map { directory.resolve(it) }.filter { it.isFile && it.delete() } +} diff --git a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractNativeImageTask.kt b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractNativeImageTask.kt index fde65285ce..e1068d814e 100644 --- a/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractNativeImageTask.kt +++ b/gradle-plugins/compose/src/main/kotlin/org/jetbrains/compose/desktop/application/tasks/AbstractNativeImageTask.kt @@ -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 @@ -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())}") } @@ -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, awt: Boolean): Pair, List> { val onLoad = compileC(graalvm, resourceText("static_onload.c"), "static_onload") @@ -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 diff --git a/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/unit/NativeImageLeftoversTest.kt b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/unit/NativeImageLeftoversTest.kt new file mode 100644 index 0000000000..af528d073d --- /dev/null +++ b/gradle-plugins/compose/src/test/kotlin/org/jetbrains/compose/test/tests/unit/NativeImageLeftoversTest.kt @@ -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(), NativeImageLeftovers.remove(OS.MacOS, dir)) + assertEquals(listOf("app", "libjava.dylib"), left()) + } +}