Skip to content

Commit 3295db9

Browse files
committed
Add attribute to infer output name from basename
This reduces duplication.
1 parent c03a5f0 commit 3295db9

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

docs/go/core/rules.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Rules
125125
<pre>
126126
go_binary(<a href="#go_binary-name">name</a>, <a href="#go_binary-basename">basename</a>, <a href="#go_binary-cdeps">cdeps</a>, <a href="#go_binary-cgo">cgo</a>, <a href="#go_binary-clinkopts">clinkopts</a>, <a href="#go_binary-copts">copts</a>, <a href="#go_binary-cppopts">cppopts</a>, <a href="#go_binary-cxxopts">cxxopts</a>, <a href="#go_binary-data">data</a>, <a href="#go_binary-deps">deps</a>, <a href="#go_binary-embed">embed</a>,
127127
<a href="#go_binary-embedsrcs">embedsrcs</a>, <a href="#go_binary-env">env</a>, <a href="#go_binary-gc_goopts">gc_goopts</a>, <a href="#go_binary-gc_linkopts">gc_linkopts</a>, <a href="#go_binary-goarch">goarch</a>, <a href="#go_binary-goos">goos</a>, <a href="#go_binary-gotags">gotags</a>, <a href="#go_binary-importpath">importpath</a>, <a href="#go_binary-linkmode">linkmode</a>, <a href="#go_binary-msan">msan</a>,
128-
<a href="#go_binary-out">out</a>, <a href="#go_binary-pgoprofile">pgoprofile</a>, <a href="#go_binary-pure">pure</a>, <a href="#go_binary-race">race</a>, <a href="#go_binary-srcs">srcs</a>, <a href="#go_binary-static">static</a>, <a href="#go_binary-x_defs">x_defs</a>)
128+
<a href="#go_binary-out">out</a>, <a href="#go_binary-out_auto">out_auto</a>, <a href="#go_binary-pgoprofile">pgoprofile</a>, <a href="#go_binary-pure">pure</a>, <a href="#go_binary-race">race</a>, <a href="#go_binary-srcs">srcs</a>, <a href="#go_binary-static">static</a>, <a href="#go_binary-x_defs">x_defs</a>)
129129
</pre>
130130

131131
This builds an executable from a set of source files,
@@ -164,7 +164,8 @@ 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 symlink filename for the generated executable. | String | optional | "" |
167+
| <a id="go_binary-out"></a>out | Create a symlink with this name for the generated executable. | String | optional | "" |
168+
| <a id="go_binary-out_auto"></a>out_auto | Create a symlink with a name inferred from basename for the generated executable. | Boolean | optional | False |
168169
| <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 |
169170
| <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" |
170171
| <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: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ _go_cc_aspect = aspect(
115115

116116
def _go_binary_impl(ctx):
117117
"""go_binary_impl emits actions for compiling and linking a go executable."""
118+
if ctx.attr.out and ctx.attr.out_auto:
119+
fail("Only one of out and out_auto must be set.")
120+
118121
go = go_context(
119122
ctx,
120123
include_deprecated_properties = False,
@@ -148,11 +151,14 @@ def _go_binary_impl(ctx):
148151
validation_output = archive.data._validation_output
149152
nogo_diagnostics = archive.data._nogo_diagnostics
150153

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)
154+
if ctx.attr.out or ctx.attr.out_auto:
155+
if ctx.attr.out:
156+
# Use declare_file instead of attr.output(). When users set output files
157+
# directly, Bazel warns them not to use the same name as the rule, which is
158+
# the common case with go_binary.
159+
executable = ctx.actions.declare_file(ctx.attr.out)
160+
else:
161+
executable = ctx.actions.declare_file(name)
156162
ctx.actions.symlink(
157163
output = executable,
158164
target_file = executable_binary,
@@ -316,7 +322,11 @@ def _go_binary_kwargs(go_cc_aspects = []):
316322
""",
317323
),
318324
"out": attr.string(
319-
doc = """Sets the output symlink filename for the generated executable.
325+
doc = """Create a symlink with this name for the generated executable.
326+
""",
327+
),
328+
"out_auto": attr.bool(
329+
doc = """Create a symlink with a name inferred from basename for the generated executable.
320330
""",
321331
),
322332
"cgo": attr.bool(

0 commit comments

Comments
 (0)