Skip to content

Commit 7c5c55d

Browse files
blazingbbqrickystewart
authored andcommitted
feat(mode): Add gofips140 mode
This captures code currently in the form of a draft PR at bazel-contrib#4449. When this PR is merged, this commit can be omitted from the cherry-picks onto our `crl-release-*` branches.
1 parent aa76655 commit 7c5c55d

File tree

14 files changed

+204
-10
lines changed

14 files changed

+204
-10
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ go_config(
130130
export_stdlib = "//go/config:export_stdlib",
131131
gc_goopts = "//go/config:gc_goopts",
132132
gc_linkopts = "//go/config:gc_linkopts",
133+
gofips140 = "//go/config:gofips140",
133134
gotags = "//go/config:tags",
134135
linkmode = "//go/config:linkmode",
135136
msan = "//go/config:msan",

docs/go/core/rules.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
[config_setting]: https://docs.bazel.build/versions/master/be/general.html#config_setting
1212
[data dependencies]: https://bazel.build/concepts/dependencies#data-dependencies
1313
[goarch]: /go/modes.rst#goarch
14+
[gofips140]: /go/modes.rst#gofips140
1415
[goos]: /go/modes.rst#goos
1516
[mode attributes]: /go/modes.rst#mode-attributes
1617
[nogo]: /go/nogo.rst#nogo
@@ -58,6 +59,7 @@ sufficient to match the capabilities of the normal go tools.
5859
- [config_setting]
5960
- [data dependencies]
6061
- [goarch]
62+
- [gofips140]
6163
- [goos]
6264
- [mode attributes]
6365
- [nogo]

docs/go/core/rules.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[config_setting]: https://docs.bazel.build/versions/master/be/general.html#config_setting
1414
[data dependencies]: https://bazel.build/concepts/dependencies#data-dependencies
1515
[goarch]: /go/modes.rst#goarch
16+
[gofips140]: /go/modes.rst#gofips140
1617
[goos]: /go/modes.rst#goos
1718
[mode attributes]: /go/modes.rst#mode-attributes
1819
[nogo]: /go/nogo.rst#nogo
@@ -60,6 +61,7 @@ sufficient to match the capabilities of the normal go tools.
6061
- [config_setting]
6162
- [data dependencies]
6263
- [goarch]
64+
- [gofips140]
6365
- [goos]
6466
- [mode attributes]
6567
- [nogo]
@@ -124,8 +126,8 @@ Rules
124126

125127
<pre>
126128
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>,
127-
<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>)
129+
<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-gofips140">gofips140</a>, <a href="#go_binary-goos">goos</a>, <a href="#go_binary-gotags">gotags</a>, <a href="#go_binary-importpath">importpath</a>,
130+
<a href="#go_binary-linkmode">linkmode</a>, <a href="#go_binary-msan">msan</a>, <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>)
129131
</pre>
130132

131133
This builds an executable from a set of source files,
@@ -136,7 +138,7 @@ This builds an executable from a set of source files,
136138
<ul>
137139
<li>[GoArchive]</li>
138140
</ul>
139-
141+
140142

141143
### **Attributes**
142144

@@ -159,6 +161,7 @@ This builds an executable from a set of source files,
159161
| <a id="go_binary-gc_goopts"></a>gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] |
160162
| <a id="go_binary-gc_linkopts"></a>gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] |
161163
| <a id="go_binary-goarch"></a>goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with <code>--platforms</code>.<br><br> This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set <code>pure</code> = <code>off</code>.<br><br> See [Cross compilation] for more information. | String | optional | "auto" |
164+
| <a id="go_binary-gofips140"></a>gofips140 | Controls the GOFIPS140 environment variable. May be any string value. Common values include <code>"off"</code> (default), <code>"latest"</code>, and specific versions like <code>"v1.0.0"</code>. See [mode attributes], specifically [gofips140]. | String | optional | "off" |
162165
| <a id="go_binary-goos"></a>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 <code>--platforms</code>.<br><br> This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set <code>pure</code> = <code>off</code>.<br><br> See [Cross compilation] for more information. | String | optional | "auto" |
163166
| <a id="go_binary-gotags"></a>gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] |
164167
| <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 | "" |
@@ -191,7 +194,7 @@ This wraps an executable built by `go_binary` to cross compile it
191194
<ul>
192195
<li>[GoArchive]</li>
193196
</ul>
194-
197+
195198

196199
### **Attributes**
197200

@@ -226,7 +229,7 @@ This builds a Go library from a set of source files that are all part of
226229
<li>[GoInfo]</li>
227230
<li>[GoArchive]</li>
228231
</ul>
229-
232+
230233

231234
### **Attributes**
232235

