summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md6
-rw-r--r--stubs/src/kotlin/proxy/KotlinProxyOverrides.java119
2 files changed, 76 insertions, 49 deletions
diff --git a/README.md b/README.md
index 38df3c6..08cf1c2 100644
--- a/README.md
+++ b/README.md
@@ -325,6 +325,12 @@ via `org.gradle.jvmargs` or the daemon's own args):
| `kotlin.proxy.log.mode` | `all` | `all` \| `classes` (loads only) \| `off` |
| `kotlin.proxy.marker` | `/tmp/kotlin-proxy-marker.txt` | fixed-path copy of every line |
+`KotlinProxyOverrides` honors one more system property, also read in the daemon JVM:
+
+| property | default | meaning |
+|---|---|---|
+| `kotlin.proxy.nativeImage` | unset | native-image distribution root (it holds `bin/kotlinc-native-image.sh`), for example `~/source/kotlin/prepare/compiler-native-image/build/dist`. If unset, the stub downloads the JetBrains release into `<user.dir>/native-image/`. |
+
The marker file is what proves the stub ran **inside the out-of-process
daemon** (its thread name is `RMI TCP Connection(N)`), regardless of the
daemon's working directory or missing `-Dkotlin.proxy.log`:
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<String> cmd = new ArrayList<String>();
- 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<String> cmd = new ArrayList<String>();
@@ -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<String> cmd = new ArrayList<String>();
+ 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<Path> paths = Files.walk(dir.toPath())) {