Nessuna descrizione

TargetArch.cmake 5.5KB

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