# Specifying CMAKE\_OSX\_SYSROOT breaks Xcode projects, but no other choice

**URL:** https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532
**Category:** Usage
**Tags:** os:macos
**Created:** [January 13, 2021, 7:50am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532 "2021-01-13T07:50:30Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![alecazam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alecazam/32/1099_2.png) [@alecazam](https://discourse.cmake.org/u/alecazam)
#### Post date: [January 13, 2021, 7:50am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/1 "2021-01-13T07:50:30Z")

</div>

If you don’t specify CMAKE\_OSX\_SYSROOT, then CMake seems to default to some invalid SDK, or uses the DEPLOYMENT\_TARGET which is far too low for the SDK. SYSROOT is effectively, the maximum build of macOS that the app supports. Anything higher should be fine.

So then I specify the build number (see below), and that works on my machine. One another machine, the newer Xcode breaks. I don’t want devs to have to chase changing this, and CMake really really breaks when it can’t find the SDK and declares invalid C and CXX and OBJ\_CXX compilers instead of just stating that it couldn’t find the SDK.

What is the cmake specification for use whatever is the available SDK in Xcode? Also setting ARCHS\_STANDARD doesnt work. I’m on CMake 3.19.1 on macOS Intel and ARM.

Snippet from CMakeLists.txt

```auto
# Xcode 12.2 ships with macosx11.0, but 12.3 ships with macosx11.1
# Xcode 12.3 completely breaks when this is set and can't find c compilers.
set(CMAKE_OSX_SYSROOT macosx11.0) # this only works, finds macosx11.0.sdk in Xcode 12.2
# set(CMAKE_OSX_SYSROOT macos) # this doesn't work, despite there being a macos.sdk

set(CMAKE_OSX_DEPLOYMENT_TARGET 10.14) # this works, great!
set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_STANDARD)") <- this doesn't work either

```

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [January 13, 2021, 8:36am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/2 "2021-01-13T08:36:08Z")

</div>

> [@alecazam](#):
>
> If you don’t specify CMAKE\_OSX\_SYSROOT, then CMake seems to default to some invalid SDK

This is not expected. Could you please share the error that you get when not specifying `CMAKE_OSX_SYSROOT` at all? You should most likely try that in a fresh build directory, since `CMAKE_OSX_SYSROOT` might be cached.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 13, 2021, 11:24am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/3 "2021-01-13T11:24:05Z")

</div>

Don’t specify the version number of the sdk. In fact, for macOS, don’t specify the SDK at all and it will automatically select the default SDK for the macOS platform. You _should_ still specify the minimum deployment version though (using `CMAKE_OSX_DEPLOYMENT_TARGET`) - this is the minimum macOS version you want to support your application running on and is (mostly) independent of the SDK you use (assuming your SDK is new enough to know about your selected minimum deployment version).

If you are working with Apple Silicon, I recommend CMake 3.19.2 and Xcode 12.2 as your minimum versions. Earlier versions are likely to have problems in some areas.

---

<div class="post-metadata">

### Author: ![alecazam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alecazam/32/1099_2.png) [@alecazam](https://discourse.cmake.org/u/alecazam)
#### Post date: [January 14, 2021, 4:53am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/4 "2021-01-14T04:53:50Z")

</div>

When I leave that out, this is what I see in Xcode 12.2 and CMake 3.19.3 (I upgraded). The build succeeds, but the XCode project is left with and invalid SDK set. I know cmake has it’s own build settings, but I like to see the Xcode project reflect correct data. When SYSROOT is set, then this field is set to a valid value. Without it, it’s not.

 ![image](https://discourse.cmake.org/uploads/default/original/2X/b/b8c29d0dd4b7951991452291d5cc0d3e6f2a2f83.png)

I guess a request is something like DEPLOYMENT\_TARGET. Have a setting like SDK\_TARGET=11.0 that says that I need 11.0 SDK or higher, fail if it’s not set. That’s way safer than what happens when SYSROOT is set, the sdk not found, and CMake fails to even gen a project. For CMake newbies, this doesn’t leave them with much to work with since they have to then understand CMake internals and debugging from the command line.

This is the symlink path on my machine to MacOSX.sdk, but I think Xcode just wants macosx11.0 for that field, not a full path as specified by CMake when SYSROOT isn’t specified.  
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk

When I set the project “Base SDK” to macOS, then the project reflects this, but I’m unable to set SYSROOT to that. I can only set macosx11.0 so that CMake I guess can look it up. This should likely be the default that says “use the latest sdk”.

SDKROOT = macosx;

I also don’t understand why ARCHS is never set to $(ARCHS\_STANDARD), despite the set call. My project only says this, and then again I have to manually set the value in Xcode.

ARCHS = “$(NATIVE\_ARCH\_ACTUAL)”;

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 22, 2021, 4:54am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/5 "2021-01-22T04:54:38Z")

</div>

You seem to be confusing the SDK version with the deployment version, at least partially. They are two distinct things and should not be specified via a single variable.

A minimum SDK version basically corresponds to a minimum Xcode version. You get one SDK with an Xcode installation and you shouldn’t really ever have to specify the SDK version. If your project requires some minimum SDK version to build, it would be better to advertise that as some minimum Xcode version (my personal view). Way back in the past, people sometimes liked to copy forward an SDK from an older Xcode to a newer Xcode, but that has never been technically supported and is something that shouldn’t be done these days (there’s never been any guarantee that this would work and things could silently go wrong).

The deployment version is specified separately by the `CMAKE_OSX_DEPLOYMENT_TARGET`, which must be set _before_ the first call to `project()` and it must be a cache variable. Your code sample sets it as an ordinary variable, so it will be discarded and replaced by the default (this may well be the source of your problems). The SDK is controlled by `CMAKE_OSX_SYSROOT` and should be left empty for macOS builds. I think there’s room for improvement in the way CMake handles the SDK setting, it should probably put `macosx` (or whatever the relevant value is) instead of the full path to the SDK in the relevant field of the Xcode project file, but I think that might require non-trivial changes to CMake due to the baked-in assumption about `CMAKE_OSX_SYSROOT` holding the full path to the SDK in some places. This is the cause of why you see that (harmless) warning in recent Xcode versions.

> [@alecazam](#):
>
> I also don’t understand why ARCHS is never set to $(ARCHS\_STANDARD), despite the set call. My project only says this, and then again I have to manually set the value in Xcode.
> 
> ARCHS = “$(NATIVE\_ARCH\_ACTUAL)”;

The default is `$(NATIVE_ARCH_ACTUAL)` so as to preserve backward compatible behavior where the build defaulted to a single architecture. This has been a fairly messy area in bringing the support for Big Sur to CMake and I’m not sure we’ve got it right, but we can’t really change that default now. I would recommend projects (maybe more toolchain files) explicitly set `CMAKE_OSX_ARCHITECTURES` to `$(ARCHS_STANDARD)` so that they can build universal binaries if they want. During development though, I would still expect `$(NATIVE_ARCH_ACTUAL)` to work and this would be more efficient (since you’d only be building for one architecture all the time).

If you are trying to set the architecture, perhaps you are setting in the wrong place. Your code sample doesn’t show where you are trying to set those variables. FWIW, I’d expect the following command line to be all you need for a macOS build that produces universal binaries with the default SDK (i.e. no need to specify anything other than `CMAKE_OSX_DEPLOYMENT_TARGET` in your project):

```auto
cmake -G Xcode -D "CMAKE_OSX_ARCHITECTURES:STRING=$(ARCHS_STANDARD)" path/to/source

```

---

<div class="post-metadata">

### Author: ![alecazam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alecazam/32/1099_2.png) [@alecazam](https://discourse.cmake.org/u/alecazam)
#### Post date: [January 22, 2021, 8:07pm UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/6 "2021-01-22T20:07:03Z")

</div>

Thanks for all the responses, I really appreciate them. I worked with and for Apple for a few years, so I know a little about working with Xcode. The issue I raised above is that there’s no good way currently to specify CMAKE\_OS\_SYSROOT that doesn’t break projects and in a very bad way for devs that get a CMake script with this set. On my recommendations on how to fix this, the response I’ve been seeing is “just don’t specify that”. But that’s not really a solution to the described problem.

My suggestion was that a variable similar to DEPLOYMENT\_TARGET is needed for SDK, let’s call it SDK\_TARGET. That would indicate in a script that the app that I’ve released needs at least an SDK of 11.0 or higher. A newer Xcode will have SDK 11.1+ and with SYSROOT=11.0 doing an == comparison, the CMake project generation breaks.

What I really am indicating is the minimum SDK requirement. DEPLOYMENT\_TARGET is the minimum OS that the app will run on, but it will only build on SDK 11.0 or higher. There’s currently no search in CMAKE if SYSROOT is set for a different SDK, or a way to enforce == or \>= to that SDK. But that’s what SDK\_TARGET could do, or possibly SYSROOT could be fixed.

My sample app is at [https://github.com/alecazam/kram](https://github.com/alecazam/kram). It’s a texture encoder and viewer. I think having sample CMake scripts that build a macOS or iOS app in the CMake repository would be a big help, but maybe I just haven’t found those. I typically have to piece all this together from various quality StackOverflow responses, but I think the CMakeLists.txt files I have are pretty solid now.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 23, 2021, 4:51am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/7 "2021-01-23T04:51:33Z")

</div>

> [@alecazam](#):
>
> There’s currently no search in CMAKE if SYSROOT is set for a different SDK, or a way to enforce == or \>= to that SDK.

I acknowledge your suggestion. CMake doesn’t in general provide direct support for enforcing a minimum compiler version for any compiler or platform that I’m aware of, but it gives the information required for you to check and enforce that yourself. For example:

```cmake
# Only supports Xcode generator
set(minXcodeVersion 11.0)
if(XCODE AND XCODE_VERSION VERSION_LESS minXcodeVersion)
    message(FATAL_ERROR "This project requires at least Xcode ${minXcodeVersion}")
endif()

# Should work for all generators but meaning of version number may be less clear.
# Replace CXX with whatever language is relevant for your project.
set(minAppleClangVersion 11.0.3.11030032) # This would be Xcode 11.7!
if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND
   CMAKE_CXX_COMPILER_VERSION VERSION_LESS minAppleClangVersion)
    message(FATAL_ERROR "This project requires at least Apple Clang ${minAppleClangVersion}")
endif()

```

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [January 23, 2021, 9:18am UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/8 "2021-01-23T09:18:39Z")

</div>

I would write something like this:

```auto
project(...)

if(APPLE)
  if(NOT DEFINED CMAKE_OSX_SYSROOT)
    message(FATAL_ERROR "Cannot check SDK version if CMAKE_OSX_SYSROOT"
      " is not defined."
    )
  endif()

  execute_process(
    COMMAND xcrun --sdk "${CMAKE_OSX_SYSROOT}" --show-sdk-version
    OUTPUT_VARIABLE sdk_version
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )

  if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
    if(sdk_version VERSION_LESS 14.2)
      message(FATAL_ERROR "This project requires at least iPhoneOS SDK"
        " 14.2, but got ${sdk_version} instead."
      )
    endif()
  else()
    if(sdk_version VERSION_LESS 11.0)
      message(FATAL_ERROR "This project requires at least macOS SDK"
        " 11.0, but got ${sdk_version} instead."
      )
    endif()
  endif()
endif()

```

---

<div class="post-metadata">

### Author: ![alecazam](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alecazam/32/1099_2.png) [@alecazam](https://discourse.cmake.org/u/alecazam)
#### Post date: [February 21, 2021, 7:26pm UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/9 "2021-02-21T19:26:04Z")

</div>

Hi Vivian,

I added your snippet, so thanks. I was hoping for a test prior to “project” failing if CMAKE\_OSX\_SYSROOT was set, but this has to be set after the project command has executed (and broken cmake). One just can’t set CMAKE\_OSX\_SYSROOT safely when sharing projects or it breaks on Xcode updates.

---

<div class="post-metadata">

### Author: ![dkgh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/96bed5/32.png) [@dkgh](https://discourse.cmake.org/u/dkgh)
#### Post date: [January 24, 2024, 11:40pm UTC](https://discourse.cmake.org/t/specifying-cmake-osx-sysroot-breaks-xcode-projects-but-no-other-choice/2532/10 "2024-01-24T23:40:15Z")

</div>

Is there a workaround for this?

I also see that when `CMAKE_OSX_SYSROOT` is unset, Xcode shows an invalid `Base SDK` with a “SDK not found” tag.
