pkg_helpers.bzl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, test_data, test_install_marker, **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. test_data:
  33. The test data to verify the tar file against.
  34. test_install_marker:
  35. The install marker within the test data to locate the installation.
  36. **kwargs:
  37. Passed to `pkg_tar` for all the rest of its attributes.
  38. """
  39. for file_ext in ["tar", "tar.gz"]:
  40. target_ext = file_ext.replace(".", "_")
  41. tar_target = name_base + "_" + target_ext + "_rule"
  42. pkg_tar(
  43. name = tar_target,
  44. extension = file_ext,
  45. package_file_name = package_file_name_base + "." + file_ext,
  46. # The compressed tar is slow, exclude building and testing that.
  47. tags = ["manual"] if file_ext == "tar.gz" else [],
  48. **kwargs
  49. )
  50. py_test(
  51. name = name_base + "_" + target_ext + "_test",
  52. size = "small",
  53. srcs = ["toolchain_tar_test.py"],
  54. data = [":" + tar_target, test_install_marker] + test_data,
  55. args = [
  56. "$(location :{})".format(tar_target),
  57. "$(location {})".format(test_install_marker),
  58. ],
  59. main = "toolchain_tar_test.py",
  60. # The compressed tar is slow, exclude building and testing that.
  61. tags = ["manual"] if file_ext == "tar.gz" else [],
  62. )