< prev index next >

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

Print this page




  76             KeyValuePair kv = KeyValuePair.valueOf(str);
  77             p = Paths.get(kv.key);
  78             pkgName = kv.value;
  79 
  80             if (!Files.isDirectory(p)) {
  81                 throw new IllegalArgumentException(format("not.a.directory", kv.key));
  82             }
  83         }
  84 
  85         Validators.validPackageName(pkgName);
  86         ctx.usePackageForFolder(p, pkgName);
  87     }
  88 
  89     private void processHeader(Object header) {
  90         Path p = Paths.get((String) header);
  91         if (!Files.isReadable(p)) {
  92             throw new IllegalArgumentException(format("cannot.read.header.file", header));
  93         }
  94         p = p.toAbsolutePath();
  95         ctx.usePackageForFolder(p.getParent(), targetPackage);
  96         ctx.sources.add(p);
  97     }
  98 
  99     private void setupLogging(Level level) {
 100         Logger logger = ctx.logger;
 101         logger.setUseParentHandlers(false);
 102         ConsoleHandler log = new ConsoleHandler();
 103         System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
 104         log.setFormatter(new SimpleFormatter());
 105         logger.setLevel(level);
 106         log.setLevel(level);
 107         logger.addHandler(log);
 108     }
 109 
 110     private void printHelp(OptionParser parser) {
 111         try {
 112             parser.printHelpOn(ctx.err);
 113         } catch (IOException ex) {
 114             if (Main.DEBUG) {
 115                 ex.printStackTrace(ctx.err);
 116             }


 143              ctx.err.println(oe.getMessage());
 144              if (Main.DEBUG) {
 145                  oe.printStackTrace(ctx.err);
 146              }
 147              printHelp(parser);
 148              return 1;
 149         }
 150 
 151         if (args.length == 0 || options.has("h") || options.has("?") || options.has("help")) {
 152              printHelp(parser);
 153              return args.length == 0? 1 : 0;
 154         }
 155 
 156         if (options.has("log")) {
 157             setupLogging(Level.parse((String) options.valueOf("log")));
 158         } else {
 159             setupLogging(Level.WARNING);
 160         }
 161 
 162         if (options.has("I")) {
 163             options.valuesOf("I").forEach(p -> ctx.clangArgs.add("-I" + p));
 164         }
 165 
 166         if (options.has("C")) {
 167             options.valuesOf("C").forEach(p -> ctx.clangArgs.add((String) p));
 168         }
 169 
 170         if (options.has("l")) {
 171             try {
 172                 options.valuesOf("l").forEach(p -> {
 173                     String lib = (String)p;
 174                     if (lib.indexOf(File.separatorChar) != -1) {
 175                         throw new IllegalArgumentException(format("l.name.should.not.be.path", lib));
 176                     }
 177                     ctx.libraryNames.add(lib);
 178                 });
 179             } catch (IllegalArgumentException iae) {
 180                 ctx.err.println(iae.getMessage());
 181                 if (Main.DEBUG) {
 182                     iae.printStackTrace(ctx.err);
 183                 }
 184                 return 1;
 185             }
 186         }
 187 
 188         if (options.has("rpath")) {
 189             // "rpath" with no "l" option!
 190             if (options.has("l")) {
 191                 options.valuesOf("rpath").forEach(p -> ctx.libraryPaths.add((String) p));
 192             } else {
 193                 ctx.err.println(format("warn.rpath.without.l"));
 194             }
 195         }
 196 
 197         if (options.has("L")) {
 198             // "L" with no "l" option!
 199             if (options.has("l")) {
 200                 options.valuesOf("L").forEach(p -> ctx.linkCheckPaths.add((String) p));
 201             } else {
 202                 ctx.err.println(format("warn.L.without.l"));
 203             }
 204         }
 205 
 206         targetPackage = options.has("t") ? (String) options.valueOf("t") : "";
 207         if (!targetPackage.isEmpty()) {
 208             Validators.validPackageName(targetPackage);
 209         }
 210 
 211         if (options.has("m")) {
 212             options.valuesOf("m").forEach(this::processPackageMapping);
 213         }
 214 
 215         try {
 216             options.nonOptionArguments().stream().forEach(this::processHeader);
 217             ctx.parse(AsmCodeFactory::new);
 218         } catch (RuntimeException re) {
 219             ctx.err.println(re.getMessage());
 220             if (Main.DEBUG) {
 221                 re.printStackTrace(ctx.err);
 222             }
 223             return 2;
 224         }
 225 
 226         if (options.has("dry-run")) {
 227             return 0;
 228         }
 229 
 230         String outputName = options.has("o")? (String)options.valueOf("o") :
 231             options.nonOptionArguments().get(0) + ".jar";
 232         Path jar = Paths.get(outputName);
 233         try {
 234             ctx.collectJarFile(jar, targetPackage);
 235         } catch (IOException ex) {
 236             ctx.err.println(format("cannot.write.jar.file", jar, ex));
 237             if (Main.DEBUG) {
 238                 ex.printStackTrace(ctx.err);
 239             }
 240             return 3;
 241         }
 242 
 243         return 0;
 244     }
 245 
 246     public static void main(String... args) {
 247         Main instance = new Main(Context.newInstance());
 248 
 249         System.exit(instance.run(args));
 250     }
 251 
 252     public static class JextractToolProvider implements ToolProvider {
 253         @Override
 254         public String name() {
 255             return "jextract";
 256         }
 257 
 258         @Override
 259         public int run(PrintWriter out, PrintWriter err, String... args) {
 260             // defensive check to throw security exception early.
 261             // Note that the successful run of jextract under security
 262             // manager would require far more permissions like loading
 263             // library (clang), file system access etc.
 264             if (System.getSecurityManager() != null) {
 265                 System.getSecurityManager().
 266                     checkPermission(new RuntimePermission("jextract"));
 267             }
 268 
 269             Main instance = new Main(Context.newInstance(out, err));
 270             return instance.run(args);
 271         }
 272     }
 273 }


  76             KeyValuePair kv = KeyValuePair.valueOf(str);
  77             p = Paths.get(kv.key);
  78             pkgName = kv.value;
  79 
  80             if (!Files.isDirectory(p)) {
  81                 throw new IllegalArgumentException(format("not.a.directory", kv.key));
  82             }
  83         }
  84 
  85         Validators.validPackageName(pkgName);
  86         ctx.usePackageForFolder(p, pkgName);
  87     }
  88 
  89     private void processHeader(Object header) {
  90         Path p = Paths.get((String) header);
  91         if (!Files.isReadable(p)) {
  92             throw new IllegalArgumentException(format("cannot.read.header.file", header));
  93         }
  94         p = p.toAbsolutePath();
  95         ctx.usePackageForFolder(p.getParent(), targetPackage);
  96         ctx.addSource(p);
  97     }
  98 
  99     private void setupLogging(Level level) {
 100         Logger logger = ctx.logger;
 101         logger.setUseParentHandlers(false);
 102         ConsoleHandler log = new ConsoleHandler();
 103         System.setProperty("java.util.logging.SimpleFormatter.format", "%4$s: %5$s%n");
 104         log.setFormatter(new SimpleFormatter());
 105         logger.setLevel(level);
 106         log.setLevel(level);
 107         logger.addHandler(log);
 108     }
 109 
 110     private void printHelp(OptionParser parser) {
 111         try {
 112             parser.printHelpOn(ctx.err);
 113         } catch (IOException ex) {
 114             if (Main.DEBUG) {
 115                 ex.printStackTrace(ctx.err);
 116             }


 143              ctx.err.println(oe.getMessage());
 144              if (Main.DEBUG) {
 145                  oe.printStackTrace(ctx.err);
 146              }
 147              printHelp(parser);
 148              return 1;
 149         }
 150 
 151         if (args.length == 0 || options.has("h") || options.has("?") || options.has("help")) {
 152              printHelp(parser);
 153              return args.length == 0? 1 : 0;
 154         }
 155 
 156         if (options.has("log")) {
 157             setupLogging(Level.parse((String) options.valueOf("log")));
 158         } else {
 159             setupLogging(Level.WARNING);
 160         }
 161 
 162         if (options.has("I")) {
 163             options.valuesOf("I").forEach(p -> ctx.addClangArg("-I" + p));
 164         }
 165 
 166         if (options.has("C")) {
 167             options.valuesOf("C").forEach(p -> ctx.addClangArg((String) p));
 168         }
 169 
 170         if (options.has("l")) {
 171             try {
 172                 options.valuesOf("l").forEach(p -> {
 173                     String lib = (String)p;
 174                     if (lib.indexOf(File.separatorChar) != -1) {
 175                         throw new IllegalArgumentException(format("l.name.should.not.be.path", lib));
 176                     }
 177                     ctx.addLibraryName(lib);
 178                 });
 179             } catch (IllegalArgumentException iae) {
 180                 ctx.err.println(iae.getMessage());
 181                 if (Main.DEBUG) {
 182                     iae.printStackTrace(ctx.err);
 183                 }
 184                 return 1;
 185             }
 186         }
 187 
 188         if (options.has("rpath")) {
 189             // "rpath" with no "l" option!
 190             if (options.has("l")) {
 191                 options.valuesOf("rpath").forEach(p -> ctx.addLibraryPath((String) p));
 192             } else {
 193                 ctx.err.println(format("warn.rpath.without.l"));
 194             }
 195         }
 196 
 197         if (options.has("L")) {
 198             // "L" with no "l" option!
 199             if (options.has("l")) {
 200                 options.valuesOf("L").forEach(p -> ctx.addLinkCheckPath((String) p));
 201             } else {
 202                 ctx.err.println(format("warn.L.without.l"));
 203             }
 204         }
 205 
 206         targetPackage = options.has("t") ? (String) options.valueOf("t") : "";
 207         if (!targetPackage.isEmpty()) {
 208             Validators.validPackageName(targetPackage);
 209         }
 210 
 211         if (options.has("m")) {
 212             options.valuesOf("m").forEach(this::processPackageMapping);
 213         }
 214 
 215         try {
 216             options.nonOptionArguments().stream().forEach(this::processHeader);
 217             ctx.parse();
 218         } catch (RuntimeException re) {
 219             ctx.err.println(re.getMessage());
 220             if (Main.DEBUG) {
 221                 re.printStackTrace(ctx.err);
 222             }
 223             return 2;
 224         }
 225 
 226         if (options.has("dry-run")) {
 227             return 0;
 228         }
 229 
 230         String outputName = options.has("o")? (String)options.valueOf("o") :
 231             options.nonOptionArguments().get(0) + ".jar";
 232         Path jar = Paths.get(outputName);
 233         try {
 234             ctx.collectJarFile(jar, targetPackage);
 235         } catch (IOException ex) {
 236             ctx.err.println(format("cannot.write.jar.file", jar, ex));
 237             if (Main.DEBUG) {
 238                 ex.printStackTrace(ctx.err);
 239             }
 240             return 3;
 241         }
 242 
 243         return 0;
 244     }
 245 
 246     public static void main(String... args) {
 247         Main instance = new Main(new Context());
 248 
 249         System.exit(instance.run(args));
 250     }
 251 
 252     public static class JextractToolProvider implements ToolProvider {
 253         @Override
 254         public String name() {
 255             return "jextract";
 256         }
 257 
 258         @Override
 259         public int run(PrintWriter out, PrintWriter err, String... args) {
 260             // defensive check to throw security exception early.
 261             // Note that the successful run of jextract under security
 262             // manager would require far more permissions like loading
 263             // library (clang), file system access etc.
 264             if (System.getSecurityManager() != null) {
 265                 System.getSecurityManager().
 266                     checkPermission(new RuntimePermission("jextract"));
 267             }
 268 
 269             Main instance = new Main(new Context(out, err));
 270             return instance.run(args);
 271         }
 272     }
 273 }
< prev index next >