Darwin: Emit deployment target that matches the SDK

Closes: #17431
This commit is contained in:
Gregor Jasny
2017-11-04 15:27:48 +01:00
parent 8f4663ffb2
commit 4017bf40de
15 changed files with 185 additions and 15 deletions

View File

@@ -0,0 +1,11 @@
iphone-deployment-target
------------------------
* The minimum deployment target set in the
:variable:`CMAKE_OSX_DEPLOYMENT_TARGET` variable used to be only
applied for macOS regardless of the selected SDK. It is now properly
set for the target platform selected by :variable:`CMAKE_OSX_SYSROOT`.
If for example the sysroot variable specifies an iOS SDK then the
value in ``CMAKE_OSX_DEPLOYMENT_TARGET`` is interpreted as minium
iOS version.

View File

@@ -1,10 +1,12 @@
CMAKE_OSX_DEPLOYMENT_TARGET
---------------------------
Specify the minimum version of OS X on which the target binaries are
to be deployed. CMake uses this value for the ``-mmacosx-version-min``
flag and to help choose the default SDK
(see :variable:`CMAKE_OSX_SYSROOT`).
Specify the minimum version of the target platform (e.g. macOS or iOS)
on which the target binaries are to be deployed. CMake uses this
variable value for the ``-mmacosx-version-min`` flag or their respective
target platform equivalents. For older Xcode versions that shipped
multiple macOS SDKs this variable also helps to choose the SDK in case
:variable:`CMAKE_OSX_SYSROOT` is unset.
If not set explicitly the value is initialized by the
``MACOSX_DEPLOYMENT_TARGET`` environment variable, if set,

View File

@@ -3,4 +3,7 @@ The value of this variable should be set prior to the first
because it may influence configuration of the toolchain and flags.
It is intended to be set locally by the user creating a build tree.
This variable is ignored on platforms other than OS X.
Despite the ``OSX`` part in the variable name(s) they apply also to
other SDKs than macOS like iOS, tvOS, or watchOS.
This variable is ignored on platforms other than Apple.

View File

@@ -17,4 +17,19 @@ macro(__darwin_compiler_clang lang)
if(NOT CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.2)
set(CMAKE_${lang}_SYSTEM_FRAMEWORK_SEARCH_FLAG "-iframework ")
endif()
if(_CMAKE_OSX_SYSROOT_PATH MATCHES "/iPhoneOS")
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-miphoneos-version-min=")
elseif(_CMAKE_OSX_SYSROOT_PATH MATCHES "/iPhoneSimulator")
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-mios-simulator-version-min=")
elseif(_CMAKE_OSX_SYSROOT_PATH MATCHES "/AppleTVOS")
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-mtvos-version-min=")
elseif(_CMAKE_OSX_SYSROOT_PATH MATCHES "/AppleTVSimulator")
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-mtvos-simulator-version-min=")
elseif(_CMAKE_OSX_SYSROOT_PATH MATCHES "/WatchOS")
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-mwatchos-version-min=")
elseif(_CMAKE_OSX_SYSROOT_PATH MATCHES "/WatchSimulator")
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-mwatchos-simulator-version-min=")
else()
set(CMAKE_${lang}_OSX_DEPLOYMENT_TARGET_FLAG "-mmacosx-version-min=")
endif()
endmacro()

View File

