Skip to content

Commit

Permalink
Separate Hwacps to freebsd and linux implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Hohsdze authored and toor1245 committed Sep 15, 2023
1 parent 2b7c47a commit befb237
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 50 deletions.
17 changes: 14 additions & 3 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

load("@bazel_skylib//lib:selects.bzl", "selects")
load("//:bazel/platforms.bzl", "PLATFORM_CPU_ARM", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_MIPS", "PLATFORM_CPU_PPC", "PLATFORM_CPU_RISCV32", "PLATFORM_CPU_RISCV64", "PLATFORM_CPU_X86_64")
load("//:bazel/platforms.bzl", "PLATFORM_OS_MACOS")
load("//:bazel/platforms.bzl", "PLATFORM_OS_MACOS", "PLATFORM_OS_LINUX", "PLATFORM_OS_FREEBSD", "PLATFORM_OS_ANDROID")

package(
default_visibility = ["//visibility:public"],
Expand Down Expand Up @@ -169,11 +169,18 @@ cc_library(

cc_library(
name = "hwcaps",
srcs = ["src/hwcaps.c"],
srcs = [
"src/hwcaps.c",
"src/hwcaps_freebsd.c",
"src/hwcaps_linux_or_android.c",
],
copts = C99_FLAGS,
defines = selects.with_or({
PLATFORM_OS_MACOS: ["HAVE_DLFCN_H"],
"//conditions:default": ["HAVE_STRONG_GETAUXVAL"],
PLATFORM_OS_FREEBSD: ["HAVE_STRONG_ELF_AUX_INFO"],
PLATFORM_OS_LINUX: ["HAVE_STRONG_GETAUXVAL"],
PLATFORM_OS_ANDROID: ["HAVE_STRONG_GETAUXVAL"],
"//conditions:default": [],
}),
includes = INCLUDES,
textual_hdrs = ["include/internal/hwcaps.h"],
Expand All @@ -189,6 +196,8 @@ cc_library(
testonly = 1,
srcs = [
"src/hwcaps.c",
"src/hwcaps_freebsd.c",
"src/hwcaps_linux_or_android.c",
"test/hwcaps_for_testing.cc",
],
hdrs = [
Expand Down Expand Up @@ -221,6 +230,7 @@ cc_library(
"src/impl_aarch64_linux_or_android.c",
"src/impl_aarch64_macos_or_iphone.c",
"src/impl_aarch64_windows.c",
"src/impl_aarch64_freebsd.c",
],
PLATFORM_CPU_MIPS: ["src/impl_mips_linux_or_android.c"],
PLATFORM_CPU_PPC: ["src/impl_ppc_linux.c"],
Expand Down Expand Up @@ -281,6 +291,7 @@ cc_library(
"src/impl_aarch64_linux_or_android.c",
"src/impl_aarch64_macos_or_iphone.c",
"src/impl_aarch64_windows.c",
"src/impl_aarch64_freebsd.c",
],
PLATFORM_CPU_MIPS: ["src/impl_mips_linux_or_android.c"],
PLATFORM_CPU_PPC: ["src/impl_ppc_linux.c"],
Expand Down
4 changes: 1 addition & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ setup_include_and_definitions(utils)
if(UNIX)
add_library(unix_based_hardware_detection OBJECT
${PROJECT_SOURCE_DIR}/include/internal/hwcaps.h
${PROJECT_SOURCE_DIR}/include/internal/hwcaps_linux_or_android.h
${PROJECT_SOURCE_DIR}/src/hwcaps_linux_or_android.c
${PROJECT_SOURCE_DIR}/src/hwcaps_freebsd.c
${PROJECT_SOURCE_DIR}/src/hwcaps.c
Expand All @@ -170,8 +169,7 @@ endif()
# library : cpu_features
#
set (CPU_FEATURES_HDRS)
set (CPU_FEATURES_SRCS
include/internal/hwcaps_linux_or_android.h)
set (CPU_FEATURES_SRCS)
add_cpu_features_headers_and_sources(CPU_FEATURES_HDRS CPU_FEATURES_SRCS)
list(APPEND CPU_FEATURES_SRCS $<TARGET_OBJECTS:utils>)
if(NOT PROCESSOR_IS_X86 AND UNIX)
Expand Down
6 changes: 6 additions & 0 deletions bazel/platforms.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ PLATFORM_CPU_RISCV64 = ("@platforms//cpu:riscv64")


PLATFORM_OS_MACOS = ("@platforms//os:macos")

PLATFORM_OS_LINUX = ("@platforms//os:linux")

PLATFORM_OS_ANDROID = ("@platforms//os:android")

PLATFORM_OS_FREEBSD = ("@platforms//os:freebsd")
5 changes: 5 additions & 0 deletions include/internal/hwcaps.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void);
bool CpuFeatures_IsHwCapsSet(const HardwareCapabilities hwcaps_mask,
const HardwareCapabilities hwcaps);

// Get pointer for the AT_PLATFORM type.
const char* CpuFeatures_GetPlatformPointer(void);
// Get pointer for the AT_BASE_PLATFORM type.
const char* CpuFeatures_GetBasePlatformPointer(void);

CPU_FEATURES_END_CPP_NAMESPACE

#endif // CPU_FEATURES_INCLUDE_INTERNAL_HWCAPS_H_
25 changes: 0 additions & 25 deletions include/internal/hwcaps_linux_or_android.h

This file was deleted.

2 changes: 2 additions & 0 deletions src/hwcaps.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "internal/hwcaps.h"

#include <stdbool.h>

static bool IsSet(const uint32_t mask, const uint32_t value) {
if (mask == 0) return false;
return (value & mask) == mask;
Expand Down
22 changes: 17 additions & 5 deletions src/hwcaps_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
#include "cpu_features_macros.h"

#ifdef CPU_FEATURES_ARCH_AARCH64
#if defined(CPU_FEATURES_OS_FREEBSD)
#ifdef CPU_FEATURES_OS_FREEBSD

#if defined(HAVE_STRONG_ELF_AUX_INFO)
#include "internal/hwcaps.h"

#ifdef CPU_FEATURES_TEST
// In test mode, hwcaps_for_testing will define the following functions.
HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void);
const char* CpuFeatures_GetPlatformPointer(void);
const char* CpuFeatures_GetBasePlatformPointer(void);
#else

#ifdef HAVE_STRONG_ELF_AUX_INFO
#include <stddef.h>
#include <sys/auxv.h>

#include "internal/hwcaps.h"

static unsigned long GetElfHwcapFromElfAuxInfo(int hwcap_type) {
unsigned long hwcap;
elf_aux_info(hwcap_type, &hwcap, sizeof(hwcap));
Expand All @@ -35,9 +42,14 @@ HardwareCapabilities CpuFeatures_GetHardwareCapabilities(void) {
capabilities.hwcaps2 = GetElfHwcapFromElfAuxInfo(AT_HWCAP2);
return capabilities;
}

const char *CpuFeatures_GetPlatformPointer(void) { return NULL; }

const char *CpuFeatures_GetBasePlatformPointer(void) { return NULL; }
#else
#error "Error"
#error "FreeBSD needs support for elf_aux_info"
#endif // HAVE_STRONG_ELF_AUX_INFO

#endif // CPU_FEATURES_TEST
#endif // CPU_FEATURES_OS_FREEBSD
#endif // CPU_FEATURES_ARCH_AARCH64
5 changes: 1 addition & 4 deletions src/hwcaps_linux_or_android.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@

#include "cpu_features_macros.h"

#ifdef CPU_FEATURES_ARCH_AARCH64
#if defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)

#include "internal/hwcaps_linux_or_android.h"

#include <stdlib.h>
#include <string.h>

#include "cpu_features_macros.h"
#include "internal/filesystem.h"
#include "internal/hwcaps.h"
#include "internal/string_view.h"

#ifdef CPU_FEATURES_TEST
Expand Down Expand Up @@ -157,4 +155,3 @@ const char *CpuFeatures_GetBasePlatformPointer(void) {

#endif // CPU_FEATURES_TEST
#endif // defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_ANDROID)
#endif // CPU_FEATURES_ARCH_AARCH64
1 change: 0 additions & 1 deletion src/impl_aarch64_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#ifdef CPU_FEATURES_OS_FREEBSD

#include "cpuinfo_aarch64.h"

#include "impl_aarch64__base_implementation.inl"

static const Aarch64Info kEmptyAarch64Info;
Expand Down
2 changes: 1 addition & 1 deletion src/impl_ppc_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

#include "internal/bit_utils.h"
#include "internal/filesystem.h"
#include "internal/hwcaps_linux_or_android.h"
#include "internal/hwcaps.h"
#include "internal/stack_line_reader.h"
#include "internal/string_view.h"

Expand Down
2 changes: 1 addition & 1 deletion src/impl_s390x_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

#include "internal/bit_utils.h"
#include "internal/filesystem.h"
#include "internal/hwcaps_linux_or_android.h"
#include "internal/hwcaps.h"
#include "internal/stack_line_reader.h"
#include "internal/string_view.h"

Expand Down
6 changes: 5 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ add_library(stack_line_reader_for_test ../src/stack_line_reader.c)
target_compile_definitions(stack_line_reader_for_test PUBLIC STACK_LINE_READER_BUFFER_SIZE=16)
target_link_libraries(stack_line_reader_for_test string_view filesystem_for_testing)
##------------------------------------------------------------------------------
add_library(all_libraries ../src/hwcaps.c ../src/stack_line_reader.c)
add_library(all_libraries
../src/hwcaps.c
../src/hwcaps_linux_or_android.c
../src/hwcaps_freebsd.c
../src/stack_line_reader.c)
target_link_libraries(all_libraries hwcaps_for_testing stack_line_reader string_view)

#
Expand Down
11 changes: 5 additions & 6 deletions test/cpuinfo_aarch64_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,8 @@ TEST_F(CpuidAarch64Test, Aarch64FeaturesEnum) {
}
}

#if defined(CPU_FEATURES_OS_LINUX)
void DisableHardwareCapabilities() { SetHardwareCapabilities(0, 0); }

// OS dependent tests
#if defined(CPU_FEATURES_OS_LINUX)
// AT_HWCAP tests
#if defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_FREEBSD)
TEST_F(CpuidAarch64Test, FromHardwareCap) {
ResetHwcaps();
SetHardwareCapabilities(AARCH64_HWCAP_FP | AARCH64_HWCAP_AES, 0);
Expand Down Expand Up @@ -199,7 +196,10 @@ TEST_F(CpuidAarch64Test, FromHardwareCap2) {
EXPECT_FALSE(info.features.dgh);
EXPECT_FALSE(info.features.rng);
}
#endif // defined(CPU_FEATURES_OS_LINUX) || defined(CPU_FEATURES_OS_FREEBSD)

// OS dependent tests
#if defined(CPU_FEATURES_OS_LINUX)
TEST_F(CpuidAarch64Test, ARMCortexA53) {
ResetHwcaps();
auto& fs = GetEmptyFilesystem();
Expand Down Expand Up @@ -386,7 +386,6 @@ TEST_F(CpuidAarch64Test, WINDOWS_AARCH64_RPI4) {
EXPECT_FALSE(info.features.jscvt);
EXPECT_FALSE(info.features.lrcpc);
}
#endif
#endif // CPU_FEATURES_OS_WINDOWS
} // namespace
} // namespace cpu_features

0 comments on commit befb237

Please sign in to comment.