Copy CPUFeatureDetect.cmake
to your project directory.
Add to your CMakeLists.txt
:
include(${PROJECT_SOURCE_DIR}/CPUFeatureDetect.cmake)
Call genCPUFeatures(namespace)
to generate variables describing the current build environment's CPU capabilities.
genCPUFeatures(MyProject)
if(MyProject_FEATURE_AVX2)
message(STATUS "[MyProject] Building with AVX2...")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2")
else()
message(STATUS "[MyProject] AVX2 not found!")
endif()
We parse the llvm compiler CPU feature list (see ./generateCPUDetailsToFeatures.py
)
for all possible CPU features to generate CPUFeatureDetect.cmake
, then when you run the
genCPUFeatures()
CMake function, we call potentially dozens of check_c_source_compiles()
checks
against all the compile feature flags to determine which CPU features are available given
your current CFLAGS
configuration.
If you don't have -march=
specified or you request -march=native
,
we run all the CPU check test programs to determine your capability. If you do specify a -march=
option, we use the CPU architecture name to determine which feature are available without running
any test programs (for cross compiling, etc).