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.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 }
--- EOF ---