bazel-contrib / bazel-contrib/rules_go
Allow the static on/off attribute of go_binary to control static linking with cgo
- Dominant language
- Go
- Stars
- 1.5k
- Forks
- 760
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 12
Description
This is a follow up from #3590 where we noticed that client cgo could also lead to static linking.
### What version of rules_go are you using?
0.40
### What version of gazelle are you using?
0.30.0
### What version of Bazel are you using?
6.2.1
### Does this issue reproduce with the latest releases of all the above?
yes
### What operating system and processor architecture are you using?
Linux, Debian, AARCH64
### Any other potentially useful information about your toolchain?
### What did you do?
```
$ cat cat .bazelrc
build --features=fully_static_link --dynamic_mode=off
$ cat WORKSPACE
workspace(name = "rules_go_static")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "6b65cb7917b4d1709f9410ffe00ecf3e160edf674b78c54a894471320862184f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
],
)
http_archive(
name = "bazel_gazelle",
sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
],
)
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
go_rules_dependencies()
go_register_toolchains(version = "1.19.3")
#go_register_toolchains(version = "1.20.3")
$ cat BUILD.file
load("@io_bazel_rules_go//go:def.bzl", "go_binary")
go_binary(
name = "c_hello",
srcs = ["c_hello.go"],
cgo = True,
static = "off", # "on"
)
$ cat c_hello.go
package main
// typedef int (*intFunc) ();
//
// int
// bridge_int_func(intFunc f)
// {
// return f();
// }
//
// int fortytwo()
// {
// return 42;
// }
import "C"
import "fmt"
func main() {
f := C.intFunc(C.fortytwo)
fmt.Println(int(C.bridge_int_func(f)))
// Output: 42
}
$ bazel build //:c_hello && file bazel-bin/c_hello_/c_hello
```
### What did you expect to see?
Given the `static = "off"`, I expected this binary to not be linked statically (regardless of my toolchain config).
### What did you see instead?
```
bazel-bin/c_hello_/c_hello: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, for GNU/Linux 3.7.0, Go BuildID=redacted, stripped
```
Contributor guide
Research direction
Reproduce the issue with .bazelrc, WORKSPACE, BUILD.file, and c_hello.go using the shown Bazel command. Start by tracing how go_binary's static attribute is handled when cgo is enabled; done means static = "off" prevents static linking even with the fully_static_link feature enabled.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100