michael@0: #!/usr/bin/env python michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: import os michael@0: import re michael@0: import subprocess michael@0: import sys michael@0: michael@0: """Prints the lowest locally available SDK version greater than or equal to a michael@0: given minimum sdk version to standard output. michael@0: michael@0: Usage: michael@0: python find_sdk.py 10.6 # Ignores SDKs < 10.6 michael@0: """ michael@0: michael@0: from optparse import OptionParser michael@0: michael@0: michael@0: def parse_version(version_str): michael@0: """'10.6' => [10, 6]""" michael@0: return map(int, re.findall(r'(\d+)', version_str)) michael@0: michael@0: michael@0: def main(): michael@0: parser = OptionParser() michael@0: parser.add_option("--verify", michael@0: action="store_true", dest="verify", default=False, michael@0: help="return the sdk argument and warn if it doesn't exist") michael@0: parser.add_option("--sdk_path", michael@0: action="store", type="string", dest="sdk_path", default="", michael@0: help="user-specified SDK path; bypasses verification") michael@0: (options, args) = parser.parse_args() michael@0: min_sdk_version = args[0] michael@0: michael@0: if sys.platform == 'darwin': michael@0: job = subprocess.Popen(['xcode-select', '-print-path'], michael@0: stdout=subprocess.PIPE, michael@0: stderr=subprocess.STDOUT) michael@0: out, err = job.communicate() michael@0: if job.returncode != 0: michael@0: print >>sys.stderr, out michael@0: print >>sys.stderr, err michael@0: raise Exception(('Error %d running xcode-select, you might have to run ' michael@0: '|sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer| ' michael@0: 'if you are using Xcode 4.') % job.returncode) michael@0: # The Developer folder moved in Xcode 4.3. michael@0: xcode43_sdk_path = os.path.join( michael@0: out.rstrip(), 'Platforms/MacOSX.platform/Developer/SDKs') michael@0: if os.path.isdir(xcode43_sdk_path): michael@0: sdk_dir = xcode43_sdk_path michael@0: else: michael@0: sdk_dir = os.path.join(out.rstrip(), 'SDKs') michael@0: sdks = [re.findall('^MacOSX(10\.\d+)\.sdk$', s) for s in os.listdir(sdk_dir)] michael@0: sdks = [s[0] for s in sdks if s] # [['10.5'], ['10.6']] => ['10.5', '10.6'] michael@0: sdks = [s for s in sdks # ['10.5', '10.6'] => ['10.6'] michael@0: if parse_version(s) >= parse_version(min_sdk_version)] michael@0: if not sdks: michael@0: raise Exception('No %s+ SDK found' % min_sdk_version) michael@0: best_sdk = sorted(sdks, key=parse_version)[0] michael@0: else: michael@0: best_sdk = "" michael@0: michael@0: if options.verify and best_sdk != min_sdk_version and not options.sdk_path: michael@0: print >>sys.stderr, '' michael@0: print >>sys.stderr, ' vvvvvvv' michael@0: print >>sys.stderr, '' michael@0: print >>sys.stderr, \ michael@0: 'This build requires the %s SDK, but it was not found on your system.' \ michael@0: % min_sdk_version michael@0: print >>sys.stderr, \ michael@0: 'Either install it, or explicitly set mac_sdk in your GYP_DEFINES.' michael@0: print >>sys.stderr, '' michael@0: print >>sys.stderr, ' ^^^^^^^' michael@0: print >>sys.stderr, '' michael@0: return min_sdk_version michael@0: michael@0: return best_sdk michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: print main()