summaryrefslogtreecommitdiffstats
path: root/build/android/findbugs_diff.py
blob: d222e324373906d2c80344c129a34b378a2e563c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
#
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Runs findbugs, and returns an error code if there are new warnings.

Other options
  --only-analyze used to only analyze the class you are interested.
  --relase-build analyze the classes in out/Release directory.
  --findbugs-args used to passin other findbugs's options.

Run
  $CHROMIUM_SRC/third_party/findbugs/bin/findbugs -textui for details.

"""

import argparse
import os
import sys

from devil.utils import run_tests_helper
from pylib import constants
from pylib.utils import findbugs

_DEFAULT_BASE_DIR = os.path.join(
    constants.DIR_SOURCE_ROOT, 'build', 'android', 'findbugs_filter')

sys.path.append(
    os.path.join(constants.DIR_SOURCE_ROOT, 'build', 'android', 'gyp'))
from util import build_utils # pylint: disable=import-error


def main():
  parser = argparse.ArgumentParser()

  parser.add_argument(
      '-v', '--verbose', action='count', help='Enable verbose logging.')
  parser.add_argument(
      '-a', '--auxclasspath', default=None, dest='auxclasspath',
      help='Set aux classpath for analysis.')
  parser.add_argument(
      '--auxclasspath-gyp', dest='auxclasspath_gyp',
      help='A gyp list containing the aux classpath for analysis')
  parser.add_argument(
      '-o', '--only-analyze', default=None,
      dest='only_analyze', help='Only analyze the given classes and packages.')
  parser.add_argument(
      '-e', '--exclude', default=None, dest='exclude',
      help='Exclude bugs matching given filter.')
  parser.add_argument(
      '-l', '--release-build', action='store_true', dest='release_build',
      help='Analyze release build instead of debug.')
  parser.add_argument(
      '-f', '--findbug-args', default=None, dest='findbug_args',
      help='Additional findbug arguments.')
  parser.add_argument(
      '-b', '--base-dir', default=_DEFAULT_BASE_DIR,
      dest='base_dir', help='Base directory for configuration file.')
  parser.add_argument(
      '--output-file', dest='output_file',
      help='Path to save the output to.')
  parser.add_argument(
      '--stamp', help='Path to touch on success.')
  parser.add_argument(
      '--depfile', help='Path to the depfile. This must be specified as the '
                        "action's first output.")

  parser.add_argument(
      'jar_paths', metavar='JAR_PATH', nargs='+',
      help='JAR file to analyze')

  args = parser.parse_args(build_utils.ExpandFileArgs(sys.argv[1:]))

  run_tests_helper.SetLogLevel(args.verbose)

  if args.auxclasspath:
    args.auxclasspath = args.auxclasspath.split(':')
  elif args.auxclasspath_gyp:
    args.auxclasspath = build_utils.ParseGypList(args.auxclasspath_gyp)

  if args.base_dir:
    if not args.exclude:
      args.exclude = os.path.join(args.base_dir, 'findbugs_exclude.xml')

  findbugs_command, findbugs_warnings = findbugs.Run(
      args.exclude, args.only_analyze, args.auxclasspath,
      args.output_file, args.findbug_args, args.jar_paths)

  if findbugs_warnings:
    print
    print '*' * 80
    print 'FindBugs run via:'
    print findbugs_command
    print
    print 'FindBugs reported the following issues:'
    for warning in sorted(findbugs_warnings):
      print str(warning)
    print '*' * 80
    print
  else:
    if args.depfile:
      build_utils.WriteDepfile(
          args.depfile,
          build_utils.GetPythonDependencies() + args.auxclasspath
              + args.jar_paths)
    if args.stamp:
      build_utils.Touch(args.stamp)

  return len(findbugs_warnings)


if __name__ == '__main__':
  sys.exit(main())