From 9f50d702f240873718c7fae5b8f5c5b4f04f7c71 Mon Sep 17 00:00:00 2001 From: Johannes Bechberger Date: Mon, 13 Jan 2025 16:03:28 +0100 Subject: [PATCH 1/3] SapMachine #1894: Add SapMachine tools plugin to jlink (cherry picked from commit bbf2165e50c17d30ba059b631c4da30768070b9f) --- .../internal/plugins/AddSapMachineTools.java | 109 ++++++++++++++++++ .../tools/jlink/resources/plugins.properties | 7 ++ src/jdk.jlink/share/classes/module-info.java | 2 + test/hotspot/jtreg/TEST.groups | 6 +- test/jdk/TEST.groups | 5 +- .../jlink/plugins/AddSapMachineToolsTest.java | 103 +++++++++++++++++ 6 files changed, 227 insertions(+), 5 deletions(-) create mode 100644 src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/AddSapMachineTools.java create mode 100644 test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/AddSapMachineTools.java b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/AddSapMachineTools.java new file mode 100644 index 00000000000..b7deaae5aea --- /dev/null +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/AddSapMachineTools.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2025 SAP SE. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.tools.jlink.internal.plugins; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import jdk.tools.jlink.internal.ExecutableImage; +import jdk.tools.jlink.internal.Platform; +import jdk.tools.jlink.internal.PostProcessor; +import jdk.tools.jlink.plugin.PluginException; +import jdk.tools.jlink.plugin.ResourcePool; +import jdk.tools.jlink.plugin.ResourcePoolBuilder; + +/** + * Adds tools that are SapMachine specific + */ +public class AddSapMachineTools extends AbstractPlugin implements PostProcessor { + + public AddSapMachineTools() { + super("add-sapmachine-tools"); + } + + @Override + public Category getType() { + return Category.ADDER; + } + + @Override + public boolean hasArguments() { + return false; + } + + @Override + public boolean hasRawArgument() { + return false; + } + + private final String[] tools = { + "bin/asprof", + "lib/" + System.mapLibraryName("asyncProfiler"), + "lib/async-profiler.jar", + "lib/converter.jar", + "legal/async/CHANGELOG.md", + "legal/async/LICENSE", + "legal/async/README.md" + }; + + @Override + public List process(ExecutableImage image) { + var targetPlatform = image.getTargetPlatform(); + var runtimePlatform = Platform.runtime(); + + if (!targetPlatform.equals(runtimePlatform)) { + throw new PluginException("Cannot add SapMachine tools: target image platform " + + targetPlatform.toString() + " is different from runtime platform " + + runtimePlatform.toString()); + } + + var sourceJavaHome = Path.of(System.getProperty("java.home")); + var targetJavaHome = image.getHome(); + + for (String tool : tools) { + var path = sourceJavaHome.resolve(tool); + var target = targetJavaHome.resolve(tool); + if (Files.exists(path)) { + try { + Files.createDirectories(target.getParent()); + Files.copy(path, target); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + } + + return null; + } + + @Override + public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) { + return in; + } +} diff --git a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties index 958c43c2ca4..40853b31ea8 100644 --- a/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties +++ b/src/jdk.jlink/share/classes/jdk/tools/jlink/resources/plugins.properties @@ -287,6 +287,13 @@ Invalid language tag: %s include-locales.localedatanotfound=\ jdk.localedata module was not specified with --add-modules option +# SapMachine 2025-09-01: SapMachine tools plugin +add-sapmachine-tools.description=\ +Add SapMachine specific tools to the image. + +add-sapmachine-tools.usage=\ +\ --add-sapmachine-tools Add SapMachine specific tools to the image. + main.status.ok=Functional. main.status.not.ok= Not functional. diff --git a/src/jdk.jlink/share/classes/module-info.java b/src/jdk.jlink/share/classes/module-info.java index d2db4c5f9e4..67532f6bf27 100644 --- a/src/jdk.jlink/share/classes/module-info.java +++ b/src/jdk.jlink/share/classes/module-info.java @@ -78,5 +78,7 @@ jdk.tools.jlink.internal.plugins.VendorVMBugURLPlugin, jdk.tools.jlink.internal.plugins.VendorVersionPlugin, jdk.tools.jlink.internal.plugins.CDSPlugin; + // SapMachine 2025-01-09: SapMachine tools plugin + jdk.tools.jlink.internal.plugins.AddSapMachineTools, } diff --git a/test/hotspot/jtreg/TEST.groups b/test/hotspot/jtreg/TEST.groups index c1966b5e089..0f755fef61f 100644 --- a/test/hotspot/jtreg/TEST.groups +++ b/test/hotspot/jtreg/TEST.groups @@ -356,7 +356,10 @@ hotspot_gc_shenandoah = \ :tier2_gc_shenandoah \ :tier3_gc_shenandoah +# SapMachine 2019-02-24 : Add tests where SAPMachine has different behavior to tier1, +# or which tests downstream-only features. tier1_runtime = \ + :tier1_sapmachine \ runtime/ \ -runtime/6626217/bug_21227.java \ -runtime/7100935 \ @@ -524,10 +527,7 @@ tier1_sapmachine = \ runtime/Vitals \ runtime/malloctrace -# SapMachine 2019-02-24 : Add tests where SAPMachine has different behavior to tier1, -# or which tests downstream-only features. tier1 = \ - :tier1_sapmachine \ :tier1_common \ :tier1_compiler \ :tier1_gc \ diff --git a/test/jdk/TEST.groups b/test/jdk/TEST.groups index 9b6cb038329..79c4bc3ac32 100644 --- a/test/jdk/TEST.groups +++ b/test/jdk/TEST.groups @@ -37,9 +37,7 @@ jdk_all = \ # # When adding tests to tier1, make sure they end up in one of the tier1_partX groups -# SapMachine 2019-01-24: Add tests where SAPMachine has different behavior to tier1 tier1 = \ - :tier1_sapmachine \ :tier1_part1 \ :tier1_part2 \ :tier1_part3 @@ -50,7 +48,9 @@ tier1_part1 = \ tier1_part2 = \ :jdk_util +# SapMachine 2019-01-24: Add tests where SAPMachine has different behavior to tier1 tier1_part3 = \ + :tier1_sapmachine \ :jdk_math \ :jdk_svc_sanity \ :jdk_foreign \ @@ -64,6 +64,7 @@ tier1_sapmachine = \ java/util/jar/Manifest/IncludeInExceptionsTest.java \ jdk/security/JavaDotSecurity/TestJDKIncludeInExceptions.java \ sun/security/lib/cacerts/VerifyCACerts.java \ + tools/jlink/plugins/AddSapMachineToolsTest.java \ tools/launcher/HelpFlagsTest.java \ tools/launcher/VersionCheck.java diff --git a/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java b/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java new file mode 100644 index 00000000000..0dd08bfb4c3 --- /dev/null +++ b/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2025 SAP SE. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code 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 + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.testng.SkipException; +import org.testng.annotations.Test; + +import jdk.test.lib.Platform; + +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import tests.Helper; + +/* @test + * @summary Test the --add-sapmachine-tools plugin + * @library ../../lib + * @library /test/lib + * @modules java.base/jdk.internal.jimage + * jdk.jlink/jdk.tools.jimage + * @run testng AddSapMachineToolsTest + */ +@Test +public class AddSapMachineToolsTest { + + private final String[] sapMachineTools = { + "bin/asprof", + "lib/" + System.mapLibraryName("asyncProfiler"), + "lib/async-profiler.jar", + "lib/converter.jar", + "legal/async/CHANGELOG.md", + "legal/async/LICENSE", + "legal/async/README.md" + }; + + @Test + public void testSapMachineTools() throws IOException { + // async profiler is not pulled in GHA builds, so skip the test there. + // checking whether we are in a GHA environment is hacky because jtreg removes environment variables, + // so we guess by checking for a user name containing the String "runner" + if (System.getProperty("user.name", "n/a").contains("runner")) { + throw new SkipException("Detected a Github Actions environment. No tools get added to SapMachine here, so skip test."); + } + + Helper helper = Helper.newHelper(); + if (helper == null) { + throw new SkipException("JDK image is not suitable for this test."); + } + + // async profiler is only available on a subset of platforms + boolean shouldHaveAsync = Platform.isOSX() || + (Platform.isLinux() && (Platform.isAArch64() || Platform.isPPC() || Platform.isX64()) && !Platform.isMusl()); + + Path sourceJavaHome = Path.of(System.getProperty("java.home")); + + if (shouldHaveAsync) { + for (String tool : sapMachineTools) { + assertTrue(Files.exists(sourceJavaHome.resolve(tool)), tool + " must exist."); + } + System.out.println("All SapMachine tools files found, as expected."); + } else { + for (String tool : sapMachineTools) { + assertFalse(Files.exists(sourceJavaHome.resolve(tool)), tool + " should not exist."); + } + System.out.println("No SapMachine tools files found, as expected."); + } + + var module = "sapmachine.tools"; + helper.generateDefaultJModule(module); + var image = helper + .generateDefaultImage(new String[] { "--add-sapmachine-tools" }, module) + .assertSuccess(); + + if (shouldHaveAsync) { + helper.checkImage(image, module, null, null, sapMachineTools); + } else { + helper.checkImage(image, module, null, sapMachineTools, null); + } + } +} From fb7c9a76b3ae3171fde81b8be5ade36454410665 Mon Sep 17 00:00:00 2001 From: Christoph Langer Date: Mon, 13 Jan 2025 18:16:01 +0100 Subject: [PATCH 2/3] Fix module-info in sapmachine 17 backport of SapMachine tools jlink plugin --- src/jdk.jlink/share/classes/module-info.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jdk.jlink/share/classes/module-info.java b/src/jdk.jlink/share/classes/module-info.java index 67532f6bf27..16a031d42e3 100644 --- a/src/jdk.jlink/share/classes/module-info.java +++ b/src/jdk.jlink/share/classes/module-info.java @@ -77,8 +77,8 @@ jdk.tools.jlink.internal.plugins.VendorBugURLPlugin, jdk.tools.jlink.internal.plugins.VendorVMBugURLPlugin, jdk.tools.jlink.internal.plugins.VendorVersionPlugin, - jdk.tools.jlink.internal.plugins.CDSPlugin; // SapMachine 2025-01-09: SapMachine tools plugin jdk.tools.jlink.internal.plugins.AddSapMachineTools, + jdk.tools.jlink.internal.plugins.CDSPlugin; } From 754253da5108f27a42b4d3e49e2773bd3980ccd4 Mon Sep 17 00:00:00 2001 From: Christoph Langer Date: Mon, 13 Jan 2025 19:59:41 +0100 Subject: [PATCH 3/3] AddSapMachineToolsTest in JDK21 needs jdk.jdeps/com.sun.tools.classfile --- test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java b/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java index 0dd08bfb4c3..854e3522bfc 100644 --- a/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java +++ b/test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java @@ -40,6 +40,7 @@ * @library ../../lib * @library /test/lib * @modules java.base/jdk.internal.jimage + * jdk.jdeps/com.sun.tools.classfile * jdk.jlink/jdk.tools.jimage * @run testng AddSapMachineToolsTest */