forked from bazelbuild/buildtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_defs.bzl
More file actions
180 lines (161 loc) · 5.32 KB
/
build_defs.bzl
File metadata and controls
180 lines (161 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""Provides go_yacc and genfile_check_test
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
load(
"@io_bazel_rules_go//go:def.bzl",
"GoSource",
)
load("@rules_shell//shell:sh_binary.bzl", "sh_binary")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
_GO_YACC_TOOL = "@org_golang_x_tools//cmd/goyacc"
def _go_yacc_impl(ctx):
args = ctx.actions.args()
args.add("-o", ctx.outputs.out)
args.add(ctx.file.src)
goroot = "%s/.." % ctx.executable._go_yacc_tool.dirname
ctx.actions.run(
executable = ctx.executable._go_yacc_tool,
arguments = [args],
inputs = [ctx.file.src],
outputs = [ctx.outputs.out],
env = {
"GOROOT": goroot,
},
)
return DefaultInfo(
files = depset([ctx.outputs.out]),
)
_go_yacc = rule(
implementation = _go_yacc_impl,
attrs = {
"src": attr.label(
allow_single_file = True,
),
"out": attr.output(),
"_go_yacc_tool": attr.label(
default = _GO_YACC_TOOL,
allow_single_file = True,
executable = True,
cfg = "exec",
),
},
)
# buildifier: disable=unnamed-macro
def go_yacc(src, out, visibility = None):
"""Runs go tool yacc -o $out $src."""
_go_yacc(
name = src + ".go_yacc",
src = src,
out = out,
visibility = visibility,
)
def _extract_go_src(ctx):
"""Thin rule that exposes the GoSource from a go_library."""
return [DefaultInfo(files = depset(ctx.attr.library[GoSource].srcs))]
extract_go_src = rule(
implementation = _extract_go_src,
attrs = {
"library": attr.label(
providers = [GoSource],
),
},
)
# buildifier: disable=unnamed-macro
def genfile_check_test(src, gen):
"""
Asserts that any checked-in generated code matches bazel gen.
Args:
src: checked in file
gen: generated file
"""
if not src:
fail("src is required", "src")
if not gen:
fail("gen is required", "gen")
native.genrule(
name = src + "_checksh",
outs = [src + "_check.sh"],
cmd = r"""cat >$@ <<'eof'
#!/bin/bash
# Script generated by @com_github_bazelbuild_buildtools//build:build_defs.bzl
# --- begin runfiles.bash initialization ---
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
set -euo pipefail
if [[ ! -d "$${RUNFILES_DIR:-/dev/null}" && ! -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$$0.runfiles_manifest"
elif [[ -f "$$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$$0.runfiles/MANIFEST"
elif [[ -f "$$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$$0.runfiles"
fi
fi
if [[ -f "$${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "$${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "$${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---
[[ "$$1" = external/* ]] && F1="$${1#external/}" || F1="$$TEST_WORKSPACE/$$1"
[[ "$$2" = external/* ]] && F2="$${2#external/}" || F2="$$TEST_WORKSPACE/$$2"
F1="$$(rlocation "$$F1")"
F2="$$(rlocation "$$F2")"
diff -q "$$F1" "$$F2"
eof
""",
)
sh_test(
name = src + "_checkshtest",
size = "small",
srcs = [src + "_check.sh"],
deps = ["@bazel_tools//tools/bash/runfiles"],
data = [src, gen],
args = ["$(location " + src + ")", "$(location " + gen + ")"],
)
# magic copy rule used to update the checked-in version
native.genrule(
name = src + "_copysh",
srcs = [gen],
outs = [src + "copy.sh"],
cmd = "echo 'cp $${BUILD_WORKSPACE_DIRECTORY}/$(location " + gen +
") $${BUILD_WORKSPACE_DIRECTORY}/" + native.package_name() + "/" + src + "' > $@",
)
sh_binary(
name = src + "_copy",
srcs = [src + "_copysh"],
data = [gen],
)
# buildifier: disable=unnamed-macro
def go_proto_checkedin_test(src, proto = "go_default_library"):
"""
Asserts that any checked-in .pb.go code matches bazel gen.
Args:
src: checked in file
proto: generated file
"""
genfile = src + "_genfile"
extract_go_src(
name = genfile + "go",
library = proto,
)
# TODO(pmbethe09): why is the extra copy needed?
native.genrule(
name = genfile,
srcs = [genfile + "go"],
outs = [genfile + ".go"],
cmd = "cp $< $@",
)
genfile_check_test(src, genfile)