< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/Context.java

Print this page

        

@@ -23,10 +23,11 @@
 package com.sun.tools.jextract;
 
 import jdk.internal.clang.*;
 import jdk.internal.foreign.LibrariesHelper;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.UncheckedIOException;

@@ -38,10 +39,11 @@
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
 import java.util.function.Function;
 import java.util.function.Predicate;
 import java.util.jar.JarOutputStream;

@@ -434,11 +436,25 @@
                     .forEach(cf -> rv.putAll(cf.collect()));
         }
         return Collections.unmodifiableMap(rv);
     }
 
-    void collectClassFiles(Path destDir, String... pkgs) throws IOException {
+    private static final String JEXTRACT_MANIFEST = "META-INFO" + File.separatorChar + "jextract.properties";
+
+    @SuppressWarnings("deprecation")
+    private byte[] getJextractProperties(String[] args) {
+        Properties props = new Properties();
+        props.setProperty("os.name", System.getProperty("os.name"));
+        props.setProperty("os.version", System.getProperty("os.version"));
+        props.setProperty("os.arch", System.getProperty("os.arch"));
+        props.setProperty("jextract.args", Arrays.toString(args));
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        props.save(baos, "jextract meta data");
+        return baos.toByteArray();
+    }
+
+    void collectClassFiles(Path destDir, String[] args, String... pkgs) throws IOException {
         try {
             collectClasses(pkgs).entrySet().stream().forEach(e -> {
                 try {
                     String path = e.getKey().replace('.', File.separatorChar) + ".class";
                     logger.fine(() -> "Writing " + path);

@@ -450,16 +466,23 @@
                     }
                 } catch (IOException ioe) {
                     throw new UncheckedIOException(ioe);
                 }
             });
+
+            Path propsPath = destDir.resolve(JEXTRACT_MANIFEST).normalize();
+            Files.createDirectories(propsPath.getParent());
+            try (OutputStream fos = Files.newOutputStream(propsPath)) {
+                fos.write(getJextractProperties(args));
+                fos.flush();
+            }
         } catch (UncheckedIOException uioe) {
             throw uioe.getCause();
         }
     }
 
-    private void writeJar(AsmCodeFactory cf, JarOutputStream jar) {
+    private void writeJar(AsmCodeFactory cf, JarOutputStream jar, String[] args) {
         cf.collect().entrySet().stream().forEach(e -> {
             try {
                 String path = e.getKey().replace('.', File.separatorChar) + ".class";
                 logger.fine(() -> "Add " + path);
                 jar.putNextEntry(new ZipEntry(path));

@@ -469,11 +492,11 @@
                 throw new UncheckedIOException(ioe);
             }
         });
     }
 
-    public void collectJarFile(final JarOutputStream jos, String... pkgs) {
+    public void collectJarFile(final JarOutputStream jos, String[] args, String... pkgs) {
         final Map<String, List<AsmCodeFactory>> mapPkgCf = getPkgCfMap();
 
         for (String pkg_name : pkgs) {
             // convert '.' to '/' to use as a path
             String entryName = Utils.toInternalName(pkg_name, "");

@@ -485,19 +508,27 @@
                     throw new UncheckedIOException(ex);
                 }
             }
             logger.fine(() -> "Produce for package " + pkg_name);
             mapPkgCf.getOrDefault(pkg_name, Collections.emptyList())
-                    .forEach(cf -> writeJar(cf, jos));
+                    .forEach(cf -> writeJar(cf, jos, args));
+        }
+
+        try {
+            jos.putNextEntry(new ZipEntry(JEXTRACT_MANIFEST));
+            jos.write(getJextractProperties(args));
+            jos.closeEntry();
+        } catch (IOException ioe) {
+            throw new UncheckedIOException(ioe);
         }
     }
 
-    void collectJarFile(final Path jar, String... pkgs) throws IOException {
+    void collectJarFile(final Path jar, String[] args, String... pkgs) throws IOException {
         logger.info(() -> "Collecting jar file " + jar);
         try (OutputStream os = Files.newOutputStream(jar, CREATE, TRUNCATE_EXISTING, WRITE);
                 JarOutputStream jo = new JarOutputStream(os)) {
-            collectJarFile(jo, pkgs);
+            collectJarFile(jo, args, pkgs);
         } catch (UncheckedIOException uioe) {
             throw uioe.getCause();
         }
     }
 
< prev index next >