Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / build / android / gyp / javac.py
blob37380629ebf2a045d1990b979b96fb76efd89898
1 #!/usr/bin/env python
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.
7 import fnmatch
8 import optparse
9 import os
10 import shutil
11 import re
12 import sys
13 import textwrap
15 from util import build_utils
16 from util import md5_check
18 import jar
20 sys.path.append(build_utils.COLORAMA_ROOT)
21 import colorama
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*$')
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]
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:])
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
54 return '\n'.join(map(ApplyColor, output.split('\n')))
57 def DoJavac(
58 classpath, classes_dir, chromium_code, java_files):
59 """Runs javac.
61 Builds |java_files| with the provided |classpath| and puts the generated
62 .class files into |classes_dir|. If |chromium_code| is true, extra lint
63 checking will be enabled.
64 """
66 jar_inputs = []
67 for path in classpath:
68 if os.path.exists(path + '.TOC'):
69 jar_inputs.append(path + '.TOC')
70 else:
71 jar_inputs.append(path)
73 javac_args = [
74 '-g',
75 # Chromium only allows UTF8 source files. Being explicit avoids
76 # javac pulling a default encoding from the user's environment.
77 '-encoding', 'UTF-8',
78 '-source', '1.7',
79 '-target', '1.7',
80 '-classpath', ':'.join(classpath),
81 '-d', classes_dir]
82 if chromium_code:
83 javac_args.extend(['-Xlint:unchecked', '-Xlint:deprecation'])
84 else:
85 # XDignore.symbol.file makes javac compile against rt.jar instead of
86 # ct.sym. This means that using a java internal package/class will not
87 # trigger a compile warning or error.
88 javac_args.extend(['-XDignore.symbol.file'])
90 javac_cmd = ['javac'] + javac_args + java_files
92 def Compile():
93 build_utils.CheckOutput(
94 javac_cmd,
95 print_stdout=chromium_code,
96 stderr_filter=ColorJavacOutput)
98 record_path = os.path.join(classes_dir, 'javac.md5.stamp')
99 md5_check.CallAndRecordIfStale(
100 Compile,
101 record_path=record_path,
102 input_paths=java_files + jar_inputs,
103 input_strings=javac_cmd)
106 _MAX_MANIFEST_LINE_LEN = 72
109 def CreateManifest(manifest_path, classpath, main_class=None,
110 manifest_entries=None):
111 """Creates a manifest file with the given parameters.
113 This generates a manifest file that compiles with the spec found at
114 http://docs.oracle.com/javase/7/docs/technotes/guides/jar/jar.html#JAR_Manifest
116 Args:
117 manifest_path: The path to the manifest file that should be created.
118 classpath: The JAR files that should be listed on the manifest file's
119 classpath.
120 main_class: If present, the class containing the main() function.
121 manifest_entries: If present, a list of (key, value) pairs to add to
122 the manifest.
125 output = ['Manifest-Version: 1.0']
126 if main_class:
127 output.append('Main-Class: %s' % main_class)
128 if manifest_entries:
129 for k, v in manifest_entries:
130 output.append('%s: %s' % (k, v))
131 if classpath:
132 sanitized_paths = []
133 for path in classpath:
134 sanitized_paths.append(os.path.basename(path.strip('"')))
135 output.append('Class-Path: %s' % ' '.join(sanitized_paths))
136 output.append('Created-By: ')
137 output.append('')
139 wrapper = textwrap.TextWrapper(break_long_words=True,
140 drop_whitespace=False,
141 subsequent_indent=' ',
142 width=_MAX_MANIFEST_LINE_LEN - 2)
143 output = '\r\n'.join(w for l in output for w in wrapper.wrap(l))
145 with open(manifest_path, 'w') as f:
146 f.write(output)
149 def main(argv):
150 colorama.init()
152 argv = build_utils.ExpandFileArgs(argv)
154 parser = optparse.OptionParser()
155 build_utils.AddDepfileOption(parser)
157 parser.add_option(
158 '--src-gendirs',
159 help='Directories containing generated java files.')
160 parser.add_option(
161 '--java-srcjars',
162 action='append',
163 default=[],
164 help='List of srcjars to include in compilation.')
165 parser.add_option(
166 '--classpath',
167 action='append',
168 help='Classpath for javac. If this is specified multiple times, they '
169 'will all be appended to construct the classpath.')
170 parser.add_option(
171 '--javac-includes',
172 help='A list of file patterns. If provided, only java files that match'
173 'one of the patterns will be compiled.')
174 parser.add_option(
175 '--jar-excluded-classes',
176 default='',
177 help='List of .class file patterns to exclude from the jar.')
179 parser.add_option(
180 '--chromium-code',
181 type='int',
182 help='Whether code being compiled should be built with stricter '
183 'warnings for chromium code.')
185 parser.add_option(
186 '--classes-dir',
187 help='Directory for compiled .class files.')
188 parser.add_option('--jar-path', help='Jar output path.')
189 parser.add_option(
190 '--main-class',
191 help='The class containing the main method.')
192 parser.add_option(
193 '--manifest-entry',
194 action='append',
195 help='Key:value pairs to add to the .jar manifest.')
197 parser.add_option('--stamp', help='Path to touch on success.')
199 options, args = parser.parse_args(argv)
201 if options.main_class and not options.jar_path:
202 parser.error('--main-class requires --jar-path')
204 classpath = []
205 for arg in options.classpath:
206 classpath += build_utils.ParseGypList(arg)
208 java_srcjars = []
209 for arg in options.java_srcjars:
210 java_srcjars += build_utils.ParseGypList(arg)
212 java_files = args
213 if options.src_gendirs:
214 src_gendirs = build_utils.ParseGypList(options.src_gendirs)
215 java_files += build_utils.FindInDirectories(src_gendirs, '*.java')
217 input_files = classpath + java_srcjars + java_files
218 with build_utils.TempDir() as temp_dir:
219 classes_dir = os.path.join(temp_dir, 'classes')
220 os.makedirs(classes_dir)
221 if java_srcjars:
222 java_dir = os.path.join(temp_dir, 'java')
223 os.makedirs(java_dir)
224 for srcjar in java_srcjars:
225 build_utils.ExtractAll(srcjar, path=java_dir, pattern='*.java')
226 java_files += build_utils.FindInDirectory(java_dir, '*.java')
228 if options.javac_includes:
229 javac_includes = build_utils.ParseGypList(options.javac_includes)
230 filtered_java_files = []
231 for f in java_files:
232 for include in javac_includes:
233 if fnmatch.fnmatch(f, include):
234 filtered_java_files.append(f)
235 break
236 java_files = filtered_java_files
238 DoJavac(
239 classpath,
240 classes_dir,
241 options.chromium_code,
242 java_files)
244 if options.jar_path:
245 if options.main_class or options.manifest_entry:
246 if options.manifest_entry:
247 entries = map(lambda e: e.split(":"), options.manifest_entry)
248 else:
249 entries = []
250 manifest_file = os.path.join(temp_dir, 'manifest')
251 CreateManifest(manifest_file, classpath, options.main_class, entries)
252 else:
253 manifest_file = None
254 jar.JarDirectory(classes_dir,
255 build_utils.ParseGypList(options.jar_excluded_classes),
256 options.jar_path,
257 manifest_file=manifest_file)
259 if options.classes_dir:
260 # Delete the old classes directory. This ensures that all .class files in
261 # the output are actually from the input .java files. For example, if a
262 # .java file is deleted or an inner class is removed, the classes
263 # directory should not contain the corresponding old .class file after
264 # running this action.
265 build_utils.DeleteDirectory(options.classes_dir)
266 shutil.copytree(classes_dir, options.classes_dir)
268 if options.depfile:
269 build_utils.WriteDepfile(
270 options.depfile,
271 input_files + build_utils.GetPythonDependencies())
273 if options.stamp:
274 build_utils.Touch(options.stamp)
277 if __name__ == '__main__':
278 sys.exit(main(sys.argv[1:]))