CMake Universal Binary - arch depending compile options

Viewed 788

I'm building a MacOS Universal Binary using set (CMAKE_OSX_ARCHITECTURES arm64 x86_64) - working fine so far.

But x86_64 should have other compile options than arm64.

Is there something like:

if (CURRENT_TARGET MATCHES x86_64)
    add_compile_options (-Wall -Ofast -ffast-math -fno-exceptions)
else()
    add_compile_options (-Wall -O3 -fno-exceptions)
endif()

Or any other solution?

1 Answers

You can do this via the XCODE_ATTRIBUTE_* property, though this will unfortunately tie your project to XCode. If you have a target my_target to which you'd like to add multi-arch-specific flags, you would write:

cmake_minimum_required(VERSION 3.20)
project(example)

add_executable(my_target main.cpp)
set_target_properties(
  my_target
  PROPERTIES
    XCODE_ATTRIBUTE_PER_ARCH_CFLAGS_x86_64 "-DBUILDING_FOR_X86_64"
    XCODE_ATTRIBUTE_PER_ARCH_CFLAGS_arm64 "-DBUILDING_FOR_ARM64"
)

Then you would build with:

$ cmake -G Xcode -S . -B build -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64"
$ cmake --build build --config Release
Related