1

[zig ld] --gc-sections workaround + tests

`zig cc` emits `--gc-sections` for the linker, which is incompatbile
with what CGo thinks about linking.

This commit adds a workaround: it will add `--no-gc-sections` to the
linking step if the command is not specified (falling back to the
default behavior of gcc/clang).

Related: https://github.com/golang/go/issues/52690
This commit is contained in:
Motiejus Jakštys
2022-05-05 13:36:58 +03:00
parent 4cc8a7b551
commit 9ce21b5276
8 changed files with 128 additions and 19 deletions

View File

@@ -3,29 +3,29 @@ load("//rules:rules_go.bzl", "go_binary")
load("@bazel-zig-cc//rules:platform.bzl", "platform_binary", "platform_test")
go_library(
name = "go_lib",
srcs = ["hello.go"],
name = "cgo_lib",
srcs = ["cgo.go"],
cgo = True,
importpath = "git.sr.ht/~motiejus/bazel-zig-cc/test/go",
importpath = "git.sr.ht/~motiejus/bazel-zig-cc/test/cgo",
visibility = ["//visibility:private"],
)
go_binary(
name = "go",
embed = [":go_lib"],
visibility = ["//visibility:public"],
go_test(
name = "cgo_test",
srcs = ["cgo_test.go"],
embed = [":cgo_lib"],
)
go_test(
name = "go_test",
srcs = ["hello_test.go"],
embed = [":go_lib"],
go_binary(
name = "cgo",
embed = [":cgo_lib"],
visibility = ["//visibility:public"],
)
[
platform_binary(
name = "go_{}".format(name),
src = "go",
name = "cgo_{}".format(name),
src = "cgo",
platform = platform,
)
for name, platform in [
@@ -39,8 +39,8 @@ go_test(
[
platform_test(
name = "go_test_{}".format(name),
src = "go_test",
name = "cgo_test_{}".format(name),
src = "cgo_test",
platform = platform,
)
for name, platform in [

24
test/gorace/BUILD Normal file
View File

@@ -0,0 +1,24 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("//rules:rules_go.bzl", "go_binary")
go_library(
name = "gorace_lib",
srcs = ["main.go"],
# keep
cgo = True,
importpath = "git.sr.ht/~motiejus/bazel-zig-cc/test/gorace",
visibility = ["//visibility:private"],
)
go_binary(
name = "gorace",
embed = [":gorace_lib"],
visibility = ["//visibility:public"],
)
go_test(
name = "gorace_test",
srcs = ["main_test.go"],
embed = [":gorace_lib"],
race = "on",
)

13
test/gorace/main.go Normal file
View File

@@ -0,0 +1,13 @@
// Package main tests that Zig can compile race-enabled tests.
//
// As of writing, this fails:
// CGO_ENABLED=1 CC="zig cc" go test -race .
//
// More context: https://github.com/ziglang/zig/issues/11398
//
// This fails, because `zig cc` adds `--gc-sections` to the linker
// flag by default, which is incompatible with cgo. bazel-zig-cc
// adds a workaround for it.
package main
func main() {}

8
test/gorace/main_test.go Normal file
View File

@@ -0,0 +1,8 @@
package main
import "testing"
func TestFoo(t *testing.T) {
_ = t
main()
}