Skip to content

Commit 98165a6

Browse files
authored
Honor //go/config:linkmode for go_test (#3629)
1 parent 61c1e91 commit 98165a6

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

docs/go/core/rules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ This builds a set of tests that can be run with `bazel test`.<br><br>
391391
| <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" |
392392
| <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 | [] |
393393
| <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 | "" |
394-
| <a id="go_test-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>`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 | "normal" |
394+
| <a id="go_test-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" |
395395
| <a id="go_test-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" |
396396
| <a id="go_test-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" |
397397
| <a id="go_test-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/test.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ load(
4646
)
4747
load(
4848
"//go/private:mode.bzl",
49-
"LINKMODE_NORMAL",
49+
"LINKMODES",
5050
)
5151
load(
5252
"@bazel_skylib//lib:structs.bzl",
@@ -292,11 +292,13 @@ _go_test_kwargs = {
292292
""",
293293
),
294294
"linkmode": attr.string(
295-
default = LINKMODE_NORMAL,
295+
default = "auto",
296+
values = ["auto"] + LINKMODES,
296297
doc = """Determines how the binary should be built and linked. This accepts some of
297298
the same values as `go build -buildmode` and works the same way.
298299
<br><br>
299300
<ul>
301+
<li>`auto` (default): Controlled by `//go/config:linkmode`, which defaults to `normal`.</li>
300302
<li>`normal`: Builds a normal executable with position-dependent code.</li>
301303
<li>`pie`: Builds a position-independent executable.</li>
302304
<li>`plugin`: Builds a shared library that can be loaded as a Go plugin. Only supported on platforms that support plugins.</li>

tests/core/go_binary/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,20 @@ linkmode_pie_wrapper(
108108
target = ":hello_nopie_bin",
109109
)
110110

111+
go_test(
112+
name = "hello_nopie_test_bin",
113+
srcs = ["hello.go"],
114+
cgo = True,
115+
tags = ["manual"],
116+
)
117+
118+
linkmode_pie_wrapper(
119+
name = "hello_pie_setting_test_bin",
120+
testonly = True,
121+
tags = ["manual"],
122+
target = ":hello_nopie_test_bin",
123+
)
124+
111125
go_test(
112126
name = "pie_test",
113127
srcs = [
@@ -120,11 +134,13 @@ go_test(
120134
":hello_nopie_bin",
121135
":hello_pie_bin",
122136
":hello_pie_setting_bin",
137+
":hello_pie_setting_test_bin",
123138
],
124139
"@io_bazel_rules_go//go/platform:linux": [
125140
":hello_nopie_bin",
126141
":hello_pie_bin",
127142
":hello_pie_setting_bin",
143+
":hello_pie_setting_test_bin",
128144
],
129145
"//conditions:default": [],
130146
}),

tests/core/go_binary/pie_darwin_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ func TestPIE(t *testing.T) {
3030
}
3131

3232
if m.Flags&macho.FlagPIE == 0 {
33-
t.Error("ELF binary is not position-independent.")
33+
t.Error("MachO binary is not position-independent.")
34+
}
35+
}
36+
37+
func TestPIESetting(t *testing.T) {
38+
m, err := openMachO("tests/core/go_binary", "hello_pie_setting_bin")
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
if m.Flags&macho.FlagPIE == 0 {
44+
t.Error("MachO binary is not position-independent.")
45+
}
46+
}
47+
48+
func TestPIESettingTest(t *testing.T) {
49+
m, err := openMachO("tests/core/go_binary", "hello_pie_setting_test_bin")
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
if m.Flags&macho.FlagPIE == 0 {
55+
t.Error("MachO binary is not position-independent.")
3456
}
3557
}

tests/core/go_binary/pie_linux_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ func TestPIESetting(t *testing.T) {
3535
}
3636
}
3737

38+
func TestPIESettingTest(t *testing.T) {
39+
e, err := openELF("tests/core/go_binary", "hello_pie_setting_test_bin")
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
// PIE binaries are implemented as shared libraries.
45+
if e.Type != elf.ET_DYN {
46+
t.Error("ELF binary is not position-independent.")
47+
}
48+
}
49+
3850
func TestPIE(t *testing.T) {
3951
e, err := openELF("tests/core/go_binary", "hello_pie_bin")
4052
if err != nil {

0 commit comments

Comments
 (0)