blob: cc068a56adf9bf9c97615985c7844983231964f9 [file] [log] [blame]
Ted Kremenek7aaa9532010-02-08 20:54:01 +00001#!/usr/bin/env python
2
3import os
4import sys
5import re
6import tempfile
7import shutil
8import stat
Ted Kremenek962da0b2010-02-09 18:46:58 +00009from AppKit import *
Ted Kremenek7aaa9532010-02-08 20:54:01 +000010
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):
Ted Kremenek7aaa9532010-02-08 20:54:01 +000018 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()
Ted Kremenek8d699372010-02-09 18:51:44 +000031 print "(+) processing:", path
Ted Kremenek757a9d12010-02-08 21:19:27 +000032 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:
Ted Kremenek8d699372010-02-09 18:51:44 +000036 print " (-) Cannot update file:", why, "\n"
Ted Kremenek757a9d12010-02-08 21:19:27 +000037 except OSError, why:
Ted Kremenek8d699372010-02-09 18:51:44 +000038 print " (-) Cannot update file:", why, "\n"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000039 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
Ted Kremenek962da0b2010-02-09 18:46:58 +000054 # determine if Xcode is running
55 for x in NSWorkspace.sharedWorkspace().runningApplications():
56 if x.localizedName().find("Xcode") >= 0:
Ted Kremenek8d699372010-02-09 18:51:44 +000057 print "(-) You must quit Xcode first before modifying its configuration files."
Ted Kremenek962da0b2010-02-09 18:46:58 +000058 return
59
Ted Kremenek7aaa9532010-02-08 20:54:01 +000060 if options.path:
61 # Expand tildes.
62 path = os.path.expanduser(options.path)
63 if not path.endswith("clang"):
Ted Kremenek8d699372010-02-09 18:51:44 +000064 print "(+) Using Clang bundled with checker build:", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000065 path = os.path.join(path, "bin", "clang");
66 else:
Ted Kremenek8d699372010-02-09 18:51:44 +000067 print "(+) Using Clang located at:", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000068 else:
Ted Kremenek8d699372010-02-09 18:51:44 +000069 print "(+) Using the Clang bundled with Xcode"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000070 path = options.default
Ted Kremenek962da0b2010-02-09 18:46:58 +000071
Ted Kremenek7aaa9532010-02-08 20:54:01 +000072 for x in FindClangSpecs('/Developer'):
73 ModifySpec(x, path)
74
75if __name__ == '__main__':
76 main()
77