Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.0) |
| 2 | |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 3 | project(CpuFeatures VERSION 0.1.0) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 4 | |
Guillaume Chatelet | 3a156da | 2018-02-08 16:59:18 +0100 | [diff] [blame] | 5 | # Default Build Type to be Release |
| 6 | if(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) |
| 10 | endif(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. |
| 14 | option(BUILD_TESTING "Enable test (depends on googletest)." OFF) |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 15 | # BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make |
| 16 | # it prominent in the GUI. |
| 17 | option(BUILD_SHARED_LIBS "Build library as shared." OFF) |
Guillaume Chatelet | 3a156da | 2018-02-08 16:59:18 +0100 | [diff] [blame] | 18 | |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 19 | include(CheckIncludeFile) |
| 20 | include(CheckSymbolExists) |
| 21 | include(GNUInstallDirs) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 22 | |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 23 | macro(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 Chatelet | b3bb904 | 2019-01-15 15:42:04 +0100 | [diff] [blame] | 30 | ) |
| 31 | endmacro() |
| 32 | |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 33 | set(PROCESSOR_IS_MIPS FALSE) |
| 34 | set(PROCESSOR_IS_ARM FALSE) |
| 35 | set(PROCESSOR_IS_AARCH64 FALSE) |
| 36 | set(PROCESSOR_IS_X86 FALSE) |
| 37 | set(PROCESSOR_IS_POWER FALSE) |
| 38 | |
Guillaume Chatelet | e8e5610 | 2019-01-15 10:52:56 +0100 | [diff] [blame] | 39 | if(CMAKE_SYSTEM_PROCESSOR MATCHES "^mips") |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 40 | set(PROCESSOR_IS_MIPS TRUE) |
Guillaume Chatelet | e8e5610 | 2019-01-15 10:52:56 +0100 | [diff] [blame] | 41 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm") |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 42 | set(PROCESSOR_IS_ARM TRUE) |
Guillaume Chatelet | e8e5610 | 2019-01-15 10:52:56 +0100 | [diff] [blame] | 43 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64") |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 44 | set(PROCESSOR_IS_AARCH64 TRUE) |
| 45 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "(x86_64)|(AMD64)|(^i.86$)") |
| 46 | set(PROCESSOR_IS_X86 TRUE) |
Guillaume Chatelet | e8e5610 | 2019-01-15 10:52:56 +0100 | [diff] [blame] | 47 | elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)") |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 48 | set(PROCESSOR_IS_POWER TRUE) |
Guillaume Chatelet | e8e5610 | 2019-01-15 10:52:56 +0100 | [diff] [blame] | 49 | endif() |
| 50 | |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 51 | macro(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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 53 | if(PROCESSOR_IS_MIPS) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 54 | 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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 56 | elseif(PROCESSOR_IS_ARM) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 57 | 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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 59 | elseif(PROCESSOR_IS_AARCH64) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 60 | 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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 62 | elseif(PROCESSOR_IS_X86) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 63 | 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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 66 | elseif(PROCESSOR_IS_POWER) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 67 | 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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 69 | else() |
| 70 | message(FATAL_ERROR "Unsupported architectures ${CMAKE_SYSTEM_PROCESSOR}") |
| 71 | endif() |
| 72 | endmacro() |
Guillaume Chatelet | e8e5610 | 2019-01-15 10:52:56 +0100 | [diff] [blame] | 73 | |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 74 | # |
| 75 | # library : utils |
| 76 | # |
| 77 | |
| 78 | add_library(utils OBJECT |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 79 | ${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 Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 86 | ) |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 87 | setup_include_and_definitions(utils) |
| 88 | |
| 89 | # |
| 90 | # library : unix_based_hardware_detection |
| 91 | # |
| 92 | |
| 93 | if(UNIX) |
| 94 | add_library(unix_based_hardware_detection OBJECT |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 95 | ${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 Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 99 | ) |
| 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 Chatelet | b3bb904 | 2019-01-15 15:42:04 +0100 | [diff] [blame] | 109 | endif() |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 110 | |
| 111 | # |
| 112 | # library : cpu_features |
| 113 | # |
| 114 | set (CPU_FEATURES_HDRS) |
| 115 | set (CPU_FEATURES_SRCS) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 116 | add_cpu_features_headers_and_sources(CPU_FEATURES_HDRS CPU_FEATURES_SRCS) |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 117 | list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:utils>) |
| 118 | if(NOT PROCESSOR_IS_X86 AND UNIX) |
| 119 | list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:unix_based_hardware_detection>) |
Guillaume Chatelet | b3bb904 | 2019-01-15 15:42:04 +0100 | [diff] [blame] | 120 | endif() |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 121 | add_library(cpu_features ${CPU_FEATURES_HDRS} ${CPU_FEATURES_SRCS}) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 122 | set_target_properties(cpu_features PROPERTIES PUBLIC_HEADER "${CPU_FEATURES_HDRS}") |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 123 | setup_include_and_definitions(cpu_features) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 124 | target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS}) |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 125 | target_include_directories(cpu_features |
| 126 | PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cpu_features> |
| 127 | ) |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 128 | |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 129 | # |
| 130 | # program : list_cpu_features |
| 131 | # |
| 132 | |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 133 | add_executable(list_cpu_features ${PROJECT_SOURCE_DIR}/src/utils/list_cpu_features.c) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 134 | target_link_libraries(list_cpu_features PRIVATE cpu_features) |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 135 | add_executable(CpuFeature::list_cpu_features ALIAS list_cpu_features) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 136 | |
| 137 | # |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 138 | # ndk_compat |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 139 | # |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 140 | |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 141 | if(ANDROID) |
| 142 | add_subdirectory(ndk_compat) |
Guillaume Chatelet | dfdac6a | 2019-01-17 18:00:21 +0100 | [diff] [blame] | 143 | endif() |
| 144 | |
| 145 | # |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 146 | # tests |
| 147 | # |
| 148 | |
| 149 | include(CTest) |
| 150 | if(BUILD_TESTING) |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 151 | # 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 Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 159 | |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 160 | execute_process( |
| 161 | COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . |
| 162 | RESULT_VARIABLE result |
| 163 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 164 | |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 165 | if(result) |
| 166 | message(FATAL_ERROR "CMake step for googletest failed: ${result}") |
| 167 | endif() |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 168 | |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 169 | execute_process( |
| 170 | COMMAND ${CMAKE_COMMAND} --build . |
| 171 | RESULT_VARIABLE result |
| 172 | WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 173 | |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 174 | if(result) |
| 175 | message(FATAL_ERROR "Build step for googletest failed: ${result}") |
| 176 | endif() |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 177 | |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 178 | # Prevent overriding the parent project's compiler/linker settings on |
| 179 | # Windows. |
| 180 | set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 181 | |
Guillaume Chatelet | 9df2d78 | 2018-02-13 10:07:02 +0100 | [diff] [blame] | 182 | # 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 Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 187 | endif() |
Guillaume Chatelet | 3a156da | 2018-02-08 16:59:18 +0100 | [diff] [blame] | 188 | |
Guillaume Chatelet | 439d371 | 2018-02-01 10:03:09 +0100 | [diff] [blame] | 189 | add_subdirectory(test) |
| 190 | endif() |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 191 | |
| 192 | # |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 193 | # Install cpu_features and list_cpu_features |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 194 | # |
| 195 | |
| 196 | include(GNUInstallDirs) |
| 197 | install(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 | ) |
| 204 | install(EXPORT CpuFeaturesTargets |
| 205 | NAMESPACE CpuFeatures:: |
| 206 | DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures |
| 207 | COMPONENT Devel |
| 208 | ) |
| 209 | include(CMakePackageConfigHelpers) |
| 210 | configure_package_config_file(cmake/CpuFeaturesConfig.cmake.in |
Guillaume Chatelet | 57917d2 | 2018-02-12 16:48:57 +0100 | [diff] [blame] | 211 | "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake" |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 212 | INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures" |
| 213 | NO_SET_AND_CHECK_MACRO |
| 214 | NO_CHECK_REQUIRED_COMPONENTS_MACRO |
| 215 | ) |
| 216 | write_basic_package_version_file( |
| 217 | "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake" |
| 218 | COMPATIBILITY SameMajorVersion |
| 219 | ) |
| 220 | install( |
| 221 | FILES |
Guillaume Chatelet | 20fa92a | 2019-01-23 12:59:46 +0100 | [diff] [blame] | 222 | "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake" |
| 223 | "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake" |
Guillaume Chatelet | 1976dcd | 2018-02-12 16:17:21 +0100 | [diff] [blame] | 224 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures" |
| 225 | COMPONENT Devel |
| 226 | ) |