blob: 133f9cc329e1de2449e1e636ecd2e5c16fa3a42c [file] [log] [blame]
Ted Kremenek5a15d922012-01-03 22:05:57 +00001#!/usr/bin/python
2
3# [PR 11661] Note that we hardwire to /usr/bin/python because we
4# want to the use the system version of Python on Mac OS X.
5# This one has the scripting bridge enabled.
Ted Kremenek7aaa9532010-02-08 20:54:01 +00006
7import os
8import sys
9import re
10import tempfile
11import shutil
12import stat
Ted Kremenek962da0b2010-02-09 18:46:58 +000013from AppKit import *
Ted Kremenek7aaa9532010-02-08 20:54:01 +000014
15def FindClangSpecs(path):
16 for root, dirs, files in os.walk(path):
17 for f in files:
18 if f.endswith(".xcspec") and f.startswith("Clang LLVM"):
19 yield os.path.join(root, f)
20
21def ModifySpec(path, pathToChecker):
Ted Kremenek7aaa9532010-02-08 20:54:01 +000022 t = tempfile.NamedTemporaryFile(delete=False)
23 foundAnalyzer = False
24 with open(path) as f:
25 for line in f:
26 if not foundAnalyzer:
27 if line.find("Static Analyzer") >= 0:
28 foundAnalyzer = True
29 else:
30 m = re.search('^(\s*ExecPath\s*=\s*")', line)
31 if m:
32 line = "".join([m.group(0), pathToChecker, '";\n'])
33 t.write(line)
34 t.close()
Ted Kremenek8d699372010-02-09 18:51:44 +000035 print "(+) processing:", path
Ted Kremenek757a9d12010-02-08 21:19:27 +000036 try:
37 shutil.copy(t.name, path)
38 os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
39 except IOError, why:
Ted Kremenek8d699372010-02-09 18:51:44 +000040 print " (-) Cannot update file:", why, "\n"
Ted Kremenek757a9d12010-02-08 21:19:27 +000041 except OSError, why:
Ted Kremenek8d699372010-02-09 18:51:44 +000042 print " (-) Cannot update file:", why, "\n"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000043 os.unlink(t.name)
44
45def main():
46 from optparse import OptionParser
47 parser = OptionParser('usage: %prog [options]')
48 parser.set_description(__doc__)
49 parser.add_option("--use-checker-build", dest="path",
50 help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1")
51 parser.add_option("--use-xcode-clang", action="store_const",
52 const="$(CLANG)", dest="default",
53 help="Use the Clang bundled with Xcode")
54 (options, args) = parser.parse_args()
55 if options.path is None and options.default is None:
56 parser.error("You must specify a version of Clang to use for static analysis. Specify '-h' for details")
57
Ted Kremenek962da0b2010-02-09 18:46:58 +000058 # determine if Xcode is running
59 for x in NSWorkspace.sharedWorkspace().runningApplications():
60 if x.localizedName().find("Xcode") >= 0:
Ted Kremenek8d699372010-02-09 18:51:44 +000061 print "(-) You must quit Xcode first before modifying its configuration files."
Ted Kremenek962da0b2010-02-09 18:46:58 +000062 return
63
Ted Kremenek7aaa9532010-02-08 20:54:01 +000064 if options.path:
65 # Expand tildes.
66 path = os.path.expanduser(options.path)
67 if not path.endswith("clang"):
Ted Kremenek8d699372010-02-09 18:51:44 +000068 print "(+) Using Clang bundled with checker build:", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000069 path = os.path.join(path, "bin", "clang");
70 else:
Ted Kremenek8d699372010-02-09 18:51:44 +000071 print "(+) Using Clang located at:", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000072 else:
Ted Kremenek8d699372010-02-09 18:51:44 +000073 print "(+) Using the Clang bundled with Xcode"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000074 path = options.default
Ted Kremenek962da0b2010-02-09 18:46:58 +000075
Ted Kremenek7aaa9532010-02-08 20:54:01 +000076 for x in FindClangSpecs('/Developer'):
77 ModifySpec(x, path)
78
79if __name__ == '__main__':
80 main()
81