xref: /aosp_15_r20/external/bazelbuild-platforms/host/extension.bzl (revision ef3a692c0746f7dadd4fb3b5728d17696f151f9c)
1def _translate_cpu(arch):
2    if arch in ["i386", "i486", "i586", "i686", "i786", "x86"]:
3        return "x86_32"
4    if arch in ["amd64", "x86_64", "x64"]:
5        return "x86_64"
6    if arch in ["ppc", "ppc64", "ppc64le"]:
7        return "ppc"
8    if arch in ["arm", "armv7l"]:
9        return "arm"
10    if arch in ["aarch64"]:
11        return "aarch64"
12    if arch in ["s390x", "s390"]:
13        return "s390x"
14    if arch in ["mips64el", "mips64"]:
15        return "mips64"
16    if arch in ["riscv64"]:
17        return "riscv64"
18    return None
19
20def _translate_os(os):
21    if os.startswith("mac os"):
22        return "osx"
23    if os.startswith("freebsd"):
24        return "freebsd"
25    if os.startswith("openbsd"):
26        return "openbsd"
27    if os.startswith("linux"):
28        return "linux"
29    if os.startswith("windows"):
30        return "windows"
31    return None
32
33def _host_platform_repo_impl(rctx):
34    cpu = _translate_cpu(rctx.os.arch)
35    os = _translate_os(rctx.os.name)
36
37    cpu = "" if cpu == None else "  '@platforms//cpu:%s',\n" % cpu
38    os = "" if os == None else "  '@platforms//os:%s',\n" % os
39
40    rctx.file("BUILD.bazel", """
41# DO NOT EDIT: automatically generated BUILD file
42exports_files(["constraints.bzl"])
43""")
44
45    rctx.file("constraints.bzl", """
46# DO NOT EDIT: automatically generated constraints list
47HOST_CONSTRAINTS = [
48%s%s]
49""" % (cpu, os))
50
51host_platform_repo = repository_rule(
52    implementation = _host_platform_repo_impl,
53    doc = """Generates constraints for the host platform. The constraints.bzl
54file contains a single <code>HOST_CONSTRAINTS</code> variable, which is a
55list of strings, each of which is a label to a <code>constraint_value</code>
56for the host platform.""",
57)
58
59def _host_platform_impl(_mctx):
60    host_platform_repo(name = "host_platform")
61
62host_platform = module_extension(
63    implementation = _host_platform_impl,
64    doc = """Generates a <code>host_platform_repo</code> repo named
65<code>host_platform</code>, containing constraints for the host platform.""",
66)
67
68