@@ -2992,7 +2992,7 @@ bool cmGlobalXCodeGenerator::CreateXCodeObjects(
buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
}
if (deploymentTarget && *deploymentTarget) {
buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
buildSettings->AddAttribute(GetDeploymentPlatform(root->GetMakefile()),
this->CreateString(deploymentTarget));
}
if (!this->GeneratorToolset.empty()) {
@@ -3627,3 +3627,24 @@ void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
dir += "/";
gt->ObjectDirectory = dir;
}
std::string cmGlobalXCodeGenerator::GetDeploymentPlatform(const cmMakefile* mf)
{
switch (mf->GetAppleSDKType()) {
case cmMakefile::AppleSDK::AppleTVOS:
case cmMakefile::AppleSDK::AppleTVSimulator:
return "TVOS_DEPLOYMENT_TARGET";
case cmMakefile::AppleSDK::IPhoneOS:
case cmMakefile::AppleSDK::IPhoneSimulator:
return "IPHONEOS_DEPLOYMENT_TARGET";
case cmMakefile::AppleSDK::WatchOS:
case cmMakefile::AppleSDK::WatchSimulator:
return "WATCHOS_DEPLOYMENT_TARGET";
case cmMakefile::AppleSDK::MacOS:
default:
return "MACOSX_DEPLOYMENT_TARGET";
}
}

View File

@@ -254,6 +254,8 @@ private:
const std::string& configName,
const cmGeneratorTarget* t) const;
static std::string GetDeploymentPlatform(const cmMakefile* mf);
void ComputeArchitectures(cmMakefile* mf);
void ComputeObjectDirArch(cmMakefile* mf);

View File

@@ -2244,25 +2244,38 @@ bool cmMakefile::PlatformIsx32() const
return false;
}
bool cmMakefile::PlatformIsAppleEmbedded() const
cmMakefile::AppleSDK cmMakefile::GetAppleSDKType() const
{
std::string sdkRoot;
sdkRoot = this->GetSafeDefinition("CMAKE_OSX_SYSROOT");
sdkRoot = cmSystemTools::LowerCase(sdkRoot);
const std::string embedded[] = {
"appletvos", "appletvsimulator", "iphoneos",
"iphonesimulator", "watchos", "watchsimulator",
struct
{
std::string name;
AppleSDK sdk;
} const sdkDatabase[]{
{ "appletvos", AppleSDK::AppleTVOS },
{ "appletvsimulator", AppleSDK::AppleTVSimulator },
{ "iphoneos", AppleSDK::IPhoneOS },
{ "iphonesimulator", AppleSDK::IPhoneSimulator },
{ "watchos", AppleSDK::WatchOS },
{ "watchsimulator", AppleSDK::WatchSimulator },
};
for (std::string const& i : embedded) {
if (sdkRoot.find(i) == 0 ||
sdkRoot.find(std::string("/") + i) != std::string::npos) {
return true;
for (auto entry : sdkDatabase) {
if (sdkRoot.find(entry.name) == 0 ||
sdkRoot.find(std::string("/") + entry.name) != std::string::npos) {
return entry.sdk;
}
}
return false;
return AppleSDK::MacOS;
}
bool cmMakefile::PlatformIsAppleEmbedded() const
{
return GetAppleSDKType() != AppleSDK::MacOS;
}
const char* cmMakefile::GetSONameFlag(const std::string& language) const

View File

@@ -439,6 +439,21 @@ public:
/** Return whether the target platform is x32. */
bool PlatformIsx32() const;
/** Apple SDK Type */
enum class AppleSDK
{
MacOS,
IPhoneOS,
IPhoneSimulator,
AppleTVOS,
AppleTVSimulator,
WatchOS,
WatchSimulator,
};
/** What SDK type points CMAKE_OSX_SYSROOT to? */
AppleSDK GetAppleSDKType() const;
/** Return whether the target platform is Apple iOS. */
bool PlatformIsAppleEmbedded() const;

View File

@@ -0,0 +1,26 @@
#include <Availability.h>
#include <TargetConditionals.h>
#if TARGET_OS_OSX
#if __MAC_OS_X_VERSION_MIN_REQUIRED != __MAC_10_11
#error macOS deployment version mismatch
#endif
#elif TARGET_OS_IOS
#if __IPHONE_OS_VERSION_MIN_REQUIRED != __IPHONE_9_1
#error iOS deployment version mismatch
#endif
#elif TARGET_OS_WATCH
#if __WATCH_OS_VERSION_MIN_REQUIRED != __WATCHOS_2_0
#error watchOS deployment version mismatch
#endif
#elif TARGET_OS_TV
#if __TV_OS_VERSION_MIN_REQUIRED != __TVOS_9_0
#error tvOS deployment version mismatch
#endif
#else
#error unknown OS
#endif
void foo()
{
}

View File

@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.10)
project(DeploymentTarget C)
# using Xcode 7.1 SDK versions for deployment targets
if(SDK MATCHES iphone)
set(CMAKE_OSX_SYSROOT ${SDK})
set(CMAKE_OSX_ARCHITECTURES "armv7;x86_64")
set(CMAKE_OSX_DEPLOYMENT_TARGET "9.1")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "NO")
elseif(SDK MATCHES watch)
set(CMAKE_OSX_SYSROOT ${SDK})
set(CMAKE_OSX_ARCHITECTURES "armv7k;i386")
set(CMAKE_OSX_DEPLOYMENT_TARGET "2.0")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "")
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "YES")
elseif(SDK MATCHES appletv)
set(CMAKE_OSX_SYSROOT ${SDK})
set(CMAKE_OSX_DEPLOYMENT_TARGET "9.0")
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "")
set(CMAKE_XCODE_ATTRIBUTE_ENABLE_BITCODE "YES")
else()
set(CMAKE_OSX_SYSROOT ${SDK})
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.11")
endif()
add_library(myFramework STATIC DeploymentTarget.c)
set_target_properties(myFramework PROPERTIES FRAMEWORK TRUE)

