From f6d1b9d25ede46567a8da89f01b3db915a25798d Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Mon, 23 Nov 2026 21:37:27 +0100 Subject: Add support for kotlin.proxy.nativeImage system property Allow overriding which native image is used. --- stubs/src/kotlin/proxy/KotlinProxyOverrides.java | 119 +++++++++++++---------- 1 file changed, 70 insertions(+), 49 deletions(-) (limited to 'stubs/src/kotlin') diff --git a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java index f87ba2a..e03c04d 100644 --- a/stubs/src/kotlin/proxy/KotlinProxyOverrides.java +++ b/stubs/src/kotlin/proxy/KotlinProxyOverrides.java @@ -100,55 +100,20 @@ public final class KotlinProxyOverrides { messageCollector.report(CompilerMessageSeverity.ERROR, "System property java.home is not set", null); return ExitCode.INTERNAL_ERROR; } - File nativeImageDir = new File(userDir, "native-image"); - File nativeImage = new File(nativeImageDir, KOTLIN_NATIVE_IMAGE + "/bin/kotlinc-native-image.sh"); - if (!nativeImage.exists()) { - nativeImageDir.mkdirs(); - - synchronized (NATIVE_IMAGE_LOCK) { - // Check again, now that we have the lock - if (!nativeImage.exists()) { - File nativeImageTar = new File(nativeImageDir, KOTLIN_NATIVE_IMAGE + ".tar.gz"); - if (!nativeImageTar.exists()) { - try (InputStream in = new URL("https://github.com/JetBrains/kotlin/releases/download/v" - + KOTLIN_VERSION + "/" + KOTLIN_NATIVE_IMAGE + ".tar.gz").openStream()) { - Files.copy(in, nativeImageTar.toPath(), StandardCopyOption.REPLACE_EXISTING); - } catch (IOException e) { - MessageCollectorUtil.reportException(messageCollector, e); - return ExitCode.INTERNAL_ERROR; - } - } - File nativeImageDirTmp = new File(nativeImageDir, "tmp"); - nativeImageDirTmp.mkdir(); - ArrayList cmd = new ArrayList(); - cmd.add("tar"); - cmd.add("-x"); - cmd.add("-f"); - cmd.add(nativeImageTar.toString()); - cmd.add("-z"); - cmd.add("-C"); - cmd.add(nativeImageDirTmp.toString()); - ProcessBuilder builder = new ProcessBuilder(cmd); - try { - Process process = builder.start(); - int exitvalue = process.waitFor(); - if (exitvalue != 0) { - // TODO: Include error stream - messageCollector.report(CompilerMessageSeverity.ERROR, "Unable to extract native image " + nativeImageTar, null); - deleteRecursive(nativeImageDirTmp); - return ExitCode.INTERNAL_ERROR; - } - File target = new File(nativeImageDir, KOTLIN_NATIVE_IMAGE); - deleteRecursive(target); - Files.move(new File(nativeImageDirTmp, KOTLIN_NATIVE_IMAGE).toPath(), - target.toPath()); - nativeImageDirTmp.delete(); - } catch (Throwable t) { - MessageCollectorUtil.reportException(messageCollector, t); - deleteRecursive(nativeImageDirTmp); - return ExitCode.SCRIPT_EXECUTION_ERROR; - } - } + File nativeImage; + // A distribution root such as prepare/compiler-native-image/build/dist in the Kotlin repository. + String customNativeImage = System.getProperty("kotlin.proxy.nativeImage"); + if (customNativeImage != null && !customNativeImage.isEmpty()) { + nativeImage = new File(customNativeImage, "bin/kotlinc-native-image.sh"); + if (!nativeImage.exists()) { + messageCollector.report(CompilerMessageSeverity.ERROR, + "kotlin.proxy.nativeImage: " + nativeImage + " does not exist", null); + return ExitCode.INTERNAL_ERROR; + } + } else { + nativeImage = downloadNativeImage(userDir, messageCollector); + if (nativeImage == null) { + return ExitCode.INTERNAL_ERROR; } } ArrayList cmd = new ArrayList(); @@ -266,6 +231,62 @@ public final class KotlinProxyOverrides { return null; } + /** Downloads and extracts the JetBrains native image into user.dir. Returns null after it reports an error. */ + private static File downloadNativeImage(String userDir, MessageCollector messageCollector) { + File nativeImageDir = new File(userDir, "native-image"); + File nativeImage = new File(nativeImageDir, KOTLIN_NATIVE_IMAGE + "/bin/kotlinc-native-image.sh"); + if (!nativeImage.exists()) { + nativeImageDir.mkdirs(); + + synchronized (NATIVE_IMAGE_LOCK) { + // Check again, now that we have the lock + if (!nativeImage.exists()) { + File nativeImageTar = new File(nativeImageDir, KOTLIN_NATIVE_IMAGE + ".tar.gz"); + if (!nativeImageTar.exists()) { + try (InputStream in = new URL("https://github.com/JetBrains/kotlin/releases/download/v" + + KOTLIN_VERSION + "/" + KOTLIN_NATIVE_IMAGE + ".tar.gz").openStream()) { + Files.copy(in, nativeImageTar.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException e) { + MessageCollectorUtil.reportException(messageCollector, e); + return null; + } + } + File nativeImageDirTmp = new File(nativeImageDir, "tmp"); + nativeImageDirTmp.mkdir(); + ArrayList cmd = new ArrayList(); + cmd.add("tar"); + cmd.add("-x"); + cmd.add("-f"); + cmd.add(nativeImageTar.toString()); + cmd.add("-z"); + cmd.add("-C"); + cmd.add(nativeImageDirTmp.toString()); + ProcessBuilder builder = new ProcessBuilder(cmd); + try { + Process process = builder.start(); + int exitvalue = process.waitFor(); + if (exitvalue != 0) { + // TODO: Include error stream + messageCollector.report(CompilerMessageSeverity.ERROR, "Unable to extract native image " + nativeImageTar, null); + deleteRecursive(nativeImageDirTmp); + return null; + } + File target = new File(nativeImageDir, KOTLIN_NATIVE_IMAGE); + deleteRecursive(target); + Files.move(new File(nativeImageDirTmp, KOTLIN_NATIVE_IMAGE).toPath(), + target.toPath()); + nativeImageDirTmp.delete(); + } catch (Throwable t) { + MessageCollectorUtil.reportException(messageCollector, t); + deleteRecursive(nativeImageDirTmp); + return null; + } + } + } + } + return nativeImage; + } + private static void deleteRecursive(File dir) { try { try (Stream paths = Files.walk(dir.toPath())) { -- cgit v1.3.1