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

Refactor Architecture, OS, Vendor and TripletProfile into enums #229

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
46 changes: 0 additions & 46 deletions src/main/java/com/gluonhq/substrate/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,6 @@ public class Constants {
public static final Path USER_SUBSTRATE_PATH = Path.of(System.getProperty("user.home"))
.resolve(".gluon").resolve("substrate");

/**
* Triplet architecture
*/
public static final String ARCH_AMD64 = "x86_64";
public static final String ARCH_ARM64 = "arm64";
public static final String ARCH_AARCH64 = "aarch64";

/**
* Triplet vendor
*/
public static final String VENDOR_APPLE = "apple";
public static final String VENDOR_LINUX = "linux";
public static final String VENDOR_MICROSOFT = "microsoft";

/**
* Triplet OS
*/
public static final String OS_DARWIN = "darwin";
public static final String OS_IOS = "ios";
public static final String OS_LINUX = "linux";
public static final String OS_WINDOWS = "windows";
public static final String OS_ANDROID = "android";

/**
* Predefined Profiles
*/
public enum Profile {
LINUX, // (x86_64-linux-linux)
MACOS, // (x86_64-apple-darwin)
WINDOWS, // (x86_64-windows-windows)
IOS, // (aarch64-apple-ios)
IOS_SIM, // (x86_64-apple-ios)
ANDROID // (aarch64-linux-android);
};

/**
* Supported hosts
*
Expand All @@ -88,17 +53,6 @@ public enum Profile {
@Deprecated public static final String TARGET_IOS = "ios";
@Deprecated public static final String TARGET_IOS_SIM = "ios-sim";

/**
* Supported profiles
*
*/
public static final String PROFILE_HOST = "host"; // either mac or linux, based on host
public static final String PROFILE_MAC = "macos";
public static final String PROFILE_LINUX = "linux";
public static final String PROFILE_IOS = "ios";
public static final String PROFILE_IOS_SIM = "ios-sim";
public static final String PROFILE_ANDROID = "android";

public static final String DEFAULT_JAVA_STATIC_SDK_VERSION = "14-ea+5";
public static final String DEFAULT_JAVAFX_STATIC_SDK_VERSION = "14-ea+gvm2";

Expand Down
17 changes: 3 additions & 14 deletions src/main/java/com/gluonhq/substrate/SubstrateDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@
import com.gluonhq.substrate.model.InternalProjectConfiguration;
import com.gluonhq.substrate.model.ProcessPaths;
import com.gluonhq.substrate.model.Triplet;
import com.gluonhq.substrate.target.AndroidTargetConfiguration;
import com.gluonhq.substrate.target.DarwinTargetConfiguration;
import com.gluonhq.substrate.target.IosTargetConfiguration;
import com.gluonhq.substrate.target.LinuxTargetConfiguration;
import com.gluonhq.substrate.target.TargetConfiguration;
import com.gluonhq.substrate.target.WindowsTargetConfiguration;
import com.gluonhq.substrate.target.TripletProfile;
import com.gluonhq.substrate.util.Strings;

import java.io.IOException;
Expand All @@ -61,7 +57,7 @@ public static void main(String[] args) throws Exception {
String appName = Optional.ofNullable(System.getProperty("appname")).orElse("anonymousApp");
String targetProfile = System.getProperty("targetProfile");

Triplet targetTriplet = targetProfile != null? new Triplet(Constants.Profile.valueOf(targetProfile.toUpperCase()))
Triplet targetTriplet = targetProfile != null? new Triplet(TripletProfile.valueOf(targetProfile.toUpperCase()))
:Triplet.fromCurrentOS();

String expected = System.getProperty("expected");
Expand Down Expand Up @@ -156,14 +152,7 @@ public SubstrateDispatcher(Path buildRoot, ProjectConfiguration config) throws I
}

private TargetConfiguration getTargetConfiguration(Triplet targetTriplet) {
switch (targetTriplet.getOs()) {
case Constants.OS_LINUX : return new LinuxTargetConfiguration(paths, config);
case Constants.OS_DARWIN : return new DarwinTargetConfiguration(paths, config);
case Constants.OS_WINDOWS: return new WindowsTargetConfiguration(paths, config);
case Constants.OS_IOS : return new IosTargetConfiguration(paths, config);
case Constants.OS_ANDROID: return new AndroidTargetConfiguration(paths, config);
default : return null;
}
return targetTriplet.getOs().getTargetConfiguration(paths, config);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import com.gluonhq.substrate.Constants;
import com.gluonhq.substrate.ProjectConfiguration;
import com.gluonhq.substrate.target.OS;
import com.gluonhq.substrate.util.Strings;

import java.io.BufferedReader;
Expand Down Expand Up @@ -374,7 +375,7 @@ public void canRunNativeImage() throws IOException {
if (!Files.exists(graalPath)) throw new IOException("Path provided for GraalVM doesn't exist: " + graalPath);
Path binPath = graalPath.resolve("bin");
if (!Files.exists(binPath)) throw new IOException("Path provided for GraalVM doesn't contain a bin directory: " + graalPath);
Path niPath = Constants.OS_WINDOWS.equals(getHostTriplet().getOs()) ?
Path niPath = OS.WINDOWS.equals(getHostTriplet().getOs()) ?
binPath.resolve("native-image.cmd") :
binPath.resolve("native-image");
if (!Files.exists(niPath)) throw new IOException("Path provided for GraalVM doesn't contain bin/native-image: " + graalPath + "\n" +
Expand Down
98 changes: 22 additions & 76 deletions src/main/java/com/gluonhq/substrate/model/Triplet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,79 +27,37 @@
*/
package com.gluonhq.substrate.model;

import com.gluonhq.substrate.Constants;
import com.gluonhq.substrate.target.*;

import java.util.Locale;

import static com.gluonhq.substrate.Constants.*;
import java.util.Objects;

public class Triplet {

private String arch;
private String vendor;
private String os;
private Architecture arch;
private OS os;
private Vendor vendor;

/**
* Creates a new triplet for the current runtime
* @return the triplet for the current runtime
* @throws IllegalArgumentException in case the current operating system is not supported
*/
public static Triplet fromCurrentOS() throws IllegalArgumentException {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);

if (osName.contains("mac")) {
return new Triplet(Constants.Profile.MACOS);
} else if (osName.contains("nux")) {
return new Triplet(Constants.Profile.LINUX);
} else if (osName.contains("windows")) {
return new Triplet(Constants.Profile.WINDOWS);
} else {
throw new IllegalArgumentException("OS " + osName + " not supported");
}
return new Triplet( TripletProfile.fromCurrentOS());
}

public Triplet(String arch, String vendor, String os) {
this.arch = arch;
this.vendor = vendor;
this.os = os;

// following should throw exception for unsupported parts
this.arch = Architecture.valueOf(arch);
this.os = OS.valueOf(os);
this.vendor = Vendor.valueOf(vendor);
}

public Triplet(Constants.Profile profile) {
switch (profile) {
case LINUX:
this.arch = ARCH_AMD64;
this.vendor = VENDOR_LINUX;
this.os = OS_LINUX;
break;
case MACOS:
this.arch = ARCH_AMD64;
this.vendor = VENDOR_APPLE;
this.os = OS_DARWIN;
break;
case WINDOWS:
this.arch = ARCH_AMD64;
this.vendor = VENDOR_MICROSOFT;
this.os = OS_WINDOWS;
break;
case IOS:
this.arch = ARCH_ARM64;
this.vendor = VENDOR_APPLE;
this.os = OS_IOS;
break;
case IOS_SIM:
this.arch = ARCH_AMD64;
this.vendor = VENDOR_APPLE;
this.os = OS_IOS;
break;
case ANDROID:
this.arch = ARCH_AARCH64;
this.vendor = VENDOR_LINUX;
this.os = OS_ANDROID;
break;
default:
throw new IllegalArgumentException("Triplet for profile "+profile+" is not supported yet");
}
public Triplet(TripletProfile profile) {
Objects.requireNonNull(profile);
this.arch = profile.getArch();
this.os = profile.getOs();
}

/*
Expand All @@ -111,34 +69,22 @@ public boolean canCompileTo(Triplet target) {
if (getOs().equals(target.getOs())) return true;

// if host is linux and target is ios, fail
return (!Constants.OS_LINUX.equals(getOs()) && !Constants.OS_WINDOWS.equals(getOs())) ||
!Constants.OS_IOS.equals(target.getOs());
return (!OS.LINUX.equals(getOs()) && !OS.WINDOWS.equals(getOs())) ||
!OS.IOS.equals(target.getOs());
}

public String getArch() {
public Architecture getArch() {
return arch;
}

public void setArch(String arch) {
this.arch = arch;
}

public String getVendor() {
public Vendor getVendor() {
return vendor;
}

public void setVendor(String vendor) {
this.vendor = vendor;
}

public String getOs() {
public OS getOs() {
return os;
}

public void setOs(String os) {
this.os = os;
}

public String getArchOs() {
return this.arch+"-"+this.os;
}
Expand All @@ -153,15 +99,15 @@ public String getOsArch() {
* @return
*/
public String getOsArch2() {
String myarch = this.arch;
if (myarch.equals("x86_64")) {
String myarch = this.arch.toString();
if ( "x86_64".equals(myarch)) {
myarch = "amd64";
}
return this.os+"-"+myarch;
}

@Override
public String toString() {
return arch + '-' + vendor + '-' + os;
return arch.toString() + '-' + vendor + '-' + os;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public boolean compile(String cp) throws IOException, InterruptedException {
extractNativeLibs(cp);
Triplet target = projectConfiguration.getTargetTriplet();
String suffix = target.getArchOs();
String jniPlatform = getJniPlatform(target.getOs());
String jniPlatform = target.getOs().getJniPlatform();
if (!compileAdditionalSources()) {
return false;
}
Expand Down Expand Up @@ -326,17 +326,6 @@ private boolean compileAdditionalSources()
return true;
}

private String getJniPlatform( String os ) {
switch (os) {
case Constants.OS_LINUX: return "LINUX_AMD64";
case Constants.OS_IOS:return "DARWIN_AARCH64";
case Constants.OS_DARWIN: return "DARWIN_AMD64";
case Constants.OS_WINDOWS: return "WINDOWS_AMD64";
case Constants.OS_ANDROID: return "LINUX_AARCH64";
default: throw new IllegalArgumentException("No support yet for " + os);
}
}

/*
* Make sure the clibraries needed for linking are available for this particular configuration.
* The clibraries path is available by default in GraalVM, but the directory for cross-platform libs may
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/gluonhq/substrate/target/Architecture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.gluonhq.substrate.target;

/**
* Triplet architecture
*/
public enum Architecture {
AMD64("x86_64"),
ARM64("arm64"),
AARCH64("aarch64");

private final String id;

private Architecture(String id ) {
this.id = id.toLowerCase();
}

@Override
public String toString() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ List<String> getTargetSpecificLinkLibraries() {
@Override
List<String> getTargetSpecificLinkFlags(boolean useJavaFX, boolean usePrismSW) {
List<String> linkFlags = new ArrayList<>(Arrays.asList("-w", "-fPIC",
"-arch", Constants.ARCH_ARM64,
"-arch", Architecture.ARM64.toString(),
"-mios-version-min=11.0",
"-isysroot", getSysroot()));
if (useJavaFX) {
Expand All @@ -101,7 +101,7 @@ List<String> getTargetSpecificLinkFlags(boolean useJavaFX, boolean usePrismSW) {
@Override
List<String> getTargetSpecificCCompileFlags() {
return Arrays.asList("-xobjective-c",
"-arch", getArch(),
"-arch", getArch().toString(),
"-isysroot", getSysroot());
}

Expand Down Expand Up @@ -222,7 +222,7 @@ String processClassPath(String classPath) throws IOException {
fileDeps.getJavaFXSDKLibsPath(),"javafx-graphics","javafx-controls" );
}

private String getArch() {
private Architecture getArch() {
return projectConfiguration.getTargetTriplet().getArch();
}

Expand All @@ -232,7 +232,7 @@ private String getSysroot() {
}

private boolean isSimulator() {
return Constants.ARCH_AMD64.equals(projectConfiguration.getTargetTriplet().getArch());
return Architecture.AMD64.equals(projectConfiguration.getTargetTriplet().getArch());
}

private void createInfoPlist(ProcessPaths paths) throws IOException {
Expand All @@ -248,7 +248,7 @@ private void createInfoPlist(ProcessPaths paths) throws IOException {

private boolean lipoMatch(Path path) {
try {
return lipoInfo(path).indexOf(getArch()) > 0;
return lipoInfo(path).indexOf(getArch().toString()) > 0;
} catch (IOException | InterruptedException e) {
Logger.logSevere("Error processing lipo for " + path);
}
Expand Down
Loading