runtime_sources.bzl 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. """Provides variables and rules to work with Clang's runtime library sources.
  5. These are organized into groups based on the runtime functionality:
  6. - CRT: The C language runtimes not provided by the C standard library, currently
  7. just infrastructure for global initialization and teardown.
  8. - Builtins: The compiler builtins library mirroring `libgcc` that provides
  9. function definitions for operations not reliably available in hardware but
  10. needed by Clang.
  11. Future runtimes we plan to add support for but not yet included:
  12. - Libunwind
  13. - Libc++ and libc++abi
  14. - Sanitizers
  15. - Profiling runtimes
  16. """
  17. load("@rules_cc//cc:cc_library.bzl", "cc_library")
  18. CRT_FILES = {
  19. "crtbegin_src": "@llvm-project//compiler-rt:builtins_crtbegin_src",
  20. "crtend_src": "@llvm-project//compiler-rt:builtins_crtend_src",
  21. }
  22. BUILTINS_FILEGROUPS = {
  23. "aarch64_srcs": "@llvm-project//compiler-rt:builtins_aarch64_srcs",
  24. "bf16_srcs": "@llvm-project//compiler-rt:builtins_bf16_srcs",
  25. "generic_srcs": "@llvm-project//compiler-rt:builtins_generic_srcs",
  26. "i386_srcs": "@llvm-project//compiler-rt:builtins_i386_srcs",
  27. "macos_srcs": "@llvm-project//compiler-rt:builtins_macos_atomic_srcs",
  28. "tf_srcs": "@llvm-project//compiler-rt:builtins_tf_srcs",
  29. "x86_64_srcs": "@llvm-project//compiler-rt:builtins_x86_64_srcs",
  30. "x86_arch_srcs": "@llvm-project//compiler-rt:builtins_x86_arch_srcs",
  31. "x86_fp80_srcs": "@llvm-project//compiler-rt:builtins_x86_fp80_srcs",
  32. }
  33. _TEMPLATE = """
  34. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  35. // Exceptions. See /LICENSE for license information.
  36. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  37. //
  38. // Generated header file of strings describing the Clang runtime library source
  39. // files.
  40. //
  41. // See toolchain/driver/runtime_sources.bzl for more details.
  42. #ifndef CARBON_TOOLCHAIN_BASE_RUNTIME_SOURCES_H_
  43. #define CARBON_TOOLCHAIN_BASE_RUNTIME_SOURCES_H_
  44. #include "llvm/ADT/StringRef.h"
  45. namespace Carbon::RuntimeSources {{
  46. inline constexpr llvm::StringLiteral CrtBegin = {crtbegin_src};
  47. inline constexpr llvm::StringLiteral CrtEnd = {crtend_src};
  48. inline constexpr llvm::StringLiteral BuiltinsGenericSrcs[] = {{
  49. {generic_srcs}
  50. }};
  51. inline constexpr llvm::StringLiteral BuiltinsMacosSrcs[] = {{
  52. {macos_srcs}
  53. }};
  54. inline constexpr llvm::StringLiteral BuiltinsBf16Srcs[] = {{
  55. {bf16_srcs}
  56. }};
  57. inline constexpr llvm::StringLiteral BuiltinsTfSrcs[] = {{
  58. {tf_srcs}
  59. }};
  60. inline constexpr llvm::StringLiteral BuiltinsX86ArchSrcs[] = {{
  61. {x86_arch_srcs}
  62. }};
  63. inline constexpr llvm::StringLiteral BuiltinsX86Fp80Srcs[] = {{
  64. {x86_fp80_srcs}
  65. }};
  66. inline constexpr llvm::StringLiteral BuiltinsAarch64Srcs[] = {{
  67. {aarch64_srcs}
  68. }};
  69. inline constexpr llvm::StringLiteral BuiltinsX86_64Srcs[] = {{
  70. {x86_64_srcs}
  71. }};
  72. inline constexpr llvm::StringLiteral BuiltinsI386Srcs[] = {{
  73. {i386_srcs}
  74. }};
  75. }} // namespace Carbon::RuntimeSources
  76. #endif // CARBON_TOOLCHAIN_BASE_RUNTIME_SOURCES_H_
  77. """
  78. def _builtins_path(file):
  79. """Returns the runtime install path for a file in CompilerRT's builtins library."""
  80. # The CompilerRT package has the builtins runtime sources in the
  81. # "lib/builtins/" subdirectory, and we install into a "builtins/"
  82. # subdirectory, so just remove the "lib/" prefix from the package-relative
  83. # label name.
  84. return file.owner.name.removeprefix("lib/")
  85. def _get_path(file_attr, to_path_fn):
  86. files = file_attr[DefaultInfo].files.to_list()
  87. if len(files) > 1:
  88. fail(msg = "Expected a single file and got {0} files.".format(len(files)))
  89. return '"{0}"'.format(to_path_fn(files[0]))
  90. def _get_paths(files_attr, to_path_fn):
  91. files = []
  92. for src in files_attr:
  93. files.extend(src[DefaultInfo].files.to_list())
  94. files.extend(src[DefaultInfo].default_runfiles.files.to_list())
  95. return "\n".join([
  96. ' "{0}",'.format(to_path_fn(f))
  97. for f in files
  98. ])
  99. def _generate_runtime_sources_h_rule(ctx):
  100. h_file = ctx.actions.declare_file(ctx.label.name)
  101. ctx.actions.write(h_file, _TEMPLATE.format(**({
  102. k: _get_path(getattr(ctx.attr, "_" + k), _builtins_path)
  103. for k in CRT_FILES.keys()
  104. } | {
  105. k: _get_paths(getattr(ctx.attr, "_" + k), _builtins_path)
  106. for k in BUILTINS_FILEGROUPS.keys()
  107. })))
  108. return [DefaultInfo(files = depset([h_file]))]
  109. generate_runtime_sources_h = rule(
  110. implementation = _generate_runtime_sources_h_rule,
  111. attrs = {
  112. "_" + k: attr.label(default = v, allow_single_file = True)
  113. for k, v in CRT_FILES.items()
  114. } | {
  115. "_" + k: attr.label_list(default = [v], allow_files = True)
  116. for k, v in BUILTINS_FILEGROUPS.items()
  117. },
  118. )
  119. def generate_runtime_sources_cc_library(name, **kwargs):
  120. """Generates a `runtime_sources.h` header and a `cc_library` rule for it.
  121. This first generates the header file with variables describing the runtime
  122. sources from Clang, and then a `cc_library` that exports that header.
  123. The `cc_library` rule name is the provided `name` and should be depended on
  124. by code that includes the generated header. The `kwargs` are expanded into
  125. the `cc_library` in case other attributes need to be configured there.
  126. """
  127. generate_runtime_sources_h(name = "runtime_sources.h")
  128. cc_library(
  129. name = name,
  130. hdrs = ["runtime_sources.h"],
  131. **kwargs
  132. )