Skip to content

Commit

Permalink
- use version-aware static library lists
Browse files Browse the repository at this point in the history
  • Loading branch information
kkriske committed Nov 18, 2023
1 parent 63c32fd commit 403c72e
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.gluonhq.substrate.model.Triplet;
import com.gluonhq.substrate.util.FileDeps;
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.JavaLib;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.ProcessRunner;
import com.gluonhq.substrate.util.Strings;
Expand Down Expand Up @@ -100,20 +101,21 @@ public abstract class AbstractTargetConfiguration implements TargetConfiguration
protected final boolean crossCompile;

private final List<String> defaultAdditionalSourceFiles = Collections.singletonList("launcher.c");

/**
* Static Java libs required for all JDK major versions
*/
private final List<String> defaultStaticJavaLibsBase = List.of("java", "nio", "zip", "net", "prefs", "jvm",
"z", "dl", "j2pkcs11", "jaas", "extnet");
/**
* Static Java libs required for JDK major == 11
*/
private final List<String> defaultStaticJavaLibs11 = List.of("sunec");
/**
* Static Java libs required for JDK major < 21
*/
private final List<String> defaultStaticJavaLibs20 = List.of("fdlibm");
private final List<JavaLib> defaultStaticJavaLibs = List.of(
JavaLib.of("java"),
JavaLib.of("nio"),
JavaLib.of("zip"),
JavaLib.of("net"),
JavaLib.of("prefs"),
JavaLib.of("jvm"),
JavaLib.upto(20, "fdlibm"),
JavaLib.of("z"),
JavaLib.of("dl"),
JavaLib.of("j2pkcs11"),
JavaLib.upto(11, "sunec"),
JavaLib.of("jaas"),
JavaLib.of("extnet")
);

