#
# Helper to set build tool & SDK versions for iOS configurations
#
# This file sets up the following configuration variables:
#
#   QMAKE_IOS_XCODE_VERSION          - version number of Xcode being used
#   QMAKE_IOS_SDK_VERSION            - version number of iOS device SDK
#   QMAKE_IOS_SIMULATOR_SDK_VERSION  - version number of iOS simulator SDK
#
# Used in:
#
#   mkspecs/common/ios/clang.conf
#   mkspecs/common/ios/llvm.conf
#   mkspecs/common/ios/qmake.conf
#   mkspecs/unsupported/macx-iosdevice-clang-legacy/qmake.conf
#   mkspecs/unsupported/macx-iosdevice-clang/qmake.conf
#   mkspecs/unsupported/macx-iosdevice-llvm-legacy/qmake.conf
#   mkspecs/unsupported/macx-iosdevice-llvm/qmake.conf
#   mkspecs/unsupported/macx-iosdevice-g++-legacy/qmake.conf
#   mkspecs/unsupported/macx-iossimulator-llvm-legacy/qmake.conf
#   mkspecs/unsupported/macx-iossimulator-llvm/qmake.conf
#   mkspecs/unsupported/macx-iossimulator-g++-legacy/qmake.conf
#

#
# IMPORTANT:
#
# Xcode versions <= 4.2.x must be installed in /Developer.
# Xcode versions >= 4.3.x must be installed in /Applications
#
# Xcode versions >= 4.10 & < 5.0 and versions >= 10.0 are not supported due to
# the way the version checks are done here. As Apple (so far) has not used
# minor version numbers greater than 3 for Xcode, and the Xcode major version
# has only changed three times in the period 2003-2012, this is viewed as an
# acceptable limitation.
#

#
# Edit values below to match iOS build environment, or leave blank for
# autodetection (slower!)
#

# Xcode version used for cross-compiling
QMAKE_IOS_XCODE_VERSION =

# iOS SDK version used for cross-compiling for iOS devices
QMAKE_IOS_SDK_VERSION   =

# iOS SDK version used for cross-compiling for the iOS simulator
QMAKE_IOS_SIMULATOR_SDK_VERSION =

#
# Do not edit values below here if using a pre-built SDK
#

# Minimum iOS version required on deployment target (if not specified, will
# default to minimum version that guarantees ARMv7 & OpenGL/ES 2.x).
#
# No part of Qt or any known plugin uses features that require iOS versions
# later than 4.0.
QMAKE_IPHONEOS_DEPLOYMENT_TARGET = 4.0

###########################################################################

# No Xcode version specified?
isEmpty(QMAKE_IOS_XCODE_VERSION) {
    # Get version string from installed Xcode
    exists(/Applications/Xcode.app/Contents/Developer) {
        QMAKE_IOS_XCODE_INFO = $$system(/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -version)
    } else: QMAKE_IOS_XCODE_INFO = $$system(/Developer/usr/bin/xcodebuild -version)

    # Extract Xcode version number from output
    QMAKE_IOS_XCODE_VERSION = $$member(QMAKE_IOS_XCODE_INFO, 1)
    QMAKE_IOS_XCODE_INFO =
}

# Make sure Xcode version is valid
!lessThan(QMAKE_IOS_XCODE_VERSION, "4.3") {
    # Xcode 4.3+ is stored in /Applications
    QMAKE_IOS_PLATFORM_PATH  = /Applications/Xcode.app/Contents/Developer/Platforms

    # Make sure Xcode path is valid
    !exists($$QMAKE_IOS_PLATFORM_PATH): error("Xcode is not installed in /Applications/Xcode.app! Edit mkspecs/common/ios/versions.conf to specify version installed.")
} else {
    # Older Xcode versions are stored in /Developer
    QMAKE_IOS_PLATFORM_PATH  = /Developer/Platforms

    # Make sure Xcode path is valid
    !exists($$QMAKE_IOS_PLATFORM_PATH): error("Xcode is not installed in /Developer! Edit mkspecs/common/ios/versions.conf to specify version installed.")
}

# iOS 4.3 is the preferred version as it is the earliest version that is armv7/gles2 only
QMAKE_IPHONEOS_PREFERRED_TARGET = 4.3

