pkg_helpers.bzl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. # Exceptions. See /LICENSE for license information.
  3. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. """Rule to create variables for package naming."""
  5. load("@rules_pkg//pkg:providers.bzl", "PackageVariablesInfo")
  6. load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
  7. load("@rules_python//python:defs.bzl", "py_test")
  8. load("//bazel/version:compute_version.bzl", "VERSION_ATTRS", "compute_version")
  9. def _pkg_naming_variables_impl(ctx):
  10. # TODO: Add support for digging the target CPU out of the toolchain here,
  11. # remapping it to a more canonical name, and add that to the variables. The
  12. # Bazel target CPU is already directly available, but it isn't likely
  13. # canonical.
  14. # TODO: Include the target OS as well as the target CPU. This likely needs
  15. # similar re-mapping as the CPU does.
  16. return PackageVariablesInfo(values = {
  17. "version": compute_version(ctx),
  18. })
  19. pkg_naming_variables = rule(
  20. implementation = _pkg_naming_variables_impl,
  21. attrs = VERSION_ATTRS,
  22. )
  23. def pkg_tar_and_test(name_base, package_file_name_base, install_data_manifest, **kwargs):
  24. """Create a `pkg_tar` and a test for both `.tar` and `.tar.gz` extensions.
  25. Args:
  26. name_base:
  27. The base name of the rules and tests. Will have `tar` or `tar_gz` added
  28. and then `_rule` for the `pkg_tar` and `_test` for the test.
  29. package_file_name_base:
  30. The base of the `package_file_name` attribute to `pkg_tar`. The file
  31. extensions will be appended after a `.`.
  32. install_data_manifest:
  33. The install data manifest file to compare with.
  34. **kwargs:
  35. Passed to `pkg_tar` for all the rest of its attributes.
  36. """
  37. for file_ext in ["tar", "tar.gz"]:
  38. target_ext = file_ext.replace(".", "_")
  39. tar_target = name_base + "_" + target_ext + "_rule"
  40. pkg_tar(
  41. name = tar_target,
  42. extension = file_ext,
  43. package_file_name = package_file_name_base + "." + file_ext,
  44. # The compressed tar is slow, exclude building and testing that.
  45. tags = ["manual"] if file_ext == "tar.gz" else [],
  46. **kwargs
  47. )
  48. py_test(
  49. name = name_base + "_" + target_ext + "_test",
  50. size = "small",
  51. srcs = ["toolchain_tar_test.py"],
  52. data = [":" + tar_target, install_data_manifest],
  53. env = {
  54. "INSTALL_DATA_MANIFEST": "$(location {})".format(install_data_manifest),
  55. "TAR_FILE": "$(location :{})".format(tar_target),
  56. },
  57. main = "toolchain_tar_test.py",
  58. # The compressed tar is slow, exclude building and testing that.
  59. tags = ["manual"] if file_ext == "tar.gz" else [],
  60. )