1*61c4878aSAndroid Build Coastguard Worker# Copyright 2024 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker# 3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker# the License at 6*61c4878aSAndroid Build Coastguard Worker# 7*61c4878aSAndroid Build Coastguard Worker# https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker# 9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker# the License. 14*61c4878aSAndroid Build Coastguard Worker"""Rules and macros related to platform compatibility.""" 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Workerload("@bazel_skylib//lib:selects.bzl", "selects") 17*61c4878aSAndroid Build Coastguard Worker 18*61c4878aSAndroid Build Coastguard Workerdef host_backend_alias(name, backend): 19*61c4878aSAndroid Build Coastguard Worker """An alias that resolves to the backend for host platforms.""" 20*61c4878aSAndroid Build Coastguard Worker native.alias( 21*61c4878aSAndroid Build Coastguard Worker name = name, 22*61c4878aSAndroid Build Coastguard Worker actual = selects.with_or({ 23*61c4878aSAndroid Build Coastguard Worker ( 24*61c4878aSAndroid Build Coastguard Worker "@platforms//os:android", 25*61c4878aSAndroid Build Coastguard Worker "@platforms//os:chromiumos", 26*61c4878aSAndroid Build Coastguard Worker "@platforms//os:linux", 27*61c4878aSAndroid Build Coastguard Worker "@platforms//os:macos", 28*61c4878aSAndroid Build Coastguard Worker "@platforms//os:windows", 29*61c4878aSAndroid Build Coastguard Worker ): backend, 30*61c4878aSAndroid Build Coastguard Worker "//conditions:default": str(Label("//pw_build:unspecified_backend")), 31*61c4878aSAndroid Build Coastguard Worker }), 32*61c4878aSAndroid Build Coastguard Worker ) 33*61c4878aSAndroid Build Coastguard Worker 34*61c4878aSAndroid Build Coastguard Workerdef boolean_constraint_value(name, **kwargs): 35*61c4878aSAndroid Build Coastguard Worker """Syntactic sugar for a constraint with just two possible values. 36*61c4878aSAndroid Build Coastguard Worker 37*61c4878aSAndroid Build Coastguard Worker Args: 38*61c4878aSAndroid Build Coastguard Worker name: The name of the "True" value of the generated constraint. 39*61c4878aSAndroid Build Coastguard Worker **kwargs: Passed on to native.constraint_value. 40*61c4878aSAndroid Build Coastguard Worker """ 41*61c4878aSAndroid Build Coastguard Worker constraint_setting_name = name + ".constraint_setting" 42*61c4878aSAndroid Build Coastguard Worker false_value_name = name + ".not" 43*61c4878aSAndroid Build Coastguard Worker 44*61c4878aSAndroid Build Coastguard Worker native.constraint_setting( 45*61c4878aSAndroid Build Coastguard Worker name = constraint_setting_name, 46*61c4878aSAndroid Build Coastguard Worker default_constraint_value = ":" + false_value_name, 47*61c4878aSAndroid Build Coastguard Worker # Do not allow anyone to declare additional values for this setting. 48*61c4878aSAndroid Build Coastguard Worker # It's boolean, so by definition it's true or false, that's it! 49*61c4878aSAndroid Build Coastguard Worker visibility = ["//visibility:private"], 50*61c4878aSAndroid Build Coastguard Worker ) 51*61c4878aSAndroid Build Coastguard Worker 52*61c4878aSAndroid Build Coastguard Worker native.constraint_value( 53*61c4878aSAndroid Build Coastguard Worker name = false_value_name, 54*61c4878aSAndroid Build Coastguard Worker constraint_setting = ":" + constraint_setting_name, 55*61c4878aSAndroid Build Coastguard Worker # The false value is not exposed at this time to avoid exposing more 56*61c4878aSAndroid Build Coastguard Worker # API surface than necessary, and for better compliance with 57*61c4878aSAndroid Build Coastguard Worker # https://bazel.build/rules/bzl-style#macros. But we may make it public 58*61c4878aSAndroid Build Coastguard Worker # in the future. 59*61c4878aSAndroid Build Coastguard Worker visibility = ["//visibility:private"], 60*61c4878aSAndroid Build Coastguard Worker ) 61*61c4878aSAndroid Build Coastguard Worker 62*61c4878aSAndroid Build Coastguard Worker native.constraint_value( 63*61c4878aSAndroid Build Coastguard Worker name = name, 64*61c4878aSAndroid Build Coastguard Worker constraint_setting = ":" + constraint_setting_name, 65*61c4878aSAndroid Build Coastguard Worker **kwargs 66*61c4878aSAndroid Build Coastguard Worker ) 67*61c4878aSAndroid Build Coastguard Worker 68*61c4878aSAndroid Build Coastguard Workerdef incompatible_with_mcu(unless_platform_has = None): 69*61c4878aSAndroid Build Coastguard Worker """Helper for expressing incompatibility with MCU platforms. 70*61c4878aSAndroid Build Coastguard Worker 71*61c4878aSAndroid Build Coastguard Worker This helper should be used in `target_compatible_with` attributes to 72*61c4878aSAndroid Build Coastguard Worker express: 73*61c4878aSAndroid Build Coastguard Worker 74*61c4878aSAndroid Build Coastguard Worker * That a target is only compatible with platforms that have a 75*61c4878aSAndroid Build Coastguard Worker full-featured OS, see 76*61c4878aSAndroid Build Coastguard Worker https://pigweed.dev/bazel_compatibility.html#cross-platform-modules-requiring-an-os 77*61c4878aSAndroid Build Coastguard Worker * That a target is compatible with platforms with a full-featured OS, and 78*61c4878aSAndroid Build Coastguard Worker also any platform that explicitly declares compatibility with it: 79*61c4878aSAndroid Build Coastguard Worker https://pigweed.dev/bazel_compatibility.html#special-case-host-compatible-platform-specific-modules 80*61c4878aSAndroid Build Coastguard Worker 81*61c4878aSAndroid Build Coastguard Worker Args: 82*61c4878aSAndroid Build Coastguard Worker unless_platform_has: A constraint_value that the target is compatible with 83*61c4878aSAndroid Build Coastguard Worker by definition. Optional. 84*61c4878aSAndroid Build Coastguard Worker """ 85*61c4878aSAndroid Build Coastguard Worker return select({ 86*61c4878aSAndroid Build Coastguard Worker "@platforms//os:none": [unless_platform_has] if (unless_platform_has != None) else ["@platforms//:incompatible"], 87*61c4878aSAndroid Build Coastguard Worker "//conditions:default": [], 88*61c4878aSAndroid Build Coastguard Worker }) 89*61c4878aSAndroid Build Coastguard Worker 90*61c4878aSAndroid Build Coastguard Workerdef minimum_cxx_20(): 91*61c4878aSAndroid Build Coastguard Worker """Helper for expressing a C++20 requirement. 92*61c4878aSAndroid Build Coastguard Worker 93*61c4878aSAndroid Build Coastguard Worker This helper should be used in `target_compatible_with` attributes to express 94*61c4878aSAndroid Build Coastguard Worker that a target requires C++20 or newer. 95*61c4878aSAndroid Build Coastguard Worker """ 96*61c4878aSAndroid Build Coastguard Worker return select({ 97*61c4878aSAndroid Build Coastguard Worker "//pw_toolchain/cc:c++20_enabled": [], 98*61c4878aSAndroid Build Coastguard Worker "//conditions:default": ["@platforms//:incompatible"], 99*61c4878aSAndroid Build Coastguard Worker }) 100