1
0

sdlcpu.cmake 5.1 KB

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