xref: /aosp_15_r20/build/bazel/platforms/platform_utils.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1*7594170eSAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project
2*7594170eSAndroid Build Coastguard Worker#
3*7594170eSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
4*7594170eSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
5*7594170eSAndroid Build Coastguard Worker# You may obtain a copy of the License at
6*7594170eSAndroid Build Coastguard Worker#
7*7594170eSAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
8*7594170eSAndroid Build Coastguard Worker#
9*7594170eSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
10*7594170eSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
11*7594170eSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*7594170eSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
13*7594170eSAndroid Build Coastguard Worker# limitations under the License.
14*7594170eSAndroid Build Coastguard Worker
15*7594170eSAndroid Build Coastguard Worker"""
16*7594170eSAndroid Build Coastguard Workerplatform_utils.bzl defines a platform_utils rule, and several
17*7594170eSAndroid Build Coastguard Workerutility functions that accept an instance of that rule and return
18*7594170eSAndroid Build Coastguard Workerinformation about the target platform. One instance of the platform_utils
19*7594170eSAndroid Build Coastguard Workerrule is defined in //build/bazel/platforms:platform_utils. All rules
20*7594170eSAndroid Build Coastguard Workerthat need it can depend on that target, and then call the util
21*7594170eSAndroid Build Coastguard Workerfunctions by doing something like `is_target_linux(ctx.attr._platform_utils)`.
22*7594170eSAndroid Build Coastguard WorkerThis works because child targets inherit their parent's configuration.
23*7594170eSAndroid Build Coastguard Worker"""
24*7594170eSAndroid Build Coastguard Worker
25*7594170eSAndroid Build Coastguard Worker_name_to_constraint = {
26*7594170eSAndroid Build Coastguard Worker    "_x86_constraint": "//build/bazel_common_rules/platforms/arch:x86",
27*7594170eSAndroid Build Coastguard Worker    "_x86_64_constraint": "//build/bazel_common_rules/platforms/arch:x86_64",
28*7594170eSAndroid Build Coastguard Worker    "_arm_constraint": "//build/bazel_common_rules/platforms/arch:arm",
29*7594170eSAndroid Build Coastguard Worker    "_arm64_constraint": "//build/bazel_common_rules/platforms/arch:arm64",
30*7594170eSAndroid Build Coastguard Worker    "_secondary_x86_constraint": "//build/bazel_common_rules/platforms/arch:secondary_x86",
31*7594170eSAndroid Build Coastguard Worker    "_secondary_x86_64_constraint": "//build/bazel_common_rules/platforms/arch:secondary_x86_64",
32*7594170eSAndroid Build Coastguard Worker    "_secondary_arm_constraint": "//build/bazel_common_rules/platforms/arch:secondary_arm",
33*7594170eSAndroid Build Coastguard Worker    "_secondary_arm64_constraint": "//build/bazel_common_rules/platforms/arch:secondary_arm64",
34*7594170eSAndroid Build Coastguard Worker    "_android_constraint": "//build/bazel_common_rules/platforms/os:android",
35*7594170eSAndroid Build Coastguard Worker    "_linux_constraint": "//build/bazel_common_rules/platforms/os:linux",
36*7594170eSAndroid Build Coastguard Worker    "_linux_musl_constraint": "//build/bazel_common_rules/platforms/os:linux_musl",
37*7594170eSAndroid Build Coastguard Worker    "_linux_bionic_constraint": "//build/bazel_common_rules/platforms/os:linux_bionic",
38*7594170eSAndroid Build Coastguard Worker    "_darwin_constraint": "//build/bazel_common_rules/platforms/os:darwin",
39*7594170eSAndroid Build Coastguard Worker}
40*7594170eSAndroid Build Coastguard Worker
41*7594170eSAndroid Build Coastguard Worker_AndroidPlatformUtilsInfo = provider(
42*7594170eSAndroid Build Coastguard Worker    "_AndroidPlatformUtilsInfo exports metadata about what platform the code is being run on.",
43*7594170eSAndroid Build Coastguard Worker    fields = {
44*7594170eSAndroid Build Coastguard Worker        "target" + name: "Whether the target platform has the constraint %s" % constraint
45*7594170eSAndroid Build Coastguard Worker        for name, constraint in _name_to_constraint.items()
46*7594170eSAndroid Build Coastguard Worker    },
47*7594170eSAndroid Build Coastguard Worker)
48*7594170eSAndroid Build Coastguard Worker
49*7594170eSAndroid Build Coastguard Workerdef _platform_utils_impl(ctx):
50*7594170eSAndroid Build Coastguard Worker    return [
51*7594170eSAndroid Build Coastguard Worker        _AndroidPlatformUtilsInfo(**{
52*7594170eSAndroid Build Coastguard Worker            "target" + name: ctx.target_platform_has_constraint(getattr(ctx.attr, name)[platform_common.ConstraintValueInfo])
53*7594170eSAndroid Build Coastguard Worker            for name in _name_to_constraint
54*7594170eSAndroid Build Coastguard Worker        }),
55*7594170eSAndroid Build Coastguard Worker    ]
56*7594170eSAndroid Build Coastguard Worker
57*7594170eSAndroid Build Coastguard Workerplatform_utils = rule(
58*7594170eSAndroid Build Coastguard Worker    implementation = _platform_utils_impl,
59*7594170eSAndroid Build Coastguard Worker    attrs = {
60*7594170eSAndroid Build Coastguard Worker        name: attr.label(
61*7594170eSAndroid Build Coastguard Worker            default = Label(constraint),
62*7594170eSAndroid Build Coastguard Worker            doc = "An internal reference to the constraint so it can be used in the rule implementation.",
63*7594170eSAndroid Build Coastguard Worker        )
64*7594170eSAndroid Build Coastguard Worker        for name, constraint in _name_to_constraint.items()
65*7594170eSAndroid Build Coastguard Worker    },
66*7594170eSAndroid Build Coastguard Worker)
67*7594170eSAndroid Build Coastguard Worker
68*7594170eSAndroid Build Coastguard Workerdef _get_platform_info(utils):
69*7594170eSAndroid Build Coastguard Worker    if _AndroidPlatformUtilsInfo not in utils:
70*7594170eSAndroid Build Coastguard Worker        fail("Provided object was not an instance of platform_utils. " +
71*7594170eSAndroid Build Coastguard Worker             "You should depend on //build/bazel/platforms:platform_utils and then pass " +
72*7594170eSAndroid Build Coastguard Worker             "ctx.attr._platform_utils to this function.")
73*7594170eSAndroid Build Coastguard Worker    return utils[_AndroidPlatformUtilsInfo]
74*7594170eSAndroid Build Coastguard Worker
75*7594170eSAndroid Build Coastguard Workerdef _is_target_linux(utils):
76*7594170eSAndroid Build Coastguard Worker    """Returns if the target platform is linux with any variation of libc."""
77*7594170eSAndroid Build Coastguard Worker    platforminfo = _get_platform_info(utils)
78*7594170eSAndroid Build Coastguard Worker    return (platforminfo.target_linux_constraint or
79*7594170eSAndroid Build Coastguard Worker            platforminfo.target_linux_musl_constraint or
80*7594170eSAndroid Build Coastguard Worker            platforminfo.target_linux_bionic_constraint)
81*7594170eSAndroid Build Coastguard Worker
82*7594170eSAndroid Build Coastguard Workerdef _is_target_android(utils):
83*7594170eSAndroid Build Coastguard Worker    """Returns if the target platform is android."""
84*7594170eSAndroid Build Coastguard Worker    return _get_platform_info(utils).target_android_constraint
85*7594170eSAndroid Build Coastguard Worker
86*7594170eSAndroid Build Coastguard Workerdef _is_target_darwin(utils):
87*7594170eSAndroid Build Coastguard Worker    """Returns if the target platform is darwin."""
88*7594170eSAndroid Build Coastguard Worker    return _get_platform_info(utils).target_darwin_constraint
89*7594170eSAndroid Build Coastguard Worker
90*7594170eSAndroid Build Coastguard Workerdef _is_target_linux_or_android(utils):
91*7594170eSAndroid Build Coastguard Worker    """Returns if the target platform is linux with any variation of libc, or android."""
92*7594170eSAndroid Build Coastguard Worker    return _is_target_linux(utils) or _is_target_android(utils)
93*7594170eSAndroid Build Coastguard Worker
94*7594170eSAndroid Build Coastguard Workerdef _is_target_bionic(utils):
95*7594170eSAndroid Build Coastguard Worker    """Returns if the target platform uses the Bionic libc"""
96*7594170eSAndroid Build Coastguard Worker    return _is_target_linux_bionic(utils) or _is_target_android(utils)
97*7594170eSAndroid Build Coastguard Worker
98*7594170eSAndroid Build Coastguard Workerdef _is_target_linux_bionic(utils):
99*7594170eSAndroid Build Coastguard Worker    """Returns if the target platform runs (non-Android) Linux and uses the Bionic libc"""
100*7594170eSAndroid Build Coastguard Worker    return _get_platform_info(utils).target_linux_bionic_constraint
101*7594170eSAndroid Build Coastguard Worker
102*7594170eSAndroid Build Coastguard Workerdef _get_target_bitness(utils):
103*7594170eSAndroid Build Coastguard Worker    """Returns 32 or 64 depending on the bitness of the target platform."""
104*7594170eSAndroid Build Coastguard Worker    platforminfo = _get_platform_info(utils)
105*7594170eSAndroid Build Coastguard Worker
106*7594170eSAndroid Build Coastguard Worker    if platforminfo.target_x86_constraint or platforminfo.target_arm_constraint:
107*7594170eSAndroid Build Coastguard Worker        return 32
108*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_x86_64_constraint or platforminfo.target_arm64_constraint:
109*7594170eSAndroid Build Coastguard Worker        return 64
110*7594170eSAndroid Build Coastguard Worker    fail("Unable to determine target bitness")
111*7594170eSAndroid Build Coastguard Worker
112*7594170eSAndroid Build Coastguard Workerdef _get_target_arch(utils):
113*7594170eSAndroid Build Coastguard Worker    """Returns 'x86', 'x86_64', 'arm', or 'arm64' depending on the target platform."""
114*7594170eSAndroid Build Coastguard Worker    platforminfo = _get_platform_info(utils)
115*7594170eSAndroid Build Coastguard Worker
116*7594170eSAndroid Build Coastguard Worker    if platforminfo.target_x86_constraint:
117*7594170eSAndroid Build Coastguard Worker        return "x86"
118*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_x86_64_constraint:
119*7594170eSAndroid Build Coastguard Worker        return "x86_64"
120*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_arm_constraint:
121*7594170eSAndroid Build Coastguard Worker        return "arm"
122*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_arm64_constraint:
123*7594170eSAndroid Build Coastguard Worker        return "arm64"
124*7594170eSAndroid Build Coastguard Worker
125*7594170eSAndroid Build Coastguard Worker    fail("Unable to determine target arch")
126*7594170eSAndroid Build Coastguard Worker
127*7594170eSAndroid Build Coastguard Workerdef _get_target_secondary_arch(utils):
128*7594170eSAndroid Build Coastguard Worker    """
129*7594170eSAndroid Build Coastguard Worker    Returns 'x86', 'x86_64', 'arm', 'arm64', or '' depending on the target platform.
130*7594170eSAndroid Build Coastguard Worker
131*7594170eSAndroid Build Coastguard Worker    If the secondary arch is the same as the primary arch, an empty string will be returned.
132*7594170eSAndroid Build Coastguard Worker    This is supposed to indicate that no secondary arch exists. The main motivation for this
133*7594170eSAndroid Build Coastguard Worker    behavior is in soong.variables, DeviceSecondaryArch and related variables are empty
134*7594170eSAndroid Build Coastguard Worker    strings when they don't exist, so a lot of code revolves around that. However in bazel
135*7594170eSAndroid Build Coastguard Worker    a constraint setting must always have a value, and a "none" value would probably
136*7594170eSAndroid Build Coastguard Worker    introduce more problems, so instead the secondary arch copies the primary arch if it
137*7594170eSAndroid Build Coastguard Worker    doesn't exist.
138*7594170eSAndroid Build Coastguard Worker    """
139*7594170eSAndroid Build Coastguard Worker    platforminfo = _get_platform_info(utils)
140*7594170eSAndroid Build Coastguard Worker
141*7594170eSAndroid Build Coastguard Worker    result = ""
142*7594170eSAndroid Build Coastguard Worker    if platforminfo.target_secondary_x86_constraint:
143*7594170eSAndroid Build Coastguard Worker        result = "x86"
144*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_secondary_x86_64_constraint:
145*7594170eSAndroid Build Coastguard Worker        result = "x86_64"
146*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_secondary_arm_constraint:
147*7594170eSAndroid Build Coastguard Worker        result = "arm"
148*7594170eSAndroid Build Coastguard Worker    elif platforminfo.target_secondary_arm64_constraint:
149*7594170eSAndroid Build Coastguard Worker        result = "arm64"
150*7594170eSAndroid Build Coastguard Worker    else:
151*7594170eSAndroid Build Coastguard Worker        fail("Unable to determine target secondary arch")
152*7594170eSAndroid Build Coastguard Worker
153*7594170eSAndroid Build Coastguard Worker    if _get_target_arch(utils) == result:
154*7594170eSAndroid Build Coastguard Worker        return ""
155*7594170eSAndroid Build Coastguard Worker    return result
156*7594170eSAndroid Build Coastguard Worker
157*7594170eSAndroid Build Coastguard Workerplatforms = struct(
158*7594170eSAndroid Build Coastguard Worker    is_target_linux = _is_target_linux,
159*7594170eSAndroid Build Coastguard Worker    is_target_android = _is_target_android,
160*7594170eSAndroid Build Coastguard Worker    is_target_bionic = _is_target_bionic,
161*7594170eSAndroid Build Coastguard Worker    is_target_darwin = _is_target_darwin,
162*7594170eSAndroid Build Coastguard Worker    is_target_linux_or_android = _is_target_linux_or_android,
163*7594170eSAndroid Build Coastguard Worker    get_target_bitness = _get_target_bitness,
164*7594170eSAndroid Build Coastguard Worker    get_target_arch = _get_target_arch,
165*7594170eSAndroid Build Coastguard Worker    get_target_secondary_arch = _get_target_secondary_arch,
166*7594170eSAndroid Build Coastguard Worker)
167