diff --git a/src/main/java/com/gluonhq/substrate/target/AbstractTargetConfiguration.java b/src/main/java/com/gluonhq/substrate/target/AbstractTargetConfiguration.java index 362763af..b8004903 100644 --- a/src/main/java/com/gluonhq/substrate/target/AbstractTargetConfiguration.java +++ b/src/main/java/com/gluonhq/substrate/target/AbstractTargetConfiguration.java @@ -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; @@ -100,20 +101,21 @@ public abstract class AbstractTargetConfiguration implements TargetConfiguration protected final boolean crossCompile; private final List defaultAdditionalSourceFiles = Collections.singletonList("launcher.c"); - - /** - * Static Java libs required for all JDK major versions - */ - private final List 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 defaultStaticJavaLibs11 = List.of("sunec"); - /** - * Static Java libs required for JDK major < 21 - */ - private final List defaultStaticJavaLibs20 = List.of("fdlibm"); + private final List 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; @@ -938,15 +940,7 @@ String getAppPath(String appName) { * linker when creating images for the specific target. */ List getStaticJavaLibs() { - int major = projectConfiguration.getJavaVersion().getMajor(); - List libs = new ArrayList<>(defaultStaticJavaLibsBase); - if (major == 11) { - libs.addAll(defaultStaticJavaLibs11); - } - if (major < 21) { - libs.addAll(defaultStaticJavaLibs20); - } - return libs; + return filterApplicableLibs(defaultStaticJavaLibs); } /** @@ -961,6 +955,19 @@ List 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 filterApplicableLibs(List 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. diff --git a/src/main/java/com/gluonhq/substrate/target/LinuxTargetConfiguration.java b/src/main/java/com/gluonhq/substrate/target/LinuxTargetConfiguration.java index ab422660..8d00b8ff 100644 --- a/src/main/java/com/gluonhq/substrate/target/LinuxTargetConfiguration.java +++ b/src/main/java/com/gluonhq/substrate/target/LinuxTargetConfiguration.java @@ -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; @@ -59,21 +60,22 @@ public class LinuxTargetConfiguration extends PosixTargetConfiguration { private static final List linuxLibs = Arrays.asList("z", "dl", "stdc++", "pthread"); - /** - * Static Java libs required for all JDK major versions - */ - private static final List 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 staticJavaLibs11 = List.of("sunec"); - /** - * Static Java libs required for JDK major < 21 - */ - private static final List staticJavaLibs20 = List.of("fdlibm"); - + private static final List 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 staticJvmLibs = Arrays.asList( "jvm", "libchelper" ); @@ -246,15 +248,7 @@ List getStaticJavaLibs() { throw new RuntimeException ("No static java libs found, cannot continue"); } - int major = projectConfiguration.getJavaVersion().getMajor(); - List 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()); } diff --git a/src/main/java/com/gluonhq/substrate/target/MacOSTargetConfiguration.java b/src/main/java/com/gluonhq/substrate/target/MacOSTargetConfiguration.java index a14ed040..bef7da63 100644 --- a/src/main/java/com/gluonhq/substrate/target/MacOSTargetConfiguration.java +++ b/src/main/java/com/gluonhq/substrate/target/MacOSTargetConfiguration.java @@ -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; @@ -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 staticJavaLibsBase = Arrays.asList( - "java", "nio", "zip", "net", "prefs", "j2pkcs11", "extnet" + private static final List 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 staticJavaLibs11 = List.of("sunec"); - /** - * Static Java libs required for JDK major < 21 - */ - private static final List staticJavaLibs20 = List.of("fdlibm"); - private static final List staticJvmLibs = Arrays.asList( "jvm", "libchelper", "darwin" ); @@ -154,15 +151,7 @@ List getTargetSpecificLinkFlags(boolean useJavaFX, boolean usePrismSW) { @Override List getStaticJavaLibs() { - int major = projectConfiguration.getJavaVersion().getMajor(); - List libs = new ArrayList<>(staticJavaLibsBase); - if (major == 11) { - libs.addAll(staticJavaLibs11); - } - if (major < 21) { - libs.addAll(staticJavaLibs20); - } - return libs; + return filterApplicableLibs(staticJavaLibs); } @Override diff --git a/src/main/java/com/gluonhq/substrate/target/WindowsTargetConfiguration.java b/src/main/java/com/gluonhq/substrate/target/WindowsTargetConfiguration.java index 962689db..0ce393e9 100644 --- a/src/main/java/com/gluonhq/substrate/target/WindowsTargetConfiguration.java +++ b/src/main/java/com/gluonhq/substrate/target/WindowsTargetConfiguration.java @@ -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; @@ -46,34 +47,30 @@ public class WindowsTargetConfiguration extends AbstractTargetConfiguration { - /** - * Static Windows libs required for all JDK major versions - */ - private static final List 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 javaWindowsLibs20 = List.of("mswsock"); - - /** - * Static Java libs required for all JDK major versions - */ - private static final List staticJavaLibsBase = List.of( - "j2pkcs11", "java", "net", "nio", "prefs", "zip", "sunmscapi"); - /** - * Static Java libs required for JDK major == 11 - */ - private static final List staticJavaLibs11 = List.of("sunec"); - /** - * Static Java libs required for JDK major < 21 - */ - private static final List staticJavaLibs20 = List.of("fdlibm"); - /** - * Static Java libs required for JDK major >= 21 - */ - private static final List staticJavaLibs21 = List.of("extnet"); - + private static final List 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 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 staticJvmLibs = Arrays.asList( "jvm", "libchelper"); @@ -161,28 +158,13 @@ String getLinkOutputName() { @Override List getStaticJavaLibs() { - int major = projectConfiguration.getJavaVersion().getMajor(); - List 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 getOtherStaticLibs() { - int major = projectConfiguration.getJavaVersion().getMajor(); List libs = new ArrayList<>(staticJvmLibs); - libs.addAll(javaWindowsLibsBase); - if (major >= 20) { - libs.addAll(javaWindowsLibs20); - } + libs.addAll(filterApplicableLibs(javaWindowsLibs)); return libs; } diff --git a/src/main/java/com/gluonhq/substrate/util/JavaLib.java b/src/main/java/com/gluonhq/substrate/util/JavaLib.java new file mode 100644 index 00000000..b3811cc3 --- /dev/null +++ b/src/main/java/com/gluonhq/substrate/util/JavaLib.java @@ -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 . + * + * 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); + } +} diff --git a/src/test/java/com/gluonhq/substrate/util/JavaLibTest.java b/src/test/java/com/gluonhq/substrate/util/JavaLibTest.java new file mode 100644 index 00000000..6021f82c --- /dev/null +++ b/src/test/java/com/gluonhq/substrate/util/JavaLibTest.java @@ -0,0 +1,67 @@ +/* + * 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 . + * + * 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; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class JavaLibTest { + @Test + void testNoBound() { + JavaLib lib = JavaLib.of(""); + assertTrue(lib.inRange(0)); + assertTrue(lib.inRange(100)); + } + + @Test + void testLowerBound() { + JavaLib lib = JavaLib.from(10, ""); + assertFalse(lib.inRange(9)); + assertTrue(lib.inRange(10)); + assertTrue(lib.inRange(11)); + } + + @Test + void testUpperBound() { + JavaLib lib = JavaLib.upto(10, ""); + assertTrue(lib.inRange(9)); + assertTrue(lib.inRange(10)); + assertFalse(lib.inRange(11)); + } + + @Test + void testRange() { + JavaLib lib = JavaLib.range(10, 12, ""); + assertFalse(lib.inRange(9)); + assertTrue(lib.inRange(10)); + assertTrue(lib.inRange(11)); + assertTrue(lib.inRange(12)); + assertFalse(lib.inRange(13)); + } +}