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