1
0

sdlcpu.cmake 5.4 KB

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