Skip to content

Automatic CMake CPU Feature Flag Availability Detection

License

Notifications You must be signed in to change notification settings

mattsta/cmake-cpu-detect

Repository files navigation

Automatic CMake CPU Feature Detection

Usage

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()

How It Works

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).

About

Automatic CMake CPU Feature Flag Availability Detection

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published