find_sdk.py 3.3 KB

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