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.1KB

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. #elif defined(__loongarch__) || defined(__loongarch64)
  39. #error cmake_ARCH loongarch64
  40. #endif
  41. #error cmake_ARCH unknown
  42. ")
  43. # Set ppc_support to TRUE before including this file or ppc and ppc64
  44. # will be treated as invalid architectures since they are no longer supported by Apple
  45. function(target_architecture output_var)
  46. if(APPLE AND CMAKE_OSX_ARCHITECTURES)
  47. # On OS X we use CMAKE_OSX_ARCHITECTURES *if* it was set
  48. # First let's normalize the order of the values
  49. # Note that it's not possible to compile PowerPC applications if you are using
  50. # the OS X SDK version 10.6 or later - you'll need 10.4/10.5 for that, so we
  51. # disable it by default
  52. # See this page for more information:
  53. # http://stackoverflow.com/questions/5333490/how-can-we-restore-ppc-ppc64-as-well-as-full-10-4-10-5-sdk-support-to-xcode-4
  54. # Architecture defaults to i386 or ppc on OS X 10.5 and earlier, depending on the CPU type detected at runtime.
  55. # On OS X 10.6+ the default is x86_64 if the CPU supports it, i386 otherwise.
  56. foreach(osx_arch ${CMAKE_OSX_ARCHITECTURES})
  57. if("${osx_arch}" STREQUAL "ppc" AND ppc_support)
  58. set(osx_arch_ppc TRUE)
  59. elseif("${osx_arch}" STREQUAL "i386")
  60. set(osx_arch_i386 TRUE)
  61. elseif("${osx_arch}" STREQUAL "x86_64")
  62. set(osx_arch_x86_64 TRUE)
  63. elseif("${osx_arch}" STREQUAL "ppc64" AND ppc_support)
  64. set(osx_arch_ppc64 TRUE)
  65. else()
  66. message(FATAL_ERROR "Invalid OS X arch name: ${osx_arch}")
  67. endif()
  68. endforeach()
  69. # Now add all the architectures in our normalized order
  70. if(osx_arch_ppc)
  71. list(APPEND ARCH ppc)
  72. endif()
  73. if(osx_arch_i386)
  74. list(APPEND ARCH i386)
  75. endif()
  76. if(osx_arch_x86_64)
  77. list(APPEND ARCH x86_64)
  78. endif()
  79. if(osx_arch_ppc64)
  80. list(APPEND ARCH ppc64)
  81. endif()
  82. else()
  83. file(WRITE "${CMAKE_BINARY_DIR}/arch.c" "${archdetect_c_code}")
  84. # Detect the architecture in a rather creative way...
  85. # This compiles a small C program which is a series of ifdefs that selects a
  86. # particular #error preprocessor directive whose message string contains the
  87. # target architecture. The program will always fail to compile (both because
  88. # file is not a valid C program, and obviously because of the presence of the
  89. # #error preprocessor directives... but by exploiting the preprocessor in this
  90. # way, we can detect the correct target architecture even when cross-compiling,
  91. # since the program itself never needs to be run (only the compiler/preprocessor)
  92. try_run(
  93. run_result_unused
  94. compile_result_unused
  95. "${CMAKE_BINARY_DIR}"
  96. "${CMAKE_BINARY_DIR}/arch.c"
  97. COMPILE_OUTPUT_VARIABLE ARCH
  98. CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
  99. )
  100. # Parse the architecture name from the compiler output
  101. string(REGEX MATCH "cmake_ARCH ([a-zA-Z0-9_]+)" ARCH "${ARCH}")
  102. # Get rid of the value marker leaving just the architecture name
  103. string(REPLACE "cmake_ARCH " "" ARCH "${ARCH}")
  104. # If we are compiling with an unknown architecture this variable should
  105. # already be set to "unknown" but in the case that it's empty (i.e. due
  106. # to a typo in the code), then set it to unknown
  107. if (NOT ARCH)
  108. set(ARCH unknown)
  109. endif()
  110. endif()
  111. set(${output_var} "${ARCH}" PARENT_SCOPE)
  112. endfunction()