Skip to content
Merged
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
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@
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;

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();
Expand Down Expand Up @@ -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<String> utilArgs = new ArrayList<>(args.length + 1);
utilArgs.add(toolPath.toString());
if (args.length > 0) {
Expand All @@ -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("<invalid>"));
log("Error: %s", e.getMessage());
printMissingToolMessage(Optional.ofNullable(toolPath.getParent()).map(Path::getParent).map(Path::toString).orElse("<invalid>"));
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -174,7 +181,7 @@ public final void runDriverExit(List<String> sulongArgs, List<String> userArgs,
} catch (IOException e) {
System.exit(1);
} catch (Exception e) {
System.err.println("Exception: " + e);
log("Exception: %s", e);
System.exit(1);
}
}
Expand Down Expand Up @@ -210,7 +217,7 @@ public final int runDriverReturn(List<String> sulongArgs, List<String> 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) {
Expand Down Expand Up @@ -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<String> toolArgs) {
Expand All @@ -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) {
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -94,23 +94,23 @@ static void runDriverWithSaveTemps(Driver driver, List<String> 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);
}
}
}
returnCode = driver.runDriverReturn(sulongArgs, userArgs, verb, hlp, earlyexit);
} catch (IOException e) {
returnCode = 1;
} catch (Exception e) {
System.err.println("Exception: " + e);
driver.log("Exception: " + e);
returnCode = 1;
} finally {
if (tempDir != null) {
try {
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);
}
}
}
Expand Down