# Building for iOS device?
!*simulator* {
    # No iOS SDK version specified?
    isEmpty(QMAKE_IOS_SDK_VERSION) {
        # Get version string from installed Xcode
        !lessThan(QMAKE_IOS_XCODE_VERSION, "4.3") {
            QMAKE_IOS_SDK_INFO = $$system(/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -showsdks)
        } else: QMAKE_IOS_SDK_INFO = $$system(/Developer/usr/bin/xcodebuild -showsdks)

        # Get names of installed device SDKs
        QMAKE_IOS_SDK_INFO = $$find(QMAKE_IOS_SDK_INFO, ^iphoneos)
        for(v, QMAKE_IOS_SDK_INFO): {
            # Extract SDK version number from output
            v = $$replace(v,iphoneos,)

            # Use latest SDK version
            greaterThan(v, $$QMAKE_IOS_SDK_VERSION): QMAKE_IOS_SDK_VERSION = $$v
        }
        QMAKE_IOS_SDK_INFO =
    }

    # Make sure iOS SDK version is valid
    !exists($$QMAKE_IOS_PLATFORM_PATH/iPhoneOS.platform/Developer/SDKs/iPhoneOS$${QMAKE_IOS_SDK_VERSION}.sdk) {
        error("iOS $$QMAKE_IOS_SDK_VERSION SDK not found! Edit mkspecs/common/ios/versions.conf to specify version installed.")
    }

    # No deployment target specified?
    isEmpty(QMAKE_IPHONEOS_DEPLOYMENT_TARGET) {
        # Use SDK version for iOS versions < preferred
        lessThan(QMAKE_IOS_SDK_VERSION, $$QMAKE_IPHONEOS_PREFERRED_TARGET) {
            QMAKE_IPHONEOS_DEPLOYMENT_TARGET = $$QMAKE_IOS_SDK_VERSION
        } else: QMAKE_IPHONEOS_DEPLOYMENT_TARGET = $$QMAKE_IPHONEOS_PREFERRED_TARGET
    }

    # Make sure iOS SDK version is >= iOS target version
    !lessThan(QMAKE_IPHONEOS_DEPLOYMENT_TARGET, $$QMAKE_IOS_SDK_VERSION) {
        error("Target iOS version is greater that iOS SDK version $$QMAKE_IOS_SDK_VERSION! Edit mkspecs/common/ios/versions.conf to specify target iOS version.")
    }
# Building for iOS simulator
} else {
    # No iOS simulator SDK version specified?
    isEmpty(QMAKE_IOS_SIMULATOR_SDK_VERSION) {
        # Get version string from installed Xcode
        !lessThan(QMAKE_IOS_XCODE_VERSION, "4.3") {
            QMAKE_IOS_SDK_INFO = $$system(/Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -showsdks)
        } else: QMAKE_IOS_SDK_INFO = $$system(/Developer/usr/bin/xcodebuild -showsdks)

        # Get names of installed device SDKs
        QMAKE_IOS_SDK_INFO = $$find(QMAKE_IOS_SDK_INFO, ^iphonesimulator)
        for(v, QMAKE_IOS_SDK_INFO): {
            # Extract SDK version number from output
            v = $$replace(v,iphonesimulator,)

            # Use latest SDK version
            greaterThan(v, $$QMAKE_IOS_SIMULATOR_SDK_VERSION): QMAKE_IOS_SIMULATOR_SDK_VERSION = $$v
        }
        QMAKE_IOS_SDK_INFO =
    }

    # Make sure iOS simulator SDK version is valid
    !exists($$QMAKE_IOS_PLATFORM_PATH/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator$${QMAKE_IOS_SIMULATOR_SDK_VERSION}.sdk) {
        error("iOS $$QMAKE_IOS_SIMULATOR_SDK_VERSION simulator SDK not found! Edit mkspecs/common/ios/versions.conf to specify version installed.")
    }

    # No deployment target specified?
    isEmpty(QMAKE_IPHONEOS_DEPLOYMENT_TARGET) {
        # Use SDK version for iOS versions < preferred
        lessThan(QMAKE_IOS_SIMULATOR_SDK_VERSION, $$QMAKE_IPHONEOS_PREFERRED_TARGET) {
            QMAKE_IPHONEOS_DEPLOYMENT_TARGET = $$QMAKE_IOS_SIMULATOR_SDK_VERSION
        } else: QMAKE_IPHONEOS_DEPLOYMENT_TARGET = $$QMAKE_IPHONEOS_PREFERRED_TARGET
    }

    # Make sure iOS simulator SDK version is >= iOS target version
    !lessThan(QMAKE_IPHONEOS_DEPLOYMENT_TARGET, $$QMAKE_IOS_SIMULATOR_SDK_VERSION) {
        error("Target iOS version is greater that iOS simulator SDK version $$QMAKE_IOS_SIMULATOR_SDK_VERSION! Edit mkspecs/common/ios/versions.conf to specify target iOS version.")
    }
}

QMAKE_IPHONEOS_PREFERRED_TARGET =
QMAKE_IOS_PLATFORM_PATH =

# Set deployment target
QMAKE_IOS_VERSION_FLAGS = -miphoneos-version-min=$$QMAKE_IPHONEOS_DEPLOYMENT_TARGET

# Set build flags
QMAKE_CFLAGS           += $$QMAKE_IOS_VERSION_FLAGS
QMAKE_CXXFLAGS         += $$QMAKE_IOS_VERSION_FLAGS
QMAKE_OBJECTIVE_CFLAGS += $$QMAKE_IOS_VERSION_FLAGS
QMAKE_LFLAGS           += $$QMAKE_IOS_VERSION_FLAGS
QMAKE_IOS_VERSION_FLAGS =
