blob: e76e30446763a543811c4438f6672366226e341c [file] [log] [blame]
Ted Kremenek5a15d922012-01-03 22:05:57 +00001#!/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 Kremenek7aaa9532010-02-08 20:54:01 +00006
Ted Kremeneke756b652013-01-04 19:04:42 +00007import sys
8if sys.version_info < (2, 7):
9 print "set-xcode-analyzer requires Python 2.7 or later"
10 sys.exit(1)
11
Ted Kremenek7aaa9532010-02-08 20:54:01 +000012import os
Ted Kremenek2775b932012-02-22 18:44:35 +000013import subprocess
Ted Kremenek7aaa9532010-02-08 20:54:01 +000014import re
15import tempfile
16import shutil
17import stat
Ted Kremenek962da0b2010-02-09 18:46:58 +000018from AppKit import *
Ted Kremenek7aaa9532010-02-08 20:54:01 +000019
20def FindClangSpecs(path):
Ted Kremenek2775b932012-02-22 18:44:35 +000021 print "(+) Searching for xcspec file in: ", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000022 for root, dirs, files in os.walk(path):
23 for f in files:
24 if f.endswith(".xcspec") and f.startswith("Clang LLVM"):
25 yield os.path.join(root, f)
26
Ted Kremenek8b032772012-10-11 00:40:41 +000027def ModifySpec(path, isBuiltinAnalyzer, pathToChecker):
Ted Kremenek7aaa9532010-02-08 20:54:01 +000028 t = tempfile.NamedTemporaryFile(delete=False)
29 foundAnalyzer = False
30 with open(path) as f:
Ted Kremenek8b032772012-10-11 00:40:41 +000031 if isBuiltinAnalyzer:
32 # First search for CLANG_ANALYZER_EXEC. Newer
33 # versions of Xcode set EXEC_PATH to be CLANG_ANALYZER_EXEC.
34 with open(path) as f2:
35 for line in f2:
36 if line.find("CLANG_ANALYZER_EXEC") >= 0:
37 pathToChecker = "$(CLANG_ANALYZER_EXEC)"
38 break
39 # Now create a new file.
Ted Kremenek7aaa9532010-02-08 20:54:01 +000040 for line in f:
41 if not foundAnalyzer:
42 if line.find("Static Analyzer") >= 0:
43 foundAnalyzer = True
44 else:
45 m = re.search('^(\s*ExecPath\s*=\s*")', line)
46 if m:
47 line = "".join([m.group(0), pathToChecker, '";\n'])
48 t.write(line)
49 t.close()
Ted Kremenek8d699372010-02-09 18:51:44 +000050 print "(+) processing:", path
Ted Kremenek757a9d12010-02-08 21:19:27 +000051 try:
52 shutil.copy(t.name, path)
53 os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
54 except IOError, why:
Ted Kremenek8d699372010-02-09 18:51:44 +000055 print " (-) Cannot update file:", why, "\n"
Ted Kremenek757a9d12010-02-08 21:19:27 +000056 except OSError, why:
Ted Kremenek8d699372010-02-09 18:51:44 +000057 print " (-) Cannot update file:", why, "\n"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000058 os.unlink(t.name)
59
60def main():
61 from optparse import OptionParser
62 parser = OptionParser('usage: %prog [options]')
63 parser.set_description(__doc__)
64 parser.add_option("--use-checker-build", dest="path",
65 help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1")
66 parser.add_option("--use-xcode-clang", action="store_const",
67 const="$(CLANG)", dest="default",
68 help="Use the Clang bundled with Xcode")
69 (options, args) = parser.parse_args()
70 if options.path is None and options.default is None:
71 parser.error("You must specify a version of Clang to use for static analysis. Specify '-h' for details")
72
Ted Kremenek962da0b2010-02-09 18:46:58 +000073 # determine if Xcode is running
74 for x in NSWorkspace.sharedWorkspace().runningApplications():
75 if x.localizedName().find("Xcode") >= 0:
Ted Kremenek8d699372010-02-09 18:51:44 +000076 print "(-) You must quit Xcode first before modifying its configuration files."
Ted Kremenek962da0b2010-02-09 18:46:58 +000077 return
78
Ted Kremenek8b032772012-10-11 00:40:41 +000079 isBuiltinAnalyzer = False
Ted Kremenek7aaa9532010-02-08 20:54:01 +000080 if options.path:
81 # Expand tildes.
82 path = os.path.expanduser(options.path)
83 if not path.endswith("clang"):
Ted Kremenek8d699372010-02-09 18:51:44 +000084 print "(+) Using Clang bundled with checker build:", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000085 path = os.path.join(path, "bin", "clang");
86 else:
Ted Kremenek8d699372010-02-09 18:51:44 +000087 print "(+) Using Clang located at:", path
Ted Kremenek7aaa9532010-02-08 20:54:01 +000088 else:
Ted Kremenek8d699372010-02-09 18:51:44 +000089 print "(+) Using the Clang bundled with Xcode"
Ted Kremenek7aaa9532010-02-08 20:54:01 +000090 path = options.default
Ted Kremenek8b032772012-10-11 00:40:41 +000091 isBuiltinAnalyzer = True
Ted Kremenek962da0b2010-02-09 18:46:58 +000092
Ted Kremenekb8971b22012-07-16 21:39:29 +000093 try:
94 xcode_path = subprocess.check_output(["xcode-select", "-print-path"])
95 except AttributeError:
96 # Fall back to the default install location when using Python < 2.7.0
97 xcode_path = "/Developer"
Ted Kremenek9d86eab2012-09-26 18:13:03 +000098 if (xcode_path.find(".app/") != -1):
Ted Kremenek2775b932012-02-22 18:44:35 +000099 # Cut off the 'Developer' dir, as the xcspec lies in another part
100 # of the Xcode.app subtree.
101 xcode_path = os.path.dirname(xcode_path)
102
Ted Kremenekb2d6bc32012-09-26 18:19:55 +0000103 foundSpec = False
Ted Kremenek2775b932012-02-22 18:44:35 +0000104 for x in FindClangSpecs(xcode_path):
Ted Kremenekb2d6bc32012-09-26 18:19:55 +0000105 foundSpec = True
Ted Kremenek8b032772012-10-11 00:40:41 +0000106 ModifySpec(x, isBuiltinAnalyzer, path)
Ted Kremenekb2d6bc32012-09-26 18:19:55 +0000107
108 if foundSpec == False:
109 print "(-) No compiler configuration file was found. Xcode's analyzer has not been updated."
Ted Kremenek7aaa9532010-02-08 20:54:01 +0000110
111if __name__ == '__main__':
112 main()
113