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 |
Ted Kremenek | 2775b93 | 2012-02-22 18:44:35 +0000 | [diff] [blame] | 8 | import subprocess |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 9 | import sys |
| 10 | import re |
| 11 | import tempfile |
| 12 | import shutil |
| 13 | import stat |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 14 | from AppKit import * |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 15 | |
| 16 | def FindClangSpecs(path): |
Ted Kremenek | 2775b93 | 2012-02-22 18:44:35 +0000 | [diff] [blame] | 17 | print "(+) Searching for xcspec file in: ", path |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 18 | for root, dirs, files in os.walk(path): |
| 19 | for f in files: |
| 20 | if f.endswith(".xcspec") and f.startswith("Clang LLVM"): |
| 21 | yield os.path.join(root, f) |
| 22 | |
| 23 | def ModifySpec(path, pathToChecker): |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 24 | t = tempfile.NamedTemporaryFile(delete=False) |
| 25 | foundAnalyzer = False |
| 26 | with open(path) as f: |
| 27 | for line in f: |
| 28 | if not foundAnalyzer: |
| 29 | if line.find("Static Analyzer") >= 0: |
| 30 | foundAnalyzer = True |
| 31 | else: |
| 32 | m = re.search('^(\s*ExecPath\s*=\s*")', line) |
| 33 | if m: |
| 34 | line = "".join([m.group(0), pathToChecker, '";\n']) |
| 35 | t.write(line) |
| 36 | t.close() |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 37 | print "(+) processing:", path |
Ted Kremenek | 757a9d1 | 2010-02-08 21:19:27 +0000 | [diff] [blame] | 38 | try: |
| 39 | shutil.copy(t.name, path) |
| 40 | os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) |
| 41 | except IOError, why: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 42 | print " (-) Cannot update file:", why, "\n" |
Ted Kremenek | 757a9d1 | 2010-02-08 21:19:27 +0000 | [diff] [blame] | 43 | except OSError, why: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 44 | print " (-) Cannot update file:", why, "\n" |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 45 | os.unlink(t.name) |
| 46 | |
| 47 | def main(): |
| 48 | from optparse import OptionParser |
| 49 | parser = OptionParser('usage: %prog [options]') |
| 50 | parser.set_description(__doc__) |
| 51 | parser.add_option("--use-checker-build", dest="path", |
| 52 | help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1") |
| 53 | parser.add_option("--use-xcode-clang", action="store_const", |
| 54 | const="$(CLANG)", dest="default", |
| 55 | help="Use the Clang bundled with Xcode") |
| 56 | (options, args) = parser.parse_args() |
| 57 | if options.path is None and options.default is None: |
| 58 | parser.error("You must specify a version of Clang to use for static analysis. Specify '-h' for details") |
| 59 | |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 60 | # determine if Xcode is running |
| 61 | for x in NSWorkspace.sharedWorkspace().runningApplications(): |
| 62 | if x.localizedName().find("Xcode") >= 0: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 63 | print "(-) You must quit Xcode first before modifying its configuration files." |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 64 | return |
| 65 | |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 66 | if options.path: |
| 67 | # Expand tildes. |
| 68 | path = os.path.expanduser(options.path) |
| 69 | if not path.endswith("clang"): |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 70 | print "(+) Using Clang bundled with checker build:", path |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 71 | path = os.path.join(path, "bin", "clang"); |
| 72 | else: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 73 | print "(+) Using Clang located at:", path |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 74 | else: |
Ted Kremenek | 8d69937 | 2010-02-09 18:51:44 +0000 | [diff] [blame] | 75 | print "(+) Using the Clang bundled with Xcode" |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 76 | path = options.default |
Ted Kremenek | 962da0b | 2010-02-09 18:46:58 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | b8971b2 | 2012-07-16 21:39:29 +0000 | [diff] [blame] | 78 | try: |
| 79 | xcode_path = subprocess.check_output(["xcode-select", "-print-path"]) |
| 80 | except AttributeError: |
| 81 | # Fall back to the default install location when using Python < 2.7.0 |
| 82 | xcode_path = "/Developer" |
Ted Kremenek | 2775b93 | 2012-02-22 18:44:35 +0000 | [diff] [blame] | 83 | if (re.search("Xcode.app", xcode_path)): |
| 84 | # Cut off the 'Developer' dir, as the xcspec lies in another part |
| 85 | # of the Xcode.app subtree. |
| 86 | xcode_path = os.path.dirname(xcode_path) |
| 87 | |
| 88 | for x in FindClangSpecs(xcode_path): |
Ted Kremenek | 7aaa953 | 2010-02-08 20:54:01 +0000 | [diff] [blame] | 89 | ModifySpec(x, path) |
| 90 | |
| 91 | if __name__ == '__main__': |
| 92 | main() |
| 93 | |