From f73a7ca46cb15d77608a4995921b4d501c21bdee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= <tor.arne.vestbo@digia.com>
Date: Mon, 8 Apr 2013 16:41:27 +0200
Subject: [PATCH] Verify Xcode installation more thoroughly before relying on
 it

We depend on Xcode for building Qt itself and user application on Mac OS.
The user may have an Xcode install that is not set up properly, in which
case we would fail compilation in mysterious ways. Instead we try to
detect misconfigured or missing Xcode installs as early as possible.

We try to detect if an Xcode install has not been chosen yet, and
if the user has not accepted the Xcode license agreement. We need to
do these checks both in configure, as early as possible, and in mkspecs
on Mac OS, as we need to error out if the user tries to build an app
with the Qt SDK, but with a broken Xcode install.

Change-Id: I4e3a11077a61dc5d4ee2c686d01044a9bb2c1c79
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
---
 configure                                     | 22 +++++++++++++++++++
 mkspecs/common/mac.conf                       |  3 +++
 mkspecs/common/xcode.conf                     |  6 ++++-
 mkspecs/unsupported/macx-ios-clang/qmake.conf |  5 ++---
 4 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/configure b/configure
index 0f178c02598..f55e2f622be 100755
--- a/configure
+++ b/configure
@@ -445,6 +445,28 @@ if [ "$OSTYPE" = "msys" ]; then
     BUILD_ON_MSYS=yes
 fi
 
+#-------------------------------------------------------------------------------
+# Verify Xcode installation on Mac OS
+#-------------------------------------------------------------------------------
+
+if [ "$BUILD_ON_MAC" = "yes" ]; then
+    if ! /usr/bin/xcode-select --print-path >/dev/null 2>&1; then
+        echo >&2
+        echo "   No Xcode is selected. Use xcode-select -switch to choose an Xcode" >&2
+        echo "   version. See the xcode-select man page for more information." >&2
+        echo >&2
+        exit 2
+    fi
+
+    if ! /usr/bin/xcrun -find xcrun >/dev/null 2>&1; then
+        echo >&2
+        echo "   Xcode not set up properly. You may need to confirm the license" >&2
+        echo "   agreement by running /usr/bin/xcodebuild without arguments." >&2
+        echo >&2
+        exit 2
+    fi
+fi
+
 #-----------------------------------------------------------------------------
 # Qt version detection
 #-----------------------------------------------------------------------------
diff --git a/mkspecs/common/mac.conf b/mkspecs/common/mac.conf
index cf54f3f5801..f31db3bb4b6 100644
--- a/mkspecs/common/mac.conf
+++ b/mkspecs/common/mac.conf
@@ -26,4 +26,7 @@ QMAKE_LIBS_THREAD       =
 QMAKE_AR                = ar cq
 QMAKE_RANLIB            = ranlib -s
 
+# We rely on Xcode to build
+include(xcode.conf)
+
 include(unix.conf)
diff --git a/mkspecs/common/xcode.conf b/mkspecs/common/xcode.conf
index 00954694219..b15266b6789 100644
--- a/mkspecs/common/xcode.conf
+++ b/mkspecs/common/xcode.conf
@@ -3,7 +3,7 @@
 #
 
 # Get path of Xcode's Developer directory
-QMAKE_XCODE_DEVELOPER_PATH = $$system("/usr/bin/xcode-select --print-path")
+QMAKE_XCODE_DEVELOPER_PATH = $$system("/usr/bin/xcode-select --print-path 2>/dev/null")
 isEmpty(QMAKE_XCODE_DEVELOPER_PATH): \
     error("Xcode path is not set. Please use xcode-select to choose Xcode installation path.")
 
@@ -11,6 +11,10 @@ isEmpty(QMAKE_XCODE_DEVELOPER_PATH): \
 !exists($$QMAKE_XCODE_DEVELOPER_PATH): \
     error("Xcode is not installed in $${QMAKE_XCODE_DEVELOPER_PATH}. Please use xcode-select to choose Xcode installation path.")
 
+# Make sure Xcode is set up properly
+isEmpty($$list($$system("/usr/bin/xcrun -find xcrun 2>/dev/null"))): \
+    error("Xcode not set up properly. You may need to confirm the license agreement by running /usr/bin/xcodebuild.")
+
 # Extract Xcode version using xcodebuild
 xcode_version = $$system("/usr/bin/xcodebuild -version")
 QMAKE_XCODE_VERSION = $$member(xcode_version, 1)
diff --git a/mkspecs/unsupported/macx-ios-clang/qmake.conf b/mkspecs/unsupported/macx-ios-clang/qmake.conf
index 32d10a06ed9..833e22e3bd3 100644
--- a/mkspecs/unsupported/macx-ios-clang/qmake.conf
+++ b/mkspecs/unsupported/macx-ios-clang/qmake.conf
@@ -15,9 +15,6 @@ DEFINES                += DARWIN_NO_CARBON QT_NO_CORESERVICES QT_NO_PRINTER QT_N
 # Universal target (iPhone and iPad)
 QMAKE_IOS_TARGETED_DEVICE_FAMILY = 1,2
 
-include(../../common/xcode.conf)
-lessThan(QMAKE_XCODE_VERSION, "4.3"): error("This mkspec requires Xcode 4.3 or later")
-
 include(../../common/ios.conf)
 include(../../common/gcc-base-mac.conf)
 include(../../common/clang.conf)
@@ -25,4 +22,6 @@ include(../../common/clang-mac.conf)
 include(../../common/ios/clang.conf)
 include(../../common/ios/qmake.conf)
 
+lessThan(QMAKE_XCODE_VERSION, "4.3"): error("This mkspec requires Xcode 4.3 or later")
+
 load(qt_config)
-- 
GitLab