sdlcpu.cmake 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. (void)argv;
  57. int result = 0;
  58. ${src_main}
  59. return result;
  60. }")
  61. set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch.c")
  62. file(WRITE "${path_src_arch_detect}" "${src_arch_detect}")
  63. set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch")
  64. set(path_bin_arch_detect "${path_dir_arch_detect}/bin")
  65. set(detected_archs)
  66. set(msg "Detecting Target CPU Architecture")
  67. message(STATUS "${msg}")
  68. include(CMakePushCheckState)
  69. set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
  70. cmake_push_check_state(RESET)
  71. try_compile(SDL_CPU_CHECK_ALL
  72. "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch"
  73. SOURCES "${path_src_arch_detect}"
  74. COPY_FILE "${path_bin_arch_detect}"
  75. )
  76. cmake_pop_check_state()
  77. if(NOT SDL_CPU_CHECK_ALL)
  78. message(STATUS "${msg} - <ERROR>")
  79. message(WARNING "Failed to compile source detecting the target CPU architecture")
  80. else()
  81. set(re "INFO<([A-Z0-9]+)=([01])>")
  82. file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}")
  83. foreach(info_arch_01 IN LISTS infos)
  84. string(REGEX MATCH "${re}" A "${info_arch_01}")
  85. if(NOT "${CMAKE_MATCH_1}" IN_LIST known_archs)
  86. message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"")
  87. continue()
  88. endif()
  89. set(arch "${CMAKE_MATCH_1}")
  90. set(arch_01 "${CMAKE_MATCH_2}")
  91. set(detected_${arch} "${arch_01}")
  92. endforeach()
  93. foreach(known_arch IN LISTS known_archs)
  94. if(detected_${known_arch})
  95. list(APPEND detected_archs ${known_arch})
  96. endif()
  97. endforeach()
  98. endif()
  99. if(detected_archs)
  100. foreach(known_arch IN LISTS known_archs)
  101. set("SDL_CPU_${known_arch}" "${detected_${known_arch}}" CACHE BOOL "Detected architecture ${known_arch}")
  102. endforeach()
  103. message(STATUS "${msg} - ${detected_archs}")
  104. else()
  105. include(CheckCSourceCompiles)
  106. cmake_push_check_state(RESET)
  107. foreach(known_arch IN LISTS known_archs)
  108. if(NOT detected_archs)
  109. set(cache_variable "SDL_CPU_${known_arch}")
  110. set(test_src "
  111. int main(int argc, char *argv[]) {
  112. #if ${arch_check_${known_arch}}
  113. return 0;
  114. #else
  115. choke
  116. #endif
  117. }
  118. ")
  119. check_c_source_compiles("${test_src}" "${cache_variable}")
  120. if(${cache_variable})
  121. set(SDL_CPU_${known_arch} "1" CACHE BOOL "Detected architecture ${known_arch}")
  122. set(detected_archs ${known_arch})
  123. else()
  124. set(SDL_CPU_${known_arch} "0" CACHE BOOL "Detected architecture ${known_arch}")
  125. endif()
  126. endif()
  127. endforeach()
  128. cmake_pop_check_state()
  129. endif()
  130. set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
  131. endfunction()