Home | History | Annotate | Download | only in tools
      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 def parse_version(version_str):
     19   """'10.6' => [10, 6]"""
     20   return map(int, re.findall(r'(\d+)', version_str))
     21 
     22 def find_sdk_dir():
     23   job = subprocess.Popen(['xcode-select', '-print-path'],
     24                          stdout=subprocess.PIPE,
     25                          stderr=subprocess.STDOUT)
     26   out, err = job.communicate()
     27   if job.returncode != 0:
     28     print >>sys.stderr, out
     29     print >>sys.stderr, err
     30     raise Exception(('Error %d running xcode-select, you might have to run '
     31       '|sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer| '
     32       'if you are using Xcode 4.') % job.returncode)
     33   # The Developer folder moved in Xcode 4.3.
     34   xcode43_sdk_path = os.path.join(
     35       out.rstrip(), 'Platforms/MacOSX.platform/Developer/SDKs')
     36   if os.path.isdir(xcode43_sdk_path):
     37     sdk_dir = xcode43_sdk_path
     38   else:
     39     sdk_dir = os.path.join(out.rstrip(), 'SDKs')
     40     
     41   return sdk_dir
     42 
     43 def main():
     44   min_sdk_version = '10.6'
     45   if len(sys.argv) > 1:
     46     min_sdk_version = sys.argv[1]
     47 
     48   sdk_dir = find_sdk_dir()
     49 
     50   sdks = [re.findall('^MacOSX(10\.\d+)\.sdk$', s) for s in os.listdir(sdk_dir)]
     51   sdks = [s[0] for s in sdks if s]  # [['10.5'], ['10.6']] => ['10.5', '10.6']
     52   sdks = [s for s in sdks  # ['10.5', '10.6'] => ['10.6']
     53           if parse_version(s) >= parse_version(min_sdk_version)]
     54   if not sdks:
     55     raise Exception('No %s+ SDK found' % min_sdk_version)
     56   best_sdk = min(sdks, key=parse_version)
     57 
     58   return best_sdk
     59 
     60 if __name__ == '__main__':
     61   if sys.platform != 'darwin':
     62     raise Exception("This script only runs on Mac")
     63   print main()
     64