duke@1: /* ohair@554: * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.main; duke@1: duke@1: import java.io.File; duke@1: import java.io.IOException; duke@1: import java.io.PrintWriter; jjg@535: import java.net.URL; jjg@535: import java.security.DigestInputStream; jjg@535: import java.security.MessageDigest; duke@1: import java.util.MissingResourceException; jjg@700: import javax.tools.JavaFileManager; jjg@700: import javax.tools.JavaFileObject; jjg@700: import javax.annotation.processing.Processor; duke@1: duke@1: import com.sun.tools.javac.code.Source; jjg@106: import com.sun.tools.javac.file.CacheFSInfo; jjg@50: import com.sun.tools.javac.file.JavacFileManager; duke@1: import com.sun.tools.javac.jvm.Target; duke@1: import com.sun.tools.javac.main.JavacOption.Option; duke@1: import com.sun.tools.javac.main.RecognizedOptions.OptionHelper; duke@1: import com.sun.tools.javac.util.*; duke@1: import com.sun.tools.javac.processing.AnnotationProcessingError; jjg@700: jjg@700: import static com.sun.tools.javac.main.OptionName.*; duke@1: duke@1: /** This class provides a commandline interface to the GJC compiler. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Main { duke@1: duke@1: /** The name of the compiler, for use in diagnostics. duke@1: */ duke@1: String ownName; duke@1: duke@1: /** The writer to use for diagnostic output. duke@1: */ duke@1: PrintWriter out; duke@1: duke@1: /** duke@1: * If true, any command line arg errors will cause an exception. duke@1: */ duke@1: boolean fatalErrors; duke@1: duke@1: /** Result codes. duke@1: */ duke@1: static final int duke@1: EXIT_OK = 0, // Compilation completed with no errors. duke@1: EXIT_ERROR = 1, // Completed but reported errors. duke@1: EXIT_CMDERR = 2, // Bad command-line arguments duke@1: EXIT_SYSERR = 3, // System error or resource exhaustion. duke@1: EXIT_ABNORMAL = 4; // Compiler terminated abnormally duke@1: duke@1: private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() { duke@1: duke@1: public void setOut(PrintWriter out) { duke@1: Main.this.out = out; duke@1: } duke@1: duke@1: public void error(String key, Object... args) { duke@1: Main.this.error(key, args); duke@1: } duke@1: duke@1: public void printVersion() { duke@1: Log.printLines(out, getLocalizedString("version", ownName, JavaCompiler.version())); duke@1: } duke@1: duke@1: public void printFullVersion() { duke@1: Log.printLines(out, getLocalizedString("fullVersion", ownName, JavaCompiler.fullVersion())); duke@1: } duke@1: duke@1: public void printHelp() { duke@1: help(); duke@1: } duke@1: duke@1: public void printXhelp() { duke@1: xhelp(); duke@1: } duke@1: duke@1: public void addFile(File f) { duke@1: if (!filenames.contains(f)) duke@1: filenames.append(f); duke@1: } duke@1: duke@1: public void addClassName(String s) { duke@1: classnames.append(s); duke@1: } duke@1: duke@1: }); duke@1: duke@1: /** duke@1: * Construct a compiler instance. duke@1: */ duke@1: public Main(String name) { duke@1: this(name, new PrintWriter(System.err, true)); duke@1: } duke@1: duke@1: /** duke@1: * Construct a compiler instance. duke@1: */ duke@1: public Main(String name, PrintWriter out) { duke@1: this.ownName = name; duke@1: this.out = out; duke@1: } duke@1: /** A table of all options that's passed to the JavaCompiler constructor. */ duke@1: private Options options = null; duke@1: duke@1: /** The list of source files to process duke@1: */ duke@1: public ListBuffer filenames = null; // XXX sb protected duke@1: duke@1: /** List of class files names passed on the command line duke@1: */ duke@1: public ListBuffer classnames = null; // XXX sb protected duke@1: duke@1: /** Print a string that explains usage. duke@1: */ duke@1: void help() { duke@1: Log.printLines(out, getLocalizedString("msg.usage.header", ownName)); duke@1: for (int i=0; i processArgs(String[] flags) { // XXX sb protected duke@1: int ac = 0; duke@1: while (ac < flags.length) { duke@1: String flag = flags[ac]; duke@1: ac++; duke@1: duke@1: Option option = null; duke@1: duke@1: if (flag.length() > 0) { duke@1: // quick hack to speed up file processing: duke@1: // if the option does not begin with '-', there is no need to check duke@1: // most of the compiler options. duke@1: int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1; duke@1: for (int j=firstOptionToCheck; jnil(), null); duke@1: } duke@1: duke@1: /** Programmatic interface for main function. duke@1: * @param args The command line parameters. duke@1: */ duke@1: public int compile(String[] args, duke@1: Context context, duke@1: List fileObjects, duke@1: Iterable processors) duke@1: { duke@1: if (options == null) duke@1: options = Options.instance(context); // creates a new one duke@1: duke@1: filenames = new ListBuffer(); duke@1: classnames = new ListBuffer(); duke@1: JavaCompiler comp = null; duke@1: /* duke@1: * TODO: Logic below about what is an acceptable command line duke@1: * should be updated to take annotation processing semantics duke@1: * into account. duke@1: */ duke@1: try { duke@1: if (args.length == 0 && fileObjects.isEmpty()) { duke@1: help(); duke@1: return EXIT_CMDERR; duke@1: } duke@1: jjg@194: List files; duke@1: try { jjg@194: files = processArgs(CommandLine.parse(args)); jjg@194: if (files == null) { duke@1: // null signals an error in options, abort duke@1: return EXIT_CMDERR; jjg@194: } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { duke@1: // it is allowed to compile nothing if just asking for help or version info jjg@700: if (options.isSet(HELP) jjg@700: || options.isSet(X) jjg@700: || options.isSet(VERSION) jjg@700: || options.isSet(FULLVERSION)) duke@1: return EXIT_OK; duke@1: error("err.no.source.files"); duke@1: return EXIT_CMDERR; duke@1: } duke@1: } catch (java.io.FileNotFoundException e) { duke@1: Log.printLines(out, ownName + ": " + duke@1: getLocalizedString("err.file.not.found", duke@1: e.getMessage())); duke@1: return EXIT_SYSERR; duke@1: } duke@1: jjg@700: boolean forceStdOut = options.isSet("stdout"); duke@1: if (forceStdOut) { duke@1: out.flush(); duke@1: out = new PrintWriter(System.out, true); duke@1: } duke@1: duke@1: context.put(Log.outKey, out); duke@1: jjg@106: // allow System property in following line as a Mustang legacy jjg@700: boolean batchMode = (options.isUnset("nonBatchMode") jjg@106: && System.getProperty("nonBatchMode") == null); jjg@106: if (batchMode) jjg@106: CacheFSInfo.preRegister(context); jjg@106: duke@1: fileManager = context.get(JavaFileManager.class); duke@1: duke@1: comp = JavaCompiler.instance(context); duke@1: if (comp == null) return EXIT_SYSERR; duke@1: jjg@194: Log log = Log.instance(context); jjg@194: jjg@194: if (!files.isEmpty()) { duke@1: // add filenames to fileObjects duke@1: comp = JavaCompiler.instance(context); duke@1: List otherFiles = List.nil(); duke@1: JavacFileManager dfm = (JavacFileManager)fileManager; jjg@194: for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) duke@1: otherFiles = otherFiles.prepend(fo); duke@1: for (JavaFileObject fo : otherFiles) duke@1: fileObjects = fileObjects.prepend(fo); duke@1: } duke@1: comp.compile(fileObjects, duke@1: classnames.toList(), duke@1: processors); duke@1: jjg@194: if (log.expectDiagKeys != null) { jjg@194: if (log.expectDiagKeys.size() == 0) { jjg@194: Log.printLines(log.noticeWriter, "all expected diagnostics found"); jjg@194: return EXIT_OK; jjg@194: } else { jjg@194: Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys); jjg@194: return EXIT_ERROR; jjg@194: } jjg@194: } jjg@194: jjg@215: if (comp.errorCount() != 0) duke@1: return EXIT_ERROR; duke@1: } catch (IOException ex) { duke@1: ioMessage(ex); duke@1: return EXIT_SYSERR; duke@1: } catch (OutOfMemoryError ex) { duke@1: resourceMessage(ex); duke@1: return EXIT_SYSERR; duke@1: } catch (StackOverflowError ex) { duke@1: resourceMessage(ex); duke@1: return EXIT_SYSERR; duke@1: } catch (FatalError ex) { duke@1: feMessage(ex); duke@1: return EXIT_SYSERR; duke@1: } catch(AnnotationProcessingError ex) { duke@1: apMessage(ex); duke@1: return EXIT_SYSERR; duke@1: } catch (ClientCodeException ex) { duke@1: // as specified by javax.tools.JavaCompiler#getTask duke@1: // and javax.tools.JavaCompiler.CompilationTask#call duke@1: throw new RuntimeException(ex.getCause()); duke@1: } catch (PropagatedException ex) { duke@1: throw ex.getCause(); duke@1: } catch (Throwable ex) { duke@1: // Nasty. If we've already reported an error, compensate duke@1: // for buggy compiler error recovery by swallowing thrown duke@1: // exceptions. duke@1: if (comp == null || comp.errorCount() == 0 || jjg@700: options == null || options.isSet("dev")) duke@1: bugMessage(ex); duke@1: return EXIT_ABNORMAL; duke@1: } finally { duke@1: if (comp != null) comp.close(); duke@1: filenames = null; duke@1: options = null; duke@1: } duke@1: return EXIT_OK; duke@1: } duke@1: duke@1: /** Print a message reporting an internal error. duke@1: */ duke@1: void bugMessage(Throwable ex) { duke@1: Log.printLines(out, getLocalizedString("msg.bug", duke@1: JavaCompiler.version())); duke@1: ex.printStackTrace(out); duke@1: } duke@1: jjg@663: /** Print a message reporting a fatal error. duke@1: */ duke@1: void feMessage(Throwable ex) { duke@1: Log.printLines(out, ex.getMessage()); jjg@700: if (ex.getCause() != null && options.isSet("dev")) { jjg@663: ex.getCause().printStackTrace(out); jjg@663: } duke@1: } duke@1: duke@1: /** Print a message reporting an input/output error. duke@1: */ duke@1: void ioMessage(Throwable ex) { duke@1: Log.printLines(out, getLocalizedString("msg.io")); duke@1: ex.printStackTrace(out); duke@1: } duke@1: duke@1: /** Print a message reporting an out-of-resources error. duke@1: */ duke@1: void resourceMessage(Throwable ex) { duke@1: Log.printLines(out, getLocalizedString("msg.resource")); duke@1: // System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG duke@1: ex.printStackTrace(out); duke@1: } duke@1: duke@1: /** Print a message reporting an uncaught exception from an duke@1: * annotation processor. duke@1: */ duke@1: void apMessage(AnnotationProcessingError ex) { duke@1: Log.printLines(out, duke@1: getLocalizedString("msg.proc.annotation.uncaught.exception")); duke@1: ex.getCause().printStackTrace(); duke@1: } duke@1: jjg@535: /** Display the location and checksum of a class. */ jjg@535: void showClass(String className) { jjg@535: out.println("javac: show class: " + className); jjg@535: URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); jjg@535: if (url == null) jjg@535: out.println(" class not found"); jjg@535: else { jjg@535: out.println(" " + url); jjg@535: try { jjg@535: final String algorithm = "MD5"; jjg@535: byte[] digest; jjg@535: MessageDigest md = MessageDigest.getInstance(algorithm); jjg@535: DigestInputStream in = new DigestInputStream(url.openStream(), md); jjg@535: try { jjg@535: byte[] buf = new byte[8192]; jjg@535: int n; jjg@535: do { n = in.read(buf); } while (n > 0); jjg@535: digest = md.digest(); jjg@535: } finally { jjg@535: in.close(); jjg@535: } jjg@535: StringBuilder sb = new StringBuilder(); jjg@535: for (byte b: digest) jjg@535: sb.append(String.format("%02x", b)); jjg@535: out.println(" " + algorithm + " checksum: " + sb); jjg@535: } catch (Exception e) { jjg@535: out.println(" cannot compute digest: " + e); jjg@535: } jjg@535: } jjg@535: } jjg@535: duke@1: private JavaFileManager fileManager; duke@1: duke@1: /* ************************************************************************ duke@1: * Internationalization duke@1: *************************************************************************/ duke@1: duke@1: /** Find a localized string in the resource bundle. duke@1: * @param key The key for the localized string. duke@1: */ duke@1: public static String getLocalizedString(String key, Object... args) { // FIXME sb private duke@1: try { duke@1: if (messages == null) mcimadamore@136: messages = new JavacMessages(javacBundleName); duke@1: return messages.getLocalizedString("javac." + key, args); duke@1: } duke@1: catch (MissingResourceException e) { duke@1: throw new Error("Fatal Error: Resource for javac is missing", e); duke@1: } duke@1: } duke@1: duke@1: public static void useRawMessages(boolean enable) { duke@1: if (enable) { mcimadamore@136: messages = new JavacMessages(javacBundleName) { duke@1: public String getLocalizedString(String key, Object... args) { duke@1: return key; duke@1: } duke@1: }; duke@1: } else { mcimadamore@136: messages = new JavacMessages(javacBundleName); duke@1: } duke@1: } duke@1: duke@1: private static final String javacBundleName = duke@1: "com.sun.tools.javac.resources.javac"; duke@1: mcimadamore@136: private static JavacMessages messages; duke@1: }