blob: 127184a339f6537584705efb6f16b04a7e16a6c1 [file] [log] [blame]
Guillaume Chatelet439d3712018-02-01 10:03:09 +01001cmake_minimum_required(VERSION 3.0)
2
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +01003project(CpuFeatures VERSION 0.1.0)
Guillaume Chatelet439d3712018-02-01 10:03:09 +01004
Guillaume Chatelet3a156da2018-02-08 16:59:18 +01005# Default Build Type to be Release
6if(NOT CMAKE_BUILD_TYPE)
7 set(CMAKE_BUILD_TYPE "Release" CACHE STRING
8 "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
9 FORCE)
10endif(NOT CMAKE_BUILD_TYPE)
11
12# BUILD_TESTING is a standard CMake variable, but we declare it here to make it
13# prominent in the GUI.
14option(BUILD_TESTING "Enable test (depends on googletest)." OFF)
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +010015# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make
16# it prominent in the GUI.
17option(BUILD_SHARED_LIBS "Build library as shared." OFF)
Guillaume Chatelet3a156da2018-02-08 16:59:18 +010018
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010019include(CheckIncludeFile)
20include(CheckSymbolExists)
21include(GNUInstallDirs)
Guillaume Chatelet439d3712018-02-01 10:03:09 +010022
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010023macro(setup_include_and_definitions TARGET_NAME)
24 target_include_directories(${TARGET_NAME}
25 PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
26 PRIVATE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/internal>
27 )
28 target_compile_definitions(${TARGET_NAME}
29 PUBLIC STACK_LINE_READER_BUFFER_SIZE=1024
Guillaume Chateletb3bb9042019-01-15 15:42:04 +010030 )
31endmacro()
32
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010033set(PROCESSOR_IS_MIPS FALSE)
34set(PROCESSOR_IS_ARM FALSE)
35set(PROCESSOR_IS_AARCH64 FALSE)
36set(PROCESSOR_IS_X86 FALSE)
37set(PROCESSOR_IS_POWER FALSE)
38
Guillaume Chatelete8e56102019-01-15 10:52:56 +010039if(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips")
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010040 set(PROCESSOR_IS_MIPS TRUE)
Guillaume Chatelete8e56102019-01-15 10:52:56 +010041elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010042 set(PROCESSOR_IS_ARM TRUE)
Guillaume Chatelete8e56102019-01-15 10:52:56 +010043elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64")
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010044 set(PROCESSOR_IS_AARCH64 TRUE)
45elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64)|(^i.86$)")
46 set(PROCESSOR_IS_X86 TRUE)
Guillaume Chatelete8e56102019-01-15 10:52:56 +010047elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)")
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010048 set(PROCESSOR_IS_POWER TRUE)
Guillaume Chatelete8e56102019-01-15 10:52:56 +010049endif()
50
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010051macro(add_cpu_features_headers_and_sources HDRS_LIST_NAME SRCS_LIST_NAME)
52 list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpu_features_macros.h)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010053 if(PROCESSOR_IS_MIPS)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010054 list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_mips.h)
55 list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_mips.c)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010056 elseif(PROCESSOR_IS_ARM)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010057 list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_arm.h)
58 list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_arm.c)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010059 elseif(PROCESSOR_IS_AARCH64)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010060 list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_aarch64.h)
61 list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_aarch64.c)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010062 elseif(PROCESSOR_IS_X86)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010063 list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_x86.h)
64 list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/internal/cpuid_x86.h)
65 list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_x86.c)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010066 elseif(PROCESSOR_IS_POWER)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010067 list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_ppc.h)
68 list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_ppc.c)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010069 else()
70 message(FATAL_ERROR "Unsupported architectures ${CMAKE_SYSTEM_PROCESSOR}")
71 endif()
72endmacro()
Guillaume Chatelete8e56102019-01-15 10:52:56 +010073
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010074#
75# library : utils
76#
77
78add_library(utils OBJECT
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010079 ${PROJECT_SOURCE_DIR}/include/internal/bit_utils.h
80 ${PROJECT_SOURCE_DIR}/include/internal/filesystem.h
81 ${PROJECT_SOURCE_DIR}/include/internal/stack_line_reader.h
82 ${PROJECT_SOURCE_DIR}/include/internal/string_view.h
83 ${PROJECT_SOURCE_DIR}/src/filesystem.c
84 ${PROJECT_SOURCE_DIR}/src/stack_line_reader.c
85 ${PROJECT_SOURCE_DIR}/src/string_view.c
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +010086)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010087setup_include_and_definitions(utils)
88
89#
90# library : unix_based_hardware_detection
91#
92
93if(UNIX)
94 add_library(unix_based_hardware_detection OBJECT
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +010095 ${PROJECT_SOURCE_DIR}/include/internal/hwcaps.h
96 ${PROJECT_SOURCE_DIR}/include/internal/unix_features_aggregator.h
97 ${PROJECT_SOURCE_DIR}/src/hwcaps.c
98 ${PROJECT_SOURCE_DIR}/src/unix_features_aggregator.c
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +010099 )
100 setup_include_and_definitions(unix_based_hardware_detection)
101 check_include_file(dlfcn.h HAVE_DLFCN_H)
102 if(HAVE_DLFCN_H)
103 target_compile_definitions(unix_based_hardware_detection PRIVATE HAVE_DLFCN_H)
104 endif()
105 check_symbol_exists(getauxval "sys/auxv.h" HAVE_STRONG_GETAUXVAL)
106 if(HAVE_STRONG_GETAUXVAL)
107 target_compile_definitions(unix_based_hardware_detection PRIVATE HAVE_STRONG_GETAUXVAL)
108 endif()
Guillaume Chateletb3bb9042019-01-15 15:42:04 +0100109endif()
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100110
111#
112# library : cpu_features
113#
114set (CPU_FEATURES_HDRS)
115set (CPU_FEATURES_SRCS)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100116add_cpu_features_headers_and_sources(CPU_FEATURES_HDRS CPU_FEATURES_SRCS)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100117list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:utils>)
118if(NOT PROCESSOR_IS_X86 AND UNIX)
119 list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:unix_based_hardware_detection>)
Guillaume Chateletb3bb9042019-01-15 15:42:04 +0100120endif()
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100121add_library(cpu_features ${CPU_FEATURES_HDRS} ${CPU_FEATURES_SRCS})
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100122set_target_properties(cpu_features PROPERTIES PUBLIC_HEADER "${CPU_FEATURES_HDRS}")
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100123setup_include_and_definitions(cpu_features)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100124target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS})
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100125target_include_directories(cpu_features
126 PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cpu_features>
127)
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100128
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100129#
130# program : list_cpu_features
131#
132
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100133add_executable(list_cpu_features ${PROJECT_SOURCE_DIR}/src/utils/list_cpu_features.c)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100134target_link_libraries(list_cpu_features PRIVATE cpu_features)
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +0100135add_executable(CpuFeature::list_cpu_features ALIAS list_cpu_features)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100136
137#
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100138# ndk_compat
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100139#
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100140
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100141if(ANDROID)
142add_subdirectory(ndk_compat)
Guillaume Chateletdfdac6a2019-01-17 18:00:21 +0100143endif()
144
145#
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100146# tests
147#
148
149include(CTest)
150if(BUILD_TESTING)
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100151 # Automatically incorporate googletest into the CMake Project if target not
152 # found.
153 if(NOT TARGET gtest OR NOT TARGET gmock_main)
154 # Download and unpack googletest at configure time.
155 configure_file(
156 cmake/googletest.CMakeLists.txt.in
157 googletest-download/CMakeLists.txt
158 )
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100159
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100160 execute_process(
161 COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
162 RESULT_VARIABLE result
163 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100164
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100165 if(result)
166 message(FATAL_ERROR "CMake step for googletest failed: ${result}")
167 endif()
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100168
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100169 execute_process(
170 COMMAND ${CMAKE_COMMAND} --build .
171 RESULT_VARIABLE result
172 WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100173
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100174 if(result)
175 message(FATAL_ERROR "Build step for googletest failed: ${result}")
176 endif()
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100177
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100178 # Prevent overriding the parent project's compiler/linker settings on
179 # Windows.
180 set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100181
Guillaume Chatelet9df2d782018-02-13 10:07:02 +0100182 # Add googletest directly to our build. This defines the gtest and
183 # gtest_main targets.
184 add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
185 ${CMAKE_BINARY_DIR}/googletest-build
186 EXCLUDE_FROM_ALL)
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100187 endif()
Guillaume Chatelet3a156da2018-02-08 16:59:18 +0100188
Guillaume Chatelet439d3712018-02-01 10:03:09 +0100189 add_subdirectory(test)
190endif()
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +0100191
192#
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100193# Install cpu_features and list_cpu_features
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +0100194#
195
196include(GNUInstallDirs)
197install(TARGETS cpu_features list_cpu_features
198 EXPORT CpuFeaturesTargets
199 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpu_features
200 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
201 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
202 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
203)
204install(EXPORT CpuFeaturesTargets
205 NAMESPACE CpuFeatures::
206 DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures
207 COMPONENT Devel
208)
209include(CMakePackageConfigHelpers)
210configure_package_config_file(cmake/CpuFeaturesConfig.cmake.in
Guillaume Chatelet57917d22018-02-12 16:48:57 +0100211 "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake"
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +0100212 INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures"
213 NO_SET_AND_CHECK_MACRO
214 NO_CHECK_REQUIRED_COMPONENTS_MACRO
215)
216write_basic_package_version_file(
217 "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake"
218 COMPATIBILITY SameMajorVersion
219)
220install(
221 FILES
Guillaume Chatelet20fa92a2019-01-23 12:59:46 +0100222 "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake"
223 "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake"
Guillaume Chatelet1976dcd2018-02-12 16:17:21 +0100224 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures"
225 COMPONENT Devel
226)