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.List;
38 import java.util.Locale;
39 import java.util.ResourceBundle;
40 import java.util.logging.ConsoleHandler;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
43 import java.util.logging.SimpleFormatter;
44 import java.util.regex.PatternSyntaxException;
45 import java.util.spi.ToolProvider;
46
47 public final class Main {
48 public static final boolean DEBUG = Boolean.getBoolean("jextract.debug");
49
50 // FIXME: Remove this if/when the macros support is deemed stable
144 } catch (OptionException oe) {
145 ctx.err.println(oe.getMessage());
146 if (Main.DEBUG) {
147 oe.printStackTrace(ctx.err);
148 }
149 printHelp(parser);
150 return 1;
151 }
152
153 if (args.length == 0 || options.has("h")) {
154 printHelp(parser);
155 return args.length == 0? 1 : 0;
156 }
157
158 if (options.has("log")) {
159 setupLogging(Level.parse((String) options.valueOf("log")));
160 } else {
161 setupLogging(Level.WARNING);
162 }
163
164 if (options.has("I")) {
165 options.valuesOf("I").forEach(p -> ctx.addClangArg("-I" + p));
166 }
167
168 if (options.has("C")) {
169 options.valuesOf("C").forEach(p -> ctx.addClangArg((String) p));
170 }
171
172 if (options.has("l")) {
173 try {
174 options.valuesOf("l").forEach(p -> {
175 String lib = (String)p;
176 if (lib.indexOf(File.separatorChar) != -1) {
177 throw new IllegalArgumentException(format("l.name.should.not.be.path", lib));
178 }
179 ctx.addLibraryName(lib);
180 });
181 } catch (IllegalArgumentException iae) {
182 ctx.err.println(iae.getMessage());
183 if (Main.DEBUG) {
265 outputName = (String) options.valueOf("o");
266 } else if (hasOutput) {
267 return 0;
268 } else {
269 outputName = options.nonOptionArguments().get(0) + ".jar";
270 }
271
272 try {
273 ctx.collectJarFile(Paths.get(outputName), targetPackage);
274 } catch (IOException ex) {
275 ctx.err.println(format("cannot.write.jar.file", outputName, ex));
276 if (Main.DEBUG) {
277 ex.printStackTrace(ctx.err);
278 }
279 return 3;
280 }
281
282 return 0;
283 }
284
285 public static void main(String... args) {
286 Main instance = new Main(new Context());
287
288 System.exit(instance.run(args));
289 }
290
291 public static class JextractToolProvider implements ToolProvider {
292 @Override
293 public String name() {
294 return "jextract";
295 }
296
297 @Override
298 public int run(PrintWriter out, PrintWriter err, String... args) {
299 // defensive check to throw security exception early.
300 // Note that the successful run of jextract under security
301 // manager would require far more permissions like loading
302 // library (clang), file system access etc.
303 if (System.getSecurityManager() != null) {
304 System.getSecurityManager().
|
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.InputStream;
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.nio.file.Files;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.text.MessageFormat;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.ResourceBundle;
41 import java.util.logging.ConsoleHandler;
42 import java.util.logging.Level;
43 import java.util.logging.Logger;
44 import java.util.logging.SimpleFormatter;
45 import java.util.regex.PatternSyntaxException;
46 import java.util.spi.ToolProvider;
47
48 public final class Main {
49 public static final boolean DEBUG = Boolean.getBoolean("jextract.debug");
50
51 // FIXME: Remove this if/when the macros support is deemed stable
145 } catch (OptionException oe) {
146 ctx.err.println(oe.getMessage());
147 if (Main.DEBUG) {
148 oe.printStackTrace(ctx.err);
149 }
150 printHelp(parser);
151 return 1;
152 }
153
154 if (args.length == 0 || options.has("h")) {
155 printHelp(parser);
156 return args.length == 0? 1 : 0;
157 }
158
159 if (options.has("log")) {
160 setupLogging(Level.parse((String) options.valueOf("log")));
161 } else {
162 setupLogging(Level.WARNING);
163 }
164
165 Path builtinIncludeDir = null;
166 try {
167 builtinIncludeDir = createBuiltinIncludeDir();
168 } catch (IOException ex) {
169 ctx.err.println(format("cannot.create.builtin.includes", ex));
170 if (Main.DEBUG) {
171 ex.printStackTrace(ctx.err);
172 }
173 return 1;
174 }
175
176 if (builtinIncludeDir != null) {
177 ctx.addClangArg("-I" + builtinIncludeDir.toString());
178 }
179
180 if (options.has("I")) {
181 options.valuesOf("I").forEach(p -> ctx.addClangArg("-I" + p));
182 }
183
184 if (options.has("C")) {
185 options.valuesOf("C").forEach(p -> ctx.addClangArg((String) p));
186 }
187
188 if (options.has("l")) {
189 try {
190 options.valuesOf("l").forEach(p -> {
191 String lib = (String)p;
192 if (lib.indexOf(File.separatorChar) != -1) {
193 throw new IllegalArgumentException(format("l.name.should.not.be.path", lib));
194 }
195 ctx.addLibraryName(lib);
196 });
197 } catch (IllegalArgumentException iae) {
198 ctx.err.println(iae.getMessage());
199 if (Main.DEBUG) {
281 outputName = (String) options.valueOf("o");
282 } else if (hasOutput) {
283 return 0;
284 } else {
285 outputName = options.nonOptionArguments().get(0) + ".jar";
286 }
287
288 try {
289 ctx.collectJarFile(Paths.get(outputName), targetPackage);
290 } catch (IOException ex) {
291 ctx.err.println(format("cannot.write.jar.file", outputName, ex));
292 if (Main.DEBUG) {
293 ex.printStackTrace(ctx.err);
294 }
295 return 3;
296 }
297
298 return 0;
299 }
300
301 private static final String[] HEADERS = { "stdarg.h" };
302
303 private Path createBuiltinIncludeDir() throws IOException {
304 Path tmpDir = Files.createTempDirectory("jextract");
305 // copy built-in header from resources.
306 for (String header : HEADERS) {
307 Path headerPath = tmpDir.resolve(header);
308 try (InputStream headerRes = Main.class.getResourceAsStream("resources/" + header)) {
309 Files.copy(headerRes, headerPath);
310 }
311 }
312 return tmpDir;
313 }
314
315 public static void main(String... args) {
316 Main instance = new Main(new Context());
317
318 System.exit(instance.run(args));
319 }
320
321 public static class JextractToolProvider implements ToolProvider {
322 @Override
323 public String name() {
324 return "jextract";
325 }
326
327 @Override
328 public int run(PrintWriter out, PrintWriter err, String... args) {
329 // defensive check to throw security exception early.
330 // Note that the successful run of jextract under security
331 // manager would require far more permissions like loading
332 // library (clang), file system access etc.
333 if (System.getSecurityManager() != null) {
334 System.getSecurityManager().
|