sdlcpu.cmake 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS)
  2. set(DETECTABLE_ARCHS ARM32 ARM64 EMSCRIPTEN LOONGARCH64 POWERPC32 POWERPC64 X86 X64)
  3. if(APPLE AND CMAKE_OSX_ARCHITECTURES)
  4. foreach(arch IN LISTS DETECTABLE_ARCHS)
  5. set(SDL_CPU_${arch} "0")
  6. endforeach()
  7. set(detected_archs)
  8. foreach(osx_arch IN LISTS CMAKE_OSX_ARCHITECTURES)
  9. if(osx_arch STREQUAL "x86_64")
  10. set(SDL_CPU_X64 "1")
  11. list(APPEND detected_archs "X64")
  12. elseif(osx_arch STREQUAL "arm64")
  13. set(SDL_CPU_ARM64 "1")
  14. list(APPEND detected_archs "ARM64")
  15. endif()
  16. endforeach()
  17. set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
  18. return()
  19. endif()
  20. set(detected_archs)
  21. foreach(arch IN LISTS DETECTABLE_ARCHS)
  22. if(SDL_CPU_${arch})
  23. list(APPEND detected_archs "${arch}")
  24. endif()
  25. endforeach()
  26. if(detected_archs)
  27. set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
  28. return()
  29. endif()
  30. set(src_arch_detect [=====[
  31. #if defined(__arm__) || defined(_M_ARM)
  32. #define ARCH_ARM32 "1"
  33. #else
  34. #define ARCH_ARM32 "0"
  35. #endif
  36. const char *arch_arm32 = "INFO<ARM32=" ARCH_ARM32 ">";
  37. #if defined(__aarch64__) || defined(_M_ARM64)
  38. #define ARCH_ARM64 "1"
  39. #else
  40. #define ARCH_ARM64 "0"
  41. #endif
  42. const char *arch_arm64 = "INFO<ARM64=" ARCH_ARM64 ">";
  43. #if defined(__EMSCRIPTEN__)
  44. #define ARCH_EMSCRIPTEN "1"
  45. #else
  46. #define ARCH_EMSCRIPTEN "0"
  47. #endif
  48. const char *arch_emscripten = "INFO<EMSCRIPTEN=" ARCH_EMSCRIPTEN ">";
  49. #if defined(__loongarch64)
  50. #define ARCH_LOONGARCH64 "1"
  51. #else
  52. #define ARCH_LOONGARCH64 "0"
  53. #endif
  54. const char *arch_loongarch64 = "INFO<LOONGARCH64=" ARCH_LOONGARCH64 ">";
  55. #if (defined(__PPC__) || defined(__powerpc__)) && !defined(__powerpc64__)
  56. #define ARCH_POWERPC32 "1"
  57. #else
  58. #define ARCH_POWERPC32 "0"
  59. #endif
  60. const char *arch_powerpc32 = "INFO<ARCH_POWERPC32=" ARCH_POWERPC32 ">";
  61. #if defined(__PPC64__) || defined(__powerpc64__)
  62. #define ARCH_POWERPC64 "1"
  63. #else
  64. #define ARCH_POWERPC64 "0"
  65. #endif
  66. const char *arch_powerpc64 = "INFO<POWERPC64=" ARCH_POWERPC64 ">";
  67. #if defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)
  68. #define ARCH_X86 "1"
  69. #else
  70. #define ARCH_X86 "0"
  71. #endif
  72. const char *arch_x86 = "INFO<X86=" ARCH_X86 ">";
  73. #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
  74. #define ARCH_X64 "1"
  75. #else
  76. #define ARCH_X64 "0"
  77. #endif
  78. const char *arch_x64 = "INFO<X64=" ARCH_X64 ">";
  79. int main(int argc, char *argv[]) {
  80. (void) argv;
  81. int result = argc;
  82. result += arch_arm32[argc];
  83. result += arch_arm64[argc];
  84. result += arch_emscripten[argc];
  85. result += arch_loongarch64[argc];
  86. result += arch_powerpc32[argc];
  87. result += arch_powerpc64[argc];
  88. result += arch_x86[argc];
  89. result += arch_x64[argc];
  90. return result;
  91. }
  92. ]=====])
  93. set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch.c")
  94. file(WRITE "${path_src_arch_detect}" "${src_arch_detect}")
  95. set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch")
  96. set(path_bin_arch_detect "${path_dir_arch_detect}/bin")
  97. set(msg "Detecting Target CPU Architecture")
  98. message(STATUS "${msg}")
  99. try_compile(COMPILED_RES
  100. "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch"
  101. SOURCES "${path_src_arch_detect}"
  102. COPY_FILE "${path_bin_arch_detect}"
  103. )
  104. if(NOT COMPILED_RES)
  105. message(STATUS "${msg} - <ERROR>")
  106. message(WARNING "Failed to compile source detecting the target CPU architecture")
  107. endif()
  108. set(re "INFO<([A-Z0-9]+)=([01])>")
  109. file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}")
  110. set(detected_archs)
  111. foreach(info_arch_01 IN LISTS infos)
  112. string(REGEX MATCH "${re}" A "${info_arch_01}")
  113. if(NOT "${CMAKE_MATCH_1}" IN_LIST DETECTABLE_ARCHS)
  114. message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"")
  115. continue()
  116. endif()
  117. set(arch "${CMAKE_MATCH_1}")
  118. set(arch_01 "${CMAKE_MATCH_2}")
  119. if(arch_01)
  120. list(APPEND detected_archs "${arch}")
  121. endif()
  122. set("SDL_CPU_${arch}" "${arch_01}" CACHE BOOL "Detected architecture ${arch}")
  123. endforeach()
  124. message(STATUS "${msg} - ${detected_archs}")
  125. set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
  126. endfunction()