Skip to content

Commit d0564b4

Browse files
Download native-image executable using gu tool
1 parent 727f9bf commit d0564b4

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

common/utils/src/main/java/org/graalvm/buildtools/utils/SharedConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public interface SharedConstants {
5353
boolean IS_WINDOWS = System.getProperty("os.name", "unknown").contains("Windows");
5454
String EXECUTABLE_EXTENSION = (IS_WINDOWS ? ".cmd" : "");
5555
String NATIVE_IMAGE_EXE = "native-image" + EXECUTABLE_EXTENSION;
56+
String GU_EXE = "gu" + EXECUTABLE_EXTENSION;
5657
String NATIVE_IMAGE_OUTPUT_FOLDER = "native";
5758
String AGENT_PROPERTY = "agent";
5859
String AGENT_OUTPUT_FOLDER = NATIVE_IMAGE_OUTPUT_FOLDER + "/agent-output";

native-gradle-plugin/src/main/java/org/graalvm/buildtools/gradle/tasks/BuildNativeImageTask.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@
6161
import org.gradle.jvm.toolchain.JavaInstallationMetadata;
6262
import org.gradle.language.base.plugins.LifecycleBasePlugin;
6363
import org.gradle.process.ExecOperations;
64+
import org.gradle.process.ExecResult;
6465

6566
import javax.inject.Inject;
6667
import java.io.File;
6768
import java.nio.file.Paths;
6869
import java.util.List;
6970

71+
import static org.graalvm.buildtools.utils.SharedConstants.GU_EXE;
7072
import static org.graalvm.buildtools.utils.SharedConstants.NATIVE_IMAGE_EXE;
7173

7274
/**
@@ -154,22 +156,40 @@ public void exec() {
154156
if (!executablePath.exists() && getGraalVMHome().isPresent()) {
155157
executablePath = Paths.get(getGraalVMHome().get()).resolve("bin").resolve(NATIVE_IMAGE_EXE).toFile();
156158
}
157-
if (executablePath.exists()) {
158-
logger.log("Using executable path: " + executablePath);
159-
String executable = executablePath.getAbsolutePath();
160-
File outputDir = getOutputDirectory().getAsFile().get();
161-
if (outputDir.isDirectory() || outputDir.mkdirs()) {
162-
getExecOperations().exec(spec -> {
163-
spec.setWorkingDir(getWorkingDirectory());
164-
spec.args(args);
165-
getService().get();
166-
spec.setExecutable(executable);
159+
160+
try {
161+
if (!executablePath.exists()) {
162+
logger.log("Native Image executable wasn't found. We will now try to download it. ");
163+
File graalVmHomeGuess = executablePath.getParentFile();
164+
165+
if (!graalVmHomeGuess.toPath().resolve(GU_EXE).toFile().exists()) {
166+
throw new GradleException("'" + GU_EXE + "' tool wasn't found. This probably means that JDK at isn't a GraalVM distribution.");
167+
}
168+
ExecResult res = getExecOperations().exec(spec -> {
169+
spec.args("install", "native-image");
170+
spec.setExecutable(Paths.get(graalVmHomeGuess.getAbsolutePath(), GU_EXE));
167171
});
168-
logger.lifecycle("Native Image written to: " + outputDir);
172+
if (res.getExitValue() != 0) {
173+
throw new GradleException("Native Image executable wasn't found, and '" + GU_EXE + "' tool failed to install it.");
174+
}
169175
}
170-
return;
176+
} catch (GradleException e) {
177+
throw new GradleException("Determining GraalVM installation failed with message: " + e.getMessage() + "\n\n"
178+
+ "Make sure to declare the GRAALVM_HOME environment variable or install GraalVM with " +
179+
"native-image in a standard location recognized by Gradle Java toolchain support");
180+
}
181+
182+
logger.log("Using executable path: " + executablePath);
183+
String executable = executablePath.getAbsolutePath();
184+
File outputDir = getOutputDirectory().getAsFile().get();
185+
if (outputDir.isDirectory() || outputDir.mkdirs()) {
186+
getExecOperations().exec(spec -> {
187+
spec.setWorkingDir(getWorkingDirectory());
188+
spec.args(args);
189+
getService().get();
190+
spec.setExecutable(executable);
191+
});
192+
logger.lifecycle("Native Image written to: " + outputDir);
171193
}
172-
throw new GradleException("Expected to find " + NATIVE_IMAGE_EXE + " executable but it wasn't found. " +
173-
"Make sure to declare the GRAALVM_HOME environment variable or install GraalVM with native-image in a standard location recognized by Gradle Java toolchain support");
174194
}
175195
}

0 commit comments

Comments
 (0)