build_defs.bzl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Provides go_yacc and genfile_check_test
  2. Copyright 2016 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. load(
  14. "@io_bazel_rules_go//go/private:providers.bzl",
  15. "GoSource",
  16. )
  17. _GO_YACC_TOOL = "@org_golang_x_tools//cmd/goyacc"
  18. def go_yacc(src, out, visibility = None):
  19. """Runs go tool yacc -o $out $src."""
  20. native.genrule(
  21. name = src + ".go_yacc",
  22. srcs = [src],
  23. outs = [out],
  24. tools = [_GO_YACC_TOOL],
  25. cmd = ("export GOROOT=$$(dirname $(location " + _GO_YACC_TOOL + "))/..;" +
  26. " $(location " + _GO_YACC_TOOL + ") " +
  27. " -o $(location " + out + ") $(SRCS) > /dev/null"),
  28. visibility = visibility,
  29. )
  30. def _extract_go_src(ctx):
  31. """Thin rule that exposes the GoSource from a go_library."""
  32. return [DefaultInfo(files = depset(ctx.attr.library[GoSource].srcs))]
  33. extract_go_src = rule(
  34. implementation = _extract_go_src,
  35. attrs = {
  36. "library": attr.label(
  37. providers = [GoSource],
  38. ),
  39. },
  40. )
  41. def genfile_check_test(src, gen):
  42. """Asserts that any checked-in generated code matches bazel gen."""
  43. if not src:
  44. fail("src is required", "src")
  45. if not gen:
  46. fail("gen is required", "gen")
  47. native.genrule(
  48. name = src + "_checksh",
  49. outs = [src + "_check.sh"],
  50. cmd = r"""cat >$@ <<'eof'
  51. #!/bin/bash
  52. # Script generated by @com_github_bazelbuild_buildtools//build:build_defs.bzl
  53. # --- begin runfiles.bash initialization ---
  54. # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
  55. set -euo pipefail
  56. if [[ ! -d "$${RUNFILES_DIR:-/dev/null}" && ! -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
  57. if [[ -f "$$0.runfiles_manifest" ]]; then
  58. export RUNFILES_MANIFEST_FILE="$$0.runfiles_manifest"
  59. elif [[ -f "$$0.runfiles/MANIFEST" ]]; then
  60. export RUNFILES_MANIFEST_FILE="$$0.runfiles/MANIFEST"
  61. elif [[ -f "$$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
  62. export RUNFILES_DIR="$$0.runfiles"
  63. fi
  64. fi
  65. if [[ -f "$${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
  66. source "$${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
  67. elif [[ -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
  68. source "$$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
  69. "$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
  70. else
  71. echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
  72. exit 1
  73. fi
  74. # --- end runfiles.bash initialization ---
  75. [[ "$$1" = external/* ]] && F1="$${1#external/}" || F1="$$TEST_WORKSPACE/$$1"
  76. [[ "$$2" = external/* ]] && F2="$${2#external/}" || F2="$$TEST_WORKSPACE/$$2"
  77. F1="$$(rlocation "$$F1")"
  78. F2="$$(rlocation "$$F2")"
  79. diff -q "$$F1" "$$F2"
  80. eof
  81. """,
  82. )
  83. native.sh_test(
  84. name = src + "_checkshtest",
  85. size = "small",
  86. srcs = [src + "_check.sh"],
  87. deps = ["@bazel_tools//tools/bash/runfiles"],
  88. data = [src, gen],
  89. args = ["$(location " + src + ")", "$(location " + gen + ")"],
  90. )
  91. # magic copy rule used to update the checked-in version
  92. native.genrule(
  93. name = src + "_copysh",
  94. srcs = [gen],
  95. outs = [src + "copy.sh"],
  96. cmd = "echo 'cp $${BUILD_WORKSPACE_DIRECTORY}/$(location " + gen +
  97. ") $${BUILD_WORKSPACE_DIRECTORY}/" + native.package_name() + "/" + src + "' > $@",
  98. )
  99. native.sh_binary(
  100. name = src + "_copy",
  101. srcs = [src + "_copysh"],
  102. data = [gen],
  103. )
  104. def go_proto_checkedin_test(src, proto = "go_default_library"):
  105. """Asserts that any checked-in .pb.go code matches bazel gen."""
  106. genfile = src + "_genfile"
  107. extract_go_src(
  108. name = genfile + "go",
  109. library = proto,
  110. )
  111. # TODO(pmbethe09): why is the extra copy needed?
  112. native.genrule(
  113. name = genfile,
  114. srcs = [genfile + "go"],
  115. outs = [genfile + ".go"],
  116. cmd = "cp $< $@",
  117. )
  118. genfile_check_test(src, genfile)