Skip to content

Commit c03a5f0

Browse files
committed
Always write the binary to the mode specific directory
and create a symlink if "out" is specified. This reduces cache invalidation on mode changes.
1 parent cd29704 commit c03a5f0

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

docs/go/core/rules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ This builds an executable from a set of source files,
144144
| Name | Description | Type | Mandatory | Default |
145145
| :------------- | :------------- | :------------- | :------------- | :------------- |
146146
| <a id="go_binary-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
147-
| <a id="go_binary-basename"></a>basename | The basename of this binary. The binary basename may also be platform-dependent: on Windows, we add an .exe extension. | String | optional | "" |
147+
| <a id="go_binary-basename"></a>basename | The basename of this binary. The binary basename may also be platform-dependent: on Windows, we add an .exe extension. When not set, it will default to the label name. | String | optional | "" |
148148
| <a id="go_binary-cdeps"></a>cdeps | The list of other libraries that the c code depends on. This can be anything that would be allowed in [cc_library deps] Only valid if <code>cgo</code> = <code>True</code>. | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | [] |
149149
| <a id="go_binary-cgo"></a>cgo | If <code>True</code>, the package may contain [cgo] code, and <code>srcs</code> may contain C, C++, Objective-C, and Objective-C++ files and non-Go assembly files. When cgo is enabled, these files will be compiled with the C/C++ toolchain and included in the package. Note that this attribute does not force cgo to be enabled. Cgo is enabled for non-cross-compiling builds when a C/C++ toolchain is configured. | Boolean | optional | False |
150150
| <a id="go_binary-clinkopts"></a>clinkopts | List of flags to add to the C link command. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. Only valid if <code>cgo</code> = <code>True</code>. | List of strings | optional | [] |
@@ -164,7 +164,7 @@ This builds an executable from a set of source files,
164164
| <a id="go_binary-importpath"></a>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 | "" |
165165
| <a id="go_binary-linkmode"></a>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. <br><br> <ul> <li>`auto` (default): Controlled by `//go/config:linkmode`, which defaults to `normal`.</li> <li>`normal`: Builds a normal executable with position-dependent code.</li> <li>`pie`: Builds a position-independent executable.</li> <li>`plugin`: Builds a shared library that can be loaded as a Go plugin. Only supported on platforms that support plugins.</li> <li>`c-shared`: Builds a shared library that can be linked into a C program.</li> <li>`c-archive`: Builds an archive that can be linked into a C program.</li> </ul> | String | optional | "auto" |
166166
| <a id="go_binary-msan"></a>msan | Controls whether code is instrumented for memory sanitization. May be one of <code>on</code>, <code>off</code>, or <code>auto</code>. Not available when cgo is disabled. In most cases, it's better to control this on the command line with <code>--@io_bazel_rules_go//go/config:msan</code>. See [mode attributes], specifically [msan]. | String | optional | "auto" |
167-
| <a id="go_binary-out"></a>out | Sets the output filename for the generated executable. When set, <code>go_binary</code> 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 | "" |
167+
| <a id="go_binary-out"></a>out | Sets the output symlink filename for the generated executable. | String | optional | "" |
168168
| <a id="go_binary-pgoprofile"></a>pgoprofile | Provides a pprof file to be used for profile guided optimization when compiling go targets. A pprof file can also be provided via <code>--@io_bazel_rules_go//go/config:pgoprofile=&lt;label of a pprof file&gt;</code>. Profile guided optimization is only supported on go 1.20+. See https://go.dev/doc/pgo for more information. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | //go/config:empty |
169169
| <a id="go_binary-pure"></a>pure | Controls whether cgo source code and dependencies are compiled and linked, similar to setting <code>CGO_ENABLED</code>. May be one of <code>on</code>, <code>off</code>, or <code>auto</code>. If <code>auto</code>, 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 <code>--@io_bazel_rules_go//go/config:pure</code>. See [mode attributes], specifically [pure]. | String | optional | "auto" |
170170
| <a id="go_binary-race"></a>race | Controls whether code is instrumented for race detection. May be one of <code>on</code>, <code>off</code>, or <code>auto</code>. Not available when cgo is disabled. In most cases, it's better to control this on the command line with <code>--@io_bazel_rules_go//go/config:race</code>. See [mode attributes], specifically [race]. | String | optional | "auto" |

go/private/rules/binary.bzl

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,31 @@ def _go_binary_impl(ctx):
136136
name = ctx.attr.basename
137137
if not name:
138138
name = ctx.label.name
139-
executable = None
140-
if ctx.attr.out:
141-
# Use declare_file instead of attr.output(). When users set output files
142-
# directly, Bazel warns them not to use the same name as the rule, which is
143-
# the common case with go_binary.
144-
executable = ctx.actions.declare_file(ctx.attr.out)
145-
archive, executable, runfiles = go.binary(
139+
archive, executable_binary, runfiles = go.binary(
146140
go,
147141
name = name,
148142
source = go_info,
149143
gc_linkopts = gc_linkopts(ctx),
150144
version_file = ctx.version_file,
151145
info_file = ctx.info_file,
152-
executable = executable,
146+
executable = None,
153147
)
154148
validation_output = archive.data._validation_output
155149
nogo_diagnostics = archive.data._nogo_diagnostics
156150

151+
if ctx.attr.out:
152+
# Use declare_file instead of attr.output(). When users set output files
153+
# directly, Bazel warns them not to use the same name as the rule, which is
154+
# the common case with go_binary.
155+
executable = ctx.actions.declare_file(ctx.attr.out)
156+
ctx.actions.symlink(
157+
output = executable,
158+
target_file = executable_binary,
159+
is_executable = True,
160+
)
161+
else:
162+
executable = executable_binary
163+
157164
providers = [
158165
archive,
159166
OutputGroupInfo(
@@ -305,15 +312,11 @@ def _go_binary_kwargs(go_cc_aspects = []):
305312
"basename": attr.string(
306313
doc = """The basename of this binary. The binary
307314
basename may also be platform-dependent: on Windows, we add an .exe extension.
315+
When not set, it will default to the label name.
308316
""",
309317
),
310318
"out": attr.string(
311-
doc = """Sets the output filename for the generated executable. When set, `go_binary`
312-
will write this file without mode-specific directory prefixes, without
313-
linkmode-specific prefixes like "lib", and without platform-specific suffixes
314-
like ".exe". Note that without a mode-specific directory prefix, the
315-
output file (but not its dependencies) will be invalidated in Bazel's cache
316-
when changing configurations.
319+
doc = """Sets the output symlink filename for the generated executable.
317320
""",
318321
),
319322
"cgo": attr.bool(

0 commit comments

Comments
 (0)