buildSrc/mac.gradle

Print this page
rev 8319 : RT-34388: [Mac] Update build system to properly support Xcode 5 and later

@@ -39,15 +39,80 @@
 // Lambda for naming the generated libs
 MAC.library = { name -> return "lib${name}.dylib" as String }
 
 MAC.libDest = "lib"
 
-// Define settings for Mac compilation. This is much easier than Windows because we know what version we're
-// compiling against and Mac always puts it in the same place. In extreme cases you can provide your own
-// properties in your home dir to override these settings or pass them on the command line via -P
-defineProperty("MACOSX_MIN_VERSION", "10.7");
-defineProperty("MACOSX_SDK_PATH", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${MACOSX_MIN_VERSION}.sdk");
+/*
+ * Define settings for Mac compilation. If we don't find the SDK in the
+ * default location then we will use "xcodebuild" to locate a suitable SDK.
+ * In extreme cases you can provide your own properties in your home dir to
+ * override these settings or pass them on the command line.
+ */
+def minSdkVersion = "10.7"
+def defaultSdkPath = "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX${minSdkVersion}.sdk";
+
+// Ordered list of SDKs to search if default path is not found
+def macosxSdks = [
+    "macosx10.7",
+    "macosx10.8",
+    "macosx10.9",
+]
+
+// Set the minimum API version that we require (developers do not need to override this)
+defineProperty("MACOSX_MIN_VERSION", "$minSdkVersion");
+
+// Create $buildDir/mac_tools.properties file and load props from it
+setupTools("mac_tools",
+    { propFile ->
+        propFile << ""
+        if (!file(defaultSdkPath).isDirectory()) {
+            // Collect all macosx sdks.
+            ByteArrayOutputStream results = new ByteArrayOutputStream();
+            exec {
+                commandLine("xcodebuild", "-version", "-showsdks");
+                setStandardOutput(results);
+            }
+
+            BufferedReader reader = new BufferedReader(new StringReader(results.toString().trim()));
+            def sdkList = []
+            while (true) {
+                def line = reader.readLine();
+                if (line == null) break;
+                def idx = line.indexOf("-sdk macosx");
+                if (idx >= 0) {
+                    sdkList += line.substring(idx+4).trim();
+                }
+            }
+
+            // Loop through the predefined list of SDKs that we recognize
+            for (String sdk : macosxSdks) {
+                if (sdkList.contains(sdk)) {
+                    results = new ByteArrayOutputStream();
+                    exec {
+                        commandLine("xcodebuild", "-version", "-sdk", "$sdk", "Path");
+                        setStandardOutput(results);
+                    }
+                    String sdkPath = results.toString().trim();
+                    if (file(sdkPath).isDirectory()) {
+                        propFile << "MACOSX_SDK_PATH=" << sdkPath << "\n";
+                        break;
+                    }
+                }
+            }
+        }
+    },
+    { properties ->
+        defineProperty("MACOSX_SDK_PATH", properties, "$defaultSdkPath")
+    }
+)
+
+println "MACOSX_MIN_VERSION = $MACOSX_MIN_VERSION"
+println "MACOSX_SDK_PATH = $MACOSX_SDK_PATH"
+
+if (!file(MACOSX_SDK_PATH).isDirectory()) {
+    throw new GradleException("FAIL: Cannot find $MACOSX_SDK_PATH")
+}
 
 def commonParams = [
         "-mmacosx-version-min=$MACOSX_MIN_VERSION",
         "-isysroot", "$MACOSX_SDK_PATH",
         "-arch", "x86_64"]