diff --git a/Makefile b/Makefile index 5cb9231a18..567b6e94ab 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,13 @@ geth: @echo "Done building." @echo "Run \"$(GOBIN)/geth\" to launch geth." +geth-static: + build/env.sh go run build/ci.go install -static ./cmd/geth + @echo "Done building." + @echo "Run \"$(GOBIN)/geth\" to launch geth." + +static: geth-static + swarm: build/env.sh go run build/ci.go install ./cmd/swarm @echo "Done building." @@ -24,6 +31,9 @@ swarm: all: build/env.sh go run build/ci.go install +all-static: + build/env.sh go run build/ci.go install -static + android: build/env.sh go run build/ci.go aar --local @echo "Done building." diff --git a/build/ci.go b/build/ci.go index 29b43783c1..a041b2efba 100644 --- a/build/ci.go +++ b/build/ci.go @@ -173,12 +173,13 @@ func main() { func doInstall(cmdline []string) { var ( - arch = flag.String("arch", "", "Architecture to cross build for") - cc = flag.String("cc", "", "C compiler to cross build with") + arch = flag.String("arch", "", "Architecture to cross build for") + cc = flag.String("cc", "", "C compiler to cross build with") + static = flag.Bool("static", false, "Build a static linked binary") ) flag.CommandLine.Parse(cmdline) env := build.Env() - + env.Static = *static // Check Go version. People regularly open issues about compilation // failure with outdated Go. This should save them the trouble. if !strings.Contains(runtime.Version(), "devel") { @@ -246,13 +247,14 @@ func buildFlags(env build.Environment) (flags []string) { if env.Commit != "" { ld = append(ld, "-X", "main.gitCommit="+env.Commit) } - if runtime.GOOS == "darwin" { - ld = append(ld, "-s") + ld = append(ld, "-s") + ld = append(ld, "-w") + + if env.Static { + ld = append(ld, "-extldflags -static") } - if len(ld) > 0 { - flags = append(flags, "-ldflags", strings.Join(ld, " ")) - } + flags = append(flags, "-ldflags", strings.Join(ld, " ")) return flags } diff --git a/internal/build/env.go b/internal/build/env.go index b553e0ed80..be93c3cb1a 100644 --- a/internal/build/env.go +++ b/internal/build/env.go @@ -41,11 +41,12 @@ type Environment struct { Buildnum string IsPullRequest bool IsCronJob bool + Static bool } func (env Environment) String() string { - return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)", - env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest) + return fmt.Sprintf("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t static:%t)", + env.Name, env.Commit, env.Branch, env.Tag, env.Buildnum, env.IsPullRequest, env.Static) } // Env returns metadata about the current CI environment, falling back to LocalEnv