How to deal with CMake macOS Universal Binary AND target SDKs?

  apple-silicon, c++, cmake, macos

I have a C/C++ CMake based project, and this is my build configuration today

  • Mac mini with x86_64 arch
  • Big Sur 11.1
  • SDK 10.14 installed
  • 10.14 compiled libraries

So my project is built on this 11.1 targeting 10.14. To do it I added some specifics CMake commands like that

...
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14" CACHE STRING "Minimum OS X deployment version" FORCE)
set(CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk")

set(CMAKE_FIND_ROOT_PATH
    "/path/to/x86_64/10.14/library1"
    "/path/to/x86_64/10.14/library2"
    ...
)
...

And run it with cmake -G"Xcode" ....

And now, I’d like to do the same achievement on an ARM (Silicon) based Mac (with 11.2). I mean what I’d like to do first is building a project targeting x86_64 with 10.14 SDK. The second step is to build a Universal Binary running on a >=11.2 arm64 and on a >=10.14 x86_64 Mac.

It is possible? If yes, how?

My failures (trying the first step):

I tried to add these CMake commands with the previous ones

...
set(CMAKE_SYSTEM_PROCESSOR "x86_64")
set(CMAKE_OSX_ARCHITECTURES "x86_64")
...

But it fails with the CMake error The C++ compiler [...] is not able to compile a simple test program.

So after removing my CMAKE_OSX_SYSROOT it builds with -target arm64-apple-macos10.14 instead of x86_64-apple-macos10.14. And then fails to link, necessarily. How to solve it?

EDIT

Reading Apple’s documentation, it sounds like it’s possible to do to it by manually working on Makefiles like that:

x86_app: main.c
    $(CC) main.c -o x86_app -target x86_64-apple-macos10.12
arm_app: main.c
    $(CC) main.c -o arm_app -target arm64-apple-macos11
universal_app: x86_app arm_app
    lipo -create -output universal_app x86_app arm_app

But how can I tell CMake to do it?

Source: Windows Questions C++

LEAVE A COMMENT