• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15"""Defines the per_platform_alias rule."""
16
17load("@local_config_platform//:constraints.bzl", "HOST_CONSTRAINTS")
18
19def _get_host_platform():
20    """Return the <cpu>-<os> platform string (e.g. x86_64-linux)."""
21
22    # HACK: HOST_CONSTRAINTS is a list containing the CPU constraint
23    # followed by the OS constraint. We use this to figure out the
24    # current CPU+OS pair.
25    if len(HOST_CONSTRAINTS) != 2:
26        fail("Unexpected HOST_CONSTRAINTS: " + HOST_CONSTRAINTS)
27    cpu_constraint, os_constraint = HOST_CONSTRAINTS
28    cpu = cpu_constraint.removeprefix("@platforms//cpu:")
29    os = os_constraint.removeprefix("@platforms//os:")
30    return cpu + "-" + os
31
32def per_platform_alias(name, platform_to_label):
33    """Aliases to a different target depending on platform.
34
35    Args:
36      name: The name of the resulting target.
37      platform_to_label: A mapping from "<cpu>-<os>" platform string
38        (e.g. x86_64-linux") to label that you wish to alias.
39    """
40    host_platform = _get_host_platform()
41    if not host_platform in platform_to_label:
42        fail("No alias provided for platform " + host_platform)
43    label = platform_to_label[host_platform]
44    native.alias(name = name, actual = label)
45