Wei Li | 28de044 | 2016-02-23 13:44:52 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2016 PDFium 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. |
| 5 | |
| 6 | """Prints the lowest locally available SDK version greater than or equal to a |
| 7 | given minimum sdk version to standard output. |
| 8 | |
| 9 | Usage: |
| 10 | python mac_find_sdk.py 10.6 # Ignores SDKs < 10.6 |
| 11 | """ |
| 12 | |
| 13 | import os |
| 14 | import re |
| 15 | import subprocess |
| 16 | import sys |
| 17 | |
| 18 | |
| 19 | from optparse import OptionParser |
| 20 | |
| 21 | |
| 22 | def parse_version(version_str): |
| 23 | """'10.6' => [10, 6]""" |
| 24 | return map(int, re.findall(r'(\d+)', version_str)) |
| 25 | |
| 26 | |
| 27 | def main(): |
| 28 | parser = OptionParser() |
| 29 | parser.add_option("--verify", |
| 30 | action="store_true", dest="verify", default=False, |
| 31 | help="return the sdk argument and warn if it doesn't exist") |
| 32 | parser.add_option("--sdk_path", |
| 33 | action="store", type="string", dest="sdk_path", default="", |
| 34 | help="user-specified SDK path; bypasses verification") |
| 35 | parser.add_option("--print_sdk_path", |
| 36 | action="store_true", dest="print_sdk_path", default=False, |
| 37 | help="Additionaly print the path the SDK (appears first).") |
| 38 | options, args = parser.parse_args() |
| 39 | if len(args) != 1: |
| 40 | parser.error('Please specify a minimum SDK version') |
| 41 | min_sdk_version = args[0] |
| 42 | |
| 43 | job = subprocess.Popen(['xcode-select', '-print-path'], |
| 44 | stdout=subprocess.PIPE, |
| 45 | stderr=subprocess.STDOUT) |
| 46 | out, err = job.communicate() |
| 47 | if job.returncode != 0: |
| 48 | print >> sys.stderr, out |
| 49 | print >> sys.stderr, err |
| 50 | raise Exception(('Error %d running xcode-select, you might have to run ' |
| 51 | '|sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer| ' |
| 52 | 'if you are using Xcode 4.') % job.returncode) |
| 53 | # The Developer folder moved in Xcode 4.3. |
| 54 | xcode43_sdk_path = os.path.join( |
| 55 | out.rstrip(), 'Platforms/MacOSX.platform/Developer/SDKs') |
| 56 | if os.path.isdir(xcode43_sdk_path): |
| 57 | sdk_dir = xcode43_sdk_path |
| 58 | else: |
| 59 | sdk_dir = os.path.join(out.rstrip(), 'SDKs') |
| 60 | sdks = [re.findall('^MacOSX(10\.\d+)\.sdk$', s) for s in os.listdir(sdk_dir)] |
| 61 | sdks = [s[0] for s in sdks if s] # [['10.5'], ['10.6']] => ['10.5', '10.6'] |
| 62 | sdks = [s for s in sdks # ['10.5', '10.6'] => ['10.6'] |
| 63 | if parse_version(s) >= parse_version(min_sdk_version)] |
| 64 | if not sdks: |
Wei Li | 78acf70 | 2016-02-24 13:00:06 -0800 | [diff] [blame] | 65 | raise Exception('No %s+ SDK found' % min_sdk_version) |
Wei Li | 28de044 | 2016-02-23 13:44:52 -0800 | [diff] [blame] | 66 | best_sdk = sorted(sdks, key=parse_version)[0] |
| 67 | |
| 68 | if options.verify and best_sdk != min_sdk_version and not options.sdk_path: |
| 69 | print >> sys.stderr, '' |
| 70 | print >> sys.stderr, ' vvvvvvv' |
| 71 | print >> sys.stderr, '' |
| 72 | print >> sys.stderr, \ |
| 73 | 'This build requires the %s SDK, but it was not found on your system.' \ |
| 74 | % min_sdk_version |
| 75 | print >> sys.stderr, \ |
| 76 | 'Either install it, or explicitly set mac_sdk in your GYP_DEFINES.' |
| 77 | print >> sys.stderr, '' |
| 78 | print >> sys.stderr, ' ^^^^^^^' |
| 79 | print >> sys.stderr, '' |
| 80 | return min_sdk_version |
| 81 | |
| 82 | if options.print_sdk_path: |
| 83 | print subprocess.check_output(['xcodebuild', '-version', '-sdk', |
| 84 | 'macosx' + best_sdk, 'Path']).strip() |
| 85 | |
| 86 | return best_sdk |
| 87 | |
| 88 | |
| 89 | if __name__ == '__main__': |
| 90 | if sys.platform != 'darwin': |
| 91 | raise Exception("This script only runs on Mac") |
| 92 | print main() |
| 93 | sys.exit(0) |