@@ -269,7 +272,7 @@ go_path(<a href="#go_path-name">name</a>, <a href="#go_path-data">data</a>, <a h
269272
`go_path` can depend on one or more Go targets (i.e., [go_library], [go_binary], or [go_test]).
270273
It will include packages from those targets, as well as their transitive dependencies.
271274
Packages will be in subdirectories named after their `importpath` or `importmap` attributes under a `src/` directory.
272-
275+
273276

274277
### **Attributes**
275278

@@ -339,7 +342,7 @@ This declares a set of source files and related dependencies that can be embedde
339342
<ul>
340343
<li>[GoInfo]</li>
341344
</ul>
342-
345+
343346

344347
### **Attributes**
345348

@@ -363,8 +366,8 @@ This declares a set of source files and related dependencies that can be embedde
363366

364367
<pre>
365368
go_test(<a href="#go_test-name">name</a>, <a href="#go_test-cdeps">cdeps</a>, <a href="#go_test-cgo">cgo</a>, <a href="#go_test-clinkopts">clinkopts</a>, <a href="#go_test-copts">copts</a>, <a href="#go_test-cppopts">cppopts</a>, <a href="#go_test-cxxopts">cxxopts</a>, <a href="#go_test-data">data</a>, <a href="#go_test-deps">deps</a>, <a href="#go_test-embed">embed</a>, <a href="#go_test-embedsrcs">embedsrcs</a>, <a href="#go_test-env">env</a>,
366-
<a href="#go_test-env_inherit">env_inherit</a>, <a href="#go_test-gc_goopts">gc_goopts</a>, <a href="#go_test-gc_linkopts">gc_linkopts</a>, <a href="#go_test-goarch">goarch</a>, <a href="#go_test-goos">goos</a>, <a href="#go_test-gotags">gotags</a>, <a href="#go_test-importpath">importpath</a>, <a href="#go_test-linkmode">linkmode</a>, <a href="#go_test-msan">msan</a>, <a href="#go_test-pure">pure</a>,
367-
<a href="#go_test-race">race</a>, <a href="#go_test-rundir">rundir</a>, <a href="#go_test-srcs">srcs</a>, <a href="#go_test-static">static</a>, <a href="#go_test-x_defs">x_defs</a>)
369+
<a href="#go_test-env_inherit">env_inherit</a>, <a href="#go_test-gc_goopts">gc_goopts</a>, <a href="#go_test-gc_linkopts">gc_linkopts</a>, <a href="#go_test-goarch">goarch</a>, <a href="#go_test-gofips140">gofips140</a>, <a href="#go_test-goos">goos</a>, <a href="#go_test-gotags">gotags</a>, <a href="#go_test-importpath">importpath</a>, <a href="#go_test-linkmode">linkmode</a>,
370+
<a href="#go_test-msan">msan</a>, <a href="#go_test-pure">pure</a>, <a href="#go_test-race">race</a>, <a href="#go_test-rundir">rundir</a>, <a href="#go_test-srcs">srcs</a>, <a href="#go_test-static">static</a>, <a href="#go_test-x_defs">x_defs</a>)
368371
</pre>
369372

370373
This builds a set of tests that can be run with `bazel test`.<br><br>
@@ -396,7 +399,7 @@ This builds a set of tests that can be run with `bazel test`.<br><br>
396399
the name based on the last component of the path. For example, a test
397400
in `//foo/bar` is named `bar_test`, and uses internal and external
398401
sources.
399-
402+
400403

401404
### **Attributes**
402405

@@ -419,6 +422,7 @@ This builds a set of tests that can be run with `bazel test`.<br><br>
419422
| <a id="go_test-gc_goopts"></a>gc_goopts | List of flags to add to the Go compilation command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] |
420423
| <a id="go_test-gc_linkopts"></a>gc_linkopts | List of flags to add to the Go link command when using the gc compiler. Subject to ["Make variable"] substitution and [Bourne shell tokenization]. | List of strings | optional | [] |
421424
| <a id="go_test-goarch"></a>goarch | Forces a binary to be cross-compiled for a specific architecture. It's usually better to control this on the command line with <code>--platforms</code>.<br><br> This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set <code>pure</code> = <code>off</code>.<br><br> See [Cross compilation] for more information. | String | optional | "auto" |
425+
| <a id="go_test-gofips140"></a>gofips140 | Controls the GOFIPS140 environment variable. May be any string value. Common values include <code>"off"</code> (default), <code>"latest"</code>, and specific versions like <code>"v1.0.0"</code>. See [mode attributes], specifically [gofips140]. | String | optional | "off" |
422426
| <a id="go_test-goos"></a>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 <code>--platforms</code>.<br><br> This disables cgo by default, since a cross-compiling C/C++ toolchain is rarely available. To force cgo, set <code>pure</code> = <code>off</code>.<br><br> See [Cross compilation] for more information. | String | optional | "auto" |
423427
| <a id="go_test-gotags"></a>gotags | Enables a list of build tags when evaluating [build constraints]. Useful for conditional compilation. | List of strings | optional | [] |
424428
| <a id="go_test-importpath"></a>importpath | The import path of this test. Tests 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 | "" |

