diff --git a/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake new file mode 100644 --- /dev/null +++ b/libc/cmake/modules/LLVMLibCCheckCpuFeatures.cmake @@ -0,0 +1,35 @@ +# Sets variables of the form CPU_FEATURES_XXX +# Where XXX is the uppercase name of the feature. +# +# Usage: +# include(LLVMLibCCheckCpuFeatures) +# if(${CPU_FEATURES_AVX2}) +# ... +# endif() +function(check_cpu_features) + if(${CMAKE_HOST_APPLE}) + execute_process( + COMMAND sysctl -a + COMMAND grep machdep.cpu.features + COMMAND cut -f2 -d: + COMMAND tr . _ + OUTPUT_VARIABLE flags_string + ) + elseif(${CMAKE_HOST_UNIX}) + execute_process( + COMMAND grep -h -m1 \\w*flags\\w* /proc/cpuinfo + COMMAND cut -f2 -d: + OUTPUT_VARIABLE flags_string + ) + else() + message(FATAL "Unsupported system") + endif() + + string(REPLACE " " ";" flags "${flags_string}") + foreach(flag ${flags}) + string(TOUPPER ${flag} uppercase_flag) + set(CPU_FEATURES_${uppercase_flag} true PARENT_SCOPE) + endforeach() +endfunction() + +check_cpu_features()