forked from kubernetes-retired/external-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.bzl
111 lines (100 loc) · 3.69 KB
/
go.bzl
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
go_filetype = ["*.go"]
def _compute_genrule_variables(resolved_srcs, resolved_outs):
variables = {"SRCS": cmd_helper.join_paths(" ", resolved_srcs),
"OUTS": cmd_helper.join_paths(" ", resolved_outs)}
if len(resolved_srcs) == 1:
variables["<"] = list(resolved_srcs)[0].path
if len(resolved_outs) == 1:
variables["@"] = list(resolved_outs)[0].path
return variables
def _go_sources_aspect_impl(target, ctx):
transitive_sources = set(target.go_sources)
for dep in ctx.rule.attr.deps:
transitive_sources = transitive_sources | dep.transitive_sources
return struct(transitive_sources = transitive_sources)
go_sources_aspect = aspect(
attr_aspects = ["deps"],
implementation = _go_sources_aspect_impl,
)
def _compute_genrule_command(ctx):
cmd = [
'set -e',
# setup main GOPATH
'export GOPATH=/tmp/gopath',
'export GO_WORKSPACE=$${GOPATH}/src/' + ctx.attr.go_prefix.go_prefix,
'mkdir -p $${GO_WORKSPACE%/*}',
'ln -s $$(pwd) $${GO_WORKSPACE}',
# setup genfile GOPATH
'export GENGOPATH=/tmp/gengopath',
'export GENGO_WORKSPACE=$${GENGOPATH}/src/' + ctx.attr.go_prefix.go_prefix,
'mkdir -p $${GENGO_WORKSPACE%/*}',
'ln -s $$(pwd)/$(GENDIR) $${GENGO_WORKSPACE}',
# drop into WORKSPACE
'export GOPATH=$${GOPATH}:$${GENGOPATH}',
'cd $${GO_WORKSPACE}',
# execute user command
ctx.attr.cmd.strip(' \t\n\r'),
]
return '\n'.join(cmd)
def _go_genrule_impl(ctx):
all_srcs = set(ctx.attr._go_toolchain.stdlib)
label_dict = {}
for dep in ctx.attr.go_deps:
all_srcs = all_srcs | dep.transitive_sources
for dep in ctx.attr.srcs:
all_srcs = all_srcs | dep.files
label_dict[dep.label] = dep.files
cmd = _compute_genrule_command(ctx)
resolved_inputs, argv, runfiles_manifests = ctx.resolve_command(
command=cmd,
attribute="cmd",
expand_locations=True,
make_variables=_compute_genrule_variables(all_srcs, set(ctx.outputs.outs)),
tools=ctx.attr.tools,
label_dict=label_dict
)
ctx.action(
inputs = list(all_srcs) + resolved_inputs,
outputs = ctx.outputs.outs,
env = ctx.configuration.default_shell_env + ctx.attr._go_toolchain.env,
command = argv,
progress_message = "%s %s" % (ctx.attr.message, ctx),
mnemonic = "GoGenrule",
)
# We have codegen procedures that depend on the "go/*" stdlib packages
# and thus depend on executing with a valid GOROOT and GOPATH containing
# some amount transitive go src of dependencies. This go_genrule enables
# the creation of these sandboxes.
go_genrule = rule(
attrs = {
"srcs": attr.label_list(allow_files = True),
"tools": attr.label_list(
cfg = "host",
allow_files = True,
),
"outs": attr.output_list(mandatory = True),
"cmd": attr.string(mandatory = True),
"go_deps": attr.label_list(
aspects = [go_sources_aspect],
),
"message": attr.string(),
"executable": attr.bool(default = False),
# Next two rules copied from bazelbuild/rules_go@a9df110cf04e167b33f10473c7e904d780d921e6
# and then modified a bit.
# These will likely break at some point in the future, pending Bazel toolchain changes.
"_go_toolchain": attr.label(
default = Label("@io_bazel_rules_go_toolchain//:go_toolchain"),
),
"go_prefix": attr.label(
providers = ["go_prefix"],
default = Label(
"//:go_prefix",
relative_to_caller_repository = True,
),
allow_files = False,
cfg = "host",
),
},
output_to_genfiles = True,
implementation = _go_genrule_impl,
)