< prev index next >

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

Print this page




  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.sun.tools.jextract;
  24 
  25 import jdk.internal.joptsimple.OptionParser;
  26 import jdk.internal.joptsimple.OptionSet;
  27 import jdk.internal.joptsimple.util.KeyValuePair;
  28 
  29 import java.io.IOException;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;

  33 import java.util.logging.ConsoleHandler;
  34 import java.util.logging.Level;
  35 import java.util.logging.Logger;
  36 import java.util.logging.SimpleFormatter;


  37 
  38 public final class Main {


  39     // FIXME: Remove this if/when the macros support is deemed stable
  40     public static boolean INCLUDE_MACROS = Boolean.parseBoolean(System.getProperty("jextract.INCLUDE_MACROS", "true"));
  41 











  42     final Context ctx;
  43     String targetPackage;
  44 
  45     public Main(Context ctx) {
  46         this.ctx = ctx;
  47     }
  48 
  49     private void processPackageMapping(Object arg) {
  50         String str = (String) arg;
  51         Path p = null;
  52         String pkgName;
  53         if (str.indexOf('=') == -1) {
  54             pkgName = str;
  55         } else {
  56             KeyValuePair kv = KeyValuePair.valueOf(str);
  57             p = Paths.get(kv.key);
  58             pkgName = kv.value;
  59 
  60             if (!Files.isDirectory(p)) {
  61                 throw new IllegalArgumentException("Not a directory: " + kv.key);
  62             }
  63         }
  64 
  65         Validators.validPackageName(pkgName);
  66         ctx.usePackageForFolder(p, pkgName);
  67     }
  68 
  69     private void processHeader(Object header) {
  70         Path p = Paths.get((String) header);
  71         if (!Files.isReadable(p)) {
  72             throw new IllegalArgumentException("Cannot read the file: " + header);
  73         }
  74         p = p.toAbsolutePath();
  75         ctx.usePackageForFolder(p.getParent(), targetPackage);
  76         ctx.sources.add(p);
  77     }
  78 
  79     private void setupLogging(Level level) {
  80         Logger logger = ctx.logger;
  81         logger.setUseParentHandlers(false);
  82         ConsoleHandler log = new ConsoleHandler();
  83         System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
  84         log.setFormatter(new SimpleFormatter());
  85         logger.setLevel(level);
  86         log.setLevel(level);
  87         logger.addHandler(log);
  88     }
  89 
  90     public void run(String[] args) {
  91         OptionParser parser = new OptionParser();
  92         parser.accepts("I", "specify include files path").withRequiredArg();
  93         parser.accepts("L", "specify library path").withRequiredArg();
  94         parser.accepts("l", "specify a library").withRequiredArg();
  95         parser.accepts("o", "specify output jar file").withRequiredArg();
  96         parser.accepts("t", "target package for specified header files").withRequiredArg();
  97         parser.accepts("m", "specify package mapping as dir=pkg").withRequiredArg();
  98         parser.accepts("h", "print help").forHelp();
  99         parser.accepts("C", "pass through argument for clang").withRequiredArg();
 100         parser.accepts("log", "specify log level in j.u.l.Level name").withRequiredArg();
 101         parser.accepts("?", "print help").forHelp();
 102         parser.nonOptions("header files");
 103 
 104         OptionSet options = parser.parse(args);
 105 
 106         if (args.length == 0 || options.has("h") || options.has("?")) {
 107             try {
 108                 parser.printHelpOn(System.out);
 109             } catch (IOException ex) {



 110             }
 111             System.exit(1);
 112         }
 113 
 114         if (options.has("log")) {
 115             setupLogging(Level.parse((String) options.valueOf("log")));


 116         }
 117 
 118         if (options.has("I")) {
 119             options.valuesOf("I").forEach(p -> ctx.clangArgs.add("-I" + p));
 120         }
 121 
 122         if (options.has("C")) {
 123             options.valuesOf("C").forEach(p -> ctx.clangArgs.add((String) p));
 124         }
 125 
 126         targetPackage = options.has("t") ? (String) options.valueOf("t") : "";
 127         if (!targetPackage.isEmpty()) {
 128             Validators.validPackageName(targetPackage);
 129         }
 130 
 131         if (options.has("m")) {
 132             options.valuesOf("m").forEach(this::processPackageMapping);
 133         }
 134 
 135         options.nonOptionArguments().stream().forEach(this::processHeader);
 136         try {

 137             ctx.parse(AsmCodeFactory::new);
 138         } catch (RuntimeException re) {


 139             re.printStackTrace(System.err);

 140             System.exit(2);
 141         }
 142 
 143         if (options.has("o")) {
 144             Path jar = Paths.get((String) options.valueOf("o"));
 145             try {
 146                 ctx.collectJarFile(jar, targetPackage);
 147             } catch (IOException ex) {
 148                 System.out.println("Error occurred producing jar file.");

 149                 ex.printStackTrace(System.err);

 150                 System.exit(3);
 151             }
 152         }
 153     }
 154 
 155     public static void main(String... args) {
 156         Main instance = new Main(Context.getInstance());
 157 
 158         instance.run(args);
 159     }
 160 
 161 }


  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.sun.tools.jextract;
  24 
  25 import jdk.internal.joptsimple.OptionParser;
  26 import jdk.internal.joptsimple.OptionSet;
  27 import jdk.internal.joptsimple.util.KeyValuePair;
  28 
  29 import java.io.IOException;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.text.MessageFormat;
  34 import java.util.logging.ConsoleHandler;
  35 import java.util.logging.Level;
  36 import java.util.logging.Logger;
  37 import java.util.logging.SimpleFormatter;
  38 import java.util.Locale;
  39 import java.util.ResourceBundle;
  40 
  41 public final class Main {
  42     public static final boolean DEBUG = Boolean.getBoolean("jextract.debug");
  43 
  44     // FIXME: Remove this if/when the macros support is deemed stable
  45     public static boolean INCLUDE_MACROS = Boolean.parseBoolean(System.getProperty("jextract.INCLUDE_MACROS", "true"));
  46 
  47     private static final String MESSAGES_RESOURCE = "com.sun.tools.jextract.resources.Messages";
  48 
  49     private static final ResourceBundle MESSAGES_BUNDLE;
  50     static {
  51         MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault());
  52     }
  53 
  54     public static String format(String msgId, Object... args) {
  55         return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args);
  56     }
  57 
  58     final Context ctx;
  59     String targetPackage;
  60 
  61     public Main(Context ctx) {
  62         this.ctx = ctx;
  63     }
  64 
  65     private void processPackageMapping(Object arg) {
  66         String str = (String) arg;
  67         Path p = null;
  68         String pkgName;
  69         if (str.indexOf('=') == -1) {
  70             pkgName = str;
  71         } else {
  72             KeyValuePair kv = KeyValuePair.valueOf(str);
  73             p = Paths.get(kv.key);
  74             pkgName = kv.value;
  75 
  76             if (!Files.isDirectory(p)) {
  77                 throw new IllegalArgumentException(format("not.a.directory", kv.key));
  78             }
  79         }
  80 
  81         Validators.validPackageName(pkgName);
  82         ctx.usePackageForFolder(p, pkgName);
  83     }
  84 
  85     private void processHeader(Object header) {
  86         Path p = Paths.get((String) header);
  87         if (!Files.isReadable(p)) {
  88             throw new IllegalArgumentException(format("cannot.read.header.file", header));
  89         }
  90         p = p.toAbsolutePath();
  91         ctx.usePackageForFolder(p.getParent(), targetPackage);
  92         ctx.sources.add(p);
  93     }
  94 
  95     private void setupLogging(Level level) {
  96         Logger logger = ctx.logger;
  97         logger.setUseParentHandlers(false);
  98         ConsoleHandler log = new ConsoleHandler();
  99         System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
 100         log.setFormatter(new SimpleFormatter());
 101         logger.setLevel(level);
 102         log.setLevel(level);
 103         logger.addHandler(log);
 104     }
 105 
 106     public void run(String[] args) {
 107         OptionParser parser = new OptionParser();
 108         parser.accepts("I", "specify include files path").withRequiredArg();
 109         parser.accepts("L", "specify library path").withRequiredArg();
 110         parser.accepts("l", "specify a library").withRequiredArg();
 111         parser.accepts("o", "specify output jar file").withRequiredArg();
 112         parser.accepts("t", "target package for specified header files").withRequiredArg();
 113         parser.accepts("m", "specify package mapping as dir=pkg").withRequiredArg();
 114         parser.accepts("h", "print help").forHelp();
 115         parser.accepts("C", "pass through argument for clang").withRequiredArg();
 116         parser.accepts("log", "specify log level in j.u.l.Level name").withRequiredArg();
 117         parser.accepts("?", "print help").forHelp();
 118         parser.nonOptions("header files");
 119 
 120         OptionSet options = parser.parse(args);
 121 
 122         if (args.length == 0 || options.has("h") || options.has("?")) {
 123             try {
 124                 parser.printHelpOn(System.out);
 125             } catch (IOException ex) {
 126                 if (Main.DEBUG) {
 127                     ex.printStackTrace(System.err);
 128                 }
 129             }
 130             System.exit(1);
 131         }
 132 
 133         if (options.has("log")) {
 134             setupLogging(Level.parse((String) options.valueOf("log")));
 135         } else {
 136             setupLogging(Level.WARNING);
 137         }
 138 
 139         if (options.has("I")) {
 140             options.valuesOf("I").forEach(p -> ctx.clangArgs.add("-I" + p));
 141         }
 142 
 143         if (options.has("C")) {
 144             options.valuesOf("C").forEach(p -> ctx.clangArgs.add((String) p));
 145         }
 146 
 147         targetPackage = options.has("t") ? (String) options.valueOf("t") : "";
 148         if (!targetPackage.isEmpty()) {
 149             Validators.validPackageName(targetPackage);
 150         }
 151 
 152         if (options.has("m")) {
 153             options.valuesOf("m").forEach(this::processPackageMapping);
 154         }
 155 

 156         try {
 157             options.nonOptionArguments().stream().forEach(this::processHeader);
 158             ctx.parse(AsmCodeFactory::new);
 159         } catch (RuntimeException re) {
 160             System.err.println(re.getMessage());
 161             if (Main.DEBUG) {
 162                 re.printStackTrace(System.err);
 163             }
 164             System.exit(2);
 165         }
 166 
 167         if (options.has("o")) {
 168             Path jar = Paths.get((String) options.valueOf("o"));
 169             try {
 170                 ctx.collectJarFile(jar, targetPackage);
 171             } catch (IOException ex) {
 172                 System.err.println(format("cannot.write.jar.file", jar, ex));
 173                 if (Main.DEBUG) {
 174                     ex.printStackTrace(System.err);
 175                 }
 176                 System.exit(3);
 177             }
 178         }
 179     }
 180 
 181     public static void main(String... args) {
 182         Main instance = new Main(Context.getInstance());
 183 
 184         instance.run(args);
 185     }
 186 
 187 }
< prev index next >