• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# cpu_features, a cross platform C99 library to get cpu features at runtime.
2
3load("@bazel_skylib//lib:selects.bzl", "selects")
4load("//:bazel/platforms.bzl", "PLATFORM_CPU_ARM", "PLATFORM_CPU_ARM64", "PLATFORM_CPU_MIPS", "PLATFORM_CPU_PPC", "PLATFORM_CPU_X86_64")
5
6package(
7    default_visibility = ["//visibility:public"],
8    licenses = ["notice"],
9)
10
11exports_files(["LICENSE"])
12
13INCLUDES = ["include"]
14
15C99_FLAGS = [
16    "-std=c99",
17    "-Wall",
18    "-Wextra",
19    "-Wmissing-declarations",
20    "-Wmissing-prototypes",
21    "-Wno-implicit-fallthrough",
22    "-Wno-unused-function",
23    "-Wold-style-definition",
24    "-Wshadow",
25    "-Wsign-compare",
26    "-Wstrict-prototypes",
27]
28
29cc_library(
30    name = "cpu_features_macros",
31    copts = C99_FLAGS,
32    includes = INCLUDES,
33    textual_hdrs = ["include/cpu_features_macros.h"],
34)
35
36cc_library(
37    name = "cpu_features_cache_info",
38    copts = C99_FLAGS,
39    includes = INCLUDES,
40    textual_hdrs = ["include/cpu_features_cache_info.h"],
41    deps = [":cpu_features_macros"],
42)
43
44cc_library(
45    name = "bit_utils",
46    copts = C99_FLAGS,
47    includes = INCLUDES,
48    textual_hdrs = ["include/internal/bit_utils.h"],
49    deps = [":cpu_features_macros"],
50)
51
52cc_test(
53    name = "bit_utils_test",
54    srcs = ["test/bit_utils_test.cc"],
55    includes = INCLUDES,
56    deps = [
57        ":bit_utils",
58        "@com_google_googletest//:gtest_main",
59    ],
60)
61
62cc_library(
63    name = "memory_utils",
64    copts = C99_FLAGS,
65    includes = INCLUDES,
66    textual_hdrs = [
67        "src/copy.inl",
68        "src/equals.inl",
69    ],
70)
71
72cc_library(
73    name = "string_view",
74    srcs = [
75        "src/string_view.c",
76    ],
77    copts = C99_FLAGS,
78    includes = INCLUDES,
79    textual_hdrs = ["include/internal/string_view.h"],
80    deps = [
81        ":cpu_features_macros",
82        ":memory_utils",
83    ],
84)
85
86cc_test(
87    name = "string_view_test",
88    srcs = ["test/string_view_test.cc"],
89    includes = INCLUDES,
90    deps = [
91        ":string_view",
92        "@com_google_googletest//:gtest_main",
93    ],
94)
95
96cc_library(
97    name = "filesystem",
98    srcs = ["src/filesystem.c"],
99    copts = C99_FLAGS,
100    includes = INCLUDES,
101    textual_hdrs = ["include/internal/filesystem.h"],
102    deps = [":cpu_features_macros"],
103)
104
105cc_library(
106    name = "filesystem_for_testing",
107    testonly = 1,
108    srcs = [
109        "src/filesystem.c",
110        "test/filesystem_for_testing.cc",
111    ],
112    hdrs = [
113        "include/internal/filesystem.h",
114        "test/filesystem_for_testing.h",
115    ],
116    defines = ["CPU_FEATURES_MOCK_FILESYSTEM"],
117    includes = INCLUDES,
118    deps = [
119        ":cpu_features_macros",
120    ],
121)
122
123cc_library(
124    name = "stack_line_reader",
125    srcs = ["src/stack_line_reader.c"],
126    copts = C99_FLAGS,
127    defines = ["STACK_LINE_READER_BUFFER_SIZE=1024"],
128    includes = INCLUDES,
129    textual_hdrs = ["include/internal/stack_line_reader.h"],
130    deps = [
131        ":cpu_features_macros",
132        ":filesystem",
133        ":string_view",
134    ],
135)
136
137cc_test(
138    name = "stack_line_reader_test",
139    srcs = [
140        "include/internal/stack_line_reader.h",
141        "src/stack_line_reader.c",
142        "test/stack_line_reader_test.cc",
143    ],
144    defines = ["STACK_LINE_READER_BUFFER_SIZE=16"],
145    includes = INCLUDES,
146    deps = [
147        ":cpu_features_macros",
148        ":filesystem_for_testing",
149        ":string_view",
150        "@com_google_googletest//:gtest_main",
151    ],
152)
153
154cc_library(
155    name = "stack_line_reader_to_use_with_filesystem_for_testing",
156    testonly = 1,
157    srcs = ["src/stack_line_reader.c"],
158    hdrs = ["include/internal/stack_line_reader.h"],
159    copts = C99_FLAGS,
160    defines = ["STACK_LINE_READER_BUFFER_SIZE=1024"],
161    includes = INCLUDES,
162    deps = [
163        ":cpu_features_macros",
164        ":filesystem_for_testing",
165        ":string_view",
166    ],
167)
168
169cc_library(
170    name = "hwcaps",
171    srcs = ["src/hwcaps.c"],
172    copts = C99_FLAGS,
173    defines = ["HAVE_STRONG_GETAUXVAL"],
174    includes = INCLUDES,
175    textual_hdrs = ["include/internal/hwcaps.h"],
176    deps = [
177        ":cpu_features_macros",
178        ":filesystem",
179        ":string_view",
180    ],
181)
182
183cc_library(
184    name = "hwcaps_for_testing",
185    testonly = 1,
186    srcs = [
187        "src/hwcaps.c",
188        "test/hwcaps_for_testing.cc",
189    ],
190    hdrs = [
191        "include/internal/hwcaps.h",
192        "test/hwcaps_for_testing.h",
193    ],
194    defines = [
195        "CPU_FEATURES_MOCK_GET_ELF_HWCAP_FROM_GETAUXVAL",
196        "CPU_FEATURES_TEST",
197    ],
198    includes = INCLUDES,
199    deps = [
200        ":cpu_features_macros",
201        ":filesystem_for_testing",
202        ":string_view",
203    ],
204)
205
206cc_library(
207    name = "cpuinfo",
208    srcs = selects.with_or({
209        PLATFORM_CPU_X86_64: [
210            "src/impl_x86_freebsd.c",
211            "src/impl_x86_linux_or_android.c",
212            "src/impl_x86_macos.c",
213            "src/impl_x86_windows.c",
214        ],
215        PLATFORM_CPU_ARM: ["src/impl_arm_linux_or_android.c"],
216        PLATFORM_CPU_ARM64: ["src/impl_aarch64_linux_or_android.c"],
217        PLATFORM_CPU_MIPS: ["src/impl_mips_linux_or_android.c"],
218        PLATFORM_CPU_PPC: ["src/impl_ppc_linux.c"],
219    }),
220    copts = C99_FLAGS,
221    includes = INCLUDES,
222    textual_hdrs = selects.with_or({
223        PLATFORM_CPU_X86_64: [
224            "src/impl_x86__base_implementation.inl",
225            "include/cpuinfo_x86.h",
226            "include/internal/cpuid_x86.h",
227            "include/internal/windows_utils.h",
228        ],
229        PLATFORM_CPU_ARM: ["include/cpuinfo_arm.h"],
230        PLATFORM_CPU_ARM64: ["include/cpuinfo_aarch64.h"],
231        PLATFORM_CPU_MIPS: ["include/cpuinfo_mips.h"],
232        PLATFORM_CPU_PPC: ["include/cpuinfo_ppc.h"],
233    }) + [
234        "src/define_introspection.inl",
235        "src/define_introspection_and_hwcaps.inl",
236    ],
237    deps = [
238        ":bit_utils",
239        ":cpu_features_cache_info",
240        ":cpu_features_macros",
241        ":filesystem",
242        ":hwcaps",
243        ":memory_utils",
244        ":stack_line_reader",
245        ":string_view",
246    ],
247)
248
249cc_library(
250    name = "cpuinfo_for_testing",
251    testonly = 1,
252    srcs = selects.with_or({
253        PLATFORM_CPU_X86_64: [
254            "src/impl_x86_freebsd.c",
255            "src/impl_x86_linux_or_android.c",
256            "src/impl_x86_macos.c",
257            "src/impl_x86_windows.c",
258        ],
259        PLATFORM_CPU_ARM: ["src/impl_arm_linux_or_android.c"],
260        PLATFORM_CPU_ARM64: ["src/impl_aarch64_linux_or_android.c"],
261        PLATFORM_CPU_MIPS: ["src/impl_mips_linux_or_android.c"],
262        PLATFORM_CPU_PPC: ["src/impl_ppc_linux.c"],
263    }),
264    hdrs = selects.with_or({
265        PLATFORM_CPU_X86_64: [
266            "include/cpuinfo_x86.h",
267            "include/internal/cpuid_x86.h",
268            "include/internal/windows_utils.h",
269        ],
270        PLATFORM_CPU_ARM: ["include/cpuinfo_arm.h"],
271        PLATFORM_CPU_ARM64: ["include/cpuinfo_aarch64.h"],
272        PLATFORM_CPU_MIPS: ["include/cpuinfo_mips.h"],
273        PLATFORM_CPU_PPC: ["include/cpuinfo_ppc.h"],
274    }),
275    copts = C99_FLAGS,
276    defines = selects.with_or({
277        PLATFORM_CPU_X86_64: ["CPU_FEATURES_MOCK_CPUID_X86"],
278        "//conditions:default": [],
279    }),
280    includes = INCLUDES,
281    textual_hdrs = selects.with_or({
282        PLATFORM_CPU_X86_64: ["src/impl_x86__base_implementation.inl"],
283        "//conditions:default": [],
284    }) + [
285        "src/define_introspection.inl",
286        "src/define_introspection_and_hwcaps.inl",
287    ],
288    deps = [
289        ":bit_utils",
290        ":cpu_features_cache_info",
291        ":cpu_features_macros",
292        ":filesystem_for_testing",
293        ":hwcaps_for_testing",
294        ":memory_utils",
295        ":stack_line_reader_to_use_with_filesystem_for_testing",
296        ":string_view",
297    ],
298)
299
300cc_test(
301    name = "cpuinfo_test",
302    srcs = selects.with_or({
303        PLATFORM_CPU_ARM64: ["test/cpuinfo_aarch64_test.cc"],
304        PLATFORM_CPU_ARM: ["test/cpuinfo_arm_test.cc"],
305        PLATFORM_CPU_MIPS: ["test/cpuinfo_mips_test.cc"],
306        PLATFORM_CPU_PPC: ["test/cpuinfo_ppc_test.cc"],
307        PLATFORM_CPU_X86_64: ["test/cpuinfo_x86_test.cc"],
308    }),
309    includes = INCLUDES,
310    deps = [
311        ":cpuinfo_for_testing",
312        ":filesystem_for_testing",
313        ":hwcaps_for_testing",
314        ":string_view",
315        "@com_google_googletest//:gtest_main",
316    ],
317)
318
319cc_binary(
320    name = "list_cpu_features",
321    srcs = ["src/utils/list_cpu_features.c"],
322    copts = C99_FLAGS,
323    includes = INCLUDES,
324    deps = [
325        ":bit_utils",
326        ":cpu_features_macros",
327        ":cpuinfo",
328    ],
329)
330