From 610327fa9732a8b5af9f6eff9e9e711a68f7d17c Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Fri, 22 Mar 2024 11:49:28 +0000 Subject: [PATCH] feat(mode): add `purego` tag when `pure` The community has agreed[1] that "pure" builds of Go code should use the `purego` build tag. This change adds that de-facto tag. The minimum version of Gazelle is bumped to `0.36.0` which includes a patch[2] for ignoring the `purego` tag so that the source flies are passed to the compiler for conditional compilation decisions. [1]: https://github.com/golang/go/issues/23172 [2]: https://github.com/bazelbuild/bazel-gazelle/pull/1767 --- MODULE.bazel | 2 +- WORKSPACE | 6 +++--- go/modes.rst | 2 ++ go/private/mode.bzl | 2 ++ tests/bcr/MODULE.bazel | 2 +- tests/core/cgo/BUILD.bazel | 12 ++++++++++++ tests/core/cgo/tag_purego.go | 9 +++++++++ tests/core/cgo/tag_purego_err.c | 1 + tests/core/cgo/tag_purego_err.go | 6 ++++++ tests/core/cgo/tag_test.go | 3 +++ 10 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tests/core/cgo/tag_purego.go create mode 100644 tests/core/cgo/tag_purego_err.c create mode 100644 tests/core/cgo/tag_purego_err.go diff --git a/MODULE.bazel b/MODULE.bazel index 23d98efe07..0dacee1b11 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -27,7 +27,7 @@ use_repo( register_toolchains("@go_toolchains//:all") -bazel_dep(name = "gazelle", version = "0.34.0") +bazel_dep(name = "gazelle", version = "0.36.0") go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps") go_deps.from_file(go_mod = "//:go.mod") diff --git a/WORKSPACE b/WORKSPACE index 849414cc35..39e2a61b04 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -71,10 +71,10 @@ bazel_skylib_workspace() http_archive( name = "bazel_gazelle", - sha256 = "b7387f72efb59f876e4daae42f1d3912d0d45563eac7cb23d1de0b094ab588cf", + sha256 = "75df288c4b31c81eb50f51e2e14f4763cb7548daae126817247064637fd9ea62", urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.34.0/bazel-gazelle-v0.34.0.tar.gz", - "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.34.0/bazel-gazelle-v0.34.0.tar.gz", + "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz", + "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.36.0/bazel-gazelle-v0.36.0.tar.gz", ], ) diff --git a/go/modes.rst b/go/modes.rst index 431049d53d..54681d9b56 100644 --- a/go/modes.rst +++ b/go/modes.rst @@ -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` | +-------------------+---------------------+------------------------------------+ diff --git a/go/private/mode.bzl b/go/private/mode.bzl index 2318fd094c..2babb7f742 100644 --- a/go/private/mode.bzl +++ b/go/private/mode.bzl @@ -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, diff --git a/tests/bcr/MODULE.bazel b/tests/bcr/MODULE.bazel index edf60dbfda..e53033c8bc 100644 --- a/tests/bcr/MODULE.bazel +++ b/tests/bcr/MODULE.bazel @@ -18,7 +18,7 @@ local_path_override( path = "../..", ) -bazel_dep(name = "gazelle", version = "0.33.0") +bazel_dep(name = "gazelle", version = "0.36.0") bazel_dep(name = "protobuf", version = "3.19.6") go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk") diff --git a/tests/core/cgo/BUILD.bazel b/tests/core/cgo/BUILD.bazel index 3fcd9875ef..16bae440b5 100644 --- a/tests/core/cgo/BUILD.bazel +++ b/tests/core/cgo/BUILD.bazel @@ -334,6 +334,7 @@ go_test( data = [ ":tag_cgo_bin", ":tag_pure_bin", + ":tag_purego_bin", ], rundir = ".", deps = ["//go/tools/bazel:go_default_library"], @@ -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 = [ diff --git a/tests/core/cgo/tag_purego.go b/tests/core/cgo/tag_purego.go new file mode 100644 index 0000000000..31d2375fac --- /dev/null +++ b/tests/core/cgo/tag_purego.go @@ -0,0 +1,9 @@ +// +build purego + +package main + +import "fmt" + +func main() { + fmt.Println("purego") +} diff --git a/tests/core/cgo/tag_purego_err.c b/tests/core/cgo/tag_purego_err.c new file mode 100644 index 0000000000..c92fa97ca8 --- /dev/null +++ b/tests/core/cgo/tag_purego_err.c @@ -0,0 +1 @@ +#error should not be compiled diff --git a/tests/core/cgo/tag_purego_err.go b/tests/core/cgo/tag_purego_err.go new file mode 100644 index 0000000000..059c2bafbd --- /dev/null +++ b/tests/core/cgo/tag_purego_err.go @@ -0,0 +1,6 @@ +// +build !purego + +package main + +// this file should not be compiled +!!! diff --git a/tests/core/cgo/tag_test.go b/tests/core/cgo/tag_test.go index b1ad4c9906..1e62747761 100644 --- a/tests/core/cgo/tag_test.go +++ b/tests/core/cgo/tag_test.go @@ -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",