go/config/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ bool_flag(
3333
visibility = ["//visibility:public"],
3434
)
3535

36+
string_flag(
37+
name = "gofips140",
38+
build_setting_default = "off",
39+
visibility = ["//visibility:public"],
40+
)
41+
3642
bool_flag(
3743
name = "debug",
3844
build_setting_default = False,

go/core.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Core Go rules
1313
.. _config_setting: https://docs.bazel.build/versions/master/be/general.html#config_setting
1414
.. _data dependencies: https://bazel.build/concepts/dependencies#data-dependencies
1515
.. _goarch: modes.rst#goarch
16+
.. _gofips140: modes.rst#gofips140
1617
.. _goos: modes.rst#goos
1718
.. _mode attributes: modes.rst#mode-attributes
1819
.. _nogo: nogo.rst#nogo

go/modes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ or using `Bazel configuration transitions`_.
6969
| ``CGO_ENABLED=0``). Packages that contain cgo code may still be built, but |
7070
| the cgo code will be filtered out, and the ``cgo`` build tag will be false. |
7171
+------------------------+---------------------+-------------------------------+
72+
| :param:`gofips140` | :type:`string` | :value:`"off"` |
73+
+------------------------+---------------------+-------------------------------+
74+
| Controls the ``GOFIPS140`` environment variable used by Go 1.24+ to select |
75+
| the version of the Go Cryptographic Module. Can be set to ``"off"`` |
76+
| (default), ``"latest"``, or a specific version like ``"v1.0.0"``. |
77+
| See the `Go 1.24 FIPS 140-3 documentation`_ for more details. |
78+
+------------------------+---------------------+-------------------------------+
7279
| :param:`debug` | :type:`bool` | :value:`false` |
7380
+------------------------+---------------------+-------------------------------+
7481
| Includes debugging information in compiled packages (using the ``-N`` and |

go/private/context.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ default_go_config_info = GoConfigInfo(
439439
race = False,
440440
msan = False,
441441
pure = False,
442+
gofips140 = "off",
442443
strip = False,
443444
debug = False,
444445
linkmode = LINKMODE_NORMAL,
@@ -527,6 +528,7 @@ def go_context(
527528
"GOROOT": goroot,
528529
"GOROOT_FINAL": "GOROOT",
529530
"CGO_ENABLED": "0" if mode.pure else "1",
531+
"GOFIPS140": mode.gofips140,
530532

531533
# If we use --action_env=GOPATH, or in other cases where environment
532534
# variables are passed through to this builder, the SDK build will try
@@ -984,6 +986,7 @@ def _go_config_impl(ctx):
984986
race = race,
985987
msan = msan,
986988
pure = ctx.attr.pure[BuildSettingInfo].value,
989+
gofips140 = ctx.attr.gofips140[BuildSettingInfo].value,
987990
strip = ctx.attr.strip,
988991
debug = ctx.attr.debug[BuildSettingInfo].value,
989992
linkmode = ctx.attr.linkmode[BuildSettingInfo].value,
@@ -1020,6 +1023,10 @@ go_config = rule(
10201023
mandatory = True,
10211024
providers = [BuildSettingInfo],
10221025
),
1026+
"gofips140": attr.label(
1027+
mandatory = True,
1028+
providers = [BuildSettingInfo],
1029+
),
10231030
"strip": attr.bool(mandatory = True),
10241031
"debug": attr.label(
10251032
mandatory = True,

go/private/mode.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def mode_string(mode):
4848
result.append("msan")
4949
if mode.pure:
5050
result.append("pure")
51+
if mode.gofips140 != "off":
52+
result.append("gofips140=" + mode.gofips140)
5153
if mode.debug:
5254
result.append("debug")
5355
if mode.strip:

go/private/rules/binary.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ def _go_binary_kwargs(go_cc_aspects = []):
367367
[pure].
368368
""",
369369
),
370+
"gofips140": attr.string(
371+
default = "off",
372+
doc = """Controls the GOFIPS140 environment variable. May be any string value.
373+
Common values include `"off"` (default), `"latest"`, and specific versions like `"v1.0.0"`.
374+
See [mode attributes], specifically [gofips140].
375+
""",
376+
),
370377
"static": attr.string(
371378
default = "auto",
372379
doc = """Controls whether a binary is statically linked. May be one of `on`,

go/private/rules/test.bzl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,13 @@ _go_test_kwargs = {
398398
[pure].
399399
""",
400400
),
401+
"gofips140": attr.string(
402+
default = "off",
403+
doc = """Controls the GOFIPS140 environment variable. May be any string value.
404+
Common values include `"off"` (default), `"latest"`, and specific versions like `"v1.0.0"`.
405+
See [mode attributes], specifically [gofips140].
406+
""",
407+
),
401408
"static": attr.string(
402409
default = "auto",
403410
doc = """Controls whether a binary is statically linked. May be one of `on`,

0 commit comments

Comments
 (0)