1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  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 }
--- EOF ---