sdlcpu.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS)
  2. set(known_archs EMSCRIPTEN ARM32 ARM64 LOONGARCH64 POWERPC32 POWERPC64 X86 X64)
  3. if(APPLE AND CMAKE_OSX_ARCHITECTURES)
  4. foreach(known_arch IN LISTS known_archs)
  5. set(SDL_CPU_${known_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(known_arch IN LISTS known_archs)
  22. if(SDL_CPU_${known_arch})
  23. list(APPEND detected_archs "${known_arch}")
  24. endif()
  25. endforeach()
  26. if(detected_archs)
  27. set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
  28. return()
  29. endif()
  30. set(arch_check_ARM32 "defined(__arm__) || defined(_M_ARM)")
  31. set(arch_check_ARM64 "defined(__aarch64__) || defined(_M_ARM64)")
  32. set(arch_check_EMSCRIPTEN "defined(__EMSCRIPTEN__)")
  33. set(arch_check_LOONGARCH64 "defined(__loongarch64)")
  34. set(arch_check_POWERPC32 "(defined(__PPC__) || defined(__powerpc__)) && !defined(__powerpc64__)")
  35. set(arch_check_POWERPC64 "defined(__PPC64__) || defined(__powerpc64__)")
  36. set(arch_check_X86 "defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)")
  37. set(arch_check_X64 "defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)")
  38. set(src_vars "")
  39. set(src_main "")
  40. foreach(known_arch IN LISTS known_archs)
  41. set(detected_${known_arch} "0")
  42. string(APPEND src_vars "
  43. #if ${arch_check_${known_arch}}
  44. #define ARCH_${known_arch} \"1\"
  45. #else
  46. #define ARCH_${known_arch} \"0\"
  47. #endif
  48. const char *arch_${known_arch} = \"INFO<${known_arch}=\" ARCH_${known_arch} \">\";
  49. ")
  50. string(APPEND src_main "
  51. result += arch_${known_arch}[argc];")
  52. endforeach()
  53. set(src_arch_detect "${src_vars}
  54. int main(int argc, char *argv[]) {
  55. (void)argv;
  56. int result = 0;
  57. ${src_main}
  58. return result;
  59. }")
  60. set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch.c")
  61. file(WRITE "${path_src_arch_detect}" "${src_arch_detect}")
  62. set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch")
  63. set(path_bin_arch_detect "${path_dir_arch_detect}/bin")
  64. set(detected_archs)
  65. set(msg "Detecting Target CPU Architecture")
  66. message(STATUS "${msg}")
  67. include(CMakePushCheckState)
  68. cmake_push_check_state(RESET)
  69. try_compile(SDL_CPU_CHECK_ALL
  70. "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch"
  71. SOURCES "${path_src_arch_detect}"
  72. COPY_FILE "${path_bin_arch_detect}"
  73. )
  74. cmake_pop_check_state()
  75. if(NOT SDL_CPU_CHECK_ALL)
  76. message(STATUS "${msg} - <ERROR>")
  77. message(WARNING "Failed to compile source detecting the target CPU architecture")
  78. else()
  79. set(re "INFO<([A-Z0-9]+)=([01])>")
  80. file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}")
  81. foreach(info_arch_01 IN LISTS infos)
  82. string(REGEX MATCH "${re}" A "${info_arch_01}")
  83. if(NOT "${CMAKE_MATCH_1}" IN_LIST known_archs)
  84. message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"")
  85. continue()
  86. endif()
  87. set(arch "${CMAKE_MATCH_1}")
  88. set(arch_01 "${CMAKE_MATCH_2}")
  89. set(detected_${arch} "${arch_01}")
  90. endforeach()
  91. foreach(known_arch IN LISTS known_archs)
  92. if(detected_${known_arch})
  93. list(APPEND detected_archs ${known_arch})
  94. endif()
  95. endforeach()
  96. endif()
  97. if(detected_archs)
  98. foreach(known_arch IN LISTS known_archs)
  99. set("SDL_CPU_${known_arch}" "${detected_${known_arch}}" CACHE BOOL "Detected architecture ${known_arch}")
  100. endforeach()
  101. message(STATUS "${msg} - ${detected_archs}")
  102. else()
  103. include(CheckCSourceCompiles)
  104. cmake_push_check_state(RESET)
  105. foreach(known_arch IN LISTS known_archs)
  106. if(NOT detected_archs)
  107. set(cache_variable "SDL_CPU_${known_arch}")
  108. set(test_src "
  109. int main(int argc, char *argv[]) {
  110. #if ${arch_check_${known_arch}}
  111. return 0;
  112. #else
  113. choke
  114. #endif
  115. }
  116. ")
  117. check_c_source_compiles("${test_src}" "${cache_variable}")
  118. if(${cache_variable})
  119. set(SDL_CPU_${known_arch} "1" CACHE BOOL "Detected architecture ${known_arch}")
  120. set(detected_archs ${known_arch})
  121. else()
  122. set(SDL_CPU_${known_arch} "0" CACHE BOOL "Detected architecture ${known_arch}")
  123. endif()
  124. endif()
  125. endforeach()
  126. cmake_pop_check_state()
  127. endif()
  128. set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
  129. endfunction()