Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(436)

Side by Side Diff: build/android/gyp/lint.py

Issue 1870073002: Properly set up classpath for Android Lint (and fix its stderr filter) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 3 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 """Runs Android's lint tool.""" 7 """Runs Android's lint tool."""
8 8
9 9
10 import argparse 10 import argparse
11 import os 11 import os
12 import re
12 import sys 13 import sys
13 import traceback 14 import traceback
14 from xml.dom import minidom 15 from xml.dom import minidom
15 16
16 from util import build_utils 17 from util import build_utils
17 18
18 19
19 _SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), 20 _SRC_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__),
20 '..', '..', '..')) 21 '..', '..', '..'))
21 22
22 23
23 def _OnStaleMd5(changes, lint_path, config_path, processed_config_path, 24 def _OnStaleMd5(changes, lint_path, config_path, processed_config_path,
24 manifest_path, result_path, product_dir, sources, jar_path, 25 manifest_path, result_path, product_dir, sources, jar_path,
25 cache_dir, resource_dir=None, classpath=None, 26 cache_dir, sdk_version, resource_dir=None, classpath=None,
26 can_fail_build=False, silent=False): 27 can_fail_build=False, silent=False):
27
28 def _RelativizePath(path): 28 def _RelativizePath(path):
29 """Returns relative path to top-level src dir. 29 """Returns relative path to top-level src dir.
30 30
31 Args: 31 Args:
32 path: A path relative to cwd. 32 path: A path relative to cwd.
33 """ 33 """
34 return os.path.relpath(os.path.abspath(path), _SRC_ROOT) 34 return os.path.relpath(os.path.abspath(path), _SRC_ROOT)
35 35
36 def _ProcessConfigFile(): 36 def _ProcessConfigFile():
37 if not config_path or not processed_config_path: 37 if not config_path or not processed_config_path:
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 src_dir = None 122 src_dir = None
123 for d in src_dirs: 123 for d in src_dirs:
124 if not os.path.exists(PathInDir(d, src)): 124 if not os.path.exists(PathInDir(d, src)):
125 src_dir = d 125 src_dir = d
126 break 126 break
127 if not src_dir: 127 if not src_dir:
128 src_dir = NewSourceDir() 128 src_dir = NewSourceDir()
129 cmd.extend(['--sources', _RelativizePath(src_dir)]) 129 cmd.extend(['--sources', _RelativizePath(src_dir)])
130 os.symlink(os.path.abspath(src), PathInDir(src_dir, src)) 130 os.symlink(os.path.abspath(src), PathInDir(src_dir, src))
131 131
132 project_dir = NewSourceDir()
133 # Create dummy project.properies file in a temporary "project" directory.
134 # It is the only way to add Android SDK to the Lint's classpath. Proper
135 # classpath is necessary for most source-level checks.
136 with open(os.path.join(project_dir, 'project.properties'), 'w') as propfile:
137 print >> propfile, 'target=android-{}'.format(sdk_version)
138
132 # Put the manifest in a temporary directory in order to avoid lint detecting 139 # Put the manifest in a temporary directory in order to avoid lint detecting
133 # sibling res/ and src/ directories (which should be pass explicitly if they 140 # sibling res/ and src/ directories (which should be pass explicitly if they
134 # are to be included). 141 # are to be included).
135 if manifest_path: 142 if manifest_path:
136 src_dir = NewSourceDir()
137 os.symlink(os.path.abspath(manifest_path), 143 os.symlink(os.path.abspath(manifest_path),
138 PathInDir(src_dir, manifest_path)) 144 PathInDir(project_dir, manifest_path))
139 cmd.append(src_dir) 145 cmd.append(project_dir)
140 146
141 if os.path.exists(result_path): 147 if os.path.exists(result_path):
142 os.remove(result_path) 148 os.remove(result_path)
143 149
144 env = {} 150 env = {}
145 stderr_filter = None 151 stderr_filter = None
146 if cache_dir: 152 if cache_dir:
153 env['_JAVA_OPTIONS'] = '-Duser.home=%s' % _RelativizePath(cache_dir)
147 # When _JAVA_OPTIONS is set, java prints to stderr: 154 # When _JAVA_OPTIONS is set, java prints to stderr:
148 # Picked up _JAVA_OPTIONS: ... 155 # Picked up _JAVA_OPTIONS: ...
149 env['_JAVA_OPTIONS'] = '-Duser.home=%s' % _RelativizePath(cache_dir) 156 #
150 stderr_filter = lambda l: '' if '_JAVA_OPTIONS' in l else l 157 # We drop this line from the output.
158 stderr_filter = lambda l: re.sub(
159 r'^.*_JAVA_OPTIONS.*\n', '', l, flags=re.MULTILINE)
agrieve 2016/04/08 13:40:29 nit: I don't think you need MULTILINE unless you u
mlopatkin 2016/04/08 16:55:16 My intention was to replace all lines that contain
agrieve 2016/04/08 17:26:50 oh garbage. Sorry about that. I just didn't see th
151 160
152 try: 161 try:
153 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT, env=env or None, 162 build_utils.CheckOutput(cmd, cwd=_SRC_ROOT, env=env or None,
154 stderr_filter=stderr_filter) 163 stderr_filter=stderr_filter)
155 except build_utils.CalledProcessError: 164 except build_utils.CalledProcessError:
156 # There is a problem with lint usage 165 # There is a problem with lint usage
157 if not os.path.exists(result_path): 166 if not os.path.exists(result_path):
158 raise 167 raise
159 168
160 # Sometimes produces empty (almost) files: 169 # Sometimes produces empty (almost) files:
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 help='Path to lint executable.') 227 help='Path to lint executable.')
219 parser.add_argument('--product-dir', required=True, 228 parser.add_argument('--product-dir', required=True,
220 help='Path to product dir.') 229 help='Path to product dir.')
221 parser.add_argument('--result-path', required=True, 230 parser.add_argument('--result-path', required=True,
222 help='Path to XML lint result file.') 231 help='Path to XML lint result file.')
223 parser.add_argument('--cache-dir', required=True, 232 parser.add_argument('--cache-dir', required=True,
224 help='Path to the directory in which the android cache ' 233 help='Path to the directory in which the android cache '
225 'directory tree should be stored.') 234 'directory tree should be stored.')
226 parser.add_argument('--platform-xml-path', required=True, 235 parser.add_argument('--platform-xml-path', required=True,
227 help='Path to api-platforms.xml') 236 help='Path to api-platforms.xml')
237 parser.add_argument('--android-sdk-version', required=True,
238 help='Version (API level) of the Android SDK used for '
239 'building.')
228 parser.add_argument('--create-cache', action='store_true', 240 parser.add_argument('--create-cache', action='store_true',
229 help='Mark the lint cache file as an output rather than ' 241 help='Mark the lint cache file as an output rather than '
230 'an input.') 242 'an input.')
231 parser.add_argument('--can-fail-build', action='store_true', 243 parser.add_argument('--can-fail-build', action='store_true',
232 help='If set, script will exit with nonzero exit status' 244 help='If set, script will exit with nonzero exit status'
233 ' if lint errors are present') 245 ' if lint errors are present')
234 parser.add_argument('--config-path', 246 parser.add_argument('--config-path',
235 help='Path to lint suppressions file.') 247 help='Path to lint suppressions file.')
236 parser.add_argument('--enable', action='store_true', 248 parser.add_argument('--enable', action='store_true',
237 help='Run lint instead of just touching stamp.') 249 help='Run lint instead of just touching stamp.')
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 input_paths.append(args.manifest_path) 293 input_paths.append(args.manifest_path)
282 if args.resource_dir: 294 if args.resource_dir:
283 input_paths.extend(build_utils.FindInDirectory(args.resource_dir, '*')) 295 input_paths.extend(build_utils.FindInDirectory(args.resource_dir, '*'))
284 if sources: 296 if sources:
285 input_paths.extend(sources) 297 input_paths.extend(sources)
286 classpath = [] 298 classpath = []
287 for gyp_list in args.classpath: 299 for gyp_list in args.classpath:
288 classpath.extend(build_utils.ParseGypList(gyp_list)) 300 classpath.extend(build_utils.ParseGypList(gyp_list))
289 input_paths.extend(classpath) 301 input_paths.extend(classpath)
290 302
291 input_strings = [] 303 input_strings = [ args.android_sdk_version ]
292 if args.processed_config_path: 304 if args.processed_config_path:
293 input_strings.append(args.processed_config_path) 305 input_strings.append(args.processed_config_path)
294 306
295 output_paths = [ args.result_path ] 307 output_paths = [ args.result_path ]
296 308
297 build_utils.CallAndWriteDepfileIfStale( 309 build_utils.CallAndWriteDepfileIfStale(
298 lambda changes: _OnStaleMd5(changes, args.lint_path, 310 lambda changes: _OnStaleMd5(changes, args.lint_path,
299 args.config_path, 311 args.config_path,
300 args.processed_config_path, 312 args.processed_config_path,
301 args.manifest_path, args.result_path, 313 args.manifest_path, args.result_path,
302 args.product_dir, sources, 314 args.product_dir, sources,
303 args.jar_path, 315 args.jar_path,
304 args.cache_dir, 316 args.cache_dir,
317 args.android_sdk_version,
305 resource_dir=args.resource_dir, 318 resource_dir=args.resource_dir,
306 classpath=classpath, 319 classpath=classpath,
307 can_fail_build=args.can_fail_build, 320 can_fail_build=args.can_fail_build,
308 silent=args.silent), 321 silent=args.silent),
309 args, 322 args,
310 input_paths=input_paths, 323 input_paths=input_paths,
311 input_strings=input_strings, 324 input_strings=input_strings,
312 output_paths=output_paths, 325 output_paths=output_paths,
313 pass_changes=True, 326 pass_changes=True,
314 depfile_deps=classpath) 327 depfile_deps=classpath)
315 328
316 329
317 if __name__ == '__main__': 330 if __name__ == '__main__':
318 sys.exit(main()) 331 sys.exit(main())
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698