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