Fix issue in about:net-internals with clicking on a column
[chromium-blink-merge.git] / build / mac / find_sdk.py
blob1d7634391fa4d66b2baeec276429836c39d53283
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 import os
7 import re
8 import subprocess
9 import sys
11 """Prints the lowest locally available SDK version greater than or equal to a
12 given minimum sdk version to standard output.
14 Usage:
15 python find_sdk.py 10.6 # Ignores SDKs < 10.6
16 """
18 from optparse import OptionParser
21 def parse_version(version_str):
22 """'10.6' => [10, 6]"""
23 return map(int, re.findall(r'(\d+)', version_str))
26 def main():
27 parser = OptionParser()
28 parser.add_option("--verify",
29 action="store_true", dest="verify", default=False,
30 help="return the sdk argument and warn if it doesn't exist")
31 parser.add_option("--sdk_path",
32 action="store", type="string", dest="sdk_path", default="",
33 help="user-specified SDK path; bypasses verification")
34 parser.add_option("--print_sdk_path",
35 action="store_true", dest="print_sdk_path", default=False,
36 help="Additionaly print the path the SDK (appears first).")
37 (options, args) = parser.parse_args()
38 min_sdk_version = args[0]
40 job = subprocess.Popen(['xcode-select', '-print-path'],
41 stdout=subprocess.PIPE,
42 stderr=subprocess.STDOUT)
43 out, err = job.communicate()
44 if job.returncode != 0:
45 print >>sys.stderr, out
46 print >>sys.stderr, err
47 raise Exception(('Error %d running xcode-select, you might have to run '
48 '|sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer| '
49 'if you are using Xcode 4.') % job.returncode)
50 # The Developer folder moved in Xcode 4.3.
51 xcode43_sdk_path = os.path.join(
52 out.rstrip(), 'Platforms/MacOSX.platform/Developer/SDKs')
53 if os.path.isdir(xcode43_sdk_path):
54 sdk_dir = xcode43_sdk_path
55 else:
56 sdk_dir = os.path.join(out.rstrip(), 'SDKs')
57 sdks = [re.findall('^MacOSX(10\.\d+)\.sdk$', s) for s in os.listdir(sdk_dir)]
58 sdks = [s[0] for s in sdks if s] # [['10.5'], ['10.6']] => ['10.5', '10.6']
59 sdks = [s for s in sdks # ['10.5', '10.6'] => ['10.6']
60 if parse_version(s) >= parse_version(min_sdk_version)]
61 if not sdks:
62 raise Exception('No %s+ SDK found' % min_sdk_version)
63 best_sdk = sorted(sdks, key=parse_version)[0]
65 if options.verify and best_sdk != min_sdk_version and not options.sdk_path:
66 print >>sys.stderr, ''
67 print >>sys.stderr, ' vvvvvvv'
68 print >>sys.stderr, ''
69 print >>sys.stderr, \
70 'This build requires the %s SDK, but it was not found on your system.' \
71 % min_sdk_version
72 print >>sys.stderr, \
73 'Either install it, or explicitly set mac_sdk in your GYP_DEFINES.'
74 print >>sys.stderr, ''
75 print >>sys.stderr, ' ^^^^^^^'
76 print >>sys.stderr, ''
77 return min_sdk_version
79 if options.print_sdk_path:
80 print subprocess.check_output(['xcodebuild', '-version', '-sdk',
81 'macosx' + best_sdk, 'Path']).strip()
83 return best_sdk
86 if __name__ == '__main__':
87 if sys.platform != 'darwin':
88 raise Exception("This script only runs on Mac")
89 print main()