forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.bzl
111 lines (100 loc) · 3.67 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
# Copyright 2019 The Kubernetes Authors.
#
# 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", "go_binary", "go_context", "go_test")
load("@io_bazel_rules_go//go/platform:list.bzl", "GOOS_GOARCH")
# Defines a go_binary rule that enables cgo on platform builds targeting Linux,
# and otherwise builds a pure go binary.
def go_binary_conditional_pure(name, tags = [], **kwargs):
go_binary(
name = name,
pure = select({
"@io_bazel_rules_go//go/platform:linux": "off",
"//conditions:default": "on",
}),
tags = ["manual"] + tags,
**kwargs
)
# Defines a go_test rule that enables cgo on platform builds targeting Linux,
# and otherwise builds a pure go binary.
def go_test_conditional_pure(name, out, tags = [], **kwargs):
tags.append("manual")
go_test(
name = out,
pure = select({
"@io_bazel_rules_go//go/platform:linux": "off",
"//conditions:default": "on",
}),
testonly = False,
tags = tags,
**kwargs
)
native.alias(
name = "name",
actual = out,
)
_GO_BUILD_MODE_TMPL = "{goos}/{goarch}/pure={pure},static={static},msan={msan},race={race}\n"
def _go_build_mode_aspect_impl(target, ctx):
if (not hasattr(ctx.rule.attr, "_is_executable") or
not ctx.rule.attr._is_executable or
ctx.rule.attr.testonly):
# We only care about exporting platform info for executable targets
# that aren't testonly (e.g. kubectl and e2e.test).
return []
mode = go_context(ctx).mode
out = ctx.actions.declare_file(
target.files_to_run.executable.basename + ".go_build_mode",
sibling = target.files_to_run.executable,
)
ctx.actions.write(out, _GO_BUILD_MODE_TMPL.format(
goos = mode.goos,
goarch = mode.goarch,
pure = str(mode.pure).lower(),
static = str(mode.static).lower(),
msan = str(mode.msan).lower(),
race = str(mode.race).lower(),
))
return [OutputGroupInfo(default = depset([out]))]
# This aspect ouputs a *.go_build_mode metadata for go binaries. This metadata
# is used for executable selection e.g. in CI.
go_build_mode_aspect = aspect(
implementation = _go_build_mode_aspect_impl,
attrs = {
"goos": attr.string(
default = "auto",
values = ["auto"] + {goos: None for goos, _ in GOOS_GOARCH}.keys(),
),
"goarch": attr.string(
default = "auto",
values = ["auto"] + {goarch: None for _, goarch in GOOS_GOARCH}.keys(),
),
"pure": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"static": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"msan": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"race": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"_go_context_data": attr.label(default = "@io_bazel_rules_go//:go_context_data"),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)