|
1 /* |
|
2 * Copyright (c) 2014, 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.sjavac.options; |
|
27 |
|
28 import java.io.File; |
|
29 import java.nio.file.Path; |
|
30 import java.nio.file.Paths; |
|
31 import java.util.ArrayList; |
|
32 import java.util.List; |
|
33 import java.util.regex.Matcher; |
|
34 import java.util.regex.Pattern; |
|
35 |
|
36 import com.sun.tools.sjavac.CopyFile; |
|
37 import com.sun.tools.sjavac.Transformer; |
|
38 |
|
39 |
|
40 /** |
|
41 * Sjavac options can be classified as: |
|
42 * |
|
43 * (1) relevant only for sjavac, such as --server |
|
44 * (2) relevant for sjavac and javac, such as -d, or |
|
45 * (3) relevant only for javac, such as -g. |
|
46 * |
|
47 * This enum represents all options from (1) and (2). Note that instances of |
|
48 * this enum only entail static information about the option. For storage of |
|
49 * option values, refer to com.sun.tools.sjavac.options.Options. |
|
50 */ |
|
51 public enum Option { |
|
52 |
|
53 SRC("-src", "Location of source files to be compiled") { |
|
54 @Override |
|
55 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
56 List<Path> paths = getFileListArg(iter, helper); |
|
57 if (paths != null) |
|
58 helper.sourceRoots(paths); |
|
59 } |
|
60 }, |
|
61 SOURCEPATH("-sourcepath", "Specify search path for sources.") { |
|
62 @Override |
|
63 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
64 List<Path> paths = getFileListArg(iter, helper); |
|
65 if (paths != null) |
|
66 helper.sourcepath(paths); |
|
67 } |
|
68 }, |
|
69 MODULEPATH("-modulepath", "Specify search path for modules.") { |
|
70 @Override |
|
71 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
72 List<Path> paths = getFileListArg(iter, helper); |
|
73 if (paths != null) |
|
74 helper.modulepath(paths); |
|
75 } |
|
76 }, |
|
77 CLASSPATH("-classpath", "Specify search path for classes.") { |
|
78 @Override |
|
79 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
80 List<Path> paths = getFileListArg(iter, helper); |
|
81 if (paths != null) |
|
82 helper.classpath(paths); |
|
83 } |
|
84 }, |
|
85 CP("-cp", "An alias for -classpath") { |
|
86 @Override |
|
87 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
88 CLASSPATH.processMatching(iter, helper); |
|
89 } |
|
90 }, |
|
91 X("-x", "Exclude directory from the subsequent source directory") { |
|
92 @Override |
|
93 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
94 String pattern = getFilePatternArg(iter, helper); |
|
95 if (pattern != null) |
|
96 helper.exclude(pattern); |
|
97 } |
|
98 }, |
|
99 I("-i", "Include only the given directory from the subsequent source directory") { |
|
100 @Override |
|
101 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
102 String pattern = getFilePatternArg(iter, helper); |
|
103 if (pattern != null) |
|
104 helper.include(pattern); |
|
105 } |
|
106 }, |
|
107 XF("-xf", "Exclude a given file") { |
|
108 @Override |
|
109 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
110 String pattern = getFilePatternArg(iter, helper); |
|
111 if (pattern != null) |
|
112 helper.excludeFile(pattern); |
|
113 } |
|
114 }, |
|
115 IF("-if", "Include only the given file") { |
|
116 @Override |
|
117 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
118 String pattern = getFilePatternArg(iter, helper); |
|
119 if (pattern != null) |
|
120 helper.includeFile(pattern); |
|
121 } |
|
122 }, |
|
123 TR("-tr", "Translate resources") { |
|
124 @Override |
|
125 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
126 |
|
127 if (!iter.hasNext()) { |
|
128 helper.reportError(arg + " must be followed by a translation rule"); |
|
129 return; |
|
130 } |
|
131 |
|
132 String trArg = iter.next(); |
|
133 |
|
134 // Validate argument syntax. Examples: |
|
135 // .prop=com.sun.tools.javac.smart.CompileProperties |
|
136 // .idl=com.sun.corba.CompileIdl |
|
137 // .g3=antlr.CompileGrammar,debug=true |
|
138 String ident = "[a-zA-Z_][a-zA-Z0-9_]*"; |
|
139 Pattern p = Pattern.compile("(?<suffix>\\." + ident + ")=" + |
|
140 "(?<class>" + ident + "(\\." + ident + ")*)" + |
|
141 "(?<extra>,.*)?"); |
|
142 // Check syntax |
|
143 Matcher m = p.matcher(trArg); |
|
144 if (!m.matches()) { |
|
145 helper.reportError("The string \"" + trArg + "\" is not a " + |
|
146 "valid translate pattern"); |
|
147 return; |
|
148 } |
|
149 |
|
150 // Extract relevant parts |
|
151 String suffix = m.group("suffix"); |
|
152 String classname = m.group("class"); |
|
153 String extra = m.group("extra"); |
|
154 |
|
155 // Valid suffix? |
|
156 if (suffix.matches("\\.(class|java)")) { |
|
157 helper.reportError("You cannot have a translator for " + |
|
158 suffix + " files!"); |
|
159 return; |
|
160 } |
|
161 |
|
162 // Construct transformer |
|
163 try { |
|
164 Class<?> trCls = Class.forName(classname); |
|
165 Transformer transformer = (Transformer) trCls.newInstance(); |
|
166 transformer.setExtra(extra); |
|
167 helper.addTransformer(suffix, transformer); |
|
168 } catch (Exception e) { |
|
169 helper.reportError("Cannot use " + classname + |
|
170 " as a translator: " + e.getMessage()); |
|
171 } |
|
172 } |
|
173 }, |
|
174 COPY("-copy", "Copy resources") { |
|
175 @Override |
|
176 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
177 if (!iter.hasNext()) { |
|
178 helper.reportError(arg + " must be followed by a resource type"); |
|
179 return; |
|
180 } |
|
181 |
|
182 String copyArg = iter.next(); |
|
183 |
|
184 // Validate argument syntax. Examples: .gif, .html |
|
185 if (!copyArg.matches("\\.[a-zA-Z_][a-zA-Z0-9_]*")) { |
|
186 helper.reportError("The string \"" + copyArg + "\" is not a " + |
|
187 "valid resource type."); |
|
188 return; |
|
189 } |
|
190 |
|
191 helper.addTransformer(copyArg, new CopyFile()); |
|
192 } |
|
193 }, |
|
194 J("-j", "Number of cores") { |
|
195 @Override |
|
196 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
197 if (!iter.hasNext() || !iter.peek().matches("\\d+")) { |
|
198 helper.reportError(arg + " must be followed by an integer"); |
|
199 return; |
|
200 } |
|
201 helper.numCores(Integer.parseInt(iter.next())); |
|
202 } |
|
203 }, |
|
204 SERVER("--server:", "Specify server configuration file of running server") { |
|
205 @Override |
|
206 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
207 helper.serverConf(iter.current().substring(arg.length())); |
|
208 } |
|
209 }, |
|
210 STARTSERVER("--startserver:", "Start server and use the given configuration file") { |
|
211 @Override |
|
212 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
213 helper.startServerConf(iter.current().substring(arg.length())); |
|
214 } |
|
215 }, |
|
216 IMPLICIT("-implicit:", "Specify how to treat implicitly referenced source code") { |
|
217 @Override |
|
218 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
219 helper.implicit(iter.current().substring(arg.length())); |
|
220 } |
|
221 }, |
|
222 LOG("--log=", "Specify logging level") { |
|
223 @Override |
|
224 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
225 helper.logLevel(iter.current().substring(arg.length())); |
|
226 } |
|
227 }, |
|
228 VERBOSE("-verbose", "Set verbosity level to \"info\"") { |
|
229 @Override |
|
230 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
231 helper.logLevel("info"); |
|
232 } |
|
233 }, |
|
234 PERMIT_UNIDENTIFIED_ARTIFACTS("--permit-unidentified-artifacts", "Keep unidentified artifacts in destination directory") { |
|
235 @Override |
|
236 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
237 helper.permitUnidentifiedArtifacts(); |
|
238 } |
|
239 }, |
|
240 PERMIT_SOURCES_WITHOUT_PACKAGE("--permit-sources-without-package", "Permit sources in the default package") { |
|
241 @Override |
|
242 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
243 helper.permitDefaultPackage(); |
|
244 } |
|
245 }, |
|
246 COMPARE_FOUND_SOURCES("--compare-found-sources", "Compare found sources with given sources") { |
|
247 @Override |
|
248 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
249 Path referenceSourceList = getFileArg(iter, helper, true, false); |
|
250 if (referenceSourceList != null) |
|
251 helper.compareFoundSources(referenceSourceList); |
|
252 } |
|
253 }, |
|
254 D("-d", "Output destination directory") { |
|
255 @Override |
|
256 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
257 Path dir = getFileArg(iter, helper, false, true); |
|
258 if (dir != null) |
|
259 helper.destDir(dir); |
|
260 } |
|
261 }, |
|
262 S("-s", "Directory for generated sources") { |
|
263 @Override |
|
264 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
265 Path dir = getFileArg(iter, helper, false, true); |
|
266 if (dir != null) |
|
267 helper.generatedSourcesDir(dir); |
|
268 } |
|
269 }, |
|
270 H("-h", "Directory for header files") { |
|
271 @Override |
|
272 protected void processMatching(ArgumentIterator iter, OptionHelper helper) { |
|
273 Path dir = getFileArg(iter, helper, false, true); |
|
274 if (dir != null) |
|
275 helper.headerDir(dir); |
|
276 } |
|
277 }; |
|
278 |
|
279 public final String arg; |
|
280 |
|
281 final String description; |
|
282 |
|
283 private Option(String arg, String description) { |
|
284 this.arg = arg; |
|
285 this.description = description; |
|
286 } |
|
287 |
|
288 /** Retrieve and verify syntax of file list argument. */ |
|
289 List<Path> getFileListArg(ArgumentIterator iter, OptionHelper helper) { |
|
290 if (!iter.hasNext()) { |
|
291 helper.reportError(arg + " must be followed by a list of files " + |
|
292 "separated by " + File.pathSeparator); |
|
293 return null; |
|
294 } |
|
295 List<Path> result = new ArrayList<>(); |
|
296 for (String pathStr : iter.next().split(File.pathSeparator)) |
|
297 result.add(Paths.get(pathStr)); |
|
298 return result; |
|
299 } |
|
300 |
|
301 /** Retrieve and verify syntax of file argument. */ |
|
302 Path getFileArg(ArgumentIterator iter, OptionHelper helper, boolean fileAcceptable, boolean dirAcceptable) { |
|
303 |
|
304 if (!iter.hasNext()) { |
|
305 String errmsg = arg + " must be followed by "; |
|
306 if (fileAcceptable && dirAcceptable) errmsg += "a file or directory."; |
|
307 else if (fileAcceptable) errmsg += "a file."; |
|
308 else if (dirAcceptable) errmsg += "a directory."; |
|
309 else throw new IllegalArgumentException("File or directory must be acceptable."); |
|
310 helper.reportError(errmsg); |
|
311 return null; |
|
312 } |
|
313 |
|
314 return Paths.get(iter.next()); |
|
315 } |
|
316 |
|
317 /** Retrieve the next file or package argument. */ |
|
318 String getFilePatternArg(ArgumentIterator iter, OptionHelper helper) { |
|
319 |
|
320 if (!iter.hasNext()) { |
|
321 helper.reportError(arg + " must be followed by a file or directory pattern."); |
|
322 return null; |
|
323 } |
|
324 |
|
325 return iter.next(); |
|
326 } |
|
327 |
|
328 // Future cleanup: Change the "=" syntax to ":" syntax to be consistent and |
|
329 // to follow the javac-option style. |
|
330 |
|
331 public boolean hasOption() { |
|
332 return arg.endsWith(":") || arg.endsWith("="); |
|
333 } |
|
334 |
|
335 |
|
336 /** |
|
337 * Process current argument of argIter. |
|
338 * |
|
339 * It's final, since the option customization is typically done in |
|
340 * processMatching. |
|
341 * |
|
342 * @param argIter Iterator to read current and succeeding arguments from. |
|
343 * @param helper The helper to report back to. |
|
344 * @return true iff the argument was processed by this option. |
|
345 */ |
|
346 public final boolean processCurrent(ArgumentIterator argIter, |
|
347 OptionHelper helper) { |
|
348 String fullArg = argIter.current(); // "-tr" or "-log=level" |
|
349 if (hasOption() ? fullArg.startsWith(arg) : fullArg.equals(arg)) { |
|
350 processMatching(argIter, helper); |
|
351 return true; |
|
352 } |
|
353 // Did not match |
|
354 return false; |
|
355 } |
|
356 |
|
357 /** Called by process if the current argument matches this option. */ |
|
358 protected abstract void processMatching(ArgumentIterator argIter, |
|
359 OptionHelper helper); |
|
360 } |