Skip to content

Commit

Permalink
Merge pull request #558 from Peergos/fix/native-image-builds
Browse files Browse the repository at this point in the history
Fix native image builds
  • Loading branch information
ianopolous authored Mar 26, 2024
2 parents 6604580 + 0ee05ea commit 1d5c016
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
23 changes: 12 additions & 11 deletions native-build/BuildNativeImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
import java.net.*;
import java.nio.file.*;
import java.util.zip.*;
import java.util.*;

public class BuildNativeImage {

public static void main(String[] a) throws Exception {
String VERSION = "22.3.3";
String JAVA_VERSION = "java17";

Files.copy(Paths.get("../server/Peergos.jar"), Paths.get("Peergos.jar"), StandardCopyOption.REPLACE_EXISTING);

String OS = canonicaliseOS(System.getProperty("os.name").toLowerCase());
Expand All @@ -17,12 +15,15 @@ public static void main(String[] a) throws Exception {
String binExt = OS.equals("windows") ? ".cmd" : "";
String extraDirs = OS.equals("darwin") ? "/Contents/Home" : "";

if (! new File("graalvm-ce-" + JAVA_VERSION + "-"+VERSION + extraDirs + "/bin/native-image" + binExt).exists())
throw new IllegalStateException("native-image not installed...");
String ext = OS.equals("windows") ? ".exe" : "";

Optional<Path> nativeImage = Files.walk(Paths.get(""), 5)
.filter(p -> p.getFileName().startsWith("native-image") && p.toFile().isFile())
.findFirst();
if (nativeImage.isEmpty())
throw new IllegalStateException("Couldn't find native image executable");

// run native-image
runCommand("graalvm-ce-" + JAVA_VERSION + "-"+VERSION + extraDirs + "/bin/native-image" + binExt +
runCommand(nativeImage.get().toString() +
" --allow-incomplete-classpath " +
"-H:EnableURLProtocols=http " +
"-H:EnableURLProtocols=https " +
Expand Down Expand Up @@ -53,19 +54,19 @@ private static String getOsArch() {

private static String canonicaliseArchitecture(String arch) {
if (arch.startsWith("arm64"))
return "arm64";
return "aarch64";
if (arch.startsWith("arm"))
return "arm";
if (arch.startsWith("x86_64"))
return "amd64";
if (arch.startsWith("amd64") || arch.startsWith("x86_64"))
return "x64";
if (arch.startsWith("x86"))
return "386";
return arch;
}

private static String canonicaliseOS(String os) {
if (os.startsWith("mac"))
return "darwin";
return "macos";
if (os.startsWith("windows"))
return "windows";
return os;
Expand Down
28 changes: 15 additions & 13 deletions native-build/InstallNativeImage.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.util.zip.*;
import java.util.*;

public class InstallNativeImage {

public static void main(String[] a) throws Exception {
String VERSION = "22.3.3";
String VERSION = "22.0.0";

String OS = canonicaliseOS(System.getProperty("os.name").toLowerCase());
String OS_ARCH = getOsArch();
System.out.println("OS-ARCH: " + OS_ARCH);
String ext = OS.equals("windows") ? ".zip" : ".tar.gz";
String JAVA_VERSION = "java17";
String filename = "graalvm-ce-" + JAVA_VERSION + "-" + OS_ARCH + "-"+VERSION+ext;
String url = "https://github.com/graalvm/graalvm-ce-builds/releases/download/vm-" +VERSION +"/" + filename;
String ext = OS.equals("windows") ? "bin.zip" : "bin.tar.gz";
String filename = "graalvm-community-jdk-" + VERSION + "_" + OS_ARCH + "_"+ext;
String url = "https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-" +VERSION +"/" + filename;

// Download graalVM
if (! new File(filename).exists())
Expand All @@ -25,10 +26,12 @@ public static void main(String[] a) throws Exception {

// install native-image
String binExt = OS.equals("windows") ? ".cmd" : "";
String extraDirs = OS.equals("darwin") ? "/Contents/Home" : "";
runCommand("./graalvm-ce-" + JAVA_VERSION + "-" + VERSION + extraDirs + "/bin/gu" + binExt + " install native-image");

if (new File("graalvm-ce-" + JAVA_VERSION + "-"+VERSION + extraDirs + "/bin/native-image" + binExt).exists())
Optional<Path> nativeImage = Files.walk(Paths.get("."), 5)
.filter(p -> p.getFileName().toString().contains("native-image") && !p.toFile().isDirectory())
.findFirst();

if (nativeImage.isPresent())
System.out.println("native-image installed");
else
throw new IllegalStateException("native-image not installed...");
Expand Down Expand Up @@ -62,25 +65,24 @@ public static int runCommand(String command) throws Exception {
private static String getOsArch() {
String os = canonicaliseOS(System.getProperty("os.name").toLowerCase());
String arch = canonicaliseArchitecture(System.getProperty("os.arch"));

return os + "-" + arch;
}

private static String canonicaliseArchitecture(String arch) {
if (arch.startsWith("arm64"))
return "arm64";
return "aarch64";
if (arch.startsWith("arm"))
return "arm";
if (arch.startsWith("x86_64"))
return "amd64";
if (arch.startsWith("amd64") || arch.startsWith("x86_64"))
return "x64";
if (arch.startsWith("x86"))
return "386";
return arch;
}

private static String canonicaliseOS(String os) {
if (os.startsWith("mac"))
return "darwin";
return "macos";
if (os.startsWith("windows"))
return "windows";
return os;
Expand Down

0 comments on commit 1d5c016

Please sign in to comment.