You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FindArch.cmake 5.0KB

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