I wanted to report back on how I ended up implementing it. I am not saying it is the best way or the only way to do it, but it is the way that makes the most sense for my project/framework. I know that a solution proposed in another thread (and in the 8th edition of the book) is to use $(ARCHS_STANDARD)
which gets expanded by Xcode, but I don’t like it because the content of this variable is not available in CMake so I can’t act on it (printing a message, generating the zip archive name, etc…) (see below).
The full source is available on github.
The gist of it:
- allow to disable universal build if you don’t want it via an option
- run
uname -m
to determine the platform hence whether universal build is even an option - check whether the generator is Xcode otherwise you can’t do a universal build
- I also set a variable (
JAMBA_ARCHIVE_ARCHITECTURE
) representing the build architecture so that it is available in other places for example to determine the name of the zip file generated and also injected in a header in the code (#define). This would be impossible to do with$(ARCHS_STANDARD)
…
# Enable/Disable universal build on Apple Silicon
option(JAMBA_ENABLE_XCODE_UNIVERSAL_BUILD "Enable building x86/arm64 universal binary on Apple Silicon" ON)
# Deployment target/architectures for macOS
if(APPLE)
# set the deployment target if provided
if(JAMBA_MACOS_DEPLOYMENT_TARGET)
set(CMAKE_OSX_DEPLOYMENT_TARGET "${JAMBA_MACOS_DEPLOYMENT_TARGET}" CACHE STRING "")
endif()
# on macOS "uname -m" returns the architecture (x86_64 or arm64)
execute_process(
COMMAND uname -m
RESULT_VARIABLE result
OUTPUT_VARIABLE JAMBA_OSX_NATIVE_ARCHITECTURE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# determine if we do universal build or native build
if(JAMBA_ENABLE_XCODE_UNIVERSAL_BUILD # is universal build enabled?
AND (CMAKE_GENERATOR STREQUAL "Xcode") # works only with Xcode
AND (JAMBA_OSX_NATIVE_ARCHITECTURE STREQUAL "arm64")) # and only when running on arm64
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "")
set(JAMBA_ARCHIVE_ARCHITECTURE "macOS_universal")
message(STATUS "macOS universal (x86_64 / arm64) build")
else()
set(JAMBA_ARCHIVE_ARCHITECTURE "macOS_${JAMBA_OSX_NATIVE_ARCHITECTURE}")
message(STATUS "macOS native ${JAMBA_OSX_NATIVE_ARCHITECTURE} build")
endif()
elseif(WIN32)
# nothing special for windows
set(JAMBA_ARCHIVE_ARCHITECTURE "win_64bits")
message(STATUS "Windows native build")
endif()```