From 02b86239db9400d7f5b1ab181fc699c59365233a Mon Sep 17 00:00:00 2001 From: Stefan Wunsch Date: Wed, 3 Mar 2021 18:35:19 +0100 Subject: [PATCH] [cmake] Enable native compilation on Apple M1 (#6152) --- ProcessorTargets.cmake | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ProcessorTargets.cmake b/ProcessorTargets.cmake index aa06b29ee..a752dceeb 100644 --- a/ProcessorTargets.cmake +++ b/ProcessorTargets.cmake @@ -6,7 +6,18 @@ set(PROC_TARGET_1_FLAGS "-mtune=generic" CACHE STRING "Processor-1 flags") # This second choice should be used for your own build only set(PROC_TARGET_2_LABEL native CACHE STRING "Processor-2 label - use it for your own build") -set(PROC_TARGET_2_FLAGS "-march=native" CACHE STRING "Processor-2 flags") + +# Get the architecture on an Apple system (x86 or arm64) +if(APPLE) + execute_process(COMMAND uname -m OUTPUT_VARIABLE APPLE_ARCHITECTURE OUTPUT_STRIP_TRAILING_WHITESPACE) +endif() + +# On Apple's M1 processor and with Apple's clang compiler, the native flag is different +if(CMAKE_CXX_COMPILER_ID STREQUAL AppleClang AND APPLE_ARCHITECTURE STREQUAL arm64) + set(PROC_TARGET_2_FLAGS "-mcpu=native" CACHE STRING "Processor-2 flags") +else() + set(PROC_TARGET_2_FLAGS "-march=native" CACHE STRING "Processor-2 flags") +endif() # The later choices is intended to be used if you want to provide specific builds, but it should match your own processor # You can cross compile but you have to know what you're doing, this mechanism has not been designed for that