1
0
mirror of https://github.com/google/cpu_features.git synced 2025-04-27 15:12:30 +02:00

Use Cmake macros to detect getauxval and dlopen

This commit is contained in:
Guillaume Chatelet 2019-01-15 15:18:08 +01:00
parent e8e5610fc4
commit 122b067087
3 changed files with 19 additions and 21 deletions

View File

@ -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.

View File

@ -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 <sys/auxv.h>
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 <dlfcn.h>
#include <sys/auxv.h>
static unsigned long GetElfHwcapFromGetauxval(uint32_t hwcap_type) {
return getauxval(hwcap_type);
}
#else
#error "This platform does not provide hardware capabilities."
#endif

View File

@ -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()