1
0
mirror of https://github.com/google/cpu_features.git synced 2025-04-28 15:33:37 +02:00

Remove need for utsname (#136)

This commit is contained in:
Guillaume Chatelet 2020-10-09 22:40:06 +02:00 committed by GitHub
parent 4795373db2
commit e63405f118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 58 deletions

View File

@ -91,11 +91,6 @@ macro(add_cpu_features_headers_and_sources HDRS_LIST_NAME SRCS_LIST_NAME)
endif() endif()
endmacro() endmacro()
if(UNIX AND PROCESSOR_IS_X86)
check_include_file(sys/utsname.h HAVE_UTSNAME_H)
endif()
# #
# library : utils # library : utils
# #
@ -154,9 +149,6 @@ target_include_directories(cpu_features
PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cpu_features> PUBLIC $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/cpu_features>
) )
if(PROCESSOR_IS_X86) if(PROCESSOR_IS_X86)
if(HAVE_UTSNAME_H)
target_compile_definitions(cpu_features PRIVATE HAVE_UTSNAME_H)
endif()
if(APPLE) if(APPLE)
target_compile_definitions(cpu_features PRIVATE HAVE_SYSCTLBYNAME) target_compile_definitions(cpu_features PRIVATE HAVE_SYSCTLBYNAME)
endif() endif()

View File

