2828import static java .nio .file .StandardCopyOption .REPLACE_EXISTING ;
2929
3030import java .io .IOException ;
31+ import java .io .InputStream ;
32+ import java .io .OutputStream ;
3133import java .nio .file .Files ;
3234import java .nio .file .Path ;
3335import java .nio .file .Paths ;
3436import java .util .Arrays ;
3537import java .util .Collections ;
38+ import java .util .Comparator ;
3639import java .util .List ;
40+ import java .util .stream .Stream ;
3741
42+ import com .oracle .svm .core .BuildArtifacts ;
3843import org .graalvm .nativeimage .ImageSingletons ;
3944import org .graalvm .nativeimage .LogHandler ;
4045import org .graalvm .nativeimage .Platform ;
4348import com .oracle .svm .core .jdk .RuntimeSupport ;
4449import com .oracle .svm .core .util .InterruptImageBuilding ;
4550import com .oracle .svm .core .util .UserError ;
51+ import com .oracle .svm .core .util .VMError ;
4652import com .oracle .svm .hosted .NativeImageOptions ;
4753import com .oracle .svm .hosted .c .util .FileUtils ;
4854import com .oracle .svm .util .LogUtils ;
4955
5056public 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