ferencd@0: # https://github.com/petroules/solar-cmake ferencd@0: # ferencd@0: # Based on the Qt 5 processor detection code, so should be very accurate ferencd@0: # https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h ferencd@0: # Currently handles arm (v5, v6, v7), x86 (32/64), ia64, and ppc (32/64) ferencd@0: ferencd@0: # Regarding POWER/PowerPC, just as is noted in the Qt source, ferencd@0: # "There are many more known variants/revisions that we do not handle/detect." ferencd@0: ferencd@0: set(archdetect_c_code " ferencd@0: #if defined(__arm__) || defined(__TARGET_ARCH_ARM) ferencd@0: #if defined(__ARM_ARCH_7__) \\ ferencd@0: || defined(__ARM_ARCH_7A__) \\ ferencd@0: || defined(__ARM_ARCH_7R__) \\ ferencd@0: || defined(__ARM_ARCH_7M__) \\ ferencd@0: || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7) ferencd@0: #error cmake_ARCH armv7 ferencd@0: #elif defined(__ARM_ARCH_6__) \\ ferencd@0: || defined(__ARM_ARCH_6J__) \\ ferencd@0: || defined(__ARM_ARCH_6T2__) \\ ferencd@0: || defined(__ARM_ARCH_6Z__) \\ ferencd@0: || defined(__ARM_ARCH_6K__) \\ ferencd@0: || defined(__ARM_ARCH_6ZK__) \\ ferencd@0: || defined(__ARM_ARCH_6M__) \\ ferencd@0: || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6) ferencd@0: #error cmake_ARCH armv6 ferencd@0: #elif defined(__ARM_ARCH_5TEJ__) \\ ferencd@0: || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5) ferencd@0: #error cmake_ARCH armv5 ferencd@0: #else ferencd@0: #error cmake_ARCH arm ferencd@0: #endif ferencd@0: #elif defined(__i386) || defined(__i386__) || defined(_M_IX86) ferencd@0: #error cmake_ARCH i386 ferencd@0: #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64) ferencd@0: #error cmake_ARCH x86_64 ferencd@0: #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) ferencd@0: #error cmake_ARCH ia64 ferencd@0: #elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\ ferencd@0: || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \\ ferencd@0: || defined(_M_MPPC) || defined(_M_PPC) ferencd@0: #if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__) ferencd@0: #error cmake_ARCH ppc64 ferencd@0: #else ferencd@0: #error cmake_ARCH ppc ferencd@0: #endif ferencd@0: #endif ferencd@0: ferencd@0: #error cmake_ARCH unknown ferencd@0: ") ferencd@0: ferencd@0: # Set ppc_support to TRUE before including this file or ppc and ppc64 ferencd@0: # will be treated as invalid architectures since they are no longer supported by Apple ferencd@0: ferencd@0: function(target_architecture output_var) ferencd@0: if(APPLE AND CMAKE_OSX_ARCHITECTURES) ferencd@0: # On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set ferencd@0: # First let's normalize the order of the values ferencd@0: ferencd@0: # Note that it's not possible to compile PowerPC applications if you are using ferencd@0: # the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we ferencd@0: # disable it by default ferencd@0: # See this page for more information: ferencd@0: # http://stackoverflow.com/questions/5333490/how-can-we-restore-ppc-ppc64-as-well-as-full-10-4-10-5-sdk-support-to-xcode-4 ferencd@0: ferencd@0: # Architecture defaults to i386 or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime. ferencd@0: # On OS X 10.6+ the default is x86_64 if the CPU supports it, i386 otherwise. ferencd@0: ferencd@0: foreach(osx_arch ${CMAKE_OSX_ARCHITECTURES}) ferencd@0: if("${osx_arch}" STREQUAL "ppc" AND ppc_support) ferencd@0: set(osx_arch_ppc TRUE) ferencd@0: elseif("${osx_arch}" STREQUAL "i386") ferencd@0: set(osx_arch_i386 TRUE) ferencd@0: elseif("${osx_arch}" STREQUAL "x86_64") ferencd@0: set(osx_arch_x86_64 TRUE) ferencd@0: elseif("${osx_arch}" STREQUAL "ppc64" AND ppc_support) ferencd@0: set(osx_arch_ppc64 TRUE) ferencd@0: else() ferencd@0: message(FATAL_ERROR "Invalid OS X arch name: ${osx_arch}") ferencd@0: endif() ferencd@0: endforeach() ferencd@0: ferencd@0: # Now add all the architectures in our normalized order ferencd@0: if(osx_arch_ppc) ferencd@0: list(APPEND ARCH ppc) ferencd@0: endif() ferencd@0: ferencd@0: if(osx_arch_i386) ferencd@0: list(APPEND ARCH i386) ferencd@0: endif() ferencd@0: ferencd@0: if(osx_arch_x86_64) ferencd@0: list(APPEND ARCH x86_64) ferencd@0: endif() ferencd@0: ferencd@0: if(osx_arch_ppc64) ferencd@0: list(APPEND ARCH ppc64) ferencd@0: endif() ferencd@0: else() ferencd@0: file(WRITE "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/arch.c" "${archdetect_c_code}") ferencd@0: ferencd@0: enable_language(C) ferencd@0: ferencd@0: # Detect the architecture in a rather creative way... ferencd@0: # This compiles a small C program which is a series of ifdefs that selects a ferencd@0: # particular #error preprocessor directive whose message string contains the ferencd@0: # target architecture. The program will always fail to compile (both because ferencd@0: # file is not a valid C program, and obviously because of the presence of the ferencd@0: # #error preprocessor directives... but by exploiting the preprocessor in this ferencd@0: # way, we can detect the correct target architecture even when cross-compiling, ferencd@0: # since the program itself never needs to be run (only the compiler/preprocessor) ferencd@0: try_run( ferencd@0: run_result_unused ferencd@0: compile_result_unused ferencd@0: "${CMAKE_BINARY_DIR}" ferencd@0: "${CMAKE_BINARY_DIR}/${CMAKE_FILES_DIRECTORY}/arch.c" ferencd@0: COMPILE_OUTPUT_VARIABLE ARCH ferencd@0: CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} ferencd@0: ) ferencd@0: ferencd@0: # Parse the architecture name from the compiler output ferencd@0: string(REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH}") ferencd@0: ferencd@0: # Get rid of the value marker leaving just the architecture name ferencd@0: string(REPLACE "cmake_ARCH " "" ARCH "${ARCH}") ferencd@0: ferencd@0: # If we are compiling with an unknown architecture this variable should ferencd@0: # already be set to "unknown" but in the case that it's empty (i.e. due ferencd@0: # to a typo in the code), then set it to unknown ferencd@0: if (NOT ARCH) ferencd@0: set(ARCH unknown) ferencd@0: endif() ferencd@0: endif() ferencd@0: ferencd@0: set(${output_var} "${ARCH}" PARENT_SCOPE) ferencd@0: endfunction()