Skip to content

Commit 35d412e

Browse files
committed
Decouple PolyglotNativeAPIFeature.
1 parent 085d1cb commit 35d412e

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

substratevm/mx.substratevm/mx_substratevm.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,28 +1234,6 @@ def _native_image_launcher_extra_jvm_args():
12341234
support_distributions=[
12351235
"substratevm:POLYGLOT_NATIVE_API_HEADERS",
12361236
],
1237-
polyglot_lib_build_args=[
1238-
"--macro:truffle",
1239-
"--features=org.graalvm.polyglot.nativeapi.PolyglotNativeAPIFeature",
1240-
"-Dorg.graalvm.polyglot.nativeapi.libraryPath=${java.home}/lib/polyglot/",
1241-
# Temporary solution for polyglot-native-api.jar on classpath, will be fixed by modularization, GR-45104.
1242-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core=ALL-UNNAMED",
1243-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.c.function=ALL-UNNAMED",
1244-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.handles=ALL-UNNAMED",
1245-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.headers=ALL-UNNAMED",
1246-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED",
1247-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.jvmstat=ALL-UNNAMED",
1248-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.thread=ALL-UNNAMED",
1249-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.threadlocal=ALL-UNNAMED",
1250-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.util=ALL-UNNAMED",
1251-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted=ALL-UNNAMED",
1252-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c=ALL-UNNAMED",
1253-
"--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c.util=ALL-UNNAMED",
1254-
"--add-exports org.graalvm.nativeimage/org.graalvm.nativeimage.impl=ALL-UNNAMED",
1255-
] + svm_experimental_options([
1256-
"-H:CStandard=C11",
1257-
"-H:+SpawnIsolates",
1258-
]),
12591237
polyglot_lib_jar_dependencies=[
12601238
"substratevm:POLYGLOT_NATIVE_API",
12611239
],

substratevm/mx.substratevm/suite.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2002,7 +2002,16 @@
20022002
"sdk:POLYGLOT",
20032003
"SVM",
20042004
],
2005-
"maven": False
2005+
"moduleInfo" : {
2006+
"name" : "org.graalvm.polyglot.nativeapi",
2007+
"requires" : [
2008+
"org.graalvm.polyglot",
2009+
"org.graalvm.nativeimage",
2010+
"org.graalvm.nativeimage.builder",
2011+
],
2012+
},
2013+
"useModulePath": True,
2014+
"maven": False,
20062015
},
20072016

