Tue, 10 Apr 2012 08:15:25 -0700
7074397: Build infrastructure changes (makefile re-write)
Summary: New makefiles transition, old and new living side by side for now.
Reviewed-by: ohair, jjg, dholmes, ohrstrom, erikj, ihse, tgranat, ykantser
Contributed-by: ohrstrom <fredrik.ohrstrom@oracle.com>, erikj <erik.joelsson@oracle.com>, ihse <magnus.ihse.bursie@oracle.com>, tgranat <torbjorn.granat@oracle.com>, ykantser <yekaterina.kantserova@oracle.com>
1 /*
2 * Copyright (c) 2010, 2011, 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 */
24 import java.io.*;
25 import java.util.*;
26 import java.util.List;
27 import javax.tools.*;
29 import com.sun.tools.javac.api.*;
30 import com.sun.tools.javac.api.DiagnosticFormatter.Configuration.DiagnosticPart;
31 import com.sun.tools.javac.api.Formattable.LocalizedString;
32 import com.sun.tools.javac.code.Flags.Flag;
33 import com.sun.tools.javac.code.Kinds.KindName;
34 import com.sun.tools.javac.code.*;
35 import com.sun.tools.javac.file.*;
36 import com.sun.tools.javac.main.Main;
37 import com.sun.tools.javac.main.JavaCompiler;
38 import com.sun.tools.javac.parser.Tokens.TokenKind;
39 import com.sun.tools.javac.util.*;
40 import com.sun.tools.javac.util.AbstractDiagnosticFormatter.SimpleConfiguration;
41 import javax.lang.model.SourceVersion;
43 /**
44 * Compiler factory for instances of Example.Compiler that use custom
45 * DiagnosticFormatter and Messages objects to track the types of args
46 * when when localizing diagnostics.
47 * The compiler objects only support "output" mode, not "check" mode.
48 */
49 class ArgTypeCompilerFactory implements Example.Compiler.Factory {
50 // Same code as Example.Compiler.DefaultFactory, but the names resolve differently
51 public Example.Compiler getCompiler(List<String> opts, boolean verbose) {
52 String first;
53 String[] rest;
54 if (opts == null || opts.isEmpty()) {
55 first = null;
56 rest = new String[0];
57 } else {
58 first = opts.get(0);
59 rest = opts.subList(1, opts.size()).toArray(new String[opts.size() - 1]);
60 }
61 if (first == null || first.equals("jsr199"))
62 return new Jsr199Compiler(verbose, rest);
63 else if (first.equals("simple"))
64 return new SimpleCompiler(verbose);
65 else if (first.equals("backdoor"))
66 return new BackdoorCompiler(verbose);
67 else
68 throw new IllegalArgumentException(first);
69 }
71 /**
72 * Compile using the JSR 199 API. The diagnostics generated are
73 * scanned for resource keys. Not all diagnostic keys are generated
74 * via the JSR 199 API -- for example, rich diagnostics are not directly
75 * accessible, and some diagnostics generated by the file manager may
76 * not be generated (for example, the JSR 199 file manager does not see
77 * -Xlint:path).
78 */
79 static class Jsr199Compiler extends Example.Compiler {
80 List<String> fmOpts;
82 Jsr199Compiler(boolean verbose, String... args) {
83 super(verbose);
84 for (int i = 0; i < args.length; i++) {
85 String arg = args[i];
86 if (arg.equals("-filemanager") && (i + 1 < args.length)) {
87 fmOpts = Arrays.asList(args[++i].split(","));
88 } else
89 throw new IllegalArgumentException(arg);
90 }
91 }
93 @Override
94 boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
95 assert out != null && keys == null;
97 if (verbose)
98 System.err.println("run_jsr199: " + opts + " " + files);
100 JavacTool tool = JavacTool.create();
102 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
103 if (fmOpts != null)
104 fm = new FileManager(fm, fmOpts);
106 Iterable<? extends JavaFileObject> fos = fm.getJavaFileObjectsFromFiles(files);
108 Context c = new Context();
109 ArgTypeMessages.preRegister(c);
110 ArgTypeJavaCompiler.preRegister(c);
111 JavacTaskImpl t = (JavacTaskImpl) tool.getTask(out, fm, null, opts, null, fos, c);
112 return t.call();
113 }
114 }
116 /**
117 * Run the test using the standard simple entry point.
118 */
119 static class SimpleCompiler extends Example.Compiler {
120 SimpleCompiler(boolean verbose) {
121 super(verbose);
122 }
124 @Override
125 boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
126 assert out != null && keys == null;
128 if (verbose)
129 System.err.println("run_simple: " + opts + " " + files);
131 List<String> args = new ArrayList<String>();
133 args.addAll(opts);
134 for (File f: files)
135 args.add(f.getPath());
137 Main main = new Main("javac", out);
138 Context c = new Context() {
139 @Override public void clear() {
140 ((JavacFileManager) get(JavaFileManager.class)).close();
141 super.clear();
142 }
143 };
144 JavacFileManager.preRegister(c); // can't create it until Log has been set up
145 ArgTypeJavaCompiler.preRegister(c);
146 ArgTypeMessages.preRegister(c);
147 Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
149 return result.isOK();
150 }
151 }
153 static class BackdoorCompiler extends Example.Compiler {
154 BackdoorCompiler(boolean verbose) {
155 super(verbose);
156 }
158 @Override
159 boolean run(PrintWriter out, Set<String> keys, boolean raw, List<String> opts, List<File> files) {
160 assert out != null && keys == null;
162 if (verbose)
163 System.err.println("run_simple: " + opts + " " + files);
165 List<String> args = new ArrayList<String>(opts);
166 for (File f: files)
167 args.add(f.getPath());
169 Context c = new Context();
170 JavacFileManager.preRegister(c); // can't create it until Log has been set up
171 ArgTypeJavaCompiler.preRegister(c);
172 ArgTypeMessages.preRegister(c);
173 Main m = new Main("javac", out);
174 Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
176 return result.isOK();
177 }
179 }
182 // <editor-fold defaultstate="collapsed" desc="Custom Javac components">
184 /**
185 * Diagnostic formatter which reports formats a diag as a series of lines
186 * containing a key, and a possibly empty set of descriptive strings for the
187 * arg types.
188 */
189 static class ArgTypeDiagnosticFormatter extends AbstractDiagnosticFormatter {
191 ArgTypeDiagnosticFormatter(Options options) {
192 super(null, new SimpleConfiguration(options,
193 EnumSet.of(DiagnosticPart.SUMMARY,
194 DiagnosticPart.DETAILS,
195 DiagnosticPart.SUBDIAGNOSTICS)));
196 }
198 @Override
199 protected String formatDiagnostic(JCDiagnostic d, Locale locale) {
200 return formatMessage(d, locale);
201 }
203 @Override
204 public String formatMessage(JCDiagnostic d, Locale l) {
205 StringBuilder buf = new StringBuilder();
206 formatMessage(d, buf);
207 return buf.toString();
208 }
210 private void formatMessage(JCDiagnostic d, StringBuilder buf) {
211 String key = d.getCode();
212 Object[] args = d.getArgs();
213 // report the primary arg types, without recursing into diag fragments
214 buf.append(getKeyArgsString(key, args));
215 // report details for any diagnostic fragments
216 for (Object arg: args) {
217 if (arg instanceof JCDiagnostic) {
218 buf.append("\n");
219 formatMessage((JCDiagnostic) arg, buf);
220 }
221 }
222 // report details for any subdiagnostics
223 for (String s: formatSubdiagnostics(d, null)) {
224 buf.append("\n");
225 buf.append(s);
226 }
227 }
229 @Override
230 public boolean isRaw() {
231 return true;
232 }
233 }
235 /**
236 * Trivial subtype of JavaCompiler to get access to the protected compilerKey field.
237 * The factory is used to ensure that the log is initialized with an instance of
238 * ArgTypeDiagnosticFormatter before we create the required JavaCompiler.
239 */
240 static class ArgTypeJavaCompiler extends JavaCompiler {
241 static void preRegister(Context context) {
242 context.put(compilerKey, new Context.Factory<JavaCompiler>() {
243 public JavaCompiler make(Context c) {
244 Log log = Log.instance(c);
245 Options options = Options.instance(c);
246 log.setDiagnosticFormatter(new ArgTypeDiagnosticFormatter(options));
247 return new JavaCompiler(c);
248 }
249 });
250 }
252 // not used
253 private ArgTypeJavaCompiler() {
254 super(null);
255 }
256 }
258 /**
259 * Diagnostic formatter which "localizes" a message as a line
260 * containing a key, and a possibly empty set of descriptive strings for the
261 * arg types.
262 */
263 static class ArgTypeMessages extends JavacMessages {
264 static void preRegister(Context context) {
265 context.put(JavacMessages.messagesKey, new Context.Factory<JavacMessages>() {
266 public JavacMessages make(Context c) {
267 return new ArgTypeMessages(c) {
268 @Override
269 public String getLocalizedString(Locale l, String key, Object... args) {
270 return getKeyArgsString(key, args);
271 }
272 };
273 }
274 });
275 }
277 ArgTypeMessages(Context context) {
278 super(context);
279 }
280 }
282 /**
283 * Utility method to generate a string for key and args
284 */
285 static String getKeyArgsString(String key, Object... args) {
286 StringBuilder buf = new StringBuilder();
287 buf.append(key);
288 String sep = ": ";
289 for (Object o : args) {
290 buf.append(sep);
291 buf.append(getArgTypeOrStringValue(o));
292 sep = ", ";
293 }
294 return buf.toString();
295 }
297 static boolean showStringValues = false;
299 static String getArgTypeOrStringValue(Object o) {
300 if (showStringValues && o instanceof String)
301 return "\"" + o + "\"";
302 return getArgType(o);
303 }
305 static String getArgType(Object o) {
306 if (o == null)
307 return "null";
308 if (o instanceof Name)
309 return "name";
310 if (o instanceof Boolean)
311 return "boolean";
312 if (o instanceof Integer)
313 return "number";
314 if (o instanceof String)
315 return "string";
316 if (o instanceof Flag)
317 return "modifier";
318 if (o instanceof KindName)
319 return "symbol kind";
320 if (o instanceof TokenKind)
321 return "token";
322 if (o instanceof Symbol)
323 return "symbol";
324 if (o instanceof Type)
325 return "type";
326 if (o instanceof List) {
327 List<?> l = (List<?>) o;
328 if (l.isEmpty())
329 return "list";
330 else
331 return "list of " + getArgType(l.get(0));
332 }
333 if (o instanceof ListBuffer)
334 return getArgType(((ListBuffer) o).toList());
335 if (o instanceof Set) {
336 Set<?> s = (Set<?>) o;
337 if (s.isEmpty())
338 return "set";
339 else
340 return "set of " + getArgType(s.iterator().next());
341 }
342 if (o instanceof SourceVersion)
343 return "source version";
344 if (o instanceof FileObject || o instanceof File)
345 return "file name";
346 if (o instanceof JCDiagnostic)
347 return "message segment";
348 if (o instanceof LocalizedString)
349 return "message segment"; // only instance is "no arguments"
350 String s = o.getClass().getSimpleName();
351 return (s.isEmpty() ? o.getClass().getName() : s);
352 }
354 // </editor-fold>
356 }