annotate cmake/TargetArch.cmake @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
rev   line source
ferencd@0 1 # Based on the Qt 5 processor detection code, so should be very accurate
ferencd@0 2 # https://qt.gitorious.org/qt/qtbase/blobs/master/src/corelib/global/qprocessordetection.h
ferencd@0 3 # Currently handles arm (v5, v6, v7), x86 (32/64), ia64, and ppc (32/64)
ferencd@0 4
ferencd@0 5 # Regarding POWER/PowerPC, just as is noted in the Qt source,
ferencd@0 6 # "There are many more known variants/revisions that we do not handle/detect."
ferencd@0 7
ferencd@0 8 set(archdetect_c_code "
ferencd@0 9 #if defined(__arm__) || defined(__TARGET_ARCH_ARM)
ferencd@0 10 #if defined(__ARM_ARCH_7__) \\
ferencd@0 11 || defined(__ARM_ARCH_7A__) \\
ferencd@0 12 || defined(__ARM_ARCH_7R__) \\
ferencd@0 13 || defined(__ARM_ARCH_7M__) \\
ferencd@0 14 || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 7)
ferencd@0 15 #error cmake_ARCH armv7
ferencd@0 16 #elif defined(__ARM_ARCH_6__) \\
ferencd@0 17 || defined(__ARM_ARCH_6J__) \\
ferencd@0 18 || defined(__ARM_ARCH_6T2__) \\
ferencd@0 19 || defined(__ARM_ARCH_6Z__) \\
ferencd@0 20 || defined(__ARM_ARCH_6K__) \\
ferencd@0 21 || defined(__ARM_ARCH_6ZK__) \\
ferencd@0 22 || defined(__ARM_ARCH_6M__) \\
ferencd@0 23 || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 6)
ferencd@0 24 #error cmake_ARCH armv6
ferencd@0 25 #elif defined(__ARM_ARCH_5TEJ__) \\
ferencd@0 26 || (defined(__TARGET_ARCH_ARM) && __TARGET_ARCH_ARM-0 >= 5)
ferencd@0 27 #error cmake_ARCH armv5
ferencd@0 28 #else
ferencd@0 29 #error cmake_ARCH arm
ferencd@0 30 #endif
ferencd@0 31 #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
ferencd@0 32 #error cmake_ARCH i386
ferencd@0 33 #elif defined(__x86_64) || defined(__x86_64__) || defined(__amd64) || defined(_M_X64)
ferencd@0 34 #error cmake_ARCH x86_64
ferencd@0 35 #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
ferencd@0 36 #error cmake_ARCH ia64
ferencd@0 37 #elif defined(__ppc__) || defined(__ppc) || defined(__powerpc__) \\
ferencd@0 38 || defined(_ARCH_COM) || defined(_ARCH_PWR) || defined(_ARCH_PPC) \\
ferencd@0 39 || defined(_M_MPPC) || defined(_M_PPC)
ferencd@0 40 #if defined(__ppc64__) || defined(__powerpc64__) || defined(__64BIT__)
ferencd@0 41 #error cmake_ARCH ppc64
ferencd@0 42 #else
ferencd@0 43 #error cmake_ARCH ppc
ferencd@0 44 #endif
ferencd@0 45 #endif
ferencd@0 46
ferencd@0 47 #error cmake_ARCH unknown
ferencd@0 48 ")
ferencd@0 49
ferencd@0 50 # Set ppc_support to TRUE before including this file or ppc and ppc64
ferencd@0 51 # will be treated as invalid architectures since they are no longer supported by Apple
ferencd@0 52
ferencd@0 53 function(target_architecture output_var)
ferencd@0 54 if(APPLE AND CMAKE_OSX_ARCHITECTURES)
ferencd@0 55 # On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set
ferencd@0 56 # First let's normalize the order of the values
ferencd@0 57
ferencd@0 58 # Note that it's not possible to compile PowerPC applications if you are using
ferencd@0 59 # the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we
ferencd@0 60 # disable it by default
ferencd@0 61 # See this page for more information:
ferencd@0 62 # 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 63
ferencd@0 64 # Architecture defaults to i386 or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime.
ferencd@0 65 # On OS X 10.6+ the default is x86_64 if the CPU supports it, i386 otherwise.
ferencd@0 66
ferencd@0 67 foreach(osx_arch ${CMAKE_OSX_ARCHITECTURES})
ferencd@0 68 if("${osx_arch}" STREQUAL "ppc" AND ppc_support)
ferencd@0 69 set(osx_arch_ppc TRUE)
ferencd@0 70 elseif("${osx_arch}" STREQUAL "i386")
ferencd@0 71 set(osx_arch_i386 TRUE)
ferencd@0 72 elseif("${osx_arch}" STREQUAL "x86_64")
ferencd@0 73 set(osx_arch_x86_64 TRUE)
ferencd@0 74 elseif("${osx_arch}" STREQUAL "ppc64" AND ppc_support)
ferencd@0 75 set(osx_arch_ppc64 TRUE)
ferencd@0 76 else()
ferencd@0 77 message(FATAL_ERROR "Invalid OS X arch name: ${osx_arch}")
ferencd@0 78 endif()
ferencd@0 79 endforeach()
ferencd@0 80
ferencd@0 81 # Now add all the architectures in our normalized order
ferencd@0 82 if(osx_arch_ppc)
ferencd@0 83 list(APPEND ARCH ppc)
ferencd@0 84 endif()
ferencd@0 85
ferencd@0 86 if(osx_arch_i386)
ferencd@0 87 list(APPEND ARCH i386)
ferencd@0 88 endif()
ferencd@0 89
ferencd@0 90 if(osx_arch_x86_64)
ferencd@0 91 list(APPEND ARCH x86_64)
ferencd@0 92 endif()
ferencd@0 93
ferencd@0 94 if(osx_arch_ppc64)
ferencd@0 95 list(APPEND ARCH ppc64)
ferencd@0 96 endif()
ferencd@0 97 else()
ferencd@0 98 file(WRITE "${CMAKE_BINARY_DIR}/arch.c" "${archdetect_c_code}")
ferencd@0 99
ferencd@0 100 enable_language(C)
ferencd@0 101
ferencd@0 102 # Detect the architecture in a rather creative way...
ferencd@0 103 # This compiles a small C program which is a series of ifdefs that selects a
ferencd@0 104 # particular #error preprocessor directive whose message string contains the
ferencd@0 105 # target architecture. The program will always fail to compile (both because
ferencd@0 106 # file is not a valid C program, and obviously because of the presence of the
ferencd@0 107 # #error preprocessor directives... but by exploiting the preprocessor in this
ferencd@0 108 # way, we can detect the correct target architecture even when cross-compiling,
ferencd@0 109 # since the program itself never needs to be run (only the compiler/preprocessor)
ferencd@0 110 try_run(
ferencd@0 111 run_result_unused
ferencd@0 112 compile_result_unused
ferencd@0 113 "${CMAKE_BINARY_DIR}"
ferencd@0 114 "${CMAKE_BINARY_DIR}/arch.c"
ferencd@0 115 COMPILE_OUTPUT_VARIABLE ARCH
ferencd@0 116 CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
ferencd@0 117 )
ferencd@0 118
ferencd@0 119 # Parse the architecture name from the compiler output
ferencd@0 120 string(REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH}")
ferencd@0 121
ferencd@0 122 # Get rid of the value marker leaving just the architecture name
ferencd@0 123 string(REPLACE "cmake_ARCH " "" ARCH "${ARCH}")
ferencd@0 124
ferencd@0 125 # If we are compiling with an unknown architecture this variable should
ferencd@0 126 # already be set to "unknown" but in the case that it's empty (i.e. due
ferencd@0 127 # to a typo in the code), then set it to unknown
ferencd@0 128 if (NOT ARCH)
ferencd@0 129 set(ARCH unknown)
ferencd@0 130 endif()
ferencd@0 131 endif()
ferencd@0 132
ferencd@0 133 set(${output_var} "${ARCH}" PARENT_SCOPE)
ferencd@0 134 endfunction()