Procházet zdrojové kódy

Merge topic 'xcode-macos-deployment-target' into release-4.1

de53a46a91 Help: Add 4.1 release note for restored Xcode default deployment target
85ba6f5d1b Merge branch 'backport-4.0-xcode-macos-deployment-target'
884260ced6 Help: Add 4.0 release note for restored Xcode default deployment target
1a37c83c7e Xcode: Restore default CMAKE_OSX_DEPLOYMENT_TARGET to run on host
e927408047 Apple: Initialize deployment target after SDK

Acked-by: Kitware Robot <[email protected]>
Merge-request: !11361
Brad King před 2 měsíci
rodič
revize
4e2aeae7ba

+ 7 - 0
Help/release/4.0.rst

@@ -301,3 +301,10 @@ Changes made since CMake 4.0.0 include the following.
 * These versions made no changes to documented features or interfaces.
 * These versions made no changes to documented features or interfaces.
   Some implementation updates were made to support ecosystem changes
   Some implementation updates were made to support ecosystem changes
   and/or fix regressions.
   and/or fix regressions.
+
+.. 4.0.5
+
+  * When building for macOS with the :generator:`Xcode` generator,
+    :variable:`CMAKE_OSX_DEPLOYMENT_TARGET` once again defaults to the
+    host's macOS version if it is older than the macOS SDK version,
+    as it did in CMake versions prior to 4.0.

+ 7 - 0
Help/release/4.1.rst

@@ -243,3 +243,10 @@ Changes made since CMake 4.1.0 include the following.
   each call site.  Previously, it used the value as of the end of the
   each call site.  Previously, it used the value as of the end of the
   directory's ``CMakeLists.txt``, as all other generator expression
   directory's ``CMakeLists.txt``, as all other generator expression
   evaluations do.
   evaluations do.
+
+.. 4.1.3
+
+  * When building for macOS with the :generator:`Xcode` generator,
+    :variable:`CMAKE_OSX_DEPLOYMENT_TARGET` once again defaults to the
+    host's macOS version if it is older than the macOS SDK version,
+    as it did in CMake versions prior to 4.0.

+ 34 - 10
Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst

@@ -1,15 +1,39 @@
 CMAKE_OSX_DEPLOYMENT_TARGET
 CMAKE_OSX_DEPLOYMENT_TARGET
 ---------------------------
 ---------------------------
 
 
