Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support fully static binaries #161

Closed
mikedanese opened this issue Oct 22, 2016 · 17 comments
Closed

support fully static binaries #161

mikedanese opened this issue Oct 22, 2016 · 17 comments
Assignees

Comments

@mikedanese
Copy link
Contributor

Users should be able to produce fully static binaries from go source, especially exclusively go source.

@pmbethe09
Copy link
Member

The current binaries presumably have a shared dependency on libpthread and libc?

I get the same result on linux if I say 'go install github.com/bazelbuild/rules_go/go/tools/gazelle/gazelle as when I
bazel build //go/tools/gazelle/gazelle:gazelle

What is the magic flag to 'go build' to make it fully static?

@mikedanese
Copy link
Contributor Author

CGO_ENABLED=0 go install github.com/bazelbuild/rules_go/go/tools/gazelle/gazelle

Should produce a static binary.

@powelljo
Copy link
Contributor

powelljo commented Dec 9, 2016

I was just looking for how to set this particular variable, and see it's been discussed here already. Has there been any progress made on passing along values of environment variables that Go builds can depend on? I see there is limited support, specifically for GOOS and GOARCH, already in def.bzl.

@GinFungYJF
Copy link
Contributor

Any update about this? If no, are there any workarounds?

@pmbethe09
Copy link
Member

@jayconrod may look into this

@jayconrod jayconrod self-assigned this Feb 24, 2017
@jayconrod
Copy link
Contributor

I'm definitely planning to improve this. Cross compilation is a slightly higher priority in the short term though.

@mikedanese
Copy link
Contributor Author

Also related: #217

Specifically, this would partially solve (although I believe this uses libc rather than the go libc implementation):

go_binary(
    name = "foo",
    srcs = ["foo.go"],
    gc_linkopts = [
        "-linkmode external",
        "-extldflags -static",
    ],
)

@GinFungYJF
Copy link
Contributor

@mikedanese The gc_linkopts attribute seems to has not been implemented.

@mikedanese
Copy link
Contributor Author

mikedanese commented Mar 1, 2017

@GinFungYJF It is not implemented in the opensource rules but it does exist in the google internal rules (which are completely different). #217 proposes we implement similar feature in opensource.

@jayconrod
Copy link
Contributor

gc_linkopts has been merged for a little while now. So static builds should work even with cgo present.

go_binary(
    name = "main",
    srcs = ["main.go"],
    go_linkopts = [
      "-extldflags",
      "-static",
    ],
    library = ":cgo_lib",
)

cgo_library(
    name = "cgo_lib",
    srcs = ["cgo_lib.go"],
    cdeps = [":foo"],
)

cc_library(
    name = "foo",
    srcs = ["foo.c"],
)

Couple things to note:

  • Linking without cgo is static by default. -linkmode external is not necessary.
  • -extldflags must be a separate string from the actual flags, which must all be one string.
  • Static linking with cgo does not work on macOS.

@mikedanese, is anything missing? Should anything work differently?

@jayconrod
Copy link
Contributor

jayconrod commented Mar 22, 2017

I'm closing this because go_linkopts gc_linkopts gives control over static linking, but please re-open if anything more needs to be done.

@mikedanese
Copy link
Contributor Author

@jayconrod I can't think of anything (other than mac support :) ). Thanks!

@gsf
Copy link

gsf commented Apr 11, 2017

The correct attribute is gc_linkopts, not go_linkopts, correct?

@jayconrod
Copy link
Contributor

Yes, gc_linkopts is the correct option.

@achew22
Copy link
Member

achew22 commented May 27, 2017

Are there more flags that need to be passed in to statically link pthread?

My rule looks like this

  go_binary(
      name = "bin",
      library = ":go_default_library",
      gc_linkopts = [
        "-extldflags",
        "-static",
      ],
  )

when I compile it

$ ldd bazel-bin/my/path/bin
        linux-vdso.so.1 =>  (0x00007ffc97130000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f021b5e6000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f021b21d000)
        /lib64/ld-linux-x86-64.so.2 (0x0000556425e6d000)

Following the dependencies, pthread is coming in from a dependency on sqlite

@mikedanese
Copy link
Contributor Author

@achew22 does this work for you:

https://github.com/kubernetes/kubernetes/blob/v1.7.0-alpha.4/cmd/kubeadm/BUILD#L11-L22

@achew22
Copy link
Member

achew22 commented May 27, 2017

Woo! That fixed it. Thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants