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.OptionException; 26 import jdk.internal.joptsimple.OptionParser; 27 import jdk.internal.joptsimple.OptionSet; 28 import jdk.internal.joptsimple.util.KeyValuePair; 29 30 import java.io.File; 31 import java.io.IOException; 32 import java.io.PrintWriter; 33 import java.nio.file.Files; 34 import java.nio.file.Path; 35 import java.nio.file.Paths; 36 import java.text.MessageFormat; 37 import java.util.logging.ConsoleHandler; 38 import java.util.logging.Level; 39 import java.util.logging.Logger; 40 import java.util.logging.SimpleFormatter; 41 import java.util.Locale; 42 import java.util.ResourceBundle; 43 import java.util.spi.ToolProvider; 44 45 public final class Main { 46 public static final boolean DEBUG = Boolean.getBoolean("jextract.debug"); 47 48 // FIXME: Remove this if/when the macros support is deemed stable 49 public static boolean INCLUDE_MACROS = Boolean.parseBoolean(System.getProperty("jextract.INCLUDE_MACROS", "true")); 50 51 private static final String MESSAGES_RESOURCE = "com.sun.tools.jextract.resources.Messages"; 52 53 private static final ResourceBundle MESSAGES_BUNDLE; 54 static { 55 MESSAGES_BUNDLE = ResourceBundle.getBundle(MESSAGES_RESOURCE, Locale.getDefault()); 56 } 57 58 public static String format(String msgId, Object... args) { 59 return new MessageFormat(MESSAGES_BUNDLE.getString(msgId)).format(args); 60 } 61 62 final Context ctx; 63 String targetPackage; 64 65 public Main(Context ctx) { 66 this.ctx = ctx; 67 } 68 69 private void processPackageMapping(Object arg) { 70 String str = (String) arg; 71 Path p = null; 72 String pkgName; 73 if (str.indexOf('=') == -1) { 74 pkgName = str; 75 } else { 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 } 117 } 118 } 119 120 public int run(String[] args) { 121 OptionParser parser = new OptionParser(); 122 parser.accepts("dry-run", format("help.dry_run")); 123 parser.accepts("I", format("help.I")).withRequiredArg(); 124 // option is expected to specify paths to load shared libraries 125 // to check & warn missing symbols during jextract session. 126 parser.accepts("L", format("help.L")).withRequiredArg(); 127 parser.accepts("l", format("help.l")).withRequiredArg(); 128 parser.accepts("o", format("help.o")).withRequiredArg(); 129 parser.accepts("t", format("help.t")).withRequiredArg(); 130 parser.accepts("m", format("help.m")).withRequiredArg(); 131 parser.accepts("h", format("help.h")).forHelp(); 132 parser.accepts("help", format("help.h")).forHelp(); 133 parser.accepts("C", format("help.C")).withRequiredArg(); 134 parser.accepts("log", format("help.log")).withRequiredArg(); 135 parser.accepts("rpath", format("help.rpath")).withRequiredArg(); 136 parser.accepts("?", format("help.h")).forHelp(); 137 parser.nonOptions(format("help.non.option")); 138 139 OptionSet options = null; 140 try { 141 options = parser.parse(args); 142 } catch (OptionException oe) { 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 }