From 03d4ccbdfb69d53862e6c4166254ba036bbf851a Mon Sep 17 00:00:00 2001 From: Bernhard Urban-Forster Date: Mon, 6 Oct 2025 15:00:31 +0200 Subject: [PATCH 1/2] [GR-70235] Sulong: Adapt toolchain wrapper to support executing with JVM --- .../launchers/AbstractToolchainWrapper.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/AbstractToolchainWrapper.java b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/AbstractToolchainWrapper.java index 752e0d71418e..b5a0d4d707f8 100644 --- a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/AbstractToolchainWrapper.java +++ b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/AbstractToolchainWrapper.java @@ -33,6 +33,7 @@ import java.nio.file.Paths; import java.util.Collections; +import org.graalvm.nativeimage.ImageInfo; import org.graalvm.nativeimage.ProcessProperties; public abstract class AbstractToolchainWrapper { @@ -61,8 +62,19 @@ private static boolean isWindows() { return System.getProperty("os.name").startsWith("Windows"); } + private static boolean isNativeImage() { + return ImageInfo.inImageCode(); + } + private static Path getCurrentExecutablePath() { - final String path = ProcessProperties.getExecutableName(); + String path = System.getProperty("org.graalvm.launcher.executablePath"); + if (path != null) { + assert !isNativeImage() : "only expected with JVM wrappers"; + return Path.of(path).toAbsolutePath().normalize(); + } + + assert isNativeImage(); + path = ProcessProperties.getExecutableName(); return path == null ? null : Paths.get(path); } @@ -89,11 +101,11 @@ protected void run(String[] args) { } Path llvmPath = toolchainPath.resolve(RELATIVE_LLVM_PATH).normalize(); - String programName; + String programName = null; if (isWindows()) { // set by the exe_link_template.cmd wrapper script programName = System.getenv("GRAALVM_ARGUMENT_VECTOR_PROGRAM_NAME"); - } else { + } else if (isNativeImage()) { programName = ProcessProperties.getArgumentVectorProgramName(); } From d158d00b8bd667bb86eccd4c6377c9c7d9f3126e Mon Sep 17 00:00:00 2001 From: Roland Schatz Date: Tue, 11 Nov 2025 12:10:42 +0100 Subject: [PATCH 2/2] Improve logging in toolchain wrappers. --- .../llvm/toolchain/launchers/BinUtil.java | 17 +++++--- .../toolchain/launchers/common/Driver.java | 39 ++++++++++++++----- .../launchers/darwin/DarwinLinker.java | 8 ++-- 3 files changed, 44 insertions(+), 20 deletions(-) diff --git a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/BinUtil.java b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/BinUtil.java index b967cdc94aae..cc63340ad2e2 100644 --- a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/BinUtil.java +++ b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/BinUtil.java @@ -34,6 +34,7 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; +import java.util.Objects; import java.util.Optional; import org.graalvm.nativeimage.ProcessProperties; @@ -41,7 +42,11 @@ import com.oracle.truffle.llvm.toolchain.launchers.AbstractToolchainWrapper.ToolchainWrapperConfig; import com.oracle.truffle.llvm.toolchain.launchers.common.Driver; -public final class BinUtil { +public final class BinUtil extends Driver { + + public BinUtil(String exe) { + super(exe); + } public static void main(String[] args) { ToolchainWrapperConfig config = AbstractToolchainWrapper.getConfig(); @@ -69,10 +74,10 @@ public static void main(String[] args) { } final Path toolPath = Driver.getLLVMBinDir().resolve(toolName); - runTool(toolPath, args); + new BinUtil(Objects.toString(toolPath.getFileName(), "")).runTool(toolPath, args); } - public static void runTool(Path toolPath, String[] args) { + public void runTool(Path toolPath, String[] args) { ArrayList utilArgs = new ArrayList<>(args.length + 1); utilArgs.add(toolPath.toString()); if (args.length > 0) { @@ -85,14 +90,14 @@ public static void runTool(Path toolPath, String[] args) { p.waitFor(); System.exit(p.exitValue()); } catch (IOException e) { - System.err.println("Error: " + e.getMessage()); - Driver.printMissingToolMessage(Optional.ofNullable(toolPath.getParent()).map(Path::getParent).map(Path::toString).orElse("")); + log("Error: %s", e.getMessage()); + printMissingToolMessage(Optional.ofNullable(toolPath.getParent()).map(Path::getParent).map(Path::toString).orElse("")); System.exit(1); } catch (InterruptedException e) { if (p != null) { p.destroyForcibly(); } - System.err.println("Error: Subprocess interrupted: " + e.getMessage()); + log("Error: Subprocess interrupted: %s", e.getMessage()); System.exit(1); } } diff --git a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/common/Driver.java b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/common/Driver.java index bc0e7713ef58..bce9387b75b3 100644 --- a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/common/Driver.java +++ b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/common/Driver.java @@ -43,18 +43,25 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; public class Driver { protected final String exe; protected final boolean isBundledTool; + private final String toolName; - public Driver(String exe, boolean inLLVMDir) { + public Driver(String toolName, String exe, boolean inLLVMDir) { + this.toolName = String.format("%s:%s", getClass().getSimpleName(), toolName); this.exe = inLLVMDir ? getLLVMExecutable(exe).toString() : exe; this.isBundledTool = inLLVMDir; } + public Driver(String exe, boolean inLLVMDir) { + this(Objects.toString(Paths.get(exe).getFileName(), ""), exe, inLLVMDir); + } + public Driver(String exe) { this(exe, true); } @@ -174,7 +181,7 @@ public final void runDriverExit(List sulongArgs, List userArgs, } catch (IOException e) { System.exit(1); } catch (Exception e) { - System.err.println("Exception: " + e); + log("Exception: %s", e); System.exit(1); } } @@ -210,7 +217,7 @@ public final int runDriverReturn(List sulongArgs, List userArgs, // set exit code int exitCode = p.exitValue(); if (verbose) { - System.err.println("exit code: " + exitCode); + log("exit code: %s", exitCode); } return exitCode; } catch (IOException ioe) { @@ -243,10 +250,10 @@ private static ProcessBuilder setupRedirectsDefault(ProcessBuilder pb) { return pb.inheritIO(); } - public static void printMissingToolMessage(String llvmRoot) { - System.err.println("Tool execution failed. Are you sure the toolchain is available at " + llvmRoot); - System.err.println(); - System.err.println("More infos: https://www.graalvm.org/docs/reference-manual/languages/llvm/"); + public void printMissingToolMessage(String llvmRoot) { + log("Tool execution failed. Are you sure the toolchain is available at %s", llvmRoot); + log(); + log("More infos: https://www.graalvm.org/docs/reference-manual/languages/llvm/"); } private void printInfos(boolean verbose, boolean help, boolean earlyExit, ArrayList toolArgs) { @@ -261,9 +268,9 @@ private void printInfos(boolean verbose, boolean help, boolean earlyExit, ArrayL System.out.println("GraalVM version: " + getVersion()); } if (verbose) { - System.err.println("GraalVM wrapper script for " + getTool()); - System.err.println("GraalVM version: " + getVersion()); - System.err.println("running: " + String.join(" ", toolArgs)); + log("GraalVM wrapper script for %s", getTool()); + log("GraalVM version: %s", getVersion()); + log("running: %s", String.join(" ", toolArgs)); } if (help) { if (!earlyExit) { @@ -281,4 +288,16 @@ private Path getTool() { public static Path getLLVMExecutable(String tool) { return getLLVMBinDir().resolve(tool); } + + public final void log() { + System.err.printf("[%s]\n", toolName); + } + + public final void log(String msg) { + System.err.printf("[%s] %s\n", toolName, msg); + } + + public final void log(String format, Object... args) { + log(String.format(format, args)); + } } diff --git a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/darwin/DarwinLinker.java b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/darwin/DarwinLinker.java index 30e2122e3b16..81bee29e682b 100644 --- a/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/darwin/DarwinLinker.java +++ b/sulong/projects/com.oracle.truffle.llvm.toolchain.launchers/src/com/oracle/truffle/llvm/toolchain/launchers/darwin/DarwinLinker.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019, 2023, Oracle and/or its affiliates. + * Copyright (c) 2019, 2025, Oracle and/or its affiliates. * * All rights reserved. * @@ -94,7 +94,7 @@ static void runDriverWithSaveTemps(Driver driver, List sulongArgs, List< } catch (Exception e) { // something went wrong -- let the normal driver run fail if (verb) { - System.err.println("Running clang with `-save-temps` failed: " + e); + driver.log("Running clang with `-save-temps` failed: " + e); } } } @@ -102,7 +102,7 @@ static void runDriverWithSaveTemps(Driver driver, List sulongArgs, List< } catch (IOException e) { returnCode = 1; } catch (Exception e) { - System.err.println("Exception: " + e); + driver.log("Exception: " + e); returnCode = 1; } finally { if (tempDir != null) { @@ -110,7 +110,7 @@ static void runDriverWithSaveTemps(Driver driver, List sulongArgs, List< Files.walk(tempDir).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); } catch (IOException e) { if (verb) { - System.err.println("Deleting the temporary directory (" + tempDir + ") failed: " + e); + driver.log("Deleting the temporary directory (" + tempDir + ") failed: " + e); } } }