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