Skip to content

Commit

Permalink
go_binary, go_test: add gotags attribute (bazel-contrib#2440)
Browse files Browse the repository at this point in the history
The gotags attribute sets which build tags are enabled when evaluting
build constraints. This is equivalent to setting
--@io_bazel_rules_go//go/config:tags on the command line.

Fixes bazel-contrib#1351
  • Loading branch information
Jay Conrod authored Apr 20, 2020
1 parent af0f76b commit 8f4ebbd
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 31 deletions.
18 changes: 14 additions & 4 deletions go/core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,19 @@ Attributes
| This is one of the `mode attributes`_ that controls whether to instrument |
| code for data race detection. It may be :value:`on`, :value:`off`, or |
| :value:`auto`. In most cases, it's better to enable race detection globally |
| with ``--features=race`` on the command line. |
| with ``--@io_bazel_rules_go//go/config:race`` on the command line. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`msan` | :type:`string` | :value:`auto` |
+----------------------------+-----------------------------+---------------------------------------+
| This is one of the `mode attributes`_ that controls whether to instrument |
| code for memory santization. It may be :value:`on`, :value:`off`, or |
| :value:`auto`. In most cases, it's better to enable memory sanitization |
| globally with ``--features=msan`` on the command line. |
| globally with ``--@io_bazel_rules_go//go/config:msan`` on the command line. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`gotags` | :type:`string_list` : :value:`[]` |
+----------------------------+-----------------------------+---------------------------------------+
| This is one of the `mode attributes`_ that controls which build tags are |
| enabled when evaluating build constraints. Useful for conditional compilation. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`goos` | :type:`string` | :value:`auto` |
+----------------------------+-----------------------------+---------------------------------------+
Expand Down Expand Up @@ -733,14 +738,19 @@ Attributes
| This is one of the `mode attributes`_ that controls whether to instrument |
| code for data race detection. It may be :value:`on`, :value:`off`, or |
| :value:`auto`. In most cases, it's better to enable race detection globally |
| with ``--features=race`` on the command line. |
| with ``--@io_bazel_rules_go//go/config:race`` on the command line. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`msan` | :type:`string` | :value:`auto` |
+----------------------------+-----------------------------+---------------------------------------+
| This is one of the `mode attributes`_ that controls whether to instrument |
| code for memory santization. It may be :value:`on`, :value:`off`, or |
| :value:`auto`. In most cases, it's better to enable memory sanitization |
| globally with ``--features=msan`` on the command line. |
| globally with ``--@io_bazel_rules_go//go/config:msan`` on the command line. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`gotags` | :type:`string_list` : :value:`[]` |
+----------------------------+-----------------------------+---------------------------------------+
| This is one of the `mode attributes`_ that controls which build tags are |
| enabled when evaluating build constraints. Useful for conditional compilation. |
+----------------------------+-----------------------------+---------------------------------------+
| :param:`goos` | :type:`string` | :value:`auto` |
+----------------------------+-----------------------------+---------------------------------------+
Expand Down
49 changes: 27 additions & 22 deletions go/modes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,51 @@ or using `Bazel configuration transitions`_.

+-------------------+----------------+-----------------------------------------+
| **Name** | **Type** | **Default value** |
+-------------------+----------------+-----------------------------------------+
| :param:`static` | :type:`bool` | :value:`false` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`static` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Statically links the target binary. May not always work since parts of the |
| standard library and other C dependencies won't tolerate static linking. |
| Works best with ``pure`` set as well. |
+-------------------+----------------+-----------------------------------------+
| :param:`race` | :type:`bool` | :value:`false` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`race` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Instruments the binary for race detection. Programs will panic when a data |
| race is detected. Requires cgo. Mutually exclusive with ``msan``. |
+-------------------+----------------+-----------------------------------------+
| :param:`msan` | :type:`bool` | :value:`false` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`msan` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Instruments the binary for memory sanitization. Requires cgo. Mutually |
| exclusive with ``race``. |
+-------------------+----------------+-----------------------------------------+
| :param:`pure` | :type:`bool` | :value:`false` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`pure` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Disables cgo, even when a C/C++ toolchain is configured (similar to setting |
| ``CGO_ENABLED=0``). Packages that contain cgo code may still be built, but |
| the cgo code will be filtered out, and the ``cgo`` build tag will be false. |
+-------------------+----------------+-----------------------------------------+
| :param:`strip` | :type:`bool` | :value:`false` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`strip` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Strips symbols from compiled packages and linked binaries (using the ``-w`` |
| flag). May also be set with the ``--strip`` command line option, which |
| affects C/C++ targets, too. |
+-------------------+----------------+-----------------------------------------+
| :param:`debug` | :type:`bool` | :value:`false` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`debug` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
| Includes debugging information in compiled packages (using the ``-N`` and |
| ``-l`` flags). |
+-------------------+----------------+-----------------------------------------+
| :param:`linkmode` | :type:`string` | :value:`"normal"` |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+
| :param:`gotags` | :type:`string_list` | :value:`[]` |
+-------------------+---------------------+------------------------------------+
| Controls which build tags are enabled when evaluating build constraints in |
| source files. Useful for conditional compilation. |
+-------------------+---------------------+------------------------------------+
| :param:`linkmode` | :type:`string` | :value:`"normal"` |
+-------------------+---------------------+------------------------------------+
| Determines how the Go binary is built and linked. Similar to ``-buildmode``. |
| Must be one of ``"normal"``, ``"shared"``, ``"pie"``, ``"plugin"``, |
| ``"c-shared"``, ``"c-archive"``. |
+-------------------+----------------+-----------------------------------------+
+-------------------+---------------------+------------------------------------+

Platforms
---------
Expand Down
2 changes: 0 additions & 2 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ def emit_compilepkg(
args.add("-importpath", importpath)
if importmap:
args.add("-p", importmap)
if go.mode.tags:
args.add("-tags", ",".join(go.mode.tags))
args.add("-package_list", go.package_list)

args.add("-o", out_lib)
Expand Down
2 changes: 0 additions & 2 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ def _go_test_impl(ctx):
"l_test=" + external_source.library.importpath,
)
arguments.add("-pkgname", internal_source.library.importpath)
if go.mode.tags:
arguments.add("-tags", ",".join(go.mode.tags))
arguments.add_all(go_srcs, before_each = "-src", format_each = "l=%s")
ctx.actions.run(
inputs = go_srcs,
Expand Down
10 changes: 9 additions & 1 deletion go/private/rules/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def go_transition_wrapper(kind, transition_kind, name, **kwargs):
regular rule. This prevents targets from being rebuilt for an alternative
configuration identical to the default configuration.
"""
transition_keys = ("goos", "goarch", "pure", "static", "msan", "race", "linkmode")
transition_keys = ("goos", "goarch", "pure", "static", "msan", "race", "gotags", "linkmode")
need_transition = any([key in kwargs for key in transition_keys])
if need_transition:
transition_kind(name = name, **kwargs)
Expand Down Expand Up @@ -89,6 +89,7 @@ def go_transition_rule(**kwargs):
default = "auto",
values = ["auto", "on", "off"],
),
"gotags": attr.string_list(default = []),
"linkmode": attr.string(
default = "auto",
values = ["auto"] + LINKMODES,
Expand Down Expand Up @@ -126,6 +127,11 @@ def _go_transition_impl(settings, attr):
_set_ternary(settings, attr, "race")
_set_ternary(settings, attr, "msan")

tags = getattr(attr, "gotags", [])
if tags:
tags_label = _filter_transition_label("@io_bazel_rules_go//go/config:tags")
settings[tags_label] = tags

linkmode = getattr(attr, "linkmode", "auto")
if linkmode != "auto":
if linkmode not in LINKMODES:
Expand All @@ -143,6 +149,7 @@ go_transition = transition(
"@io_bazel_rules_go//go/config:msan",
"@io_bazel_rules_go//go/config:race",
"@io_bazel_rules_go//go/config:pure",
"@io_bazel_rules_go//go/config:tags",
"@io_bazel_rules_go//go/config:linkmode",
]],
outputs = [_filter_transition_label(label) for label in [
Expand All @@ -151,6 +158,7 @@ go_transition = transition(
"@io_bazel_rules_go//go/config:msan",
"@io_bazel_rules_go//go/config:race",
"@io_bazel_rules_go//go/config:pure",
"@io_bazel_rules_go//go/config:tags",
"@io_bazel_rules_go//go/config:linkmode",
]],
)
Expand Down
20 changes: 20 additions & 0 deletions tests/core/go_binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,23 @@ go_binary(
static = "on",
tags = ["manual"],
)

go_binary(
name = "tags_bin",
srcs = [
"tags_main_bad.go",
"tags_main_good.go",
],
gotags = ["good"],
deps = [":tags_lib"],
)

go_library(
name = "tags_lib",
srcs = [
"tags_lib_bad.go",
"tags_lib_good.go",
],
importpath = "tags_lib",
tags = ["manual"],
)
5 changes: 5 additions & 0 deletions tests/core/go_binary/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ produce static binaries. Verifies `#2168`_.

This test only runs on Linux. The darwin external linker cannot produce
static binaries since there is no static version of C runtime libraries.

tags_bin
--------
Checks that setting ``gotags`` affects source filtering. This binary won't build
without a specific tag being set.
5 changes: 5 additions & 0 deletions tests/core/go_binary/tags_lib_bad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !good

package lib

var Does Not = Compile
1 change: 1 addition & 0 deletions tests/core/go_binary/tags_lib_good.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package lib
5 changes: 5 additions & 0 deletions tests/core/go_binary/tags_main_bad.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !good

package main

var Does Not = Compile
8 changes: 8 additions & 0 deletions tests/core/go_binary/tags_main_good.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build good

package main

import _ "tags_lib"

func main() {
}
9 changes: 9 additions & 0 deletions tests/core/go_test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,12 @@ go_test(
],
importpath = "example.com/imports/test_main",
)

go_test(
name = "tags_test",
srcs = [
"tags_bad_test.go",
"tags_good_test.go",
],
gotags = ["good"],
)
6 changes: 6 additions & 0 deletions tests/core/go_test/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,9 @@ testmain_import_test
Check if all packages in all source files are imported to test main, to ensure
a consistent test behaviour. This ensures a consistent behaviour when thinking
about global indirect depencencies.

tags_test
---------

Checks that setting ``gotags`` affects source filtering. The test will fail
unless a specific tag is set.
9 changes: 9 additions & 0 deletions tests/core/go_test/tags_bad_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build !good

package tags_test

import "testing"

func Test(t *testing.T) {
t.Fail()
}
7 changes: 7 additions & 0 deletions tests/core/go_test/tags_good_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build good

package tags_test

import "testing"

func Test(t *testing.T) {}

0 comments on commit 8f4ebbd

Please sign in to comment.