1
0
mirror of https://github.com/google/cpu_features.git synced 2025-07-01 05:11:15 +02:00

[NFC] Add bazel support to cpu_features (#222)

This commit is contained in:
Guillaume Chatelet
2022-02-03 13:56:31 +01:00
committed by GitHub
parent 5f5e6d620f
commit e38dc6d2a8
4 changed files with 360 additions and 0 deletions

6
.gitignore vendored
View File

@ -11,3 +11,9 @@ out/
.vscode/
.vs/
*.swp
# Bazel artifacts
**/bazel-*
# Per-user bazelrc files
user.bazelrc

320
BUILD.bazel Normal file
View File

@ -0,0 +1,320 @@
# cpu_features, a cross platform C99 library to get cpu features at runtime.
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_X86_64")
package(
default_visibility = ["//visibility:public"],
licenses = ["notice"],
)
exports_files(["LICENSE"])
CC_FLAGS = [
"-Iinclude",
]
C99_FLAGS = CC_FLAGS + [
"-std=c99",
"-Wall",
"-Wextra",
"-Wmissing-declarations",
"-Wmissing-prototypes",
"-Wno-implicit-fallthrough",
"-Wno-unused-function",
"-Wold-style-definition",
"-Wshadow",
"-Wsign-compare",
"-Wstrict-prototypes",
]
cc_library(
name = "cpu_features_macros",
hdrs = ["include/cpu_features_macros.h"],
copts = C99_FLAGS,
)
cc_library(
name = "cpu_features_cache_info",
hdrs = ["include/cpu_features_cache_info.h"],
copts = C99_FLAGS,
deps = [":cpu_features_macros"],
)
cc_library(
name = "bit_utils",
hdrs = ["include/internal/bit_utils.h"],
copts = C99_FLAGS,
deps = [":cpu_features_macros"],
)
cc_test(
name = "bit_utils_test",
srcs = ["test/bit_utils_test.cc"],
copts = CC_FLAGS,
deps = [
":bit_utils",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "memory_utils",
copts = C99_FLAGS,
textual_hdrs = [
"src/copy.inl",
"src/equals.inl",
],
)
cc_library(
name = "string_view",
srcs = [
"src/string_view.c",
],
hdrs = ["include/internal/string_view.h"],
copts = C99_FLAGS,
deps = [
":cpu_features_macros",
":memory_utils",
],
)
cc_test(
name = "string_view_test",
srcs = ["test/string_view_test.cc"],
copts = CC_FLAGS,
deps = [
":string_view",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "filesystem",
srcs = ["src/filesystem.c"],
hdrs = ["include/internal/filesystem.h"],
copts = C99_FLAGS,
deps = [":cpu_features_macros"],
)
cc_library(
name = "filesystem_for_testing",
testonly = 1,
srcs = [
"src/filesystem.c",
"test/filesystem_for_testing.cc",
],
hdrs = [
"include/internal/filesystem.h",
"test/filesystem_for_testing.h",
],
copts = CC_FLAGS,
defines = ["CPU_FEATURES_MOCK_FILESYSTEM"],
deps = [
":cpu_features_macros",
],
)
cc_library(
name = "stack_line_reader",
srcs = ["src/stack_line_reader.c"],
hdrs = ["include/internal/stack_line_reader.h"],
copts = C99_FLAGS,
defines = ["STACK_LINE_READER_BUFFER_SIZE=1024"],
deps = [
":cpu_features_macros",
":filesystem",
":string_view",
],
)
cc_test(
name = "stack_line_reader_test",
srcs = [
"include/internal/stack_line_reader.h",
"src/stack_line_reader.c",
"test/stack_line_reader_test.cc",
],
copts = CC_FLAGS,
defines = ["STACK_LINE_READER_BUFFER_SIZE=16"],
deps = [
":cpu_features_macros",
":filesystem_for_testing",
":string_view",
"@com_google_googletest//:gtest_main",
],
)
cc_library(
name = "stack_line_reader_to_use_with_filesystem_for_testing",
testonly = 1,
srcs = ["src/stack_line_reader.c"],
hdrs = ["include/internal/stack_line_reader.h"],
copts = C99_FLAGS,
defines = ["STACK_LINE_READER_BUFFER_SIZE=1024"],
deps = [
":cpu_features_macros",
":filesystem_for_testing",
":string_view",
],
)
cc_library(
name = "hwcaps",
srcs = ["src/hwcaps.c"],
hdrs = ["include/internal/hwcaps.h"],
copts = C99_FLAGS,
defines = ["HAVE_STRONG_GETAUXVAL"],
deps = [
":cpu_features_macros",
":filesystem",
":string_view",
],
)
cc_library(
name = "hwcaps_for_testing",
testonly = 1,
srcs = [
"src/hwcaps.c",
"test/hwcaps_for_testing.cc",
],
hdrs = [
"include/internal/hwcaps.h",
"test/hwcaps_for_testing.h",
],
copts = CC_FLAGS,
defines = [
"CPU_FEATURES_MOCK_GET_ELF_HWCAP_FROM_GETAUXVAL",
"CPU_FEATURES_TEST",
],
deps = [
":cpu_features_macros",
":filesystem_for_testing",
":string_view",
],
)
cc_library(
name = "cpuinfo",
srcs = selects.with_or({
PLATFORM_CPU_X86_64: [
"src/impl_x86_freebsd.c",
"src/impl_x86_linux_or_android.c",
"src/impl_x86_macos.c",
"src/impl_x86_windows.c",
],
PLATFORM_CPU_ARM: ["src/impl_arm_linux_or_android.c"],
PLATFORM_CPU_ARM64: ["src/impl_aarch64_linux_or_android.c"],
PLATFORM_CPU_MIPS: ["src/impl_mips_linux_or_android.c"],
PLATFORM_CPU_PPC: ["src/impl_ppc_linux.c"],
}),
hdrs = selects.with_or({
PLATFORM_CPU_X86_64: [
"include/cpuinfo_x86.h",
"include/internal/cpuid_x86.h",
],
PLATFORM_CPU_ARM: ["include/cpuinfo_arm.h"],
PLATFORM_CPU_ARM64: ["include/cpuinfo_aarch64.h"],
PLATFORM_CPU_MIPS: ["include/cpuinfo_mips.h"],
PLATFORM_CPU_PPC: ["include/cpuinfo_ppc.h"],
}),
copts = C99_FLAGS,
textual_hdrs = selects.with_or({
PLATFORM_CPU_X86_64: ["src/impl_x86__base_implementation.inl"],
"//conditions:default": [],
}) + [
"src/define_introspection.inl",
"src/define_introspection_and_hwcaps.inl",
],
deps = [
":bit_utils",
":cpu_features_cache_info",
":cpu_features_macros",
":filesystem",
":hwcaps",
":memory_utils",
":stack_line_reader",
":string_view",
],
)
cc_library(
name = "cpuinfo_for_testing",
testonly = 1,
srcs = selects.with_or({
PLATFORM_CPU_X86_64: [
"src/impl_x86_freebsd.c",
"src/impl_x86_linux_or_android.c",
"src/impl_x86_macos.c",
"src/impl_x86_windows.c",
],
PLATFORM_CPU_ARM: ["src/impl_arm_linux_or_android.c"],
PLATFORM_CPU_ARM64: ["src/impl_aarch64_linux_or_android.c"],
PLATFORM_CPU_MIPS: ["src/impl_mips_linux_or_android.c"],
PLATFORM_CPU_PPC: ["src/impl_ppc_linux.c"],
}),
hdrs = selects.with_or({
PLATFORM_CPU_X86_64: [
"include/cpuinfo_x86.h",
"include/internal/cpuid_x86.h",
],
PLATFORM_CPU_ARM: ["include/cpuinfo_arm.h"],
PLATFORM_CPU_ARM64: ["include/cpuinfo_aarch64.h"],
PLATFORM_CPU_MIPS: ["include/cpuinfo_mips.h"],
PLATFORM_CPU_PPC: ["include/cpuinfo_ppc.h"],
}),
copts = C99_FLAGS,
defines = selects.with_or({
PLATFORM_CPU_X86_64: ["CPU_FEATURES_MOCK_CPUID_X86"],
"//conditions:default": [],
}),
textual_hdrs = selects.with_or({
PLATFORM_CPU_X86_64: ["src/impl_x86__base_implementation.inl"],
"//conditions:default": [],
}) + [
"src/define_introspection.inl",
"src/define_introspection_and_hwcaps.inl",
],
deps = [
":bit_utils",
":cpu_features_cache_info",
":cpu_features_macros",
":filesystem_for_testing",
":hwcaps_for_testing",
":memory_utils",
":stack_line_reader_to_use_with_filesystem_for_testing",
":string_view",
],
)
cc_test(
name = "cpuinfo_test",
srcs = selects.with_or({
PLATFORM_CPU_ARM64: ["test/cpuinfo_aarch64_test.cc"],
PLATFORM_CPU_ARM: ["test/cpuinfo_arm_test.cc"],
PLATFORM_CPU_MIPS: ["test/cpuinfo_mips_test.cc"],
PLATFORM_CPU_PPC: ["test/cpuinfo_ppc_test.cc"],
PLATFORM_CPU_X86_64: ["test/cpuinfo_x86_test.cc"],
}),
copts = CC_FLAGS,
deps = [
":cpuinfo_for_testing",
":filesystem_for_testing",
":hwcaps_for_testing",
":string_view",
"@com_google_googletest//:gtest_main",
],
)
cc_binary(
name = "list_cpu_features",
srcs = ["src/utils/list_cpu_features.c"],
copts = C99_FLAGS,
deps = [
":bit_utils",
":cpu_features_macros",
":cpuinfo",
],
)

23
WORKSPACE Normal file
View File

@ -0,0 +1,23 @@
workspace(name = "com_google_cpufeatures")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_google_googletest",
sha256 = "269cebe2be1f607f91f52630ff5bec3275b948c65d4bac323ebd05e90d84f7a9",
strip_prefix = "googletest-e2239ee6043f73722e7aa812a459f54a28552929",
urls = ["https://github.com/google/googletest/archive/e2239ee6043f73722e7aa812a459f54a28552929.zip"],
)
http_archive(
name = "bazel_skylib",
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
],
)
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
bazel_skylib_workspace()

11
bazel/platforms.bzl Normal file
View File

@ -0,0 +1,11 @@
"""Defines global variables that lists target cpus"""
PLATFORM_CPU_X86_64 = ("@platforms//cpu:x86_64")
PLATFORM_CPU_ARM = ("@platforms//cpu:arm")
PLATFORM_CPU_ARM64 = ("@platforms//cpu:arm64")
PLATFORM_CPU_MIPS = ("@platforms//cpu:mips64")
PLATFORM_CPU_PPC = ("@platforms//cpu:ppc")