Home > Software engineering >  cmake add_compile_definitions for specific arch for xcode
cmake add_compile_definitions for specific arch for xcode

Time:05-21

When I use add_definitions(-DENABLE_SOMETHING) or add_compile_definitions(-DENABLE_SOMETHING) in CMake to generate a XCode Project, it will work for all archs in build setting.Just like below:enter image description here

How could I use add_definitions or add_compile_definitions for specific arch, like this: enter image description here

Because I want to define different macros for arm64 and armv7.

I tried to append or set xcode property XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[arch=arm64], but it will override the value generated before and only the new macro remained. enter image description here

I know there is a simple way to solve this by generating xcode project for two times with different arch, like this

cmake . -G Xcode -DARCHS="armv7"
cmake . -G Xcode -DARCHS="arm64"

but I wanna to know How to fix this avoid running the command two times.

CodePudding user response:

Answer my question. I gave up looking for a pure-cmake way to solve this, and using compiler(llvm or gcc) inside arch macro to figure out which arch it is.

    #if defined(__aarch64__) // arm64
    #define MACRO_FOR_ARM64
    #elif defined(__arm__) // armv7
    #define MACRO_FOR_ARMV7
    #endif

I defined this in a common header file and it will be included by any files I want to use the macro.

CodePudding user response:

Nice question! I think you're on the right track with using the XCODE_ATTRIBUTE_<an-attribute> target property. The documentation says it accepts generator expressions, so maybe you can do something like this?

# early on:
set_property(
  TARGET MyApp
  PROPERTY XCODE_ATTRIBUTE_GCC_PREPROCESSOR_DEFINITIONS[arch=arm64]
  "$<JOIN:$<TARGET_PROPERTY:MyApp,COMPILE_DEFINITIONS>;$<TARGET_PROPERTY:MyApp,ARM64_COMPILE_DEFINITIONS>, >"
)

# later on:
if (CMAKE_GENERATOR MATCHES "Xcode")
  set_property(TARGET MyApp APPEND PROPERTY ARM64_COMPILE_DEFINITIONS ENABLE_SOMETHING)
elseif (ARCHS MATCHES "arm64")
  target_compile_definitions(MyApp PRIVATE ENABLE_SOMETHING)
endif ()

The idea here being that you get what the usual compile definitions would be (via the generator expression), plus the definitions added to the custom property ARM64_COMPILE_DEFINITIONS.

  • Related