From 122b06708722f075a2f7194e16f370dcf1dbd0c4 Mon Sep 17 00:00:00 2001 From: Guillaume Chatelet Date: Tue, 15 Jan 2019 15:18:08 +0100 Subject: [PATCH] Use Cmake macros to detect getauxval and dlopen --- CMakeLists.txt | 15 ++++++++++++--- src/hwcaps.c | 23 ++++++----------------- test/CMakeLists.txt | 2 +- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a5fbfc6..8150178 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,12 @@ option(BUILD_SHARED_LIBS "Build library as shared." OFF) # library : cpu_features # +include (CheckIncludeFile) +include (CheckSymbolExists) + +check_symbol_exists(getauxval sys/auxv.h HAVE_STRONG_GETAUXVAL) +check_include_file(dlfcn.h HAVE_DLFCN_H) + set(_HDRS include/cpu_features_macros.h ) @@ -55,7 +61,7 @@ elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)") list(APPEND _HDRS include/cpuinfo_ppc.h) list(APPEND _SRCS src/cpuinfo_ppc.c) else() - message(SEND_ERROR "Unsupported architectures ${CMAKE_SYSTEM_PROCESSOR}") + message(FATAL_ERROR "Unsupported architectures ${CMAKE_SYSTEM_PROCESSOR}") endif() add_library(cpu_features @@ -71,8 +77,11 @@ target_include_directories(cpu_features include/internal ) set_target_properties(cpu_features PROPERTIES PUBLIC_HEADER "${_HDRS}") -target_compile_definitions(cpu_features - PUBLIC STACK_LINE_READER_BUFFER_SIZE=1024) +target_compile_definitions(cpu_features PUBLIC + STACK_LINE_READER_BUFFER_SIZE=1024 + HAVE_STRONG_GETAUXVAL=${HAVE_STRONG_GETAUXVAL} + HAVE_DLFCN_H=${HAVE_DLFCN_H} +) target_link_libraries(cpu_features PUBLIC ${CMAKE_DL_LIBS}) # The use of shared libraries is discouraged. diff --git a/src/hwcaps.c b/src/hwcaps.c index d8c6df2..815e5c1 100644 --- a/src/hwcaps.c +++ b/src/hwcaps.c @@ -31,22 +31,18 @@ } while (0) #endif -#if defined(CPU_FEATURES_ARCH_MIPS) || defined(CPU_FEATURES_ARCH_ANY_ARM) -#define HWCAPS_ANDROID_MIPS_OR_ARM -#endif - -#if defined(CPU_FEATURES_OS_LINUX_OR_ANDROID) && \ - !defined(HWCAPS_ANDROID_MIPS_OR_ARM) -#define HWCAPS_REGULAR_LINUX -#endif - //////////////////////////////////////////////////////////////////////////////// // Implementation of GetElfHwcapFromGetauxval //////////////////////////////////////////////////////////////////////////////// #if defined(CPU_FEATURES_MOCK_GET_ELF_HWCAP_FROM_GETAUXVAL) // Implementation will be provided by test/hwcaps_for_testing.cc. -#elif defined(HWCAPS_ANDROID_MIPS_OR_ARM) +#elif defined(HAVE_STRONG_GETAUXVAL) +#include +static unsigned long GetElfHwcapFromGetauxval(uint32_t hwcap_type) { + return getauxval(hwcap_type); +} +#elif defined(HAVE_DLFCN_H) // On Android we probe the system's C library for a 'getauxval' function and // call it if it exits, or return 0 for failure. This function is available // since API level 20. @@ -91,13 +87,6 @@ static uint32_t GetElfHwcapFromGetauxval(uint32_t hwcap_type) { dlclose(libc_handle); return ret; } -#elif defined(CPU_FEATURES_OS_LINUX_OR_ANDROID) -// On Regular Linux we simply use getauxval. -#include -#include -static unsigned long GetElfHwcapFromGetauxval(uint32_t hwcap_type) { - return getauxval(hwcap_type); -} #else #error "This platform does not provide hardware capabilities." #endif diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b4e83f8..6f4a7cd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -58,7 +58,7 @@ add_test(NAME linux_features_aggregator_test COMMAND linux_features_aggregator_t ## cpuinfo_x86_test if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") add_executable(cpuinfo_x86_test cpuinfo_x86_test.cc ../src/cpuinfo_x86.c) -target_compile_definitions(stack_line_reader PUBLIC CPU_FEATURES_MOCK_CPUID_X86) +target_compile_definitions(cpuinfo_x86_test PUBLIC CPU_FEATURES_MOCK_CPUID_X86) target_link_libraries(cpuinfo_x86_test all_libraries) add_test(NAME cpuinfo_x86_test COMMAND cpuinfo_x86_test) endif()