How to set CMAKE_OSX_DEPLOYMENT_TARGET, conditionally?

Our project uses a single CMakeLists.txt file for both iOS and MacOS apps. We would like to set CMAKE_OSX_DEPLOYMENT_TARGET from the CMakeLists.txt file depending on the target OS(iOS, MacOS).

CMAKE_OSX_DEPLOYMENT_TARGET has to be set before project() call and CMAKE_SYSTEM_NAME would be undefined before project() call. Do we have any way to set CMAKE_OSX_DEPLOYMENT_TARGET conditionally?

cc: @RaisinTen @craig.scott for better reach

You could take advantage of the fact that CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET will override CMAKE_OSX_DEPLOYMENT_TARGET for iOS. Set the former to your iOS deployment target the latter to your macOS deployment target. In theory, I think that should work.

The IPHONEOS_DEPLOYMENT_TARGET is just one of the build settings Xcode supports. It also has MACOSX_DEPLOYMENT_TARGET, TVOS_DEPLOYMENT_TARGET, and so on. You should be able to set all of these using CMAKE_XCODE_ATTRIBUTE_... variables if you need different values for different target architectures. I believe they should all override whatever CMAKE_OSX_DEPLOYMENT_TARGET is set to, which you might not even have to set at all then.

1 Like