|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test") |
| 15 | +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_test") |
| 16 | +load("@io_bazel_rules_go//go/platform:list.bzl", "GOOS_GOARCH") |
16 | 17 |
|
17 | 18 | # Defines a go_binary rule that enables cgo on platform builds targeting Linux,
|
18 | 19 | # and otherwise builds a pure go binary.
|
@@ -47,3 +48,64 @@ def go_test_conditional_pure(name, out, tags = [], **kwargs):
|
47 | 48 | name = "name",
|
48 | 49 | actual = out,
|
49 | 50 | )
|
| 51 | + |
| 52 | +_GO_BUILD_MODE_TMPL = "{goos}/{goarch}/pure={pure},static={static},msan={msan},race={race}\n" |
| 53 | + |
| 54 | +def _go_build_mode_aspect_impl(target, ctx): |
| 55 | + if (not hasattr(ctx.rule.attr, "_is_executable") or |
| 56 | + not ctx.rule.attr._is_executable or |
| 57 | + ctx.rule.attr.testonly): |
| 58 | + # We only care about exporting platform info for executable targets |
| 59 | + # that aren't testonly (e.g. kubectl and e2e.test). |
| 60 | + return [] |
| 61 | + |
| 62 | + mode = go_context(ctx).mode |
| 63 | + |
| 64 | + out = ctx.actions.declare_file( |
| 65 | + target.files_to_run.executable.basename + ".go_build_mode", |
| 66 | + sibling = target.files_to_run.executable, |
| 67 | + ) |
| 68 | + ctx.actions.write(out, _GO_BUILD_MODE_TMPL.format( |
| 69 | + goos = mode.goos, |
| 70 | + goarch = mode.goarch, |
| 71 | + pure = str(mode.pure).lower(), |
| 72 | + static = str(mode.static).lower(), |
| 73 | + msan = str(mode.msan).lower(), |
| 74 | + race = str(mode.race).lower(), |
| 75 | + )) |
| 76 | + |
| 77 | + return [OutputGroupInfo(default = depset([out]))] |
| 78 | + |
| 79 | +# This aspect ouputs a *.go_build_mode metadata for go binaries. This metadata |
| 80 | +# is used for executable selection e.g. in CI. |
| 81 | +go_build_mode_aspect = aspect( |
| 82 | + implementation = _go_build_mode_aspect_impl, |
| 83 | + attrs = { |
| 84 | + "goos": attr.string( |
| 85 | + default = "auto", |
| 86 | + values = ["auto"] + {goos: None for goos, _ in GOOS_GOARCH}.keys(), |
| 87 | + ), |
| 88 | + "goarch": attr.string( |
| 89 | + default = "auto", |
| 90 | + values = ["auto"] + {goarch: None for _, goarch in GOOS_GOARCH}.keys(), |
| 91 | + ), |
| 92 | + "pure": attr.string( |
| 93 | + default = "auto", |
| 94 | + values = ["auto", "on", "off"], |
| 95 | + ), |
| 96 | + "static": attr.string( |
| 97 | + default = "auto", |
| 98 | + values = ["auto", "on", "off"], |
| 99 | + ), |
| 100 | + "msan": attr.string( |
| 101 | + default = "auto", |
| 102 | + values = ["auto", "on", "off"], |
| 103 | + ), |
| 104 | + "race": attr.string( |
| 105 | + default = "auto", |
| 106 | + values = ["auto", "on", "off"], |
| 107 | + ), |
| 108 | + "_go_context_data": attr.label(default = "@io_bazel_rules_go//:go_context_data"), |
| 109 | + }, |
| 110 | + toolchains = ["@io_bazel_rules_go//go:toolchain"], |
| 111 | +) |
0 commit comments