Refactor architecture tests. This makes the call-sites of the macro a bit more straightforward to understand, but is otherwise exactly the same as before. PiperOrigin-RevId: 495507057
diff --git a/build_defs/arch_tests.bzl b/build_defs/arch_tests.bzl index ba67cc6..77c661a 100644 --- a/build_defs/arch_tests.bzl +++ b/build_defs/arch_tests.bzl
@@ -2,41 +2,54 @@ load("//build_defs:internal_shell.bzl", "inline_sh_test") -def _arch_test_impl( +_ARCHITECTURE_MATCHER = { + "aarch64": "ELF 64-bit LSB.* ARM aarch64", + "x86_64": "ELF 64-bit LSB.* x86_64", +} + +def binary_architecture_test( name, - platform, - file_platform, - bazel_binaries = [], + architecture, + binaries = [], system_binaries = [], + target_compatible_with = [], + tags = [], **kwargs): - """Bazel rule to verify that a Bazel or system binary is built for the aarch64 architecture. + """Bazel rule to verify that binaries are built for a specific architecture. + + This is used as a sanity test of our test infrastructure, to make sure that we're + actually cross-compiling for the correct platform. Args: name: the name of the test. - platform: a diagnostic name for this architecture. - file_platform: the expected output of `file`. - bazel_binaries: a set of binary targets to inspect. - system_binaries: a set of paths to system executables to inspect. + architecture: an identifier for the architecture being tested. + binaries: a set of binary targets to inspect. + system_binaries: a set of system executables to inspect. + target_compatible_with: config settings this test is compatible with. + tags: tags for filtering these tests. **kwargs: other keyword arguments that are passed to the test. """ + if architecture not in _ARCHITECTURE_MATCHER: + fail("Unknown architecture: %s", architecture) inline_sh_test( name = name, - tools = bazel_binaries, + tools = binaries, cmd = """ for binary in "%s"; do (file -L $$binary | grep -q "%s") \ || (echo "Test binary is not an %s binary: "; file -L $$binary; exit 1) done """ % ( - " ".join(["$(rootpaths %s)" % b for b in bazel_binaries] + system_binaries), - file_platform, - platform, + " ".join(["$(rootpaths %s)" % b for b in binaries] + system_binaries), + _ARCHITECTURE_MATCHER[architecture], + architecture, ), - target_compatible_with = select({ - "//build_defs:" + platform: [], - "//conditions:default": ["@platforms//:incompatible"], - }), + target_compatible_with = target_compatible_with, + tags = tags + [ + "binary_architecture_test", + "%s_test" % architecture, + ], **kwargs )
diff --git a/python/BUILD.bazel b/python/BUILD.bazel index cc0772c..552a536 100644 --- a/python/BUILD.bazel +++ b/python/BUILD.bazel
@@ -6,11 +6,12 @@ # //:protobuf_python # //:well_known_types_py_pb2 +load("@bazel_skylib//lib:selects.bzl", "selects") load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix") load("@rules_python//python:defs.bzl", "py_library") load("@pip_deps//:requirements.bzl", "requirement") load("//:protobuf.bzl", "internal_py_proto_library") -load("//build_defs:arch_tests.bzl", "aarch64_test", "x86_64_test") +load("//build_defs:arch_tests.bzl", "binary_architecture_test") load("//build_defs:cpp_opts.bzl", "COPTS") load("//conformance:defs.bzl", "conformance_test") load(":internal.bzl", "internal_copy_files", "internal_py_test") @@ -119,20 +120,38 @@ }), ) -aarch64_test( +selects.config_setting_group( + name = "_aarch64_and_fast_cpp", + match_all = ["//build_defs:aarch64", ":use_fast_cpp_protos"], +) +binary_architecture_test( name = "aarch64_test", - bazel_binaries = [ + architecture = "aarch64", + binaries = [ "google/protobuf/internal/_api_implementation.so", "google/protobuf/pyext/_message.so", ], + target_compatible_with = select({ + ":_aarch64_and_fast_cpp": [], + "//conditions:default": ["@platforms//:incompatible"], + }), ) -x86_64_test( +selects.config_setting_group( + name = "_x86_64_and_fast_cpp", + match_all = ["//build_defs:x86_64", ":use_fast_cpp_protos"], +) +binary_architecture_test( name = "x86_64_test", - bazel_binaries = [ + architecture = "x86_64", + binaries = [ "google/protobuf/internal/_api_implementation.so", "google/protobuf/pyext/_message.so", ], + target_compatible_with = select({ + ":_x86_64_and_fast_cpp": [], + "//conditions:default": ["@platforms//:incompatible"], + }), ) py_library(
diff --git a/src/google/protobuf/compiler/BUILD.bazel b/src/google/protobuf/compiler/BUILD.bazel index 8cc1186..3ef0414 100644 --- a/src/google/protobuf/compiler/BUILD.bazel +++ b/src/google/protobuf/compiler/BUILD.bazel
@@ -10,7 +10,7 @@ "strip_prefix", ) load("@rules_proto//proto:defs.bzl", "proto_library") -load("//build_defs:arch_tests.bzl", "aarch64_test", "x86_64_test") +load("//build_defs:arch_tests.bzl", "binary_architecture_test") load("//build_defs:cpp_opts.bzl", "COPTS", "LINK_OPTS", "PROTOC_LINK_OPTS") proto_library( @@ -144,14 +144,28 @@ ) # Test that the protoc binary is built for the correct architecture. -aarch64_test( - name = "protoc_aarch64_test", - bazel_binaries = ["//:protoc"], +binary_architecture_test( + name = "aarch64_test", + architecture = "aarch64", + binaries = [ + "//:protoc", + ], + target_compatible_with = select({ + "//build_defs:aarch64": [], + "//conditions:default": ["@platforms//:incompatible"], + }), ) -x86_64_test( - name = "protoc_x86_64_test", - bazel_binaries = ["//:protoc"], +binary_architecture_test( + name = "x86_64_test", + architecture = "x86_64", + binaries = [ + "//:protoc", + ], + target_compatible_with = select({ + "//build_defs:x86_64": [], + "//conditions:default": ["@platforms//:incompatible"], + }), ) ################################################################################