• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2
3import os
4import sys
5import re
6import tempfile
7import shutil
8import stat
9from AppKit import *
10
11def FindClangSpecs(path):
12  for root, dirs, files in os.walk(path):
13    for f in files:
14      if f.endswith(".xcspec") and f.startswith("Clang LLVM"):
15        yield os.path.join(root, f)
16
17def ModifySpec(path, pathToChecker):
18  t = tempfile.NamedTemporaryFile(delete=False)
19  foundAnalyzer = False
20  with open(path) as f:
21    for line in f:
22      if not foundAnalyzer:
23        if line.find("Static Analyzer") >= 0:
24          foundAnalyzer = True
25      else:
26        m = re.search('^(\s*ExecPath\s*=\s*")', line)
27        if m:
28          line = "".join([m.group(0), pathToChecker, '";\n'])
29      t.write(line)
30  t.close()
31  print "(+) processing:", path
32  try:
33    shutil.copy(t.name, path)
34    os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
35  except IOError, why:
36    print "    (-) Cannot update file:", why, "\n"
37  except OSError, why:
38    print "    (-) Cannot update file:", why, "\n"
39  os.unlink(t.name)
40
41def main():
42  from optparse import OptionParser
43  parser = OptionParser('usage: %prog [options]')
44  parser.set_description(__doc__)
45  parser.add_option("--use-checker-build", dest="path",
46                    help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1")
47  parser.add_option("--use-xcode-clang", action="store_const",
48                    const="$(CLANG)", dest="default",
49                    help="Use the Clang bundled with Xcode")
50  (options, args) = parser.parse_args()
51  if options.path is None and options.default is None:
52    parser.error("You must specify a version of Clang to use for static analysis.  Specify '-h' for details")
53
54  # determine if Xcode is running
55  for x in NSWorkspace.sharedWorkspace().runningApplications():
56    if x.localizedName().find("Xcode") >= 0:
57      print "(-) You must quit Xcode first before modifying its configuration files."
58      return
59
60  if options.path:
61    # Expand tildes.
62    path = os.path.expanduser(options.path)
63    if not path.endswith("clang"):
64      print "(+) Using Clang bundled with checker build:", path
65      path = os.path.join(path, "bin", "clang");
66    else:
67      print "(+) Using Clang located at:", path
68  else:
69    print "(+) Using the Clang bundled with Xcode"
70    path = options.default
71
72  for x in FindClangSpecs('/Developer'):
73    ModifySpec(x, path)
74
75if __name__ == '__main__':
76  main()
77
78