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

build: add static linking support #25492

Merged
merged 6 commits into from Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Expand Up @@ -14,7 +14,7 @@ COPY go.sum /go-ethereum/
RUN cd /go-ethereum && go mod download

ADD . /go-ethereum
RUN cd /go-ethereum && go run build/ci.go install ./cmd/geth
RUN cd /go-ethereum && go run build/ci.go install -static ./cmd/geth

# Pull Geth into a second stage deploy alpine container
FROM alpine:latest
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.alltools
Expand Up @@ -14,7 +14,7 @@ COPY go.sum /go-ethereum/
RUN cd /go-ethereum && go mod download

ADD . /go-ethereum
RUN cd /go-ethereum && go run build/ci.go install
RUN cd /go-ethereum && go run build/ci.go install -static

# Pull all binaries into a second stage deploy alpine container
FROM alpine:latest
Expand Down
16 changes: 12 additions & 4 deletions build/ci.go
Expand Up @@ -200,9 +200,10 @@ func main() {

func doInstall(cmdline []string) {
var (
dlgo = flag.Bool("dlgo", false, "Download Go and build with it")
arch = flag.String("arch", "", "Architecture to cross build for")
cc = flag.String("cc", "", "C compiler to cross build with")
dlgo = flag.Bool("dlgo", false, "Download Go and build with it")
arch = flag.String("arch", "", "Architecture to cross build for")
cc = flag.String("cc", "", "C compiler to cross build with")
staticlink = flag.Bool("static", false, "Static link build with")
)
flag.CommandLine.Parse(cmdline)

Expand All @@ -215,6 +216,9 @@ func doInstall(cmdline []string) {

// Configure the build.
env := build.Env()
if *staticlink {
env.StaticLink = true
}
gobuild := tc.Go("build", buildFlags(env)...)

// arm64 CI builders are memory-constrained and can't handle concurrent builds,
Expand Down Expand Up @@ -265,7 +269,11 @@ func buildFlags(env build.Environment) (flags []string) {
// Enforce the stacksize to 8M, which is the case on most platforms apart from
// alpine Linux.
if runtime.GOOS == "linux" {
ld = append(ld, "-extldflags", "-Wl,-z,stack-size=0x800000")
staticlinkflag := ""
if env.StaticLink {
staticlinkflag = "-static"
}
ld = append(ld, "-extldflags", fmt.Sprintf("'-Wl,-z,stack-size=0x800000 %s'", staticlinkflag))
}
if len(ld) > 0 {
flags = append(flags, "-ldflags", strings.Join(ld, " "))
Expand Down
9 changes: 7 additions & 2 deletions internal/build/env.go
Expand Up @@ -34,6 +34,7 @@ var (
BuildnumFlag = flag.String("buildnum", "", `Overrides CI build number`)
PullRequestFlag = flag.Bool("pull-request", false, `Overrides pull request status of the build`)
CronJobFlag = flag.Bool("cron-job", false, `Overrides cron job status of the build`)
StaticLink = flag.Bool("static-link", false, `Overrides static link status of the build`)
)

// Environment contains metadata provided by the build environment.
Expand All @@ -45,11 +46,12 @@ type Environment struct {
Buildnum string
IsPullRequest bool
IsCronJob bool
StaticLink bool
fjl marked this conversation as resolved.
Show resolved Hide resolved
}

func (env Environment) String() string {
return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)",
env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest)
return fmt.Sprintf("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t static:%t)",
env.Name, env.Commit, env.Date, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest, env.StaticLink)
}

// Env returns metadata about the current CI environment, falling back to LocalEnv
Expand Down Expand Up @@ -168,5 +170,8 @@ func applyEnvFlags(env Environment) Environment {
if *CronJobFlag {
env.IsCronJob = true
}
if *StaticLink {
env.StaticLink = true
}
return env
}