
isEmpty(QMAKE_MAC_SDK): \
    error("QMAKE_MAC_SDK must be set when using CONFIG += sdk.")

contains(QMAKE_MAC_SDK, .*/.*): \
    error("QMAKE_MAC_SDK can only contain short-form SDK names (eg. macosx, iphoneos)")

isEmpty(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.path) {
    QMAKE_MAC_SDK_PATH = $$system("/usr/bin/xcodebuild -sdk $$QMAKE_MAC_SDK -version Path 2>/dev/null")
    isEmpty(QMAKE_MAC_SDK_PATH): error("Could not resolve SDK path for \'$$QMAKE_MAC_SDK\'")
    cache(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.path, set stash, QMAKE_MAC_SDK_PATH)
} else {
    QMAKE_MAC_SDK_PATH = $$eval(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.path)
}

isEmpty(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.platform_path) {
    QMAKE_MAC_SDK_PLATFORM_PATH = $$system("/usr/bin/xcodebuild -sdk $$QMAKE_MAC_SDK -version PlatformPath 2>/dev/null")
    isEmpty(QMAKE_MAC_SDK_PLATFORM_PATH): error("Could not resolve SDK platform path for \'$$QMAKE_MAC_SDK\'")
    cache(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.platform_path, set stash, QMAKE_MAC_SDK_PLATFORM_PATH)
} else {
    QMAKE_MAC_SDK_PLATFORM_PATH = $$eval(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.platform_path)
}

isEmpty(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.version) {
    QMAKE_MAC_SDK_VERSION = $$system("/usr/bin/xcodebuild -sdk $$QMAKE_MAC_SDK -version SDKVersion 2>/dev/null")
    isEmpty(QMAKE_MAC_SDK_VERSION): error("Could not resolve SDK version for \'$$QMAKE_MAC_SDK\'")
    cache(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.version, set stash, QMAKE_MAC_SDK_VERSION)
} else {
    QMAKE_MAC_SDK_VERSION = $$eval(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.version)
}

!equals(MAKEFILE_GENERATOR, XCODE) {
    QMAKE_CFLAGS += -isysroot $$QMAKE_MAC_SDK_PATH
    QMAKE_CXXFLAGS += -isysroot $$QMAKE_MAC_SDK_PATH
    QMAKE_LFLAGS += -Wl,-syslibroot,$$QMAKE_MAC_SDK_PATH
}

sysrootified =
for(val, QMAKE_INCDIR_OPENGL): sysrootified += $${QMAKE_MAC_SDK_PATH}$$val
QMAKE_INCDIR_OPENGL = $$sysrootified

QMAKESPEC_NAME = $$basename(QMAKESPEC)

# Resolve SDK version of various tools
for(tool, $$list(QMAKE_CC QMAKE_CXX QMAKE_FIX_RPATH QMAKE_AR QMAKE_RANLIB QMAKE_LINK QMAKE_LINK_SHLIB QMAKE_ACTOOL)) {
    tool_variable = QMAKE_MAC_SDK.$${QMAKESPEC_NAME}.$${QMAKE_MAC_SDK}.$${tool}
    !isEmpty($$tool_variable) {
        $$tool = $$eval($$tool_variable)
        next()
    }

    value = $$eval($$tool)
    isEmpty(value): next()

    sysrooted = $$system("/usr/bin/xcrun -sdk $$QMAKE_MAC_SDK -find $$first(value) 2>/dev/null")
    isEmpty(sysrooted): next()

    $$tool = $$sysrooted $$member(value, 1, -1)
    cache($$tool_variable, set stash, $$tool)
}

isEmpty(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.platform_name) {
    QMAKE_MAC_PLATFORM_NAME = $$system("/usr/libexec/PlistBuddy -c 'print DefaultProperties:PLATFORM_NAME' $$QMAKE_MAC_SDK_PATH/SDKSettings.plist 2>/dev/null")
    isEmpty(QMAKE_MAC_PLATFORM_NAME): error("Could not resolve platform name for SDK '$$QMAKE_MAC_SDK'")
    cache(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.platform_name, set stash, QMAKE_MAC_PLATFORM_NAME)
} else {
    QMAKE_MAC_PLATFORM_NAME = $$eval(QMAKE_MAC_SDK.$${QMAKE_MAC_SDK}.platform_name)
}

!equals(MAKEFILE_GENERATOR, XCODE) {
    # FIXME: Get the version_min_flag out of the platform's 'Native Build System.xcspec'
    version_identifier = $$replace(QMAKE_MAC_PLATFORM_NAME, iphonesimulator, ios-simulator)

    ios:!host_build: \
        deployment_target = $$QMAKE_IOS_DEPLOYMENT_TARGET
    else: \
        deployment_target = $$QMAKE_MACOSX_DEPLOYMENT_TARGET

    version_min_flag = -m$${version_identifier}-version-min=$$deployment_target
    QMAKE_CFLAGS += $$version_min_flag
    QMAKE_CXXFLAGS += $$version_min_flag
    QMAKE_LFLAGS += $$version_min_flag
}
