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