blob: cf0ba3a1af06716058244b71c83f3881292ed448 [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
9
10def FindClangSpecs(path):
11 for root, dirs, files in os.walk(path):
12 for f in files:
13 if f.endswith(".xcspec") and f.startswith("Clang LLVM"):
14 yield os.path.join(root, f)
15
16def ModifySpec(path, pathToChecker):
Ted Kremenek7aaa9532010-02-08 20:54:01 +000017 t = tempfile.NamedTemporaryFile(delete=False)
18 foundAnalyzer = False
19 with open(path) as f:
20 for line in f:
21 if not foundAnalyzer:
22 if line.find("Static Analyzer") >= 0:
23 foundAnalyzer = True
24 else:
25 m = re.search('^(\s*ExecPath\s*=\s*")', line)
26 if m:
27 line = "".join([m.group(0), pathToChecker, '";\n'])
28 t.write(line)
29 t.close()
Ted Kremenek757a9d12010-02-08 21:19:27 +000030 print "(+)", path
31 try:
32 shutil.copy(t.name, path)
33 os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
34 except IOError, why:
35 print "\n Cannot update file:", why, "\n"
36 except OSError, why:
37 print "\n Cannot update file:", why, "\n"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000038 os.unlink(t.name)
39
40def main():
41 from optparse import OptionParser
42 parser = OptionParser('usage: %prog [options]')
43 parser.set_description(__doc__)
44 parser.add_option("--use-checker-build", dest="path",
45 help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1")
46 parser.add_option("--use-xcode-clang", action="store_const",
47 const="$(CLANG)", dest="default",
48 help="Use the Clang bundled with Xcode")
49 (options, args) = parser.parse_args()
50 if options.path is None and options.default is None:
51 parser.error("You must specify a version of Clang to use for static analysis. Specify '-h' for details")
52
53 if options.path:
54 # Expand tildes.
55 path = os.path.expanduser(options.path)
56 if not path.endswith("clang"):
57 print "Using Clang bundled with checker build:", path
58 path = os.path.join(path, "bin", "clang");
59 else:
60 print "Using Clang located at:", path
61 else:
62 print "Using the Clang bundled with Xcode"
63 path = options.default
64
Ted Kremenek757a9d12010-02-08 21:19:27 +000065 print ""
Ted Kremenek7aaa9532010-02-08 20:54:01 +000066 for x in FindClangSpecs('/Developer'):
67 ModifySpec(x, path)
68
69if __name__ == '__main__':
70 main()
71