-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SapMachine #1894: Add SapMachine tools plugin to jlink
(cherry picked from commit bbf2165)
- Loading branch information
1 parent
904e594
commit ae7f8e5
Showing
7 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/jdk.jlink/share/classes/jdk/tools/jlink/internal/plugins/AddSapMachineTools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
test/jdk/tools/jlink/plugins/AddSapMachineToolsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |