summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/cmake/sdlcpu.cmake
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/cmake/sdlcpu.cmake')
-rw-r--r--src/contrib/SDL-3.2.20/cmake/sdlcpu.cmake156
1 files changed, 156 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/cmake/sdlcpu.cmake b/src/contrib/SDL-3.2.20/cmake/sdlcpu.cmake
new file mode 100644
index 0000000..0c2ca1f
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/cmake/sdlcpu.cmake
@@ -0,0 +1,156 @@
1function(SDL_DetectTargetCPUArchitectures DETECTED_ARCHS)
2
3 set(known_archs EMSCRIPTEN ARM32 ARM64 ARM64EC LOONGARCH64 POWERPC32 POWERPC64 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")
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")
13 list(APPEND detected_archs "X64")
14 elseif(osx_arch STREQUAL "arm64")
15 set(SDL_CPU_ARM64 "1")
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_X86 "defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) ||defined( __i386) || defined(_M_IX86)")
43 set(arch_check_X64 "(defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)) && !defined(_M_ARM64EC)")
44
45 set(src_vars "")
46 set(src_main "")
47 foreach(known_arch IN LISTS known_archs)
48 set(detected_${known_arch} "0")
49
50 string(APPEND src_vars "
51#if ${arch_check_${known_arch}}
52#define ARCH_${known_arch} \"1\"
53#else
54#define ARCH_${known_arch} \"0\"
55#endif
56const char *arch_${known_arch} = \"INFO<${known_arch}=\" ARCH_${known_arch} \">\";
57")
58 string(APPEND src_main "
59 result += arch_${known_arch}[argc];")
60 endforeach()
61
62 set(src_arch_detect "${src_vars}
63int main(int argc, char *argv[]) {
64 int result = 0;
65 (void)argv;
66${src_main}
67 return result;
68}")
69
70 if(CMAKE_C_COMPILER)
71 set(ext ".c")
72 elseif(CMAKE_CXX_COMPILER)
73 set(ext ".cpp")
74 else()
75 enable_language(C)
76 set(ext ".c")
77 endif()
78 set(path_src_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch${ext}")
79 file(WRITE "${path_src_arch_detect}" "${src_arch_detect}")
80 set(path_dir_arch_detect "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch")
81 set(path_bin_arch_detect "${path_dir_arch_detect}/bin")
82
83 set(detected_archs)
84
85 set(msg "Detecting Target CPU Architecture")
86 message(STATUS "${msg}")
87
88 include(CMakePushCheckState)
89
90 set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
91
92 cmake_push_check_state(RESET)
93 try_compile(SDL_CPU_CHECK_ALL
94 "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/CMakeTmp/SDL_detect_arch"
95 SOURCES "${path_src_arch_detect}"
96 COPY_FILE "${path_bin_arch_detect}"
97 )
98 cmake_pop_check_state()
99 if(NOT SDL_CPU_CHECK_ALL)
100 message(STATUS "${msg} - <ERROR>")
101 message(WARNING "Failed to compile source detecting the target CPU architecture")
102 else()
103 set(re "INFO<([A-Z0-9]+)=([01])>")
104 file(STRINGS "${path_bin_arch_detect}" infos REGEX "${re}")
105
106 foreach(info_arch_01 IN LISTS infos)
107 string(REGEX MATCH "${re}" A "${info_arch_01}")
108 if(NOT "${CMAKE_MATCH_1}" IN_LIST known_archs)
109 message(WARNING "Unknown architecture: \"${CMAKE_MATCH_1}\"")
110 continue()
111 endif()
112 set(arch "${CMAKE_MATCH_1}")
113 set(arch_01 "${CMAKE_MATCH_2}")
114 set(detected_${arch} "${arch_01}")
115 endforeach()
116
117 foreach(known_arch IN LISTS known_archs)
118 if(detected_${known_arch})
119 list(APPEND detected_archs ${known_arch})
120 endif()
121 endforeach()
122 endif()
123
124 if(detected_archs)
125 foreach(known_arch IN LISTS known_archs)
126 set("SDL_CPU_${known_arch}" "${detected_${known_arch}}" CACHE BOOL "Detected architecture ${known_arch}")
127 endforeach()
128 message(STATUS "${msg} - ${detected_archs}")
129 else()
130 include(CheckCSourceCompiles)
131 cmake_push_check_state(RESET)
132 foreach(known_arch IN LISTS known_archs)
133 if(NOT detected_archs)
134 set(cache_variable "SDL_CPU_${known_arch}")
135 set(test_src "
136 int main(int argc, char *argv[]) {
137 #if ${arch_check_${known_arch}}
138 return 0;
139 #else
140 choke
141 #endif
142 }
143 ")
144 check_c_source_compiles("${test_src}" "${cache_variable}")
145 if(${cache_variable})
146 set(SDL_CPU_${known_arch} "1" CACHE BOOL "Detected architecture ${known_arch}")
147 set(detected_archs ${known_arch})
148 else()
149 set(SDL_CPU_${known_arch} "0" CACHE BOOL "Detected architecture ${known_arch}")
150 endif()
151 endif()
152 endforeach()
153 cmake_pop_check_state()
154 endif()
155 set("${DETECTED_ARCHS}" "${detected_archs}" PARENT_SCOPE)
156endfunction()