OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # | |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 import fnmatch | |
8 import optparse | |
9 import os | |
10 import shutil | |
11 import re | |
12 import sys | |
13 import textwrap | |
14 | |
15 from util import build_utils | |
16 from util import md5_check | |
17 | |
18 import jar | |
19 | |
20 sys.path.append(build_utils.COLORAMA_ROOT) | |
21 import colorama | |
22 | |
23 | |
24 def ColorJavacOutput(output): | |
25 fileline_prefix = r'(?P<fileline>(?P<file>[-.\w/\\]+.java):(?P<line>[0-9]+):)' | |
26 warning_re = re.compile( | |
27 fileline_prefix + r'(?P<full_message> warning: (?P<message>.*))$') | |
28 error_re = re.compile( | |
29 fileline_prefix + r'(?P<full_message> (?P<message>.*))$') | |
30 marker_re = re.compile(r'\s*(?P<marker>\^)\s*$') | |
31 | |
32 warning_color = ['full_message', colorama.Fore.YELLOW + colorama.Style.DIM] | |
33 error_color = ['full_message', colorama.Fore.MAGENTA + colorama.Style.BRIGHT] | |
34 marker_color = ['marker', colorama.Fore.BLUE + colorama.Style.BRIGHT] | |
35 | |
36 def Colorize(line, regex, color): | |
37 match = regex.match(line) | |
38 start = match.start(color[0]) | |
39 end = match.end(color[0]) | |
40 return (line[:start] | |
41 + color[1] + line[start:end] | |
42 + colorama.Fore.RESET + colorama.Style.RESET_ALL | |
43 + line[end:]) | |
44 | |
45 def ApplyColor(line): | |
46 if warning_re.match(line): | |
47 line = Colorize(line, warning_re, warning_color) | |
48 elif error_re.match(line): | |
49 line = Colorize(line, error_re, error_color) | |
50 elif marker_re.match(line): | |
51 line = Colorize(line, marker_re, marker_color) | |
52 return line | |
53 | |
54 return '\n'.join(map(ApplyColor, output.split('\n'))) | |
55 | |
56 | |
57 ERRORPRONE_OPTIONS = [ | |
58 '-Xepdisable:' | |
59 # Something in chrome_private_java makes this check crash. | |
60 'com.google.errorprone.bugpatterns.ClassCanBeStatic,' | |
61 # These crash on lots of targets. | |
62 'com.google.errorprone.bugpatterns.WrongParameterPackage,' | |
63 'com.google.errorprone.bugpatterns.GuiceOverridesGuiceInjectableMethod,' | |
64 'com.google.errorprone.bugpatterns.GuiceOverridesJavaxInjectableMethod,' | |
65 'com.google.errorprone.bugpatterns.ElementsCountedInLoop' | |
66 ] | |
67 | |
68 def DoJavac( | |
69 bootclasspath, classpath, classes_dir, chromium_code, | |
70 use_errorprone_path, java_files): | |
71 """Runs javac. | |
72 | |
73 Builds |java_files| with the provided |classpath| and puts the generated | |
74 .class files into |classes_dir|. If |chromium_code| is true, extra lint | |
75 checking will be enabled. | |
76 """ | |
77 | |
78 jar_inputs = [] | |
79 for path in classpath: | |
80 if os.path.exists(path + '.TOC'): | |
81 jar_inputs.append(path + '.TOC') | |
82 else: | |
83 jar_inputs.append(path) | |
84 | |
85 javac_args = [ | |
86 '-g', | |
87 # Chromium only allows UTF8 source files. Being explicit avoids | |
88 # javac pulling a default encoding from the user's environment. | |
89 '-encoding', 'UTF-8', | |
90 '-classpath', ':'.join(classpath), | |
91 '-d', classes_dir] | |
92 | |
93 if bootclasspath: | |
94 javac_args.extend([ | |
95 '-bootclasspath', ':'.join(bootclasspath), | |
96 '-source', '1.7', | |
97 '-target', '1.7', | |
98 ]) | |
99 | |
100 if chromium_code: | |
101 # TODO(aurimas): re-enable '-Xlint:deprecation' checks once they are fixed. | |
102 javac_args.extend(['-Xlint:unchecked']) | |
103 else: | |
104 # XDignore.symbol.file makes javac compile against rt.jar instead of | |
105 # ct.sym. This means that using a java internal package/class will not | |
106 # trigger a compile warning or error. | |
107 javac_args.extend(['-XDignore.symbol.file']) | |
108 | |
109 if use_errorprone_path: | |
110 javac_cmd = [use_errorprone_path] + ERRORPRONE_OPTIONS | |
111 else: | |
112 javac_cmd = ['javac'] | |
113 | |
114 javac_cmd = javac_cmd + javac_args + java_files | |
115 | |
116 def Compile(): | |
117 build_utils.CheckOutput( | |
118 javac_cmd, | |
119 print_stdout=chromium_code, | |
120 stderr_filter=ColorJavacOutput) | |
121 | |
122 record_path = os.path.join(classes_dir, 'javac.md5.stamp') | |
123 md5_check.CallAndRecordIfStale( | |
124 Compile, | |
125 record_path=record_path, | |
126 input_paths=java_files + jar_inputs, | |
127 input_strings=javac_cmd) | |
128 | |
129 | |
130 _MAX_MANIFEST_LINE_LEN = 72 | |
131 | |
132 | |
133 def CreateManifest(manifest_path, classpath, main_class=None, | |
134 manifest_entries=None): | |
135 """Creates a manifest file with the given parameters. | |
136 | |
137 This generates a manifest file that compiles with the spec found at | |
138 http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#JAR_Manifes
t | |
139 | |
140 Args: | |
141 manifest_path: The path to the manifest file that should be created. | |
142 classpath: The JAR files that should be listed on the manifest file's | |
143 classpath. | |
144 main_class: If present, the class containing the main() function. | |
145 manifest_entries: If present, a list of (key, value) pairs to add to | |
146 the manifest. | |
147 | |
148 """ | |
149 output = ['Manifest-Version: 1.0'] | |
150 if main_class: | |
151 output.append('Main-Class: %s' % main_class) | |
152 if manifest_entries: | |
153 for k, v in manifest_entries: | |
154 output.append('%s: %s' % (k, v)) | |
155 if classpath: | |
156 sanitized_paths = [] | |
157 for path in classpath: | |
158 sanitized_paths.append(os.path.basename(path.strip('"'))) | |
159 output.append('Class-Path: %s' % ' '.join(sanitized_paths)) | |
160 output.append('Created-By: ') | |
161 output.append('') | |
162 | |
163 wrapper = textwrap.TextWrapper(break_long_words=True, | |
164 drop_whitespace=False, | |
165 subsequent_indent=' ', | |
166 width=_MAX_MANIFEST_LINE_LEN - 2) | |
167 output = '\r\n'.join(w for l in output for w in wrapper.wrap(l)) | |
168 | |
169 with open(manifest_path, 'w') as f: | |
170 f.write(output) | |
171 | |
172 | |
173 def main(argv): | |
174 colorama.init() | |
175 | |
176 argv = build_utils.ExpandFileArgs(argv) | |
177 | |
178 parser = optparse.OptionParser() | |
179 build_utils.AddDepfileOption(parser) | |
180 | |
181 parser.add_option( | |
182 '--src-gendirs', | |
183 help='Directories containing generated java files.') | |
184 parser.add_option( | |
185 '--java-srcjars', | |
186 action='append', | |
187 default=[], | |
188 help='List of srcjars to include in compilation.') | |
189 parser.add_option( | |
190 '--bootclasspath', | |
191 action='append', | |
192 default=[], | |
193 help='Boot classpath for javac. If this is specified multiple times, ' | |
194 'they will all be appended to construct the classpath.') | |
195 parser.add_option( | |
196 '--classpath', | |
197 action='append', | |
198 help='Classpath for javac. If this is specified multiple times, they ' | |
199 'will all be appended to construct the classpath.') | |
200 parser.add_option( | |
201 '--javac-includes', | |
202 help='A list of file patterns. If provided, only java files that match' | |
203 'one of the patterns will be compiled.') | |
204 parser.add_option( | |
205 '--jar-excluded-classes', | |
206 default='', | |
207 help='List of .class file patterns to exclude from the jar.') | |
208 | |
209 parser.add_option( | |
210 '--chromium-code', | |
211 type='int', | |
212 help='Whether code being compiled should be built with stricter ' | |
213 'warnings for chromium code.') | |
214 | |
215 parser.add_option( | |
216 '--use-errorprone-path', | |
217 help='Use the Errorprone compiler at this path.') | |
218 | |
219 parser.add_option( | |
220 '--classes-dir', | |
221 help='Directory for compiled .class files.') | |
222 parser.add_option('--jar-path', help='Jar output path.') | |
223 parser.add_option( | |
224 '--main-class', | |
225 help='The class containing the main method.') | |
226 parser.add_option( | |
227 '--manifest-entry', | |
228 action='append', | |
229 help='Key:value pairs to add to the .jar manifest.') | |
230 | |
231 parser.add_option('--stamp', help='Path to touch on success.') | |
232 | |
233 options, args = parser.parse_args(argv) | |
234 | |
235 if options.main_class and not options.jar_path: | |
236 parser.error('--main-class requires --jar-path') | |
237 | |
238 bootclasspath = [] | |
239 for arg in options.bootclasspath: | |
240 bootclasspath += build_utils.ParseGypList(arg) | |
241 | |
242 classpath = [] | |
243 for arg in options.classpath: | |
244 classpath += build_utils.ParseGypList(arg) | |
245 | |
246 java_srcjars = [] | |
247 for arg in options.java_srcjars: | |
248 java_srcjars += build_utils.ParseGypList(arg) | |
249 | |
250 java_files = args | |
251 if options.src_gendirs: | |
252 src_gendirs = build_utils.ParseGypList(options.src_gendirs) | |
253 java_files += build_utils.FindInDirectories(src_gendirs, '*.java') | |
254 | |
255 input_files = bootclasspath + classpath + java_srcjars + java_files | |
256 with build_utils.TempDir() as temp_dir: | |
257 classes_dir = os.path.join(temp_dir, 'classes') | |
258 os.makedirs(classes_dir) | |
259 if java_srcjars: | |
260 java_dir = os.path.join(temp_dir, 'java') | |
261 os.makedirs(java_dir) | |
262 for srcjar in java_srcjars: | |
263 build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java') | |
264 java_files += build_utils.FindInDirectory(java_dir, '*.java') | |
265 | |
266 if options.javac_includes: | |
267 javac_includes = build_utils.ParseGypList(options.javac_includes) | |
268 filtered_java_files = [] | |
269 for f in java_files: | |
270 for include in javac_includes: | |
271 if fnmatch.fnmatch(f, include): | |
272 filtered_java_files.append(f) | |
273 break | |
274 java_files = filtered_java_files | |
275 | |
276 if len(java_files) != 0: | |
277 DoJavac( | |
278 bootclasspath, | |
279 classpath, | |
280 classes_dir, | |
281 options.chromium_code, | |
282 options.use_errorprone_path, | |
283 java_files) | |
284 | |
285 if options.jar_path: | |
286 if options.main_class or options.manifest_entry: | |
287 if options.manifest_entry: | |
288 entries = map(lambda e: e.split(":"), options.manifest_entry) | |
289 else: | |
290 entries = [] | |
291 manifest_file = os.path.join(temp_dir, 'manifest') | |
292 CreateManifest(manifest_file, classpath, options.main_class, entries) | |
293 else: | |
294 manifest_file = None | |
295 jar.JarDirectory(classes_dir, | |
296 build_utils.ParseGypList(options.jar_excluded_classes), | |
297 options.jar_path, | |
298 manifest_file=manifest_file) | |
299 | |
300 if options.classes_dir: | |
301 # Delete the old classes directory. This ensures that all .class files in | |
302 # the output are actually from the input .java files. For example, if a | |
303 # .java file is deleted or an inner class is removed, the classes | |
304 # directory should not contain the corresponding old .class file after | |
305 # running this action. | |
306 build_utils.DeleteDirectory(options.classes_dir) | |
307 shutil.copytree(classes_dir, options.classes_dir) | |
308 | |
309 if options.depfile: | |
310 build_utils.WriteDepfile( | |
311 options.depfile, | |
312 input_files + build_utils.GetPythonDependencies()) | |
313 | |
314 if options.stamp: | |
315 build_utils.Touch(options.stamp) | |
316 | |
317 | |
318 if __name__ == '__main__': | |
319 sys.exit(main(sys.argv[1:])) | |
320 | |
321 | |
OLD | NEW |