diff options
author | Cole Faust <colefaust@google.com> | 2023-03-21 12:09:34 -0700 |
---|---|---|
committer | Cole Faust <colefaust@google.com> | 2023-03-22 17:06:42 -0700 |
commit | 8f2368eb192c28e98e35eb014b0b2ee9cf416e70 (patch) | |
tree | bfea7f4642f889105cca078b62594a81ee566cae /platforms/platform_utils.bzl | |
parent | e8a8c9b8bfffe7200da375880b10ebb26f541756 (diff) | |
download | bazel-8f2368eb192c28e98e35eb014b0b2ee9cf416e70.tar.gz |
Make the secondary arch into a constraint setting
Currently, apex rules read the secondary arch directly from the
soong.variables file, which means that when transitioning into a
different product, the new secondary arch wouldn't be picked up.
Instead, make the secondary arch its own constraint setting/values.
One hiccup with this is that it's not really clear what to do when
there is no secondary arch. I think that making a "none" constraint
value might introduce more confusing behavior / edge cases, so instead
I decided to make the secondary arch match the primary arch when it
doesn't exist. The platform utils will convert it back to an empty
string to match the soong.variables behavior.
Bug: 269577299
Test: Diffed the result of `b build --config=android //build/bazel/vendor/google:build.bazel.examples.apex.minimal_apex_aab` before and after this cl. For some reason there is a diff in the apex.pb file in the aab, but it's just differences in the order of fields: https://diff.googleplex.com/#key=7rKBCiPxby9p
Change-Id: Ifc41045feef9d05691cf49bcfc1b1c2fac15177e
Diffstat (limited to 'platforms/platform_utils.bzl')
-rw-r--r-- | platforms/platform_utils.bzl | 51 |
1 files changed, 44 insertions, 7 deletions
diff --git a/platforms/platform_utils.bzl b/platforms/platform_utils.bzl index 556aa75f..c1c403e6 100644 --- a/platforms/platform_utils.bzl +++ b/platforms/platform_utils.bzl @@ -12,19 +12,25 @@ # See the License for the specific language governing permissions and # limitations under the License. -# platform_utils.bzl defines a platform_utils rule, and several -# utility functions that accept an instance of that rule and return -# information about the target platform. One instance of the platform_utils -# rule is defined in //build/bazel/platforms:platform_utils. All rules -# that need it can depend on that target, and then call the util -# functions by doing something like `is_target_linux(ctx.attr._platform_utils)`. -# This works because child targets inherit their parent's configuration. +""" +platform_utils.bzl defines a platform_utils rule, and several +utility functions that accept an instance of that rule and return +information about the target platform. One instance of the platform_utils +rule is defined in //build/bazel/platforms:platform_utils. All rules +that need it can depend on that target, and then call the util +functions by doing something like `is_target_linux(ctx.attr._platform_utils)`. +This works because child targets inherit their parent's configuration. +""" _name_to_constraint = { "_x86_constraint": "//build/bazel/platforms/arch:x86", "_x86_64_constraint": "//build/bazel/platforms/arch:x86_64", "_arm_constraint": "//build/bazel/platforms/arch:arm", "_arm64_constraint": "//build/bazel/platforms/arch:arm64", + "_secondary_x86_constraint": "//build/bazel/platforms/arch:secondary_x86", + "_secondary_x86_64_constraint": "//build/bazel/platforms/arch:secondary_x86_64", + "_secondary_arm_constraint": "//build/bazel/platforms/arch:secondary_arm", + "_secondary_arm64_constraint": "//build/bazel/platforms/arch:secondary_arm64", "_android_constraint": "//build/bazel/platforms/os:android", "_linux_constraint": "//build/bazel/platforms/os:linux", "_linux_musl_constraint": "//build/bazel/platforms/os:linux_musl", @@ -134,6 +140,36 @@ def _get_target_arch(utils): fail("Unable to determine target arch") +def _get_target_secondary_arch(utils): + """ + Returns 'x86', 'x86_64', 'arm', 'arm64', or '' depending on the target platform. + + If the secondary arch is the same as the primary arch, an empty string will be returned. + This is supposed to indicate that no secondary arch exists. The main motivation for this + behavior is in soong.variables, DeviceSecondaryArch and related variables are empty + strings when they don't exist, so a lot of code revolves around that. However in bazel + a constraint setting must always have a value, and a "none" value would probably + introduce more problems, so instead the secondary arch copies the primary arch if it + doesn't exist. + """ + platforminfo = _get_platform_info(utils) + + result = "" + if platforminfo.target_secondary_x86_constraint: + result = "x86" + elif platforminfo.target_secondary_x86_64_constraint: + result = "x86_64" + elif platforminfo.target_secondary_arm_constraint: + result = "arm" + elif platforminfo.target_secondary_arm64_constraint: + result = "arm64" + else: + fail("Unable to determine target secondary arch") + + if _get_target_arch(utils) == result: + return "" + return result + platforms = struct( is_target_linux = _is_target_linux, is_target_android = _is_target_android, @@ -146,4 +182,5 @@ platforms = struct( is_target_arm64 = _is_target_arm64, get_target_bitness = _get_target_bitness, get_target_arch = _get_target_arch, + get_target_secondary_arch = _get_target_secondary_arch, ) |