20082017
"POLYGLOT_NATIVE_API_HEADERS" : {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
ForceOnModulePath = org.graalvm.polyglot.nativeapi
2+
Args = --macro:truffle-svm \
3+
--features=org.graalvm.polyglot.nativeapi.PolyglotNativeAPIFeature \
4+
--add-exports org.graalvm.nativeimage/org.graalvm.nativeimage.impl=org.graalvm.polyglot.nativeapi \
5+
--add-exports org.graalvm.nativeimage.base/com.oracle.svm.util=org.graalvm.polyglot.nativeapi \
6+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core=org.graalvm.polyglot.nativeapi \
7+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.c.function=org.graalvm.polyglot.nativeapi \
8+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.handles=org.graalvm.polyglot.nativeapi \
9+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.headers=org.graalvm.polyglot.nativeapi \
10+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=org.graalvm.polyglot.nativeapi \
11+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.jvmstat=org.graalvm.polyglot.nativeapi \
12+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.thread=org.graalvm.polyglot.nativeapi \
13+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.threadlocal=org.graalvm.polyglot.nativeapi \
14+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.core.util=org.graalvm.polyglot.nativeapi \
15+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted=org.graalvm.polyglot.nativeapi \
16+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c=org.graalvm.polyglot.nativeapi \
17+
--add-exports org.graalvm.nativeimage.builder/com.oracle.svm.hosted.c.util=org.graalvm.polyglot.nativeapi \
18+
-H:+UnlockExperimentalVMOptions \
19+
-H:CStandard=C11 \
20+
-H:+SpawnIsolates \
21+
-H:-UnlockExperimentalVMOptions

substratevm/src/org.graalvm.polyglot.nativeapi/src/org/graalvm/polyglot/nativeapi/PolyglotNativeAPIFeature.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@
2828
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
2929

3030
import java.io.IOException;
31+
import java.io.InputStream;
32+
import java.io.OutputStream;
3133
import java.nio.file.Files;
3234
import java.nio.file.Path;
3335
import java.nio.file.Paths;
3436
import java.util.Arrays;
3537
import java.util.Collections;
38+
import java.util.Comparator;
3639
import java.util.List;
40+
import java.util.stream.Stream;
3741

42+
import com.oracle.svm.core.BuildArtifacts;
3843
import org.graalvm.nativeimage.ImageSingletons;
3944
import org.graalvm.nativeimage.LogHandler;
4045
import org.graalvm.nativeimage.Platform;
@@ -43,17 +48,31 @@
4348
import com.oracle.svm.core.jdk.RuntimeSupport;
4449
import com.oracle.svm.core.util.InterruptImageBuilding;
4550
import com.oracle.svm.core.util.UserError;
51+
import com.oracle.svm.core.util.VMError;
4652
import com.oracle.svm.hosted.NativeImageOptions;
4753
import com.oracle.svm.hosted.c.util.FileUtils;
4854
import com.oracle.svm.util.LogUtils;
4955

5056
public class PolyglotNativeAPIFeature implements Feature {
5157

58+
private Path tmpHeadersDir;
59+
5260
@Override
5361
public void afterRegistration(AfterRegistrationAccess access) {
5462
if (!NativeImageOptions.getCStandard().compatibleWith(C11)) {
5563
throw UserError.abort("Polyglot native API supports only the C11 standard. Pass -H:CStandard=C11 on the command line to make the build work.");
5664
}
65+
try {
66+
Path tmpDir = Files.createTempDirectory("polyglot_types");
67+
Path destination = tmpDir.resolve("polyglot_types.h");
68+
try (InputStream in = getClass().getResourceAsStream("/polyglot_types.h"); OutputStream out = Files.newOutputStream(destination)) {
69+
out.write(in.readAllBytes());
70+
}
71+
tmpHeadersDir = tmpDir;
72+
System.setProperty("org.graalvm.polyglot.nativeapi.libraryPath", tmpDir.toString());
73+
} catch (IOException e) {
74+
throw VMError.shouldNotReachHere("Cannot copy header files into a temporary directory.", e);
75+
}
5776
ImageSingletons.add(LogHandler.class, new PolyglotNativeAPI.PolyglotNativeLogHandler());
5877
RuntimeSupport.getRuntimeSupport().addStartupHook(PolyglotNativeAPI.startupHook);
5978
}
@@ -68,9 +87,23 @@ public void afterImageWrite(AfterImageWriteAccess access) {
6887
try {
6988
Files.copy(source, destination, REPLACE_EXISTING);
7089
} catch (IOException e) {
71-
throw new RuntimeException(e);
90+
throw VMError.shouldNotReachHere(String.format("Copying of header file %s failed.", headerFile), e);
7291
}
92+
BuildArtifacts.singleton().add(BuildArtifacts.ArtifactType.C_HEADER, destination);
7393
});
94+
if (tmpHeadersDir != null && Files.exists(tmpHeadersDir)) {
95+
try (Stream<Path> filesToDelete = Files.walk(tmpHeadersDir)) {
96+
filesToDelete.sorted(Comparator.reverseOrder()).forEach(f -> {
97+
try {
98+
Files.deleteIfExists(f);
99+
} catch (IOException e) {
100+
throw VMError.shouldNotReachHere("Deletion the temporary headers directory failed.", e);
101+
}
102+
});
103+
} catch (IOException e) {
104+
throw VMError.shouldNotReachHere("Deletion the temporary headers directory failed.", e);
105+
}
106+
}
74107
if (Platform.includedIn(Platform.DARWIN.class)) {
75108
// on Darwin, change the `id` install name
76109
String id = System.getProperty("org.graalvm.polyglot.install_name_id");

0 commit comments

Comments
 (0)