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. 5 6"""Prints the lowest locally available SDK version greater than or equal to a 7given minimum sdk version to standard output. 8 9Usage: 10 python find_sdk.py 10.6 # Ignores SDKs < 10.6 11""" 12 13import os 14import re 15import subprocess 16import sys 17 18 19from optparse import OptionParser 20 21 22def parse_version(version_str): 23 """'10.6' => [10, 6]""" 24 return map(int, re.findall(r'(\d+)', version_str)) 25 26 27def 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 min_sdk_version = args[0] 40 41 job = subprocess.Popen(['xcode-select', '-print-path'], 42 stdout=subprocess.PIPE, 43 stderr=subprocess.STDOUT) 44 out, err = job.communicate() 45 if job.returncode != 0: 46 print >> sys.stderr, out 47 print >> sys.stderr, err 48 raise Exception(('Error %d running xcode-select, you might have to run ' 49 '|sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer| ' 50 'if you are using Xcode 4.') % job.returncode) 51 # The Developer folder moved in Xcode 4.3. 52 xcode43_sdk_path = os.path.join( 53 out.rstrip(), 'Platforms/MacOSX.platform/Developer/SDKs') 54 if os.path.isdir(xcode43_sdk_path): 55 sdk_dir = xcode43_sdk_path 56 else: 57 sdk_dir = os.path.join(out.rstrip(), 'SDKs') 58 sdks = [re.findall('^MacOSX(10\.\d+)\.sdk$', s) for s in os.listdir(sdk_dir)] 59 sdks = [s[0] for s in sdks if s] # [['10.5'], ['10.6']] => ['10.5', '10.6'] 60 sdks = [s for s in sdks # ['10.5', '10.6'] => ['10.6'] 61 if parse_version(s) >= parse_version(min_sdk_version)] 62 if not sdks: 63 raise Exception('No %s+ SDK found' % min_sdk_version) 64 best_sdk = sorted(sdks, key=parse_version)[0] 65 66 if options.verify and best_sdk != min_sdk_version and not options.sdk_path: 67 print >> sys.stderr, '' 68 print >> sys.stderr, ' vvvvvvv' 69 print >> sys.stderr, '' 70 print >> sys.stderr, \ 71 'This build requires the %s SDK, but it was not found on your system.' \ 72 % min_sdk_version 73 print >> sys.stderr, \ 74 'Either install it, or explicitly set mac_sdk in your GYP_DEFINES.' 75 print >> sys.stderr, '' 76 print >> sys.stderr, ' ^^^^^^^' 77 print >> sys.stderr, '' 78 return min_sdk_version 79 80 if options.print_sdk_path: 81 print subprocess.check_output(['xcodebuild', '-version', '-sdk', 82 'macosx' + best_sdk, 'Path']).strip() 83 84 return best_sdk 85 86 87if __name__ == '__main__': 88 if sys.platform != 'darwin': 89 raise Exception("This script only runs on Mac") 90 print main() 91