xref: /aosp_15_r20/external/bazelbuild-rules_go/go/toolchain/toolchains.bzl (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1# Copyright 2019 The Bazel Authors. All rights reserved.
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
15load(
16    "//go/private:platforms.bzl",
17    "GOARCH_CONSTRAINTS",
18    "GOOS_CONSTRAINTS",
19    "PLATFORMS",
20)
21
22def declare_constraints():
23    """Generates constraint_values and platform targets for valid platforms.
24
25    Each constraint_value corresponds to a valid goos or goarch.
26    The goos and goarch values belong to the constraint_settings
27    @platforms//os:os and @platforms//cpu:cpu, respectively.
28    To avoid redundancy, if there is an equivalent value in @platforms,
29    we define an alias here instead of another constraint_value.
30
31    Each platform defined here selects a goos and goarch constraint value.
32    These platforms may be used with --platforms for cross-compilation,
33    though users may create their own platforms (and
34    @bazel_tools//platforms:default_platform will be used most of the time).
35    """
36    for goos, constraint in GOOS_CONSTRAINTS.items():
37        if constraint.startswith("@io_bazel_rules_go//go/toolchain:"):
38            native.constraint_value(
39                name = goos,
40                constraint_setting = "@platforms//os:os",
41            )
42        else:
43            native.alias(
44                name = goos,
45                actual = constraint,
46            )
47
48    for goarch, constraint in GOARCH_CONSTRAINTS.items():
49        if constraint.startswith("@io_bazel_rules_go//go/toolchain:"):
50            native.constraint_value(
51                name = goarch,
52                constraint_setting = "@platforms//cpu:cpu",
53            )
54        else:
55            native.alias(
56                name = goarch,
57                actual = constraint,
58            )
59
60    native.constraint_setting(
61        name = "cgo_constraint",
62    )
63
64    native.constraint_value(
65        name = "cgo_on",
66        constraint_setting = ":cgo_constraint",
67    )
68
69    native.constraint_value(
70        name = "cgo_off",
71        constraint_setting = ":cgo_constraint",
72    )
73
74    for p in PLATFORMS:
75        native.platform(
76            name = p.name,
77            constraint_values = p.constraints,
78        )
79