| 1 |
function(pj_canonicalize_arch arch out_arch) |
| 2 |
string(TOLOWER "${arch}" arch) |
| 3 |
|
| 4 |
if(arch MATCHES "^(arm64|armv8|aarch64)") |
| 5 |
set(canonical_arch "arm64") |
| 6 |
elseif(arch MATCHES "^arm7") |
| 7 |
set(canonical_arch "armv7") |
| 8 |
elseif(arch MATCHES "^arm4") |
| 9 |
set(canonical_arch "armv4") |
| 10 |
elseif(arch MATCHES "^arm") |
| 11 |
set(canonical_arch "arm") |
| 12 |
elseif(arch MATCHES "^(x86_64|amd64)") |
| 13 |
set(canonical_arch "x86_64") |
| 14 |
elseif(arch MATCHES "^(x86|i[3-6]86)") |
| 15 |
set(canonical_arch "i386") |
| 16 |
elseif(arch MATCHES "^(ppc|powerpc)") |
| 17 |
set(canonical_arch "powerpc") |
| 18 |
elseif(arch MATCHES "^mips64") |
| 19 |
set(canonical_arch "mips64") |
| 20 |
elseif(arch MATCHES "^mips") |
| 21 |
set(canonical_arch "mips") |
| 22 |
elseif(arch MATCHES "^ia64") |
| 23 |
set(canonical_arch "ia64") |
| 24 |
elseif(arch MATCHES "^m68k") |
| 25 |
set(canonical_arch "m68k") |
| 26 |
elseif(arch MATCHES "^alpha") |
| 27 |
set(canonical_arch "alpha") |
| 28 |
elseif(arch MATCHES "^sparc") |
| 29 |
set(canonical_arch "sparc") |
| 30 |
elseif(arch MATCHES "^nios") |
| 31 |
set(canonical_arch "nios") |
| 32 |
else() |
| 33 |
set(canonical_arch "${arch}") |
| 34 |
endif() |
| 35 |
|
| 36 |
set("${out_arch}" "${canonical_arch}" PARENT_SCOPE) |
| 37 |
endfunction() |
| 38 |
|
| 39 |
function(pj_detect_arch out_arch) |
| 40 |
# if a cached value exists, return it |
| 41 |
if(DEFINED CACHE{_pj_detected_arch}) |
| 42 |
set("${out_arch}" "$CACHE{_pj_detected_arch}" PARENT_SCOPE) |
| 43 |
return() |
| 44 |
endif() |
| 45 |
|
| 46 |
# try getting the value from toolchain variables |
| 47 |
if(APPLE AND CMAKE_OSX_ARCHITECTURES) |
| 48 |
list(GET CMAKE_OSX_ARCHITECTURES 0 arch) |
| 49 |
elseif(ANDROID AND CMAKE_ANDROID_ARCH) |
| 50 |
set(arch "${CMAKE_ANDROID_ARCH}") |
| 51 |
elseif(MSVC) |
| 52 |
if(MSVC_C_ARCHITECTURE_ID) |
| 53 |
set(arch "${MSVC_C_ARCHITECTURE_ID}") |
| 54 |
elseif(MSVC_CXX_ARCHITECTURE_ID) |
| 55 |
set(arch "${MSVC_CXX_ARCHITECTURE_ID}") |
| 56 |
endif() |
| 57 |
elseif(CMAKE_CROSSCOMPILING) |
| 58 |
if(CMAKE_C_COMPILER_TARGET) |
| 59 |
set(arch "${CMAKE_C_COMPILER_TARGET}") |
| 60 |
elseif(CMAKE_CXX_COMPILER_TARGET) |
| 61 |
set(arch "${CMAKE_CXX_COMPILER_TARGET}") |
| 62 |
endif() |
| 63 |
endif() |
| 64 |
|
| 65 |
# try getting the value from a compile check |
| 66 |
if(NOT arch) |
| 67 |
try_run(_run_result compileResult |
| 68 |
SOURCES |
| 69 |
"${CMAKE_CURRENT_FUNCTION_LIST_DIR}/detect-arch.c" |
| 70 |
COMPILE_OUTPUT_VARIABLE |
| 71 |
compileOutput |
| 72 |
) |
| 73 |
|
| 74 |
string(REGEX REPLACE ".*arch:([a-zA-Z0-9_]+)" "\\1" arch "${compileOutput}") |
| 75 |
endif() |
| 76 |
|
| 77 |
# get value from cmake variable |
| 78 |
if(NOT arch) |
| 79 |
set(arch "${CMAKE_SYSTEM_PROCESSOR}") |
| 80 |
endif() |
| 81 |
|
| 82 |
# if not value is detect, return "unknown" |
| 83 |
if(NOT arch) |
| 84 |
set("${out_arch}" "unknown" PARENT_SCOPE) |
| 85 |
return() |
| 86 |
endif() |
| 87 |
|
| 88 |
# canonicalize value |
| 89 |
pj_canonicalize_arch("${arch}" arch) |
| 90 |
|
| 91 |
# cache value |
| 92 |
set(_pj_detected_arch "${arch}" CACHE INTERNAL "Detected CPU architecture") |
| 93 |
|
| 94 |
# return canonical value |
| 95 |
set("${out_arch}" "${arch}" PARENT_SCOPE) |
| 96 |
endfunction() |
| 97 |
|
| 98 |
function(pj_detect_arch_simd_ext out_simd out_flags) |
| 99 |
# if a cached values exists, return them |
| 100 |
if(DEFINED CACHE{_pj_detected_simd_ext}) |
| 101 |
set("${out_simd}" "$CACHE{_pj_detected_simd_ext}" PARENT_SCOPE) |
| 102 |
set("${out_flags}" "$CACHE{_pj_detected_simd_ext_flags}" PARENT_SCOPE) |
| 103 |
return() |
| 104 |
endif() |
| 105 |
|
| 106 |
pj_detect_arch(arch) |
| 107 |
if(arch MATCHES "^arm") |
| 108 |
set(simd_inst neon) |
| 109 |
set(simd_check_source [=[ |
| 110 |
#if defined(_M_ARM64) || defined(_M_ARM64EC) |
| 111 |
# include <arm64_neon.h> |
| 112 |
#else |
| 113 |
# include <arm_neon.h> |
| 114 |
#endif |
| 115 |
|
| 116 |
int main() { |
| 117 |
return 0; |
| 118 |
} |
| 119 |
]=]) |
| 120 |
|
| 121 |
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") |
| 122 |
if(arch STREQUAL "arm64") |
| 123 |
set(simd_flags "-march=armv8-a+simd") |
| 124 |
else() |
| 125 |
set(simd_flags "-mfpu=neon") |
| 126 |
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm.*gnueabihf") |
| 127 |
list(APPEND simd_flags "-mfloat-abi=hard") |
| 128 |
elseif(ANDROID) |
| 129 |
if(CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi") |
| 130 |
list(APPEND simd_flags "-march=armv7") |
| 131 |
endif() |
| 132 |
if(CMAKE_ANDROID_ARM_MODE) |
| 133 |
list(APPEND simd_flags "-mfloat-abi=softfp") |
| 134 |
endif() |
| 135 |
if(NOT CMAKE_ANDROID_ARM_MODE) |
| 136 |
list(APPEND simd_flags "-mthumb") |
| 137 |
endif() |
| 138 |
endif() |
| 139 |
endif() |
| 140 |
endif() |
| 141 |
elseif(arch MATCHES "^(i386|x86_64|x64)$") |
| 142 |
set(simd_inst sse2) |
| 143 |
set(simd_check_source [=[ |
| 144 |
#include <immintrin.h> |
| 145 |
|
| 146 |
__m128i f(__m128i x, __m128i y) { |
| 147 |
return _mm_sad_epu8(x, y); |
| 148 |
} |
| 149 |
|
| 150 |
int main(void) { |
| 151 |
return 0; |
| 152 |
} |
| 153 |
]=]) |
| 154 |
|
| 155 |
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang" AND UNIX) |
| 156 |
if(CYGWIN OR MINGW) |
| 157 |
set(simd_flags "-msse2") |
| 158 |
else() |
| 159 |
set(simd_flags "-mfma") |
| 160 |
endif() |
| 161 |
elseif(MSVC AND NOT (arch STREQUAL "x86_64" OR arch STREQUAL "x64")) |
| 162 |
set(simd_flags "/arch:SSE2") |
| 163 |
endif() |
| 164 |
elseif(arch STREQUAL "mips") |
| 165 |
set(simd_inst mips) |
| 166 |
endif() |
| 167 |
|
| 168 |
set(_simd_supported TRUE) |
| 169 |
|
| 170 |
if(simd_check_source) |
| 171 |
include(CheckCSourceCompiles) |
| 172 |
include(CMakePushCheckState) |
| 173 |
|
| 174 |
cmake_push_check_state(RESET) |
| 175 |
set(CMAKE_REQUIRED_FLAGS ${simd_flags}) |
| 176 |
check_c_source_compiles("${simd_check_source}" _simd_supported |
| 177 |
FAIL_REGEX "not supported" |
| 178 |
) |
| 179 |
cmake_pop_check_state() |
| 180 |
endif() |
| 181 |
|
| 182 |
if(_simd_supported) |
| 183 |
# cache values |
| 184 |
set(_pj_detected_simd_ext "${simd_inst}" CACHE INTERNAL "SIMD extensions") |
| 185 |
set(_pj_detected_simd_ext_flags "${simd_flags}" CACHE INTERNAL "") |
| 186 |
|
| 187 |
set("${out_simd}" "${simd_inst}" PARENT_SCOPE) |
| 188 |
set("${out_flags}" "${simd_flags}" PARENT_SCOPE) |
| 189 |
else() |
| 190 |
set("${out_simd}" "" PARENT_SCOPE) |
| 191 |
set("${out_flags}" "" PARENT_SCOPE) |
| 192 |
endif() |
| 193 |
endfunction() |