sdlcpu.cmake 5.3 KB

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