Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import re |
| 6 | import tempfile |
| 7 | import shutil |
| 8 | import stat |
| 9 | |
| 10 | def 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 | |
| 16 | def ModifySpec(path, pathToChecker): |
| 17 | print "Updating:", path |
| 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 | shutil.copy(t.name, path) |
| 32 | os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) |
| 33 | os.unlink(t.name) |
| 34 | |
| 35 | def main(): |
| 36 | from optparse import OptionParser |
| 37 | parser = OptionParser('usage: %prog [options]') |
| 38 | parser.set_description(__doc__) |
| 39 | parser.add_option("--use-checker-build", dest="path", |
| 40 | help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1") |
| 41 | parser.add_option("--use-xcode-clang", action="store_const", |
| 42 | const="$(CLANG)", dest="default", |
| 43 | help="Use the Clang bundled with Xcode") |
| 44 | (options, args) = parser.parse_args() |
| 45 | if options.path is None and options.default is None: |
| 46 | parser.error("You must specify a version of Clang to use for static analysis. Specify '-h' for details") |
| 47 | |
| 48 | if options.path: |
| 49 | # Expand tildes. |
| 50 | path = os.path.expanduser(options.path) |
| 51 | if not path.endswith("clang"): |
| 52 | print "Using Clang bundled with checker build:", path |
| 53 | path = os.path.join(path, "bin", "clang"); |
| 54 | else: |
| 55 | print "Using Clang located at:", path |
| 56 | else: |
| 57 | print "Using the Clang bundled with Xcode" |
| 58 | path = options.default |
| 59 | |
| 60 | for x in FindClangSpecs('/Developer'): |
| 61 | ModifySpec(x, path) |
| 62 | |
| 63 | if __name__ == '__main__': |
| 64 | main() |
| 65 | |