From 08a14a9f76dfd439fe6512645d40b8be98a45d0f Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 17 Jul 2023 11:47:59 +0200 Subject: [PATCH] Fix `//go/config:linkmode` flag value not being effective The value of the flag was always overriden by the `linkmode` `go_binary` attribute, even if that attribute was at its default (`normal`) value. Instead, use a default of `auto` to distinguish this case from the case where no attribute value has been set explicitly. Fixes #3614 Closes #3615 --- docs/go/core/rules.md | 2 +- go/private/rules/binary.bzl | 6 +++-- tests/core/go_binary/BUILD.bazel | 9 +++++++ tests/core/go_binary/linkmode.bzl | 35 ++++++++++++++++++++++++++ tests/core/go_binary/pie_linux_test.go | 12 +++++++++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/core/go_binary/linkmode.bzl diff --git a/docs/go/core/rules.md b/docs/go/core/rules.md index d30a47378d..52507673af 100644 --- a/docs/go/core/rules.md +++ b/docs/go/core/rules.md @@ -165,7 +165,7 @@ This builds an executable from a set of source files, | goos | Forces a binary to be cross-compiled for a specific operating system. It's usually better to control this on the command line with --platforms.

This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set pure = off.

See [Cross compilation] for more information. | String | optional | "auto" | | gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] | | importpath | The import path of this binary. Binaries can't actually be imported, but this may be used by [go_path] and other tools to report the location of source files. This may be inferred from embedded libraries. | String | optional | "" | -| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.

| String | optional | "normal" | +| linkmode | Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.

| String | optional | "auto" | | msan | Controls whether code is instrumented for memory sanitization. May be one of on, off, or auto. Not available when cgo is disabled. In most cases, it's better to control this on the command line with --@io_bazel_rules_go//go/config:msan. See [mode attributes], specifically [msan]. | String | optional | "auto" | | out | Sets the output filename for the generated executable. When set, go_binary will write this file without mode-specific directory prefixes, without linkmode-specific prefixes like "lib", and without platform-specific suffixes like ".exe". Note that without a mode-specific directory prefix, the output file (but not its dependencies) will be invalidated in Bazel's cache when changing configurations. | String | optional | "" | | pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting CGO_ENABLED. May be one of on, off, or auto. If auto, pure mode is enabled when no C/C++ toolchain is configured or when cross-compiling. It's usually better to control this on the command line with --@io_bazel_rules_go//go/config:pure. See [mode attributes], specifically [pure]. | String | optional | "auto" | diff --git a/go/private/rules/binary.bzl b/go/private/rules/binary.bzl index 51530b04ca..690eb14a5e 100644 --- a/go/private/rules/binary.bzl +++ b/go/private/rules/binary.bzl @@ -37,10 +37,10 @@ load( ) load( "//go/private:mode.bzl", + "LINKMODES", "LINKMODES_EXECUTABLE", "LINKMODE_C_ARCHIVE", "LINKMODE_C_SHARED", - "LINKMODE_NORMAL", "LINKMODE_PLUGIN", "LINKMODE_SHARED", ) @@ -385,11 +385,13 @@ _go_binary_kwargs = { """, ), "linkmode": attr.string( - default = LINKMODE_NORMAL, + default = "auto", + values = ["auto"] + LINKMODES, doc = """Determines how the binary should be built and linked. This accepts some of the same values as `go build -buildmode` and works the same way.