-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,
-and otherwise computed based on the host platform.
+Specify the minimum version of the target platform, e.g., macOS or iOS,
+on which the target binaries are to be deployed.
+
+For builds targeting macOS (:variable:`CMAKE_SYSTEM_NAME` is ``Darwin``), if
+``CMAKE_OSX_DEPLOYMENT_TARGET`` is not explicitly set, a default is set:
+
+* If the ``MACOSX_DEPLOYMENT_TARGET`` environment variable is non-empty,
+  its value is the default.
+
+* Otherwise, if using the :generator:`Xcode` generator, and the host's
+  macOS version is older than the macOS SDK (:variable:`CMAKE_OSX_SYSROOT`,
+  if set, or Xcode's default SDK), the host's macOS version is the default.
+
+  .. versionchanged:: 4.0
+
+    Previously this was done for all generators, not just Xcode.
+
+* Otherwise, the default is empty.
+
+The effects of ``CMAKE_OSX_DEPLOYMENT_TARGET`` depend on the generator:
+
+:generator:`Xcode`
+
+  If ``CMAKE_OSX_DEPLOYMENT_TARGET`` is set to a non-empty value, it is added
+  to the generated Xcode project as the ``MACOSX_DEPLOYMENT_TARGET`` setting.
+  Otherwise, no such setting is added, so Xcode's default deployed target is
+  used, typically based on the SDK version.
+
+Other Generators
+
+  If ``CMAKE_OSX_DEPLOYMENT_TARGET`` is set to a non-empty value, it is passed
+  to the compiler via the ``-mmacosx-version-min`` flag or equivalent.
+  Otherwise, no such flag is added, so the compiler's default deployment
+  target is used.
 
 
 .. include:: include/CMAKE_OSX_VARIABLE.rst
 .. include:: include/CMAKE_OSX_VARIABLE.rst

+ 45 - 15
Modules/Platform/Darwin-Initialize.cmake

@@ -15,12 +15,6 @@ else()
   set(OSX_DEVELOPER_ROOT "")
   set(OSX_DEVELOPER_ROOT "")
 endif()
 endif()
 
 
-if(NOT CMAKE_CROSSCOMPILING)
-  execute_process(COMMAND sw_vers -productVersion
-    OUTPUT_VARIABLE _CMAKE_HOST_OSX_VERSION
-    OUTPUT_STRIP_TRAILING_WHITESPACE)
-endif()
-
 # Save CMAKE_OSX_ARCHITECTURES from the environment.
 # Save CMAKE_OSX_ARCHITECTURES from the environment.
 set(CMAKE_OSX_ARCHITECTURES "$ENV{CMAKE_OSX_ARCHITECTURES}" CACHE STRING
 set(CMAKE_OSX_ARCHITECTURES "$ENV{CMAKE_OSX_ARCHITECTURES}" CACHE STRING
   "Build architectures for OSX")
   "Build architectures for OSX")
@@ -49,15 +43,6 @@ endif()
 # Platform/Apple-${CMAKE_CXX_COMPILER_ID}-<LANG>
 # Platform/Apple-${CMAKE_CXX_COMPILER_ID}-<LANG>
 set(CMAKE_EFFECTIVE_SYSTEM_NAME "Apple")
 set(CMAKE_EFFECTIVE_SYSTEM_NAME "Apple")
 
 
-#----------------------------------------------------------------------------
-# CMAKE_OSX_DEPLOYMENT_TARGET
-
-# Set cache variable - end user may change this during ccmake or cmake-gui configure.
-if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
-  set(CMAKE_OSX_DEPLOYMENT_TARGET "$ENV{MACOSX_DEPLOYMENT_TARGET}" CACHE STRING
-    "Minimum OS X version to target for deployment (at runtime); newer APIs weak linked. Set to empty string for default value.")
-endif()
-
 #----------------------------------------------------------------------------
 #----------------------------------------------------------------------------
 # CMAKE_OSX_SYSROOT
 # CMAKE_OSX_SYSROOT
 
 
@@ -296,3 +281,48 @@ if(NOT CMAKE_OSX_SYSROOT)
     /usr/local/lib
     /usr/local/lib
   )
   )
 endif()
 endif()
+
+#----------------------------------------------------------------------------
+# CMAKE_OSX_DEPLOYMENT_TARGET
+
+if(NOT CMAKE_CROSSCOMPILING)
+  execute_process(COMMAND sw_vers -productVersion
+    OUTPUT_VARIABLE _CMAKE_HOST_OSX_VERSION
+    OUTPUT_STRIP_TRAILING_WHITESPACE)
+endif()
+
+if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND NOT DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
+  set(_CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT "$ENV{MACOSX_DEPLOYMENT_TARGET}")
+
+  # Xcode chooses a default macOS deployment target based on the macOS SDK
+  # version, which may be too new for binaries to run on the host.
+  if(NOT _CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT
+      AND CMAKE_GENERATOR STREQUAL "Xcode" AND NOT CMAKE_CROSSCOMPILING
+      AND _CMAKE_HOST_OSX_VERSION MATCHES "^([0-9]+\\.[0-9]+)")
+    set(_macos_version "${CMAKE_MATCH_1}")
+    if(CMAKE_OSX_SYSROOT)
+      set(_sdk_macosx --sdk ${CMAKE_OSX_SYSROOT})
+    else()
+      set(_sdk_macosx)
+    endif()
+    execute_process(
+      COMMAND xcrun ${_sdk_macosx} --show-sdk-version
+      OUTPUT_VARIABLE _sdk_version OUTPUT_STRIP_TRAILING_WHITESPACE
+      ERROR_VARIABLE _sdk_version_error
+      RESULT_VARIABLE _sdk_version_result
+    )
+    if(_sdk_version_result EQUAL 0 AND _sdk_version
+        AND "${_macos_version}" VERSION_LESS "${_sdk_version}")
+      set(_CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT "${_macos_version}")
+    endif()
+    unset(_sdk_macosx)
+    unset(_sdk_version_result)
+    unset(_sdk_version_error)
+    unset(_sdk_version)
+    unset(_macos_version)
+  endif()
+
+  set(CMAKE_OSX_DEPLOYMENT_TARGET "${_CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT}" CACHE STRING
+    "Minimum OS X version to target for deployment (at runtime); newer APIs weak linked. Set to empty string for default value.")
+  unset(_CMAKE_OSX_DEPLOYMENT_TARGET_DEFAULT)
+endif()