Skip to content

Commit

Permalink
Honor //go/config:linkmode for go_test (#3629)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum authored Jul 18, 2023
1 parent 61c1e91 commit 98165a6
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/go/core/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ This builds a set of tests that can be run with `bazel test`.<br><br>
| <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" |
| <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 | [] |
| <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 | "" |
| <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" |
| <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" |
| <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" |
| <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" |
| <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" |
Expand Down
6 changes: 4 additions & 2 deletions go/private/rules/test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ load(
)
load(
"//go/private:mode.bzl",
"LINKMODE_NORMAL",
"LINKMODES",
)
load(
"@bazel_skylib//lib:structs.bzl",
Expand Down Expand Up @@ -292,11 +292,13 @@ _go_test_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.
<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>
Expand Down
16 changes: 16 additions & 0 deletions tests/core/go_binary/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ linkmode_pie_wrapper(
target = ":hello_nopie_bin",
)

go_test(
name = "hello_nopie_test_bin",
srcs = ["hello.go"],
cgo = True,
tags = ["manual"],
)

linkmode_pie_wrapper(
name = "hello_pie_setting_test_bin",
testonly = True,
tags = ["manual"],
target = ":hello_nopie_test_bin",
)

go_test(
name = "pie_test",
srcs = [
Expand All @@ -120,11 +134,13 @@ go_test(
":hello_nopie_bin",
":hello_pie_bin",
":hello_pie_setting_bin",
":hello_pie_setting_test_bin",
],
"@io_bazel_rules_go//go/platform:linux": [
":hello_nopie_bin",
":hello_pie_bin",
":hello_pie_setting_bin",
":hello_pie_setting_test_bin",
],
"//conditions:default": [],
}),
Expand Down
24 changes: 23 additions & 1 deletion tests/core/go_binary/pie_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ func TestPIE(t *testing.T) {
}

if m.Flags&macho.FlagPIE == 0 {
t.Error("ELF binary is not position-independent.")
t.Error("MachO binary is not position-independent.")
}
}

func TestPIESetting(t *testing.T) {
m, err := openMachO("tests/core/go_binary", "hello_pie_setting_bin")
if err != nil {
t.Fatal(err)
}

if m.Flags&macho.FlagPIE == 0 {
t.Error("MachO binary is not position-independent.")
}
}

func TestPIESettingTest(t *testing.T) {
m, err := openMachO("tests/core/go_binary", "hello_pie_setting_test_bin")
if err != nil {
t.Fatal(err)
}

if m.Flags&macho.FlagPIE == 0 {
t.Error("MachO binary is not position-independent.")
}
}
12 changes: 12 additions & 0 deletions tests/core/go_binary/pie_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func TestPIESetting(t *testing.T) {
}
}

func TestPIESettingTest(t *testing.T) {
e, err := openELF("tests/core/go_binary", "hello_pie_setting_test_bin")
if err != nil {
t.Fatal(err)
}

// PIE binaries are implemented as shared libraries.
if e.Type != elf.ET_DYN {
t.Error("ELF binary is not position-independent.")
}
}

func TestPIE(t *testing.T) {
e, err := openELF("tests/core/go_binary", "hello_pie_bin")
if err != nil {
Expand Down

0 comments on commit 98165a6

Please sign in to comment.