Skip to content

Commit

Permalink
- add java and graalvm version fields in InternalProjectConfiguration
Browse files Browse the repository at this point in the history
- only add '--add-exports' argument when needed
  • Loading branch information
kkriske committed Nov 15, 2023
1 parent 6a65d3b commit 8dd325b
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 86 deletions.
20 changes: 8 additions & 12 deletions src/main/java/com/gluonhq/substrate/SubstrateDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,29 +379,25 @@ public SubstrateDispatcher(Path buildRoot, ProjectConfiguration config) throws I
System.out.println("Configuration: " + this.config);
}

Version javaVersion = this.config.checkGraalVMJavaVersion();
this.config.checkGraalVMVersion(javaVersion);
this.config.checkGraalVMVendor();

Triplet targetTriplet = config.getTargetTriplet();

this.targetConfiguration = Objects.requireNonNull(getTargetConfiguration(targetTriplet, javaVersion),
this.targetConfiguration = Objects.requireNonNull(getTargetConfiguration(targetTriplet),
"Error: Target Configuration was not found for " + targetTriplet);

Logger.logInit(paths.getLogPath().toString(), this.config.isVerbose());
}

private TargetConfiguration getTargetConfiguration(Triplet targetTriplet, Version javaVersion) throws IOException {
private TargetConfiguration getTargetConfiguration(Triplet targetTriplet) throws IOException {
if (!Constants.OS_WEB.equals(targetTriplet.getOs()) && !config.getHostTriplet().canCompileTo(targetTriplet)) {
throw new IllegalArgumentException("We currently can't compile to " + targetTriplet + " when running on " + config.getHostTriplet());
}
switch (targetTriplet.getOs()) {
case Constants.OS_LINUX : return new LinuxTargetConfiguration(paths, config, javaVersion);
case Constants.OS_DARWIN : return new MacOSTargetConfiguration(paths, config, javaVersion);
case Constants.OS_WINDOWS: return new WindowsTargetConfiguration(paths, config, javaVersion);
case Constants.OS_IOS : return new IosTargetConfiguration(paths, config, javaVersion);
case Constants.OS_ANDROID: return new AndroidTargetConfiguration(paths, config, javaVersion);
case Constants.OS_WEB : return new WebTargetConfiguration(paths, config, javaVersion);
case Constants.OS_LINUX : return new LinuxTargetConfiguration(paths, config);
case Constants.OS_DARWIN : return new MacOSTargetConfiguration(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);
case Constants.OS_WEB : return new WebTargetConfiguration(paths, config);
default : return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public class InternalProjectConfiguration {
private boolean useJavaFX = false;
private boolean usePrismSW = false;
private boolean enableCheckHash = true;
private boolean usesJDK11 = false;
private boolean sharedLibrary = false;
private boolean staticLibrary = false;

Expand All @@ -75,12 +74,14 @@ public class InternalProjectConfiguration {
private List<String> releaseSymbolsList;

private final ProjectConfiguration publicConfig;
private final Version javaVersion;
private final Version graalVersion;

/**
* Private projects configuration, which includes everything, including public settings
* @param config public project configuration
*/
public InternalProjectConfiguration(ProjectConfiguration config) {
public InternalProjectConfiguration(ProjectConfiguration config) throws IOException {

this.publicConfig = Objects.requireNonNull(config);

Expand Down Expand Up @@ -115,13 +116,19 @@ public InternalProjectConfiguration(ProjectConfiguration config) {
}

performHostChecks();

javaVersion = findGraalVMJavaVersion();
graalVersion = findGraalVersion();
checkGraalVMJavaVersion();
checkGraalVMVersion();
checkGraalVMVendor();
}

public Path getGraalPath() {
return Objects.requireNonNull(this.publicConfig.getGraalPath(), "GraalVM Path is not defined");
}

public Version getGraalVersion() throws IOException {
private Version findGraalVersion() throws IOException {
String pattern = "GraalVM .*?(\\d\\d.\\d.\\d)";
ProcessRunner graalJava;
try {
Expand All @@ -140,6 +147,10 @@ public Version getGraalVersion() throws IOException {
return new Version(m.group(1));
}

public Version getGraalVersion() {
return graalVersion;
}

/**
* Returns the version string for the static JDK libs.
* If this has not been specified before, the default will be
Expand Down Expand Up @@ -469,16 +480,15 @@ public ReleaseConfiguration getReleaseConfiguration() {
}

public boolean usesJDK11() {
return usesJDK11;
return javaVersion.getMajor() == 11;
}

/**
* Check if the GraalVM provided by the configuration is supported
* @throws IOException if the GraalVM version is older than the minimum supported version
*/
public void checkGraalVMVersion(Version javaVersion) throws IOException {
private void checkGraalVMVersion() throws IOException {
if (isOldGraalVMVersioningScheme(javaVersion)) {
Version graalVersion = getGraalVersion();
if (graalVersion.compareTo(new Version(Constants.GRAALVM_MIN_VERSION)) < 0) {
throw new IOException("Current GraalVM version (" + graalVersion + ") not supported.\n" +
"Please upgrade to " + Constants.GRAALVM_MIN_VERSION + " or higher");
Expand All @@ -502,21 +512,18 @@ public boolean isOldGraalVMVersioningScheme(Version javaVersion) {
* Check if the GraalVM's java provided by the configuration is supported
* @throws IOException if the GraalVM's java version is older than the minimum supported version
*/
public Version checkGraalVMJavaVersion() throws IOException {
Version javaVersion = getGraalVMJavaVersion();
private void checkGraalVMJavaVersion() throws IOException {
if (javaVersion.compareTo(new Version(Constants.GRAALVM_JAVA_MIN_VERSION)) < 0) {
throw new IOException("Current GraalVM's java version (" + javaVersion + ") not supported.\n" +
"Please upgrade to " + Constants.GRAALVM_JAVA_MIN_VERSION + " or higher");
}
usesJDK11 = javaVersion.getMajor() == 11;
return javaVersion;
}

/**
* Check if Gluon is the vendor of the GraalVM build, or else logs a message.
* @throws IOException if the GraalVM path or the GraalVM/release file don't exist
*/
public void checkGraalVMVendor() throws IOException {
private void checkGraalVMVendor() throws IOException {
Path graalPath = getGraalPath();
if (!Files.exists(graalPath)) {
throw new IOException("Path provided for GraalVM doesn't exist: " + graalPath);
Expand Down Expand Up @@ -663,13 +670,13 @@ private void checkGraalVMPermissions(String graalvmHome) {
}

/**
* Gets the Java version that GraalVM bundles
* try to find the Java version that GraalVM bundles
* @return the Java version of the GraalVM's build
* @throws NullPointerException when the configuration is null
* @throws IllegalArgumentException when the configuration doesn't contain a property graalPath
* @throws IOException if the Java version can't be found
*/
public Version getGraalVMJavaVersion() throws IOException {
private Version findGraalVMJavaVersion() throws IOException {
ProcessRunner graalJava = null;
try {
graalJava = new ProcessRunner(getGraalVMBinPath().resolve("java").toString(), "-version");
Expand All @@ -693,6 +700,14 @@ public Version getGraalVMJavaVersion() throws IOException {
return new Version(m.group(1));
}

/**
* Gets the Java version that GraalVM bundles
* @return the Java version of the GraalVM build
*/
public Version getJavaVersion() {
return javaVersion;
}

@Override
public String toString() {
return "ProjectConfiguration{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public abstract class AbstractTargetConfiguration implements TargetConfiguration
"png", "jpg", "jpeg", "gif", "bmp", "ttf", "raw",
"xml", "fxml", "css", "gls", "json", "dat",
"license", "frag", "vert", "obj", "mtl", "js");
/**
* Manual registration of the HomeFinderFeature required until GraalVM for JDK 21.
*/
private static final String HOME_FINDER_FEATURE = "org.graalvm.home.HomeFinderFeature";

private static final List<String> baseNativeImageArguments = Arrays.asList(
"-Djdk.internal.lambda.eagerlyInitialize=false",
Expand All @@ -83,8 +87,7 @@ public abstract class AbstractTargetConfiguration implements TargetConfiguration
"-H:+ReportExceptionStackTraces",
"-H:-DeadlockWatchdogExitOnTimeout",
"-H:DeadlockWatchdogInterval=0",
"-H:+RemoveSaturatedTypeFlows",
"--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED" // Required for GluonFeature
"-H:+RemoveSaturatedTypeFlows"
);
private static final List<String> verboseNativeImageArguments = Arrays.asList(
"-H:+PrintAnalysisCallTree",
Expand All @@ -94,18 +97,16 @@ public abstract class AbstractTargetConfiguration implements TargetConfiguration
final FileDeps fileDeps;
final InternalProjectConfiguration projectConfiguration;
final ProcessPaths paths;
final Version javaVersion;
protected final boolean crossCompile;

private final List<String> defaultAdditionalSourceFiles = Collections.singletonList("launcher.c");
private final List<String> defaultStaticJavaLibs = List.of("java", "nio", "zip", "net", "prefs", "jvm",
"fdlibm", "z", "dl", "j2pkcs11", "sunec", "jaas", "extnet");

AbstractTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) {
AbstractTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) {
this.projectConfiguration = configuration;
this.fileDeps = new FileDeps(configuration);
this.paths = paths;
this.javaVersion = javaVersion;
this.crossCompile = !configuration.getHostTriplet().equals(configuration.getTargetTriplet());
}

Expand Down Expand Up @@ -140,6 +141,8 @@ public boolean compile() throws IOException, InterruptedException {

baseNativeImageArguments.forEach(compileRunner::addArg);

compileRunner.addArgs(getNativeImageArguments());

if (!projectConfiguration.isSharedLibrary() ||
!projectConfiguration.getTargetTriplet().equals(Triplet.fromCurrentOS())) {
compileRunner.addArg("-H:+ExitAfterRelocatableImageWrite");
Expand Down Expand Up @@ -381,13 +384,8 @@ private String getJniPlatformArg() {

private String getJniPlatform() {
Triplet target = projectConfiguration.getTargetTriplet();
boolean graalVM221 = false;
try {
Version graalVersion = projectConfiguration.getGraalVersion();
graalVM221 = ((graalVersion.getMajor() > 21) && (graalVersion.getMinor() > 0));
} catch (IOException ex) {
Logger.logFatal(ex, "Could not detect GraalVM version, stopping now.");
}
Version graalVersion = projectConfiguration.getGraalVersion();
boolean graalVM221 = ((graalVersion.getMajor() > 21) && (graalVersion.getMinor() > 0));
String os = target.getOs();
String arch = target.getArch();
switch (os) {
Expand Down Expand Up @@ -493,14 +491,18 @@ private String getNativeImagePath() {
.toString();
}

protected List<String> getNativeImageArguments() {
return List.of();
}

protected List<String> getEnabledFeatures() {
return List.of();
}

private List<String> getEnabledFeaturesArgs() {
List<String> args = new ArrayList<>();
if (javaVersion.getMajor() < 21) {
args.add("--features=org.graalvm.home.HomeFinderFeature");
if (projectConfiguration.getJavaVersion().getMajor() < 21) {
args.add("--features=" + HOME_FINDER_FEATURE);
}
for (String feature : getEnabledFeatures()) {
args.add("--features=" + feature);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.ProcessRunner;
import com.gluonhq.substrate.util.Version;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -97,8 +96,8 @@ public class AndroidTargetConfiguration extends PosixTargetConfiguration {
private static final String javafxWebLib = "-lwebview";
private final String capLocation = ANDROID_NATIVE_FOLDER + "cap/";

public AndroidTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) throws IOException {
super(paths,configuration, javaVersion);
public AndroidTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) throws IOException {
super(paths,configuration);

this.sdk = fileDeps.getAndroidSDKPath().toString();
this.ndk = fileDeps.getAndroidNDKPath().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
import com.gluonhq.substrate.model.InternalProjectConfiguration;
import com.gluonhq.substrate.model.ProcessPaths;
import com.gluonhq.substrate.util.ProcessRunner;
import com.gluonhq.substrate.util.Version;

import java.io.IOException;
import java.nio.file.Path;

abstract class DarwinTargetConfiguration extends PosixTargetConfiguration {

DarwinTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) {
super(paths, configuration, javaVersion);
DarwinTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) {
super(paths, configuration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.ProcessRunner;
import com.gluonhq.substrate.util.Version;
import com.gluonhq.substrate.util.XcodeUtils;
import com.gluonhq.substrate.util.ios.CodeSigning;
import com.gluonhq.substrate.util.ios.Deploy;
Expand Down Expand Up @@ -79,8 +78,8 @@ public class IosTargetConfiguration extends DarwinTargetConfiguration {
private static final String capLocation= "/native/ios/cap/";
private static final String iosCheck = "ios/check";

public IosTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) {
super(paths, configuration, javaVersion);
public IosTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) {
super(paths, configuration);

if (!isSimulator() && projectConfiguration.getBackend() == null) {
projectConfiguration.setBackend(Constants.BACKEND_LIR);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class LinuxTargetConfiguration extends PosixTargetConfiguration {
"-lWTF", "-licuuc", "-licudata"
);

private static final List<String> nativeImageArguments = List.of(
"--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED" // required for the GluonFeature
);
private static final List<String> enabledFeatures = List.of(
"com.gluonhq.substrate.feature.GluonFeature"
);
Expand All @@ -116,8 +119,8 @@ public class LinuxTargetConfiguration extends PosixTargetConfiguration {

private final boolean isAarch64;

public LinuxTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) throws IOException {
super(paths, configuration, javaVersion);
public LinuxTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) throws IOException {
super(paths, configuration);
this.isAarch64 = projectConfiguration.getTargetTriplet().getArch().equals(Constants.ARCH_AARCH64);
sysroot = fileDeps.getSysrootPath().toString();
}
Expand Down Expand Up @@ -240,7 +243,7 @@ List<String> getStaticJavaLibs() {
throw new RuntimeException ("No static java libs found, cannot continue");
}
List<String> libs;
if (javaVersion.getMajor() >= 21) {
if (projectConfiguration.getJavaVersion().getMajor() >= 21) {
libs = staticJavaLibs21;
} else {
libs = staticJavaLibs;
Expand Down Expand Up @@ -274,6 +277,11 @@ protected List<Path> getLinkerLibraryPaths() throws IOException {
return linkerLibraryPaths;
}

@Override
protected List<String> getNativeImageArguments() {
return nativeImageArguments;
}

@Override
public List<String> getEnabledFeatures() {
return enabledFeatures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import com.gluonhq.substrate.model.ProcessPaths;
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.Version;
import com.gluonhq.substrate.util.XcodeUtils;
import com.gluonhq.substrate.util.macos.CodeSigning;
import com.gluonhq.substrate.util.macos.InfoPlist;
Expand Down Expand Up @@ -73,8 +72,8 @@ public class MacOSTargetConfiguration extends DarwinTargetConfiguration {
"WTF", "icuuc", "icudata"
);

public MacOSTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) {
super(paths, configuration, javaVersion);
public MacOSTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) {
super(paths, configuration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

abstract class PosixTargetConfiguration extends AbstractTargetConfiguration {

PosixTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration, Version javaVersion) {
super(paths, configuration, javaVersion);
PosixTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) {
super(paths, configuration);
}

@Override
Expand Down
Loading

0 comments on commit 8dd325b

Please sign in to comment.