Skip to content

Commit

Permalink
[GR-10101] Make NativeImage callable from maven plugin.
Browse files Browse the repository at this point in the history
PullRequest: graal/1588
  • Loading branch information
olpaw committed May 29, 2018
2 parents 184bc7a + ec7e1a8 commit 12f8df1
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
import com.oracle.svm.jni.hosted.JNIFeature;
import com.oracle.svm.reflect.hosted.ReflectionFeature;

class NativeImage {
public class NativeImage {

static final boolean IS_AOT = Boolean.getBoolean("com.oracle.graalvm.isaot");

Expand Down Expand Up @@ -176,34 +176,58 @@ private static <T> String oH(OptionKey<T> option) {
final Registry optionRegistry;
private LinkedHashSet<EnabledOption> enabledLanguages;

protected NativeImage() {
workDir = Paths.get(".").toAbsolutePath().normalize();
assert workDir != null;
if (IS_AOT) {
Path executablePath = Paths.get((String) Compiler.command(new Object[]{PosixExecutableName.getKey()}));
assert executablePath != null;
Path binDir = executablePath.getParent();
Path rootDirCandidate = binDir.getParent();
if (rootDirCandidate.endsWith(platform)) {
rootDirCandidate = rootDirCandidate.getParent();
}
if (rootDirCandidate.endsWith(Paths.get("lib", "svm"))) {
rootDirCandidate = rootDirCandidate.getParent().getParent();
}
rootDir = rootDirCandidate;
} else {
String rootDirProperty = "native-image.root";
String rootDirString = System.getProperty(rootDirProperty);
if (rootDirString == null) {
throw showError("Running on JVM requires setting " + rootDirProperty + " system property");
}
try {
rootDir = canonicalize(Paths.get(rootDirString));
} catch (NativeImageError e) {
throw showError("Invalid " + rootDirProperty + " setting " + rootDirString + "\n" + e.getMessage());
public interface PathsProvider {
Path getWorkDir();

Path getRootDir();
}

private static class DefaultPathsProvider implements PathsProvider {
private final Path workDir;
private final Path rootDir;

DefaultPathsProvider() {
workDir = Paths.get(".").toAbsolutePath().normalize();
if (IS_AOT) {
Path executablePath = Paths.get((String) Compiler.command(new Object[]{PosixExecutableName.getKey()}));
assert executablePath != null;
Path binDir = executablePath.getParent();
Path rootDirCandidate = binDir.getParent();
if (rootDirCandidate.endsWith(platform)) {
rootDirCandidate = rootDirCandidate.getParent();
}
if (rootDirCandidate.endsWith(Paths.get("lib", "svm"))) {
rootDirCandidate = rootDirCandidate.getParent().getParent();
}
rootDir = rootDirCandidate;
} else {
String rootDirProperty = "native-image.root";
String rootDirString = System.getProperty(rootDirProperty);
if (rootDirString == null) {
throw showError("Running on JVM requires setting " + rootDirProperty + " system property");
}
rootDir = Paths.get(rootDirString);
}
}
assert rootDir != null;

@Override
public Path getWorkDir() {
return workDir;
}

@Override
public Path getRootDir() {
return rootDir;
}
}

protected NativeImage(PathsProvider pathsProvider) {
workDir = pathsProvider.getWorkDir();
try {
rootDir = canonicalize(pathsProvider.getRootDir());
} catch (NativeImageError e) {
throw showError("PathsProvider provides invalid rootDir " + pathsProvider.getRootDir() + "\n" + e.getMessage());
}

String configFileEnvVarKey = "NATIVE_IMAGE_CONFIG_FILE";
String configFile = System.getenv(configFileEnvVarKey);
Expand Down Expand Up @@ -242,7 +266,7 @@ protected NativeImage() {
addImageBuilderArg(oHPath + workDir);

/* Discover supported MacroOptions */
optionRegistry = new MacroOption.Registry(canonicalize(getRootDir()));
optionRegistry = new MacroOption.Registry(canonicalize(rootDir));

/* Default handler needs to be fist */
registerOptionHandler(new DefaultOptionHandler(this));
Expand All @@ -259,10 +283,6 @@ protected void registerOptionHandler(OptionHandler<? extends NativeImage> handle
optionHandlers.add(handler);
}

protected Path getRootDir() {
return rootDir;
}

protected Map<String, String> getUserConfigProperties() {
return userConfigProperties;
}
Expand Down Expand Up @@ -291,7 +311,7 @@ protected static void ensureDirectoryExists(Path dir) {
}

private void prepareImageBuildArgs() {
Path svmDir = getRootDir().resolve(Paths.get("lib", "svm"));
Path svmDir = rootDir.resolve(Paths.get("lib", "svm"));
getJars(svmDir.resolve("builder")).forEach(this::addImageBuilderClasspath);
getJars(svmDir).forEach(this::addImageProvidedClasspath);
Path clibrariesDir = svmDir.resolve("clibraries").resolve(platform);
Expand All @@ -300,7 +320,7 @@ private void prepareImageBuildArgs() {
addImageBuilderArg(oHInspectServerContentPath + svmDir.resolve("inspect"));
}

Path jvmciDir = getRootDir().resolve(Paths.get("lib", "jvmci"));
Path jvmciDir = rootDir.resolve(Paths.get("lib", "jvmci"));
getJars(jvmciDir).forEach((Consumer<? super Path>) this::addImageBuilderClasspath);
try {
addImageBuilderJavaArgs(Files.list(jvmciDir)
Expand All @@ -312,7 +332,7 @@ private void prepareImageBuildArgs() {
showError("Unable to use jar-files from directory " + jvmciDir, e);
}

Path bootDir = getRootDir().resolve(Paths.get("lib", "boot"));
Path bootDir = rootDir.resolve(Paths.get("lib", "boot"));
getJars(bootDir).forEach((Consumer<? super Path>) this::addImageBuilderBootClasspath);
}

Expand Down Expand Up @@ -368,7 +388,7 @@ private Stream<String> getRelativeLauncherClassPath() {
}

private Stream<Path> getAbsoluteLauncherClassPath() {
return getRelativeLauncherClassPath().map(s -> Paths.get(s.replace('/', File.separatorChar))).map(p -> getRootDir().resolve(p));
return getRelativeLauncherClassPath().map(s -> Paths.get(s.replace('/', File.separatorChar))).map(p -> rootDir.resolve(p));
}

protected static String consolidateSingleValueArg(Collection<String> args, String argPrefix) {
Expand Down Expand Up @@ -530,7 +550,7 @@ private void completeImageBuildArgs(String[] args) {
addImageClasspath(p);
}
});
addImageClasspath(getRootDir().resolve(Paths.get("lib", "graalvm", "launcher-common.jar")));
addImageClasspath(rootDir.resolve(Paths.get("lib", "graalvm", "launcher-common.jar")));
}

if (!leftoverArgs.isEmpty()) {
Expand Down Expand Up @@ -599,15 +619,7 @@ protected void buildImage(LinkedHashSet<String> javaArgs, LinkedHashSet<Path> bc

public static void main(String[] args) {
try {
NativeImage nativeImage = IS_AOT ? new NativeImageServer() : new NativeImage();

if (args.length == 0) {
nativeImage.showMessage(usageText);
System.exit(0);
}

nativeImage.prepareImageBuildArgs();
nativeImage.completeImageBuildArgs(args);
build(new DefaultPathsProvider(), args);
} catch (NativeImageError e) {
boolean verbose = Boolean.valueOf(System.getenv("VERBOSE_GRAALVM_LAUNCHERS"));
NativeImage.show(System.err::println, "Error: " + e.getMessage());
Expand All @@ -623,6 +635,16 @@ public static void main(String[] args) {
}
}

public static void build(PathsProvider pathsProvider, String[] args) {
NativeImage nativeImage = IS_AOT ? new NativeImageServer(pathsProvider) : new NativeImage(pathsProvider);
if (args.length == 0) {
nativeImage.showMessage(usageText);
} else {
nativeImage.prepareImageBuildArgs();
nativeImage.completeImageBuildArgs(args);
}
}

Path canonicalize(Path path) {
Path absolutePath = path.isAbsolute() ? path : workDir.resolve(path);
boolean hasWildcard = absolutePath.endsWith("*");
Expand All @@ -647,7 +669,7 @@ Path canonicalize(Path path) {
}

Path getJavaHome() {
Path javaHomePath = getRootDir().getParent();
Path javaHomePath = rootDir.getParent();
Path binJava = Paths.get("bin", "java");
if (Files.isExecutable(javaHomePath.resolve(binJava))) {
return javaHomePath;
Expand Down Expand Up @@ -752,7 +774,7 @@ void showWarning(String message) {
}

@SuppressWarnings("serial")
static final class NativeImageError extends Error {
public static final class NativeImageError extends Error {
private NativeImageError(String message) {
super(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ final class NativeImageServer extends NativeImage {
private volatile Server building = null;
private final List<FileChannel> openFileChannels = new ArrayList<>();

NativeImageServer() {
super();
NativeImageServer(PathsProvider pathsProvider) {
super(pathsProvider);
registerOptionHandler(new ServerOptionHandler(this));
}

Expand Down

0 comments on commit 12f8df1

Please sign in to comment.