Skip to content

Commit

Permalink
feat(mode): add purego tag when pure
Browse files Browse the repository at this point in the history
The community has agreed[1] that "pure" builds of Go code should
use the `purego` build tag.

This change adds that de-facto tag.

[1]: golang/go#23172
  • Loading branch information
mattyclarkson committed Mar 22, 2024
1 parent 5a0e2c7 commit 63781e0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go/modes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ or using `Bazel configuration transitions`_.
| 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. |
| Sets the de-facto ``purego`` build tag for conditional inclusion of source |
| files. |
+-------------------+---------------------+------------------------------------+
| :param:`debug` | :type:`bool` | :value:`false` |
+-------------------+---------------------+------------------------------------+
Expand Down
2 changes: 2 additions & 0 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ def get_mode(ctx, go_toolchain, cgo_context_info, go_config_info):
tags.append("race")
if msan:
tags.append("msan")
if pure:
tags.append("purego")

return struct(
static = static,
Expand Down
12 changes: 12 additions & 0 deletions tests/core/cgo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ go_test(
data = [
":tag_cgo_bin",
":tag_pure_bin",
":tag_purego_bin",
],
rundir = ".",
deps = ["//go/tools/bazel:go_default_library"],
Expand All @@ -350,6 +351,17 @@ go_binary(
pure = "on",
)

go_binary(
name = "tag_purego_bin",
srcs = [
"tag_purego.go",
"tag_purego_err.c",
"tag_purego_err.go",
],
cgo = True,
pure = "on",
)

go_binary(
name = "tag_cgo_bin",
srcs = [
Expand Down
9 changes: 9 additions & 0 deletions tests/core/cgo/tag_purego.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build purego

package main

import "fmt"

func main() {
fmt.Println("purego")
}
1 change: 1 addition & 0 deletions tests/core/cgo/tag_purego_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#error should not be compiled
6 changes: 6 additions & 0 deletions tests/core/cgo/tag_purego_err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build !purego

package main

// this file should not be compiled
!!!
3 changes: 3 additions & 0 deletions tests/core/cgo/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func Test(t *testing.T) {
{
name: "tag_pure_bin",
want: "pure",
}, {
name: "tag_purego_bin",
want: "purego",
}, {
name: "tag_cgo_bin",
want: "cgo",
Expand Down

0 comments on commit 63781e0

Please sign in to comment.