build_defs.bzl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # Copyright (c) 2009-2021, Google LLC
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of Google LLC nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. """Internal rules for building upb."""
  26. load(":upb_proto_library.bzl", "GeneratedSrcsInfo")
  27. UPB_DEFAULT_CPPOPTS = select({
  28. "//:windows": [],
  29. "//conditions:default": [
  30. # copybara:strip_for_google3_begin
  31. "-Wextra",
  32. # "-Wshorten-64-to-32", # not in GCC (and my Kokoro images doesn't have Clang)
  33. "-Werror",
  34. "-Wno-long-long",
  35. # copybara:strip_end
  36. ],
  37. })
  38. UPB_DEFAULT_COPTS = select({
  39. "//:windows": [],
  40. "//:fasttable_enabled_setting": ["-std=gnu99", "-DUPB_ENABLE_FASTTABLE"],
  41. "//conditions:default": [
  42. # copybara:strip_for_google3_begin
  43. "-std=c99",
  44. "-pedantic",
  45. "-Werror=pedantic",
  46. "-Wall",
  47. "-Wstrict-prototypes",
  48. # GCC (at least) emits spurious warnings for this that cannot be fixed
  49. # without introducing redundant initialization (with runtime cost):
  50. # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
  51. #"-Wno-maybe-uninitialized",
  52. # copybara:strip_end
  53. ],
  54. })
  55. def _librule(name):
  56. return name + "_lib"
  57. runfiles_init = """\
  58. # --- begin runfiles.bash initialization v2 ---
  59. # Copy-pasted from the Bazel Bash runfiles library v2.
  60. set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
  61. source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
  62. source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
  63. source "$0.runfiles/$f" 2>/dev/null || \
  64. source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
  65. source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
  66. { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
  67. # --- end runfiles.bash initialization v2 ---
  68. """
  69. def _get_real_short_path(file):
  70. # For some reason, files from other archives have short paths that look like:
  71. # ../com_google_protobuf/google/protobuf/descriptor.proto
  72. short_path = file.short_path
  73. if short_path.startswith("../"):
  74. second_slash = short_path.index("/", 3)
  75. short_path = short_path[second_slash + 1:]
  76. return short_path
  77. def _get_real_root(file):
  78. real_short_path = _get_real_short_path(file)
  79. return file.path[:-len(real_short_path) - 1]
  80. def _get_real_roots(files):
  81. roots = {}
  82. for file in files:
  83. real_root = _get_real_root(file)
  84. if real_root:
  85. roots[real_root] = True
  86. return roots.keys()
  87. def _remove_prefix(str, prefix):
  88. if not str.startswith(prefix):
  89. fail("%s doesn't start with %s" % (str, prefix))
  90. return str[len(prefix):]
  91. def _remove_suffix(str, suffix):
  92. if not str.endswith(suffix):
  93. fail("%s doesn't end with %s" % (str, suffix))
  94. return str[:-len(suffix)]
  95. def make_shell_script(name, contents, out):
  96. contents = runfiles_init + contents # copybara:strip_for_google3
  97. contents = contents.replace("$", "$$")
  98. native.genrule(
  99. name = "gen_" + name,
  100. outs = [out],
  101. cmd = "(cat <<'HEREDOC'\n%s\nHEREDOC\n) > $@" % contents,
  102. )
  103. # upb_amalgamation() rule, with file_list aspect.
  104. SrcList = provider(
  105. fields = {
  106. "srcs": "list of srcs",
  107. },
  108. )
  109. def _file_list_aspect_impl(target, ctx):
  110. if GeneratedSrcsInfo in target:
  111. srcs = target[GeneratedSrcsInfo]
  112. return [SrcList(srcs = srcs.srcs + srcs.hdrs)]
  113. srcs = []
  114. for src in ctx.rule.attr.srcs:
  115. srcs += src.files.to_list()
  116. for hdr in ctx.rule.attr.hdrs:
  117. srcs += hdr.files.to_list()
  118. for hdr in ctx.rule.attr.textual_hdrs:
  119. srcs += hdr.files.to_list()
  120. return [SrcList(srcs = srcs)]
  121. _file_list_aspect = aspect(
  122. implementation = _file_list_aspect_impl,
  123. )
  124. def _upb_amalgamation(ctx):
  125. inputs = []
  126. for lib in ctx.attr.libs:
  127. inputs += lib[SrcList].srcs
  128. srcs = [src for src in inputs if src.path.endswith("c")]
  129. ctx.actions.run(
  130. inputs = inputs,
  131. outputs = ctx.outputs.outs,
  132. arguments = [ctx.bin_dir.path + "/", ctx.attr.prefix] + [f.path for f in srcs] + ["-I" + root for root in _get_real_roots(inputs)],
  133. progress_message = "Making amalgamation",
  134. executable = ctx.executable._amalgamator,
  135. )
  136. return []
  137. upb_amalgamation = rule(
  138. attrs = {
  139. "_amalgamator": attr.label(
  140. executable = True,
  141. cfg = "exec",
  142. default = "//bazel:amalgamate",
  143. ),
  144. "prefix": attr.string(
  145. default = "",
  146. ),
  147. "libs": attr.label_list(aspects = [_file_list_aspect]),
  148. "outs": attr.output_list(),
  149. },
  150. implementation = _upb_amalgamation,
  151. )