From e927408047f5a2323bd08073a73e515c02c39107 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 22 Oct 2025 17:06:33 -0400 Subject: [PATCH 1/4] Apple: Initialize deployment target after SDK --- Modules/Platform/Darwin-Initialize.cmake | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/Modules/Platform/Darwin-Initialize.cmake b/Modules/Platform/Darwin-Initialize.cmake index 196ab18b0b..09d02a74c7 100644 --- a/Modules/Platform/Darwin-Initialize.cmake +++ b/Modules/Platform/Darwin-Initialize.cmake @@ -15,12 +15,6 @@ else() set(OSX_DEVELOPER_ROOT "") 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. set(CMAKE_OSX_ARCHITECTURES "$ENV{CMAKE_OSX_ARCHITECTURES}" CACHE STRING "Build architectures for OSX") @@ -49,15 +43,6 @@ endif() # Platform/Apple-${CMAKE_CXX_COMPILER_ID}- 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 @@ -296,3 +281,18 @@ if(NOT CMAKE_OSX_SYSROOT) /usr/local/lib ) 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() + +# 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() From 1a37c83c7e6b32cd474f903e93e7e2ce827cde09 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 21 Oct 2025 19:31:43 -0400 Subject: [PATCH 2/4] Xcode: Restore default CMAKE_OSX_DEPLOYMENT_TARGET to run on host Xcode by default targets the SDK's macOS version rather than the host's macOS version. In commit 7b19531291 (macOS: Do not pass any SDK/-isysroot to compilers by default, 2024-11-06, v4.0.0-rc1~511^2) we reverted commit 24aafbde11 (Xcode: Adjust deployment target SDK version to host version, 2015-10-11, v3.4.0-rc2~6^2), but it is still needed for Xcode. Restore the behavior so binaries run on the host by default. Fixes: #27309 --- Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst | 42 +++++++++++++++---- Modules/Platform/Darwin-Initialize.cmake | 36 ++++++++++++++-- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst b/Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst index 9df5edda64..ef10c75b32 100644 --- a/Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst +++ b/Help/variable/CMAKE_OSX_DEPLOYMENT_TARGET.rst @@ -1,15 +1,39 @@ 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. +Specify the minimum version of the target platform, e.g., macOS or iOS, +on which the target binaries are to be deployed. -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. +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:: CMAKE_OSX_VARIABLE.txt diff --git a/Modules/Platform/Darwin-Initialize.cmake b/Modules/Platform/Darwin-Initialize.cmake index 09d02a74c7..0c0a206ced 100644 --- a/Modules/Platform/Darwin-Initialize.cmake +++ b/Modules/Platform/Darwin-Initialize.cmake @@ -291,8 +291,38 @@ if(NOT CMAKE_CROSSCOMPILING) OUTPUT_STRIP_TRAILING_WHITESPACE) endif() -# 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 +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() From 884260ced6bc5748a552b8d7566cb91a8dd261f8 Mon Sep 17 00:00:00 2001 From: Brad King Date: Sun, 26 Oct 2025 11:17:44 -0400 Subject: [PATCH 3/4] Help: Add 4.0 release note for restored Xcode default deployment target --- Help/release/4.0.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Help/release/4.0.rst b/Help/release/4.0.rst index e66623aef2..0c7a011d4b 100644 --- a/Help/release/4.0.rst +++ b/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. Some implementation updates were made to support ecosystem changes 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. From de53a46a9194c81cd026436a9d11bf9256819160 Mon Sep 17 00:00:00 2001 From: Brad King Date: Sun, 26 Oct 2025 11:20:10 -0400 Subject: [PATCH 4/4] Help: Add 4.1 release note for restored Xcode default deployment target --- Help/release/4.1.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Help/release/4.1.rst b/Help/release/4.1.rst index a984269501..4b2dbdd767 100644 --- a/Help/release/4.1.rst +++ b/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 directory's ``CMakeLists.txt``, as all other generator expression 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.