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

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