Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supporting jdk toolchain configuration #622

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -171,6 +171,13 @@ public abstract class AbstractNativeImageMojo extends AbstractNativeMojo {
@Component
protected ToolchainManager toolchainManager;

/**
Set this to true to ensure that native-image is found in your toolchain and not in an unrelated JDK or in your PATH.
Will fail the build in case no toolchain was found or if it does not contain native-image.
*/
@Parameter(property = "enforceToolchain")
protected boolean enforceToolchain;

@Inject
protected AbstractNativeImageMojo() {
imageClasspath = new ArrayList<>();
Expand Down Expand Up @@ -411,7 +418,7 @@ protected String getClasspath() throws MojoExecutionException {

protected void buildImage() throws MojoExecutionException {
checkRequiredVersionIfNeeded();
Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImage(logger);
Path nativeImageExecutable = NativeImageConfigurationUtils.getNativeImageSupportingToolchain(logger, toolchainManager, session, enforceToolchain);

try {
ProcessBuilder processBuilder = new ProcessBuilder(nativeImageExecutable.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@

package org.graalvm.buildtools.maven.config;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.logging.Logger;
import org.graalvm.buildtools.utils.NativeImageConfigurationUtils;

Expand All @@ -58,6 +61,19 @@ public abstract class AbstractMergeAgentFilesMojo extends AbstractMojo {
@Component
protected Logger logger;

@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession session;

@Component
protected ToolchainManager toolchainManager;

/**
Set this to true to ensure that native-image is found in your toolchain and not in an unrelated JDK or in your PATH.
Will fail the build in case no toolchain was found or if it does not contain native-image.
*/
@Parameter(property = "enforceToolchain")
protected boolean enforceToolchain;

private File mergerExecutable;

public File getMergerExecutable() throws MojoExecutionException {
Expand All @@ -69,7 +85,7 @@ public File getMergerExecutable() throws MojoExecutionException {
}

private void initializeMergerExecutable() throws MojoExecutionException {
Path nativeImage = NativeImageConfigurationUtils.getNativeImage(logger);
Path nativeImage = NativeImageConfigurationUtils.getNativeImageSupportingToolchain(logger, toolchainManager, session, enforceToolchain);
File nativeImageExecutable = nativeImage.toAbsolutePath().toFile();
String nativeImageConfigureFileName = nativeImageConfigureFileName();
File mergerExecutable = new File(nativeImageExecutable.getParentFile(), nativeImageConfigureFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@

package org.graalvm.buildtools.utils;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.logging.Logger;

import java.io.File;
Expand All @@ -60,6 +63,7 @@ public abstract class NativeImageConfigurationUtils implements SharedConstants {
public static final String NATIVE_TESTS_EXE = "native-tests" + EXECUTABLE_EXTENSION;
public static final String MAVEN_GROUP_ID = "org.graalvm.buildtools";
public static Path nativeImageExeCache;
public static Path nativeImageExeCacheSupportingToolchain;

public static Path getJavaHomeNativeImage(String javaHomeVariable, Boolean failFast, Logger logger) throws MojoExecutionException {
String graalHome = System.getenv(javaHomeVariable);
Expand Down Expand Up @@ -106,6 +110,51 @@ public static Path getNativeImageFromPath() {
return exePath.map(path -> path.resolve(NATIVE_IMAGE_EXE)).orElse(null);
}

public static Path getNativeImageSupportingToolchain(Logger logger, ToolchainManager toolchainManager, MavenSession session, boolean enforceToolchain) throws MojoExecutionException {
if (nativeImageExeCacheSupportingToolchain != null) {
return nativeImageExeCacheSupportingToolchain;
}

Path nativeImage = getToolchainNativeImage(logger, toolchainManager, session, enforceToolchain);
if (nativeImage != null) {
nativeImageExeCacheSupportingToolchain = nativeImage;
nativeImageExeCache = nativeImage;
return nativeImage;
}

return getNativeImage(logger);
}

public static Path getToolchainNativeImage(Logger logger, ToolchainManager toolchainManager, MavenSession session, boolean enforceToolchain) throws MojoExecutionException {
final Toolchain toolchain = toolchainManager.getToolchainFromBuildContext("jdk", session);

if (toolchain != null) {
String javaPath = toolchain.findTool("java");

if (javaPath != null) {
Path nativeImagePath = Paths.get(javaPath).getParent().resolve(NATIVE_IMAGE_EXE).toAbsolutePath();
if (!Files.exists(nativeImagePath)) {
final String message = "No " + NATIVE_IMAGE_EXE + " found in the jdk toolchain configuration: " + nativeImagePath.getParent().getParent();
if (enforceToolchain) {
throw new MojoExecutionException(message);
}
logger.warn(message);
return null;
}
return nativeImagePath;
}
throw new MojoExecutionException("No java found the toolchain configuration.");

} else {
final String message = "No jdk toolchain configuration found";
if (enforceToolchain) {
throw new MojoExecutionException(message);
}
logger.warn(message);
}
return null;
}

public static Path getNativeImage(Logger logger) throws MojoExecutionException {
if (nativeImageExeCache != null) {
return nativeImageExeCache;
Expand Down