Skip to content

Commit

Permalink
Remove macro from merger init and throw better error if executable do…
Browse files Browse the repository at this point in the history
…es not exist
  • Loading branch information
dnestoro committed May 23, 2024
1 parent 096ea1f commit 4c61693
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@
import org.codehaus.plexus.util.FileUtils;
import org.graalvm.buildtools.maven.config.AbstractMergeAgentFilesMojo;
import org.graalvm.buildtools.maven.config.agent.AgentConfiguration;
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -116,14 +114,12 @@ public void execute() throws MojoExecutionException {
private void mergeForGivenDir(String agentOutputDirectory) throws MojoExecutionException {
File baseDir = new File(agentOutputDirectory);
if (baseDir.exists()) {
Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger);
tryInstallMergeExecutable(nativeImageExecutable);
List<File> sessionDirectories = sessionDirectoriesFrom(baseDir.listFiles()).collect(Collectors.toList());
if (sessionDirectories.size() == 0) {
sessionDirectories = Collections.singletonList(baseDir);
}

invokeMerge(mergerExecutable, sessionDirectories, baseDir);
invokeMerge(sessionDirectories, baseDir);
} else {
getLog().debug("Agent output directory " + baseDir + " doesn't exist. Skipping merge.");
}
Expand All @@ -135,11 +131,8 @@ private static Stream<File> sessionDirectoriesFrom(File[] files) {
.filter(f -> f.getName().startsWith("session-"));
}

private void invokeMerge(File mergerExecutable, List<File> inputDirectories, File outputDirectory) throws MojoExecutionException {
if (!mergerExecutable.exists()) {
getLog().warn("Cannot merge agent files because native-image-configure is not installed. Please upgrade to a newer version of GraalVM.");
return;
}
private void invokeMerge(List<File> inputDirectories, File outputDirectory) throws MojoExecutionException {
File mergerExecutable = getMergerExecutable();
try {
if (inputDirectories.isEmpty()) {
getLog().warn("Skipping merging of agent files since there are no input directories.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@
import org.graalvm.buildtools.maven.config.AbstractMergeAgentFilesMojo;
import org.graalvm.buildtools.maven.config.agent.AgentConfiguration;
import org.graalvm.buildtools.maven.config.agent.MetadataCopyConfiguration;
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -113,8 +111,6 @@ public void execute() throws MojoExecutionException {
}
}

Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger);
tryInstallMergeExecutable(nativeImageExecutable);
executeCopy(buildDirectory, destinationDir);
getLog().info("Metadata copy process finished.");
}
Expand Down Expand Up @@ -155,7 +151,7 @@ private void executeCopy(String buildDirectory, String destinationDir) throws Mo
logger.info("Copying files from: " + sourceDirsInfo);

List<String> nativeImageConfigureOptions = new StandardAgentMode().getNativeImageConfigureOptions(sourceDirectories, Collections.singletonList(destinationDir));
nativeImageConfigureOptions.add(0, mergerExecutable.getAbsolutePath());
nativeImageConfigureOptions.add(0, getMergerExecutable().getAbsolutePath());
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageConfigureOptions);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
package org.graalvm.buildtools.maven.config;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.codehaus.plexus.logging.Logger;
import org.graalvm.buildtools.utils.NativeImageUtils;
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import static org.graalvm.buildtools.utils.NativeImageUtils.nativeImageConfigureFileName;
Expand All @@ -58,32 +58,24 @@ public abstract class AbstractMergeAgentFilesMojo extends AbstractMojo {
@Component
protected Logger logger;

protected File mergerExecutable;
private File mergerExecutable;

protected void tryInstallMergeExecutable(Path nativeImageExecutablePath) {
if (mergerExecutable != null && mergerExecutable.exists()) {
return;
public File getMergerExecutable() throws MojoExecutionException {
if (mergerExecutable == null) {
initializeMergerExecutable();
}

File nativeImageExecutable = nativeImageExecutablePath.toAbsolutePath().toFile();
return mergerExecutable;
}

private void initializeMergerExecutable() throws MojoExecutionException {
Path nativeImage = NativeImageConfigurationUtils.getNativeImage(logger);
File nativeImageExecutable = nativeImage.toAbsolutePath().toFile();
File mergerExecutable = new File(nativeImageExecutable.getParentFile(), nativeImageConfigureFileName());
if (!mergerExecutable.exists()) {
getLog().info("Installing native image merger to " + mergerExecutable);
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageExecutable.toString());
processBuilder.command().add("--macro:native-image-configure-launcher");
processBuilder.directory(mergerExecutable.getParentFile());
processBuilder.inheritIO();

try {
Process installProcess = processBuilder.start();
if (installProcess.waitFor() != 0) {
getLog().warn("Installation of native image merging tool failed");
}
NativeImageUtils.maybeCreateConfigureUtilSymlink(mergerExecutable, nativeImageExecutablePath);
} catch (IOException | InterruptedException e) {
// ignore since we will handle that if the installer doesn't exist later
}

throw new MojoExecutionException("'" + nativeImageConfigureFileName() + "' tool was not found in your " + nativeImage + "." +
"This probably means that the JDK at '" + nativeImage + "' is not a GraalVM distribution."
);
}

this.mergerExecutable = mergerExecutable;
Expand Down

0 comments on commit 4c61693

Please sign in to comment.