install_filegroups.bzl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. """Rules for constructing install information."""
  5. load("@rules_pkg//pkg:mappings.bzl", "pkg_attributes", "pkg_filegroup", "pkg_files", "pkg_mklink", "strip_prefix")
  6. load("symlink_helpers.bzl", "symlink_file", "symlink_filegroup")
  7. def install_filegroup(name, filegroup_target):
  8. """Adds a filegroup for install.
  9. Used in the `install_dirs` dict.
  10. Args:
  11. name: The base directory for the filegroup.
  12. filegroup_target: The bazel filegroup target to install.
  13. """
  14. return {
  15. "filegroup": filegroup_target,
  16. "is_driver": False,
  17. "name": name,
  18. }
  19. def install_symlink(name, symlink_to):
  20. """Adds a symlink for install.
  21. Used in the `install_dirs` dict.
  22. Args:
  23. name: The filename to use.
  24. symlink_to: A relative path for the symlink.
  25. """
  26. return {
  27. "is_driver": False,
  28. "name": name,
  29. "symlink": symlink_to,
  30. }
  31. def install_target(name, target, executable = False, is_driver = False):
  32. """Adds a target for install.
  33. Used in the `install_dirs` dict.
  34. Args:
  35. name: The filename to use.
  36. target: The bazel target being installed.
  37. executable: True if executable.
  38. is_driver: False if it should be included in the `no_driver_name`
  39. filegroup.
  40. """
  41. return {
  42. "executable": executable,
  43. "is_driver": is_driver,
  44. "name": name,
  45. "target": target,
  46. }
  47. def make_install_filegroups(name, no_driver_name, pkg_name, install_dirs, prefix):
  48. """Makes filegroups of install data.
  49. Args:
  50. name: The name of the main filegroup, that contains all install_data.
  51. no_driver_name: The name of a filegroup which excludes the driver. This is
  52. for the driver to depend on and get other files, without a circular
  53. dependency.
  54. pkg_name: The name of a pkg_filegroup for tar.
  55. install_dirs: A dict of {directory: [install_* rules]}. This is used to
  56. structure files to be installed.
  57. prefix: A prefix for files in the native (non-pkg) filegroups.
  58. """
  59. all_srcs = []
  60. no_driver_srcs = []
  61. pkg_srcs = []
  62. for dir, entries in install_dirs.items():
  63. for entry in entries:
  64. path = "{0}/{1}".format(dir, entry["name"])
  65. prefixed_path = "{0}/{1}".format(prefix, path)
  66. all_srcs.append(prefixed_path)
  67. if not entry["is_driver"]:
  68. no_driver_srcs.append(prefixed_path)
  69. pkg_path = path + ".pkg"
  70. pkg_srcs.append(pkg_path)
  71. if "target" in entry:
  72. if entry["executable"]:
  73. symlink_file(
  74. name = prefixed_path,
  75. symlink_binary = entry["target"],
  76. )
  77. mode = "0755"
  78. else:
  79. symlink_file(
  80. name = prefixed_path,
  81. symlink_label = entry["target"],
  82. )
  83. mode = "0644"
  84. pkg_files(
  85. name = pkg_path,
  86. srcs = [entry["target"]],
  87. attributes = pkg_attributes(mode = mode),
  88. renames = {entry["target"]: path},
  89. )
  90. elif "filegroup" in entry:
  91. symlink_filegroup(
  92. name = prefixed_path,
  93. out_prefix = prefixed_path,
  94. srcs = [entry["filegroup"]],
  95. )
  96. pkg_files(
  97. name = pkg_path,
  98. srcs = [prefixed_path],
  99. strip_prefix = strip_prefix.from_pkg(prefix),
  100. )
  101. elif "symlink" in entry:
  102. symlink_to = "{0}/{1}/{2}".format(prefix, dir, entry["symlink"])
  103. symlink_file(
  104. name = prefixed_path,
  105. symlink_label = symlink_to,
  106. )
  107. pkg_mklink(
  108. name = pkg_path,
  109. link_name = path,
  110. target = entry["symlink"],
  111. )
  112. else:
  113. fail("Unrecognized structure: {0}".format(entry))
  114. native.filegroup(name = name, srcs = all_srcs)
  115. native.filegroup(name = no_driver_name, srcs = no_driver_srcs)
  116. pkg_filegroup(name = pkg_name, srcs = pkg_srcs)