blob: a888a20b3d5e95cb7fe6fd8319fe96c5a5e561cc [file] [log] [blame]
def _translate_cpu(arch):
if arch in ["i386", "i486", "i586", "i686", "i786", "x86"]:
return "x86_32"
if arch in ["amd64", "x86_64", "x64"]:
return "x86_64"
if arch in ["ppc", "ppc64", "ppc64le"]:
return "ppc"
if arch in ["arm", "armv7l"]:
return "arm"
if arch in ["aarch64"]:
return "aarch64"
if arch in ["s390x", "s390"]:
return "s390x"
if arch in ["mips64el", "mips64"]:
return "mips64"
if arch in ["riscv64"]:
return "riscv64"
return None
def _translate_os(os):
if os.startswith("mac os"):
return "osx"
if os.startswith("freebsd"):
return "freebsd"
if os.startswith("openbsd"):
return "openbsd"
if os.startswith("linux"):
return "linux"
if os.startswith("windows"):
return "windows"
return None
def _host_platform_repo_impl(rctx):
cpu = _translate_cpu(rctx.os.arch)
os = _translate_os(rctx.os.name)
cpu = "" if cpu == None else " '@platforms//cpu:%s',\n" % cpu
os = "" if os == None else " '@platforms//os:%s',\n" % os
rctx.file("BUILD.bazel", """
# DO NOT EDIT: automatically generated BUILD file
exports_files(["constraints.bzl"])
""")
rctx.file("constraints.bzl", """
# DO NOT EDIT: automatically generated constraints list
HOST_CONSTRAINTS = [
%s%s]
""" % (cpu, os))
host_platform_repo = repository_rule(
implementation = _host_platform_repo_impl,
doc = """Generates constraints for the host platform. The constraints.bzl
file contains a single <code>HOST_CONSTRAINTS</code> variable, which is a
list of strings, each of which is a label to a <code>constraint_value</code>
for the host platform.""",
local = True,
)
def _host_platform_impl(module_ctx):
host_platform_repo(name = "host_platform")
# module_ctx.extension_metadata has the paramater `reproducible` as of Bazel 7.1.0. We can't
# test for it directly and would ideally use bazel_features to check for it, but adding a
# dependency on it would require complicating the WORKSPACE setup. Thus, test for it by
# checking the availability of another feature introduced in 7.1.0.
if hasattr(module_ctx, "extension_metadata") and hasattr(module_ctx, "watch"):
return module_ctx.extension_metadata(reproducible = True)
else:
return None
host_platform = module_extension(
implementation = _host_platform_impl,
doc = """Generates a <code>host_platform_repo</code> repo named
<code>host_platform</code>, containing constraints for the host platform.""",
)