AbstractTargetConfiguration(ProcessPaths paths, InternalProjectConfiguration configuration) {
this.projectConfiguration = configuration;
Expand Down Expand Up @@ -938,15 +940,7 @@ String getAppPath(String appName) {
* linker when creating images for the specific target.
*/
List<String> getStaticJavaLibs() {
int major = projectConfiguration.getJavaVersion().getMajor();
List<String> libs = new ArrayList<>(defaultStaticJavaLibsBase);
if (major == 11) {
libs.addAll(defaultStaticJavaLibs11);
}
if (major < 21) {
libs.addAll(defaultStaticJavaLibs20);
}
return libs;
return filterApplicableLibs(defaultStaticJavaLibs);
}

/**
Expand All @@ -961,6 +955,19 @@ List<String> getOtherStaticLibs() {
return List.of();
}

/**
* Return the list of library names applicable to the used java version.
* @param libs List to validate based on {@link JavaLib#inRange(int)}.
* @return The list of library names applicable to the used java version.
*/
List<String> filterApplicableLibs(List<JavaLib> libs) {
int major = projectConfiguration.getJavaVersion().getMajor();
return libs.stream()
.filter(l -> l.inRange(major))
.map(JavaLib::getLibName)
.toList();
}

/**
* Return the arguments that need to be passed to the linker for including
* the Java libraries in the final image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.gluonhq.substrate.model.InternalProjectConfiguration;
import com.gluonhq.substrate.model.ProcessPaths;
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.JavaLib;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.ProcessRunner;
import com.gluonhq.substrate.util.Version;
Expand Down Expand Up @@ -59,21 +60,22 @@ public class LinuxTargetConfiguration extends PosixTargetConfiguration {

private static final List<String> linuxLibs = Arrays.asList("z", "dl", "stdc++", "pthread");

/**
* Static Java libs required for all JDK major versions
*/
private static final List<String> staticJavaLibsBase = List.of(
"java", "nio", "zip", "net", "prefs", "j2pkcs11", "extnet",
"fontmanager", "javajpeg", "lcms", "awt_headless", "awt");
/**
* Static Java libs required for JDK major == 11
*/
private static final List<String> staticJavaLibs11 = List.of("sunec");
/**
* Static Java libs required for JDK major < 21
*/
private static final List<String> staticJavaLibs20 = List.of("fdlibm");

private static final List<JavaLib> staticJavaLibs = List.of(
JavaLib.of("java"),
JavaLib.of("nio"),
JavaLib.of("zip"),
JavaLib.of("net"),
JavaLib.of("prefs"),
JavaLib.of("j2pkcs11"),
JavaLib.upto(11, "sunec"),
JavaLib.of("extnet"),
JavaLib.upto(20, "fdlibm"),
JavaLib.of("fontmanager"),
JavaLib.of("javajpeg"),
JavaLib.of("lcms"),
JavaLib.of("awt_headless"),
JavaLib.of("awt")
);
private static final List<String> staticJvmLibs = Arrays.asList(
"jvm", "libchelper"
);
Expand Down Expand Up @@ -246,15 +248,7 @@ List<String> getStaticJavaLibs() {
throw new RuntimeException ("No static java libs found, cannot continue");
}

int major = projectConfiguration.getJavaVersion().getMajor();
List<String> libs = new ArrayList<>(staticJavaLibsBase);
if (major == 11) {
libs.addAll(staticJavaLibs11);
}
if (major < 21) {
libs.addAll(staticJavaLibs20);
}
return libs.stream()
return filterApplicableLibs(staticJavaLibs).stream()
.map(lib -> javaStaticLibPath.resolve("lib" + lib + ".a").toString())
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.gluonhq.substrate.model.InternalProjectConfiguration;
import com.gluonhq.substrate.model.ProcessPaths;
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.JavaLib;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.XcodeUtils;
import com.gluonhq.substrate.util.macos.CodeSigning;
Expand All @@ -56,21 +57,17 @@ public class MacOSTargetConfiguration extends DarwinTargetConfiguration {
"ApplicationServices", "OpenGL", "QuartzCore", "Security", "Accelerate"
);

/**
* Static Java libs required for all JDK major versions
*/
private static final List<String> staticJavaLibsBase = Arrays.asList(
"java", "nio", "zip", "net", "prefs", "j2pkcs11", "extnet"
private static final List<JavaLib> staticJavaLibs = Arrays.asList(
JavaLib.of("java"),
JavaLib.of("nio"),
JavaLib.of("zip"),
JavaLib.of("net"),
JavaLib.of("prefs"),
JavaLib.of("j2pkcs11"),
JavaLib.upto(20, "fdlibm"),
JavaLib.upto(11, "sunec"),
JavaLib.of("extnet")
);
/**
* Static Java libs required for JDK major == 11
*/
private static final List<String> staticJavaLibs11 = List.of("sunec");
/**
* Static Java libs required for JDK major < 21
*/
private static final List<String> staticJavaLibs20 = List.of("fdlibm");

private static final List<String> staticJvmLibs = Arrays.asList(
"jvm", "libchelper", "darwin"
);
Expand Down Expand Up @@ -154,15 +151,7 @@ List<String> getTargetSpecificLinkFlags(boolean useJavaFX, boolean usePrismSW) {

@Override
List<String> getStaticJavaLibs() {
int major = projectConfiguration.getJavaVersion().getMajor();
List<String> libs = new ArrayList<>(staticJavaLibsBase);
if (major == 11) {
libs.addAll(staticJavaLibs11);
}
if (major < 21) {
libs.addAll(staticJavaLibs20);
}
return libs;
return filterApplicableLibs(staticJavaLibs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.gluonhq.substrate.model.InternalProjectConfiguration;
import com.gluonhq.substrate.model.ProcessPaths;
import com.gluonhq.substrate.util.FileOps;
import com.gluonhq.substrate.util.JavaLib;
import com.gluonhq.substrate.util.Logger;
import com.gluonhq.substrate.util.ProcessRunner;
import com.gluonhq.substrate.util.windows.MSIBundler;
Expand All @@ -46,34 +47,30 @@

public class WindowsTargetConfiguration extends AbstractTargetConfiguration {

/**
* Static Windows libs required for all JDK major versions
*/
private static final List<String> javaWindowsLibsBase = List.of(
"advapi32", "iphlpapi", "secur32", "userenv", "version", "ws2_32", "winhttp", "ncrypt", "crypt32");
/**
* Static Windows libs required for JDK major >= 20
*/
private static final List<String> javaWindowsLibs20 = List.of("mswsock");

/**
* Static Java libs required for all JDK major versions
*/
private static final List<String> staticJavaLibsBase = List.of(
"j2pkcs11", "java", "net", "nio", "prefs", "zip", "sunmscapi");
/**
* Static Java libs required for JDK major == 11
*/
private static final List<String> staticJavaLibs11 = List.of("sunec");
/**
* Static Java libs required for JDK major < 21
*/
private static final List<String> staticJavaLibs20 = List.of("fdlibm");
/**
* Static Java libs required for JDK major >= 21
*/
private static final List<String> staticJavaLibs21 = List.of("extnet");

private static final List<JavaLib> javaWindowsLibs = List.of(
JavaLib.of("advapi32"),
JavaLib.of("iphlpapi"),
JavaLib.of("secur32"),
JavaLib.of("userenv"),
JavaLib.of("version"),
JavaLib.of("ws2_32"),
JavaLib.of("winhttp"),
JavaLib.of("ncrypt"),
JavaLib.of("crypt32"),
JavaLib.from(20, "mswsock")
);
private static final List<JavaLib> staticJavaLibs = List.of(
JavaLib.of("j2pkcs11"),
JavaLib.of("java"),
JavaLib.of("net"),
JavaLib.of("nio"),
JavaLib.of("prefs"),
JavaLib.upto(20, "fdlibm"),
JavaLib.upto(11, "sunec"),
JavaLib.of("zip"),
JavaLib.of("sunmscapi"),
JavaLib.from(21, "extnet")
);
private static final List<String> staticJvmLibs = Arrays.asList(
"jvm", "libchelper");

Expand Down Expand Up @@ -161,28 +158,13 @@ String getLinkOutputName() {

@Override
List<String> getStaticJavaLibs() {
int major = projectConfiguration.getJavaVersion().getMajor();
List<String> libs = new ArrayList<>(staticJavaLibsBase);
if (major == 11) {
libs.addAll(staticJavaLibs11);
}
if (major < 21) {
libs.addAll(staticJavaLibs20);
}
if (major >= 21) {
libs.addAll(staticJavaLibs21);
}
return libs;
return filterApplicableLibs(staticJavaLibs);
}

@Override
List<String> getOtherStaticLibs() {
int major = projectConfiguration.getJavaVersion().getMajor();
List<String> libs = new ArrayList<>(staticJvmLibs);
libs.addAll(javaWindowsLibsBase);
if (major >= 20) {
libs.addAll(javaWindowsLibs20);
}
libs.addAll(filterApplicableLibs(javaWindowsLibs));
return libs;
}

Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/gluonhq/substrate/util/JavaLib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2023, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.substrate.util;

/**
* This class represents a java library constrained to the optional lower and upper bounds.
*/
public class JavaLib {
/**
* lower bound of the applicable version range, inclusive
*/
private final Integer lowerBound;
/**
* upper bound of the applicable version range, inclusive
*/
private final Integer upperBound;
private final String libName;

private JavaLib(Integer lowerBound, Integer upperBound, String libName) {
this.lowerBound = lowerBound;
this.upperBound = upperBound;
this.libName = libName;
}

public String getLibName() {
return libName;
}

public boolean inRange(int javaMajorVersion) {
// Java version is lower than the lower bound
if (lowerBound != null && javaMajorVersion < lowerBound) return false;
// Java version is higher than the upper bound
if (upperBound != null && upperBound < javaMajorVersion) return false;
return true;
}

public static JavaLib of(String libName) {
return new JavaLib(null, null, libName);
}

public static JavaLib range(int lowerBound, int upperBound, String libName) {
return new JavaLib(lowerBound, upperBound, libName);
}

public static JavaLib from(int lowerBound, String libName) {
return new JavaLib(lowerBound, null, libName);
}

public static JavaLib upto(int upperBound, String libName) {
return new JavaLib(null, upperBound, libName);
}
}
Loading

0 comments on commit 403c72e

Please sign in to comment.