diff --git a/CMakeLists.txt b/CMakeLists.txt index 667b9c0..127184a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,24 +48,24 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)") set(PROCESSOR_IS_POWER TRUE) endif() -macro(add_arch_sources HDRS_LIST_NAME SRCS_LIST_NAME) - list(APPEND ${HDRS_LIST_NAME} include/cpu_features_macros.h) +macro(add_cpu_features_headers_and_sources HDRS_LIST_NAME SRCS_LIST_NAME) + list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpu_features_macros.h) if(PROCESSOR_IS_MIPS) - list(APPEND ${HDRS_LIST_NAME} include/cpuinfo_mips.h) - list(APPEND ${SRCS_LIST_NAME} src/cpuinfo_mips.c) + list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_mips.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_mips.c) elseif(PROCESSOR_IS_ARM) - list(APPEND ${HDRS_LIST_NAME} include/cpuinfo_arm.h) - list(APPEND ${SRCS_LIST_NAME} src/cpuinfo_arm.c) + list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_arm.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_arm.c) elseif(PROCESSOR_IS_AARCH64) - list(APPEND ${HDRS_LIST_NAME} include/cpuinfo_aarch64.h) - list(APPEND ${SRCS_LIST_NAME} src/cpuinfo_aarch64.c) + list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_aarch64.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_aarch64.c) elseif(PROCESSOR_IS_X86) - list(APPEND ${HDRS_LIST_NAME} include/cpuinfo_x86.h) - list(APPEND ${HDRS_LIST_NAME} include/internal/cpuid_x86.h) - list(APPEND ${SRCS_LIST_NAME} src/cpuinfo_x86.c) + list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_x86.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/internal/cpuid_x86.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_x86.c) elseif(PROCESSOR_IS_POWER) - list(APPEND ${HDRS_LIST_NAME} include/cpuinfo_ppc.h) - list(APPEND ${SRCS_LIST_NAME} src/cpuinfo_ppc.c) + list(APPEND ${HDRS_LIST_NAME} ${PROJECT_SOURCE_DIR}/include/cpuinfo_ppc.h) + list(APPEND ${SRCS_LIST_NAME} ${PROJECT_SOURCE_DIR}/src/cpuinfo_ppc.c) else() message(FATAL_ERROR "Unsupported architectures ${CMAKE_SYSTEM_PROCESSOR}") endif() @@ -76,13 +76,13 @@ endmacro() # add_library(utils OBJECT - include/internal/bit_utils.h - include/internal/filesystem.h - include/internal/stack_line_reader.h - include/internal/string_view.h - src/filesystem.c - src/stack_line_reader.c - src/string_view.c + ${PROJECT_SOURCE_DIR}/include/internal/bit_utils.h + ${PROJECT_SOURCE_DIR}/include/internal/filesystem.h + ${PROJECT_SOURCE_DIR}/include/internal/stack_line_reader.h + ${PROJECT_SOURCE_DIR}/include/internal/string_view.h + ${PROJECT_SOURCE_DIR}/src/filesystem.c + ${PROJECT_SOURCE_DIR}/src/stack_line_reader.c + ${PROJECT_SOURCE_DIR}/src/string_view.c ) setup_include_and_definitions(utils) @@ -92,10 +92,10 @@ setup_include_and_definitions(utils) if(UNIX) add_library(unix_based_hardware_detection OBJECT - include/internal/hwcaps.h - include/internal/unix_features_aggregator.h - src/hwcaps.c - src/unix_features_aggregator.c + ${PROJECT_SOURCE_DIR}/include/internal/hwcaps.h + ${PROJECT_SOURCE_DIR}/include/internal/unix_features_aggregator.h + ${PROJECT_SOURCE_DIR}/src/hwcaps.c + ${PROJECT_SOURCE_DIR}/src/unix_features_aggregator.c ) setup_include_and_definitions(unix_based_hardware_detection) check_include_file(dlfcn.h HAVE_DLFCN_H) @@ -113,48 +113,33 @@ endif() # set (CPU_FEATURES_HDRS) set (CPU_FEATURES_SRCS) -add_arch_sources(CPU_FEATURES_HDRS CPU_FEATURES_SRCS) +add_cpu_features_headers_and_sources(CPU_FEATURES_HDRS CPU_FEATURES_SRCS) list(APPEND CPU_FEATURES_SRCS $) if(NOT PROCESSOR_IS_X86 AND UNIX) list(APPEND CPU_FEATURES_SRCS $) endif() add_library(cpu_features ${CPU_FEATURES_HDRS} ${CPU_FEATURES_SRCS}) +set_target_properties(cpu_features PROPERTIES PUBLIC_HEADER "${CPU_FEATURES_HDRS}") setup_include_and_definitions(cpu_features) target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS}) target_include_directories(cpu_features PUBLIC $ ) + # # program : list_cpu_features # -add_executable(list_cpu_features src/utils/list_cpu_features.c) +add_executable(list_cpu_features ${PROJECT_SOURCE_DIR}/src/utils/list_cpu_features.c) target_link_libraries(list_cpu_features PRIVATE cpu_features) add_executable(CpuFeature::list_cpu_features ALIAS list_cpu_features) # -# library : NDK compat +# ndk_compat # -if(ANDROID) - find_package(Threads) - set (NDK_COMPAT_HDRS ndk_compat/cpu-features.h) - set (NDK_COMPAT_SRCS - ndk_compat/cpu-features.c - $ - $ - ) - add_arch_sources(NDK_COMPAT_HDRS NDK_COMPAT_SRCS) - add_library(ndk_compat ${NDK_COMPAT_HDRS} ${NDK_COMPAT_SRCS}) - setup_include_and_definitions(ndk_compat) - target_link_libraries(ndk_compat PUBLIC ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) -endif() -# -# program : NDK compat test program -# -if(ANDROID AND ENABLE_TESTING) - add_executable(ndk-compat-test ndk_compat/ndk-compat-test.c) - target_link_libraries(ndk-compat-test PRIVATE ndk_compat) +if(ANDROID) +add_subdirectory(ndk_compat) endif() # @@ -205,7 +190,7 @@ if(BUILD_TESTING) endif() # -# Install +# Install cpu_features and list_cpu_features # include(GNUInstallDirs) @@ -234,8 +219,8 @@ write_basic_package_version_file( ) install( FILES - "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake" - "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake" + "${PROJECT_BINARY_DIR}/CpuFeaturesConfig.cmake" + "${PROJECT_BINARY_DIR}/CpuFeaturesConfigVersion.cmake" DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeatures" COMPONENT Devel ) diff --git a/README.md b/README.md index 039175b..29d7946 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ instructions) at runtime. - [Code samples](#codesample) - [Running sample code](#usagesample) - [What's supported](#support) +- [Android NDK's drop in replacement](#ndk) - [License](#license) - [Build with cmake](#cmake) @@ -132,7 +133,7 @@ flags : aes,avx,cx16,smx,sse4_1,sse4_2,ssse3 ## What's supported -| | x86³ | ARM | AArch64 | MIPSel | POWER | +| | x86³ | ARM | AArch64 | MIPS⁴ | POWER | |---------|:----:|:-------:|:-------:|:------:|:-------:| | Android | yes² | yes¹ | yes¹ | yes¹ | N/A | | iOS | N/A | not yet | not yet | N/A | N/A | @@ -151,7 +152,15 @@ flags : aes,avx,cx16,smx,sse4_1,sse4_2,ssse3 3. **Microarchitecture detection.** On x86 some features are not always implemented efficiently in hardware (e.g. AVX on Sandybridge). Exposing the microarchitecture allows the client to reject particular microarchitectures. +4. All flavors of Mips are supported, little and big endian as well as 32/64 + bits. + +## Android NDK's drop in replacement + +[cpu_features](https://github.com/google/cpu_features) is now officially +supporting Android and offers a drop in replacement of for the NDK's [cpu-features.h](https://android.googlesource.com/platform/ndk/+/master/sources/android/cpufeatures/cpu-features.h) +, see [ndk_compat](ndk_compat) folder for details. ## License diff --git a/cmake/CpuFeaturesNdkCompatConfig.cmake.in b/cmake/CpuFeaturesNdkCompatConfig.cmake.in new file mode 100644 index 0000000..5a53ffd --- /dev/null +++ b/cmake/CpuFeaturesNdkCompatConfig.cmake.in @@ -0,0 +1,3 @@ +# CpuFeaturesNdkCompat CMake configuration file + +include("${CMAKE_CURRENT_LIST_DIR}/CpuFeaturesNdkCompatTargets.cmake") diff --git a/ndk_compat/CMakeLists.txt b/ndk_compat/CMakeLists.txt new file mode 100644 index 0000000..d95e523 --- /dev/null +++ b/ndk_compat/CMakeLists.txt @@ -0,0 +1,59 @@ + +# +# library : NDK compat +# +find_package(Threads REQUIRED) +set (NDK_COMPAT_HDRS cpu-features.h) +set (NDK_COMPAT_SRCS + cpu-features.c + $ + $ +) +# Note that following `add_cpu_features_headers_and_sources` will use +# NDK_COMPAT_SRCS in lieu of NDK_COMPAT_HDRS because we don't want cpu_features +# headers to be installed alongside ndk_compat. +add_cpu_features_headers_and_sources(NDK_COMPAT_SRCS NDK_COMPAT_SRCS) +add_library(ndk_compat ${NDK_COMPAT_HDRS} ${NDK_COMPAT_SRCS}) +setup_include_and_definitions(ndk_compat) +target_link_libraries(ndk_compat PUBLIC ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT}) +set_target_properties(ndk_compat PROPERTIES PUBLIC_HEADER "${NDK_COMPAT_HDRS}") + +include(GNUInstallDirs) +install(TARGETS ndk_compat + EXPORT CpuFeaturesNdkCompatTargets + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ndk_compat + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} +) +install(EXPORT CpuFeaturesNdkCompatTargets + NAMESPACE CpuFeatures:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeaturesNdkCompat + COMPONENT Devel +) +include(CMakePackageConfigHelpers) +configure_package_config_file(${PROJECT_SOURCE_DIR}/cmake/CpuFeaturesNdkCompatConfig.cmake.in + "${PROJECT_BINARY_DIR}/CpuFeaturesNdkCompatConfig.cmake" + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeaturesNdkCompat" + NO_SET_AND_CHECK_MACRO + NO_CHECK_REQUIRED_COMPONENTS_MACRO +) +write_basic_package_version_file( + "${PROJECT_BINARY_DIR}/CpuFeaturesNdkCompatConfigVersion.cmake" + COMPATIBILITY SameMajorVersion +) +install( + FILES + "${PROJECT_BINARY_DIR}/CpuFeaturesNdkCompatConfig.cmake" + "${PROJECT_BINARY_DIR}/CpuFeaturesNdkCompatConfigVersion.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/CpuFeaturesNdkCompat" + COMPONENT Devel +) + +# +# program : NDK compat test program +# +if(ENABLE_TESTING) + add_executable(ndk-compat-test ndk-compat-test.c) + target_link_libraries(ndk-compat-test PRIVATE ndk_compat) +endif() diff --git a/ndk_compat/cpu-features.h b/ndk_compat/cpu-features.h index 3ec22ea..51bea53 100644 --- a/ndk_compat/cpu-features.h +++ b/ndk_compat/cpu-features.h @@ -283,7 +283,9 @@ enum { ANDROID_CPU_MIPS_FEATURE_MSA = (1 << 1), }; -/* Return the number of CPU cores detected on this device. */ +/* Return the number of CPU cores detected on this device. + * Please note the current implementation supports up to 32 cpus. + */ extern int android_getCpuCount(void); /* The following is used to force the CPU count and features