xref: /aosp_15_r20/external/protobuf/protobuf_release.bzl (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1"""
2Generates package naming variables for use with rules_pkg.
3"""
4
5load("@rules_pkg//:providers.bzl", "PackageVariablesInfo")
6load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
7load(":protobuf_version.bzl", "PROTOC_VERSION")
8
9def _package_naming_impl(ctx):
10  values = {}
11  values["version"] = PROTOC_VERSION
12
13  # infer from the current cpp toolchain.
14  toolchain = find_cpp_toolchain(ctx)
15  cpu = toolchain.cpu
16  system_name = toolchain.target_gnu_system_name
17
18  # rename cpus to match what we want artifacts to be
19  if cpu == "systemz":
20    cpu = "s390_64"
21  elif cpu == "aarch64":
22    cpu = "aarch_64"
23  elif cpu == "ppc64":
24    cpu = "ppcle_64"
25
26  # use the system name to determine the os and then create platform names
27  if "apple" in system_name:
28    values["platform"] = "osx-" + cpu
29  elif "linux" in system_name:
30    values["platform"] = "linux-" + cpu
31  elif "mingw" in system_name:
32    if cpu == "x86_64":
33      values["platform"] = "win64"
34    else:
35      values["platform"] = "win32"
36  else:
37    values["platform"] = "unknown"
38
39  return PackageVariablesInfo(values = values)
40
41
42package_naming = rule(
43  implementation = _package_naming_impl,
44    attrs = {
45      # Necessary data dependency for find_cpp_toolchain.
46      "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
47    },
48    toolchains = ["@bazel_tools//tools/cpp:toolchain_type"],
49    incompatible_use_toolchain_transition = True,
50)
51