Atlas - sdlcpu.cmake

Home / ext / SDL / cmake Lines: 1 | Size: 5419 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS) 2 3 set(known_archs EMSCRIPTEN ARM32 ARM64 ARM64EC LOONGARCH64 POWERPC32 POWERPC64 RISCV32 RISCV64 X86 X64) 4 5 if(APPLE AND CMAKE_OSX_ARCHITECTURES) 6 foreach(known_arch IN LISTS known_archs) 7 set(SDL_CPU_${known_arch} "0" PARENT_SCOPE) 8 endforeach() 9 set(detected_archs) 10 foreach(osx_arch IN LISTS CMAKE_OSX_ARCHITECTURES) 11 if(osx_arch STREQUAL "x86_64") 12 set(SDL_CPU_X64 "1" PARENT_SCOPE) 13 list(APPEND detected_archs "X64") 14 elseif(osx_arch STREQUAL "arm64") 15 set(SDL_CPU_ARM64 "1" PARENT_SCOPE) 16 list(APPEND detected_archs "ARM64") 17 endif() 18 endforeach() 19 set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE) 20 return() 21 endif() 22 23 set(detected_archs) 24 foreach(known_arch IN LISTS known_archs) 25 if(SDL_CPU_${known_arch}) 26 list(APPEND detected_archs "${known_arch}") 27 endif() 28 endforeach() 29 30 if(detected_archs) 31 set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE) 32 return() 33 endif() 34 35 set(arch_check_ARM32 "defined(__arm__) || defined(_M_ARM)") 36 set(arch_check_ARM64 "defined(__aarch64__) || defined(_M_ARM64)") 37 set(arch_check_ARM64EC "defined(_M_ARM64EC)") 38 set(arch_check_EMSCRIPTEN "defined(__EMSCRIPTEN__)") 39 set(arch_check_LOONGARCH64 "defined(__loongarch64)") 40 set(arch_check_POWERPC32 "(defined(__PPC__) || defined(__powerpc__)) && !defined(__powerpc64__)") 41 set(arch_check_POWERPC64 "defined(__PPC64__) || defined(__powerpc64__)") 42 set(arch_check_RISCV32 "defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 32") 43 set(arch_check_RISCV64 "defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64") 44 set(arch_check_X86 "defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)") 45 set(arch_check_X64 "(defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)) && !defined(_M_ARM64EC)") 46 47 set(src_vars "") 48 set(src_main "") 49 foreach(known_arch IN LISTS known_archs) 50 set(detected_${known_arch} "0") 51 52 string(APPEND src_vars " 53#if ${arch_check_${known_arch}} 54#define ARCH_${known_arch} \"1\" 55#else 56#define ARCH_${known_arch} \"0\" 57#endif 58const char *arch_${known_arch} = \"INFO<${known_arch}=\" ARCH_${known_arch} \">\"; 59") 60 string(APPEND src_main " 61 result += arch_${known_arch}[argc];") 62 endforeach() 63 64 set(src_arch_detect "${src_vars} 65int main(int argc, char *argv[]) { 66 int result = 0; 67 (void)argv; 68${src_main} 69 return result; 70}") 71 72 if(CMAKE_C_COMPILER) 73 set(ext ".c") 74 elseif(CMAKE_CXX_COMPILER) 75 set(ext ".cpp") 76 else() 77 enable_language(C) 78 set(ext ".c") 79 endif() 80 set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch${ext}") 81 file(WRITE "${path_src_arch_detect}" "${src_arch_detect}") 82 set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch") 83 set(path_bin_arch_detect "${path_dir_arch_detect}/bin") 84 85 set(detected_archs) 86 87 set(msg "Detecting Target CPU Architecture") 88 message(STATUS "${msg}") 89 90 include(CMakePushCheckState) 91 92 set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY") 93 94 cmake_push_check_state(RESET) 95 try_compile(SDL_CPU_CHECK_ALL 96 "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch" 97 SOURCES "${path_src_arch_detect}" 98 COPY_FILE "${path_bin_arch_detect}" 99 ) 100 cmake_pop_check_state() 101 if(NOT SDL_CPU_CHECK_ALL) 102 message(STATUS "${msg} - <ERROR>") 103 message(WARNING "Failed to compile source detecting the target CPU architecture") 104 else() 105 set(re "INFO<([A-Z0-9]+)=([01])>") 106 file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}") 107 108 foreach(info_arch_01 IN LISTS infos) 109 string(REGEX MATCH "${re}" A "${info_arch_01}") 110 if(NOT "${CMAKE_MATCH_1}" IN_LIST known_archs) 111 message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"") 112 continue() 113 endif() 114 set(arch "${CMAKE_MATCH_1}") 115 set(arch_01 "${CMAKE_MATCH_2}") 116 set(detected_${arch} "${arch_01}") 117 endforeach() 118 119 foreach(known_arch IN LISTS known_archs) 120 if(detected_${known_arch}) 121 list(APPEND detected_archs ${known_arch}) 122 endif() 123 endforeach() 124 endif() 125 126 if(detected_archs) 127 foreach(known_arch IN LISTS known_archs) 128 set("SDL_CPU_${known_arch}" "${detected_${known_arch}}" CACHE BOOL "Detected architecture ${known_arch}") 129 endforeach() 130 message(STATUS "${msg} - ${detected_archs}") 131 else() 132 include(CheckCSourceCompiles) 133 cmake_push_check_state(RESET) 134 foreach(known_arch IN LISTS known_archs) 135 if(NOT detected_archs) 136 set(cache_variable "SDL_CPU_${known_arch}") 137 set(test_src " 138 int main(int argc, char *argv[]) { 139 #if ${arch_check_${known_arch}} 140 return 0; 141 #else 142 choke 143 #endif 144 } 145 ") 146 check_c_source_compiles("${test_src}" "${cache_variable}") 147 if(${cache_variable}) 148 set(SDL_CPU_${known_arch} "1" CACHE BOOL "Detected architecture ${known_arch}") 149 set(detected_archs ${known_arch}) 150 else() 151 set(SDL_CPU_${known_arch} "0" CACHE BOOL "Detected architecture ${known_arch}") 152 endif() 153 endif() 154 endforeach() 155 cmake_pop_check_state() 156 endif() 157 set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE) 158endfunction() 159
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.