Skip to content

Commit

Permalink
Merge pull request #586 from graalvm/dnestoro/RefactorInitializationO…
Browse files Browse the repository at this point in the history
…fConfigurtaionFileMerger

Refactor Initialization of Configuration File Merger
  • Loading branch information
dnestoro authored May 23, 2024
2 parents 096ea1f + 8be17a9 commit 07965b0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 41 deletions.
2 changes: 2 additions & 0 deletions docs/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ If you are using alternative build systems, see <<alternative-build-systems.adoc

=== Release 0.10.3

- Remove usage of macro from merger tool initialization and throw better error if executable does not exist

==== Gradle plugin

- Add retries when downloading the metadata repository when using a URL directly
Expand Down
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,26 @@ 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();
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
}
return mergerExecutable;
}

private void initializeMergerExecutable() throws MojoExecutionException {
Path nativeImage = NativeImageConfigurationUtils.getNativeImage(logger);
File nativeImageExecutable = nativeImage.toAbsolutePath().toFile();
String nativeImageConfigureFileName = nativeImageConfigureFileName();
File mergerExecutable = new File(nativeImageExecutable.getParentFile(), nativeImageConfigureFileName);
if (!mergerExecutable.exists()) {
throw new MojoExecutionException("The '" + nativeImageConfigureFileName + "' tool was not found in the GraalVM JDK at '" + nativeImageExecutable.getParentFile().getParentFile() + "'." +
"This probably means that you are using a GraalVM distribution that is not fully supported by the Native Build Tools. " +
"Please try again, for example, with Oracle GraalVM or GraalVM Community Edition."
);
}

this.mergerExecutable = mergerExecutable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ public static Path getNativeImage(Logger logger) throws MojoExecutionException {
}

if (nativeImage == null) {
throw new RuntimeException("GraalVM native-image is missing on your system. " + System.lineSeparator() +
"Make sure that GRAALVM_HOME environment variable is present.");
throw new RuntimeException("The 'native-image' tool was not found on your system. " +
"Make sure that the JAVA_HOME or GRAALVM_HOME environment variables point to a GraalVM JDK, or that 'native-image' is on the system path.");
}

nativeImageExeCache = nativeImage;
Expand Down

0 comments on commit 07965b0

Please sign in to comment.