@ -29,16 +29,18 @@
// microarchitectures. // microarchitectures.
#if defined(CPU_FEATURES_OS_WINDOWS) #if defined(CPU_FEATURES_OS_WINDOWS)
#include <windows.h> // IsProcessorFeaturePresent #include <windows.h> // IsProcessorFeaturePresent
#elif defined(HAVE_UTSNAME_H) #elif defined(CPU_FEATURES_OS_LINUX_OR_ANDROID)
#include <sys/utsname.h>
#include "internal/filesystem.h" // Needed to parse /proc/cpuinfo #include "internal/filesystem.h" // Needed to parse /proc/cpuinfo
#include "internal/stack_line_reader.h" // Needed to parse /proc/cpuinfo #include "internal/stack_line_reader.h" // Needed to parse /proc/cpuinfo
#include "internal/string_view.h" // Needed to parse /proc/cpuinfo #include "internal/string_view.h" // Needed to parse /proc/cpuinfo
#if defined(HAVE_SYSCTLBYNAME) #elif defined(CPU_FEATURES_OS_DARWIN)
#if !defined(HAVE_SYSCTLBYNAME)
#error "Darwin needs support for sysctlbyname"
#endif
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif // HAVE_SYSCTLBYNAME #else
#endif // HAVE_UTSNAME_H #error "Unsupported OS"
#endif // CPU_FEATURES_OS
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Definitions for CpuId and GetXCR0Eax. // Definitions for CpuId and GetXCR0Eax.
@ -1139,7 +1141,7 @@ static bool GetWindowsIsProcessorFeaturePresent(DWORD ProcessorFeature) {
#endif #endif
#endif // CPU_FEATURES_OS_WINDOWS #endif // CPU_FEATURES_OS_WINDOWS
#if defined(CPU_FEATURES_OS_DARWIN) && defined(HAVE_SYSCTLBYNAME) #if defined(CPU_FEATURES_OS_DARWIN)
#if defined(CPU_FEATURES_MOCK_CPUID_X86) #if defined(CPU_FEATURES_MOCK_CPUID_X86)
extern bool GetDarwinSysCtlByName(const char*); extern bool GetDarwinSysCtlByName(const char*);
#else // CPU_FEATURES_MOCK_CPUID_X86 #else // CPU_FEATURES_MOCK_CPUID_X86
@ -1150,7 +1152,7 @@ static bool GetDarwinSysCtlByName(const char* name) {
return failure ? false : enabled; return failure ? false : enabled;
} }
#endif #endif
#endif // CPU_FEATURES_OS_DARWIN && HAVE_SYSCTLBYNAME #endif // CPU_FEATURES_OS_DARWIN
static void DetectSseViaOs(X86Features* features) { static void DetectSseViaOs(X86Features* features) {
#if defined(CPU_FEATURES_OS_WINDOWS) #if defined(CPU_FEATURES_OS_WINDOWS)
@ -1161,50 +1163,40 @@ static void DetectSseViaOs(X86Features* features) {
GetWindowsIsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE); GetWindowsIsProcessorFeaturePresent(PF_XMMI64_INSTRUCTIONS_AVAILABLE);
features->sse3 = features->sse3 =
GetWindowsIsProcessorFeaturePresent(PF_SSE3_INSTRUCTIONS_AVAILABLE); GetWindowsIsProcessorFeaturePresent(PF_SSE3_INSTRUCTIONS_AVAILABLE);
#elif defined(HAVE_UTSNAME_H) #elif defined(CPU_FEATURES_OS_DARWIN)
struct utsname buf; // Handling Darwin platform through sysctlbyname.
uname(&buf); features->sse = GetDarwinSysCtlByName("hw.optional.sse");
#if defined(CPU_FEATURES_OS_DARWIN) && defined(HAVE_SYSCTLBYNAME) features->sse2 = GetDarwinSysCtlByName("hw.optional.sse2");
if (CpuFeatures_StringView_IsEquals(str(buf.sysname), str("Darwin"))) { features->sse3 = GetDarwinSysCtlByName("hw.optional.sse3");
// Handling Darwin platform through sysctlbyname when available. features->ssse3 = GetDarwinSysCtlByName("hw.optional.supplementalsse3");
features->sse = GetDarwinSysCtlByName("hw.optional.sse"); features->sse4_1 = GetDarwinSysCtlByName("hw.optional.sse4_1");
features->sse2 = GetDarwinSysCtlByName("hw.optional.sse2"); features->sse4_2 = GetDarwinSysCtlByName("hw.optional.sse4_2");
features->sse3 = GetDarwinSysCtlByName("hw.optional.sse3");
features->ssse3 = GetDarwinSysCtlByName("hw.optional.supplementalsse3");
features->sse4_1 = GetDarwinSysCtlByName("hw.optional.sse4_1");
features->sse4_2 = GetDarwinSysCtlByName("hw.optional.sse4_2");
}
#elif defined(CPU_FEATURES_OS_LINUX_OR_ANDROID) #elif defined(CPU_FEATURES_OS_LINUX_OR_ANDROID)
if (CpuFeatures_StringView_IsEquals(str(buf.sysname), str("Linux"))) { // Handling Linux platform through /proc/cpuinfo.
// Handling Linux platform through /proc/cpuinfo when available. const int fd = CpuFeatures_OpenFile("/proc/cpuinfo");
const int fd = CpuFeatures_OpenFile("/proc/cpuinfo"); if (fd >= 0) {
if (fd >= 0) { StackLineReader reader;
StackLineReader reader; StackLineReader_Initialize(&reader, fd);
StackLineReader_Initialize(&reader, fd); for (;;) {
for (;;) { const LineResult result = StackLineReader_NextLine(&reader);
const LineResult result = StackLineReader_NextLine(&reader); const StringView line = result.line;
const StringView line = result.line; StringView key, value;
StringView key, value; if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) {
if (CpuFeatures_StringView_GetAttributeKeyValue(line, &key, &value)) { if (CpuFeatures_StringView_IsEquals(key, str("flags"))) {
if (CpuFeatures_StringView_IsEquals(key, str("flags"))) { features->sse = CpuFeatures_StringView_HasWord(value, "sse");
features->sse = CpuFeatures_StringView_HasWord(value, "sse"); features->sse2 = CpuFeatures_StringView_HasWord(value, "sse2");
features->sse2 = CpuFeatures_StringView_HasWord(value, "sse2"); features->sse3 = CpuFeatures_StringView_HasWord(value, "sse3");
features->sse3 = CpuFeatures_StringView_HasWord(value, "sse3"); features->ssse3 = CpuFeatures_StringView_HasWord(value, "ssse3");
features->ssse3 = CpuFeatures_StringView_HasWord(value, "ssse3"); features->sse4_1 = CpuFeatures_StringView_HasWord(value, "sse4_1");
features->sse4_1 = CpuFeatures_StringView_HasWord(value, "sse4_1"); features->sse4_2 = CpuFeatures_StringView_HasWord(value, "sse4_2");
features->sse4_2 = CpuFeatures_StringView_HasWord(value, "sse4_2"); break;
break;
}
} }
if (result.eof) break;
} }
CpuFeatures_CloseFile(fd); if (result.eof) break;
} }
CpuFeatures_CloseFile(fd);
} }
#else // CPU_FEATURES_OS_DARWIN || CPU_FEATURES_OS_LINUX_OR_ANDROID #else
#error "Unsupported fallback detection of SSE OS support."
#endif
#else // HAVE_UTSNAME_H
#error "Unsupported fallback detection of SSE OS support." #error "Unsupported fallback detection of SSE OS support."
#endif #endif
} }

View File

@ -55,9 +55,6 @@ add_test(NAME unix_features_aggregator_test COMMAND unix_features_aggregator_tes
if(PROCESSOR_IS_X86) if(PROCESSOR_IS_X86)
add_executable(cpuinfo_x86_test cpuinfo_x86_test.cc ../src/cpuinfo_x86.c) add_executable(cpuinfo_x86_test cpuinfo_x86_test.cc ../src/cpuinfo_x86.c)
target_compile_definitions(cpuinfo_x86_test PUBLIC CPU_FEATURES_MOCK_CPUID_X86) target_compile_definitions(cpuinfo_x86_test PUBLIC CPU_FEATURES_MOCK_CPUID_X86)
if(HAVE_UTSNAME_H)
target_compile_definitions(cpuinfo_x86_test PRIVATE HAVE_UTSNAME_H)
endif()
if(APPLE) if(APPLE)
target_compile_definitions(cpuinfo_x86_test PRIVATE HAVE_SYSCTLBYNAME) target_compile_definitions(cpuinfo_x86_test PRIVATE HAVE_SYSCTLBYNAME)
endif() endif()