View File

@@ -216,3 +216,21 @@ endfunction()
if(NOT XCODE_VERSION VERSION_LESS 7)
XcodeSchemaGeneration()
endif()
if(XCODE_VERSION VERSION_GREATER_EQUAL 8)
function(deploymeny_target_test SDK)
set(RunCMake_TEST_BINARY_DIR ${RunCMake_BINARY_DIR}/DeploymentTarget-${SDK}-build)
set(RunCMake_TEST_NO_CLEAN 1)
set(RunCMake_TEST_OPTIONS "-DSDK=${SDK}")
file(REMOVE_RECURSE "${RunCMake_TEST_BINARY_DIR}")
file(MAKE_DIRECTORY "${RunCMake_TEST_BINARY_DIR}")
run_cmake(DeploymentTarget)
run_cmake_command(DeploymentTarget-${SDK} ${CMAKE_COMMAND} --build .)
endfunction()
foreach(SDK macosx iphoneos iphonesimulator appletvos appletvsimulator watchos watchsimulator)
deploymeny_target_test(${SDK})
endforeach()
endif()

View File

@@ -3,6 +3,9 @@
cmake_minimum_required(VERSION 3.3)
enable_language(C)
# due to lack of toolchain file it might point to running macOS version
unset(CMAKE_OSX_DEPLOYMENT_TARGET CACHE)
if(TEST_IOS)
set(CMAKE_OSX_SYSROOT iphoneos)
set(CMAKE_OSX_ARCHITECTURES "armv7")

View File

@@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.3)
project(IOSInstallCombined CXX)
# due to lack of toolchain file it might point to running macOS version
unset(CMAKE_OSX_DEPLOYMENT_TARGET CACHE)
set(CMAKE_OSX_SYSROOT iphoneos)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
set(CMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf")

View File

@@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.3)
project(XcodeIOSInstallCombinedPrune CXX)
# due to lack of toolchain file it might point to running macOS version
unset(CMAKE_OSX_DEPLOYMENT_TARGET CACHE)
set(CMAKE_OSX_SYSROOT iphoneos)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
set(CMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf")

View File

@@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.3)
project(XcodeIOSInstallCombinedSingleArch CXX)
# due to lack of toolchain file it might point to running macOS version
unset(CMAKE_OSX_DEPLOYMENT_TARGET CACHE)
set(CMAKE_OSX_SYSROOT iphoneos)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
set(CMAKE_XCODE_ATTRIBUTE_DEBUG_INFORMATION_FORMAT "dwarf")