Fix gyptest-sdkroot when oldest SDK is >= 10.8

When using Mac OS X Mavericks with Xcode 5.0.2, the only SDK installed
are 10.8 and 10.9. Fix the test by using the oldest installed and
supported SDK.

BUG=381
R=mark@chromium.org

Review URL: https://codereview.chromium.org/83073002

git-svn-id: http://gyp.googlecode.com/svn/trunk@1810 78cadc50-ecff-11dd-a971-7dbc132099af
diff --git a/test/mac/gyptest-sdkroot.py b/test/mac/gyptest-sdkroot.py
index 20edd36..711726e 100644
--- a/test/mac/gyptest-sdkroot.py
+++ b/test/mac/gyptest-sdkroot.py
@@ -17,22 +17,29 @@
 if sys.platform == 'darwin':
   test = TestGyp.TestGyp(formats=['ninja', 'make', 'xcode'])
 
-  # Make sure this works on the bots, which only have the 10.6 sdk, and on
-  # dev machines, which usually don't have the 10.6 sdk.
-  sdk = '10.6'
-  DEVNULL = open(os.devnull, 'wb')
-  proc = subprocess.Popen(['xcodebuild', '-version', '-sdk', 'macosx' + sdk],
-                          stdout=DEVNULL, stderr=DEVNULL)
-  proc.communicate()
-  DEVNULL.close()
-  if proc.returncode:
-    sdk = '10.7'
+  def GetSDKPath(sdk):
+    """Return SDKROOT if the SDK version |sdk| is installed or empty string."""
+    DEVNULL = open(os.devnull, 'wb')
+    try:
+      proc = subprocess.Popen(
+          ['xcodebuild', '-version', '-sdk', 'macosx' + sdk, 'Path'],
+          stdout=subprocess.PIPE, stderr=DEVNULL)
+      return proc.communicate()[0].rstrip('\n')
+    finally:
+      DEVNULL.close()
 
-  proc = subprocess.Popen(['xcodebuild', '-version',
-                           '-sdk', 'macosx' + sdk, 'Path'],
-                          stdout=subprocess.PIPE)
-  sdk_path = proc.communicate()[0].rstrip('\n')
-  if proc.returncode != 0:
+  def SelectSDK():
+    """Select the oldest SDK installed (greater than 10.6)."""
+    for sdk in ['10.6', '10.7', '10.8', '10.9']:
+      path = GetSDKPath(sdk)
+      if path:
+        return True, sdk, path
+    return False, '', ''
+
+  # Make sure this works on the bots, which only have the 10.6 sdk, and on
+  # dev machines which usually don't have the 10.6 sdk.
+  sdk_found, sdk, sdk_path = SelectSDK()
+  if not sdk_found:
     test.fail_test()
 
   test.write('sdkroot/test.gyp', test.read('sdkroot/test.gyp') % sdk)
diff --git a/test/mac/sdkroot/test_shorthand.sh b/test/mac/sdkroot/test_shorthand.sh
index d768fba..ac4ac22 100755
--- a/test/mac/sdkroot/test_shorthand.sh
+++ b/test/mac/sdkroot/test_shorthand.sh
@@ -5,8 +5,16 @@
 
 set -e
 
-if ! expected=$(xcodebuild -version -sdk macosx10.6 Path 2>/dev/null) ; then
-  expected=$(xcodebuild -version -sdk macosx10.7 Path)
+found=false
+for sdk in 10.6 10.7 10.8 10.9 ; do
+  if expected=$(xcodebuild -version -sdk macosx$sdk Path 2>/dev/null) ; then
+    found=true
+    break
+  fi
+done
+if ! $found ; then
+  echo >&2 "cannot find installed SDK"
+  exit 1
 fi
 
 test $SDKROOT = $expected