1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Utilities for rule implementations to interact with platform definitions.""" 16 17# Merge ARCH_CONSTRAINT_ATTRS with the rule attrs to use get_arch(ctx). 18ARCH_CONSTRAINT_ATTRS = { 19 "_x86_constraint": attr.label(default = Label("//build/bazel/platforms/arch:x86")), 20 "_x86_64_constraint": attr.label(default = Label("//build/bazel/platforms/arch:x86_64")), 21 "_arm_constraint": attr.label(default = Label("//build/bazel/platforms/arch:arm")), 22 "_arm64_constraint": attr.label(default = Label("//build/bazel/platforms/arch:arm64")), 23} 24 25# get_arch takes a rule context with ARCH_CONSTRAINT_ATTRS and returns the string representation 26# of the target platform by executing the target_platform_has_constraint boilerplate. 27def get_arch(ctx): 28 if not hasattr(ctx.attr, "_x86_constraint") or \ 29 not hasattr(ctx.attr, "_x86_64_constraint") or \ 30 not hasattr(ctx.attr, "_arm_constraint") or \ 31 not hasattr(ctx.attr, "_arm64_constraint"): 32 fail("Could not get the target architecture of this rule due to missing constraint attrs.", 33 "Have you merged ARCH_CONSTRAINT_ATTRS into this rule's attributes?") 34 35 x86_constraint = ctx.attr._x86_constraint[platform_common.ConstraintValueInfo] 36 x86_64_constraint = ctx.attr._x86_64_constraint[platform_common.ConstraintValueInfo] 37 arm_constraint = ctx.attr._arm_constraint[platform_common.ConstraintValueInfo] 38 arm64_constraint = ctx.attr._arm64_constraint[platform_common.ConstraintValueInfo] 39 40 if ctx.target_platform_has_constraint(x86_constraint): 41 return "x86" 42 elif ctx.target_platform_has_constraint(x86_64_constraint): 43 return "x86_64" 44 elif ctx.target_platform_has_constraint(arm_constraint): 45 return "arm" 46 elif ctx.target_platform_has_constraint(arm64_constraint): 47 return "arm64" 48