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 |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 9 | from AppKit import * |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 10 | |
| 11 | def 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 | |
| 17 | def ModifySpec(path, pathToChecker): |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 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() |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 31 | print "(+) processing:", path |
Ted Kremenek | 757a9d1 | 2010-02-08 21:19:27 +0000 | [diff] [blame] | 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: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 36 | print " (-) Cannot update file:", why, "\n" |
Ted Kremenek | 757a9d1 | 2010-02-08 21:19:27 +0000 | [diff] [blame] | 37 | except OSError, why: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 38 | print " (-) Cannot update file:", why, "\n" |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 39 | os.unlink(t.name) |
| 40 | |
| 41 | def 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 Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 54 | # determine if Xcode is running |
| 55 | for x in NSWorkspace.sharedWorkspace().runningApplications(): |
| 56 | if x.localizedName().find("Xcode") >= 0: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 57 | print "(-) You must quit Xcode first before modifying its configuration files." |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 58 | return |
| 59 | |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 60 | if options.path: |
| 61 | # Expand tildes. |
| 62 | path = os.path.expanduser(options.path) |
| 63 | if not path.endswith("clang"): |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 64 | print "(+) Using Clang bundled with checker build:", path |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 65 | path = os.path.join(path, "bin", "clang"); |
| 66 | else: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 67 | print "(+) Using Clang located at:", path |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 68 | else: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 69 | print "(+) Using the Clang bundled with Xcode" |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 70 | path = options.default |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 72 | for x in FindClangSpecs('/Developer'): |
| 73 | ModifySpec(x, path) |
| 74 | |
| 75 | if __name__ == '__main__': |
| 76 | main() |
| 77 | |