Specifying CMAKE_OSX_SYSROOT breaks Xcode projects, but no other choice

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

# 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

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.

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.

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.

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)”;