1 cmake_minimum_required(VERSION 3.0)
3 project(CpuFeatures VERSION 0.1.0)
5 # ANBOX allow to build in our more strict build environment
6 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=switch-default -Wno-error=unused-parameter -Wno-error=overflow")
8 if("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")
9 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cast-align")
12 # Default Build Type to be Release
13 if(NOT CMAKE_BUILD_TYPE)
14 set(CMAKE_BUILD_TYPE "Release" CACHE STRING
15 "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
17 endif(NOT CMAKE_BUILD_TYPE)
19 # BUILD_TESTING is a standard CMake variable, but we declare it here to make it
20 # prominent in the GUI.
21 option(BUILD_TESTING "Enable test (depends on googletest)." OFF)
22 # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make
23 # it prominent in the GUI.
24 option(BUILD_SHARED_LIBS "Build library as shared." OFF)
27 # library : cpu_features
31 include/cpuinfo_aarch64.h
33 include/cpuinfo_mips.h
36 include/cpu_features_macros.h
39 add_library(cpu_features
41 include/internal/bit_utils.h
42 include/internal/linux_features_aggregator.h
43 include/internal/cpuid_x86.h
44 include/internal/filesystem.h
45 include/internal/hwcaps.h
46 include/internal/stack_line_reader.h
47 include/internal/string_view.h
48 include/cpu_features_macros.h
49 src/linux_features_aggregator.c
50 src/cpuid_x86_clang_gcc.c
59 src/stack_line_reader.c
63 target_include_directories(cpu_features
65 $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
66 $<INSTALL_INTERFACE:include/cpu_features>
70 set_target_properties(cpu_features PROPERTIES PUBLIC_HEADER "${_HDRS}")
71 target_compile_definitions(cpu_features
72 PUBLIC STACK_LINE_READER_BUFFER_SIZE=1024)
73 target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS})
75 # The use of shared libraries is discouraged.
76 # For API / ABI compatibility reasons, it is recommended to build and use
77 # cpu_features in a subdirectory of your project or as an embedded dependency.
79 set_property(TARGET cpu_features PROPERTY POSITION_INDEPENDENT_CODE ON)
81 add_library(CpuFeature::cpu_features ALIAS cpu_features)
84 # program : list_cpu_features
87 add_executable(list_cpu_features src/utils/list_cpu_features.c)
88 target_link_libraries(list_cpu_features PRIVATE cpu_features)
89 add_executable(CpuFeature::list_cpu_features ALIAS list_cpu_features)
96 # ANBOX: disable building tests
97 set(BUILD_TESTING OFF)
99 # Automatically incorporate googletest into the CMake Project if target not
101 if(NOT TARGET gtest OR NOT TARGET gmock_main)
102 # Download and unpack googletest at configure time.
104 cmake/googletest.CMakeLists.txt.in
105 googletest-download/CMakeLists.txt
109 COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
110 RESULT_VARIABLE result
111 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
114 message(FATAL_ERROR "CMake step for googletest failed: ${result}")
118 COMMAND ${CMAKE_COMMAND} --build .
119 RESULT_VARIABLE result
120 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
123 message(FATAL_ERROR "Build step for googletest failed: ${result}")
126 # Prevent overriding the parent project's compiler/linker settings on
128 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
130 # Add googletest directly to our build. This defines the gtest and
131 # gtest_main targets.
132 add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
133 ${CMAKE_BINARY_DIR}/googletest-build
137 add_subdirectory(test)
144 include(GNUInstallDirs)
145 install(TARGETS cpu_features list_cpu_features
146 EXPORT CpuFeaturesTargets
147 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpu_features
148 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
149 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
150 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
152 install(EXPORT CpuFeaturesTargets
153 NAMESPACE CpuFeatures::
154 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures
157 include(CMakePackageConfigHelpers)
158 configure_package_config_file(cmake/CpuFeaturesConfig.cmake.in
159 "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake"
160 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures"
161 NO_SET_AND_CHECK_MACRO
162 NO_CHECK_REQUIRED_COMPONENTS_MACRO
164 write_basic_package_version_file(
165 "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake"
166 COMPATIBILITY SameMajorVersion
170 "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake"
171 "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake"
172 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures"