diff --git a/build.go b/build.go index 9c22ac951..ce2e95e46 100644 --- a/build.go +++ b/build.go @@ -24,6 +24,7 @@ import ( "os" "os/exec" "os/user" + "path" "path/filepath" "regexp" "runtime" @@ -59,7 +60,7 @@ type target struct { debpre string debpost string description string - buildPkg string + buildPkgs []string binaryName string archiveFiles []archiveFile systemdServices []string @@ -76,9 +77,8 @@ type archiveFile struct { var targets = map[string]target{ "all": { // Only valid for the "build" and "install" commands as it lacks all - // the archive creation stuff. - buildPkg: "github.com/syncthing/syncthing/cmd/...", - tags: []string{"purego"}, + // the archive creation stuff. buildPkgs gets filled out in init() + tags: []string{"purego"}, }, "syncthing": { // The default target for "build", "install", "tar", "zip", "deb", etc. @@ -87,7 +87,7 @@ var targets = map[string]target{ debdeps: []string{"libc6", "procps"}, debpost: "script/post-upgrade", description: "Open Source Continuous File Synchronization", - buildPkg: "github.com/syncthing/syncthing/cmd/syncthing", + buildPkgs: []string{"github.com/syncthing/syncthing/cmd/syncthing"}, binaryName: "syncthing", // .exe will be added automatically for Windows builds archiveFiles: []archiveFile{ {src: "{{binary}}", dst: "{{binary}}", perm: 0755}, @@ -131,7 +131,7 @@ var targets = map[string]target{ debdeps: []string{"libc6"}, debpre: "cmd/stdiscosrv/scripts/preinst", description: "Syncthing Discovery Server", - buildPkg: "github.com/syncthing/syncthing/cmd/stdiscosrv", + buildPkgs: []string{"github.com/syncthing/syncthing/cmd/stdiscosrv"}, binaryName: "stdiscosrv", // .exe will be added automatically for Windows builds archiveFiles: []archiveFile{ {src: "{{binary}}", dst: "{{binary}}", perm: 0755}, @@ -159,7 +159,7 @@ var targets = map[string]target{ debdeps: []string{"libc6"}, debpre: "cmd/strelaysrv/scripts/preinst", description: "Syncthing Relay Server", - buildPkg: "github.com/syncthing/syncthing/cmd/strelaysrv", + buildPkgs: []string{"github.com/syncthing/syncthing/cmd/strelaysrv"}, binaryName: "strelaysrv", // .exe will be added automatically for Windows builds archiveFiles: []archiveFile{ {src: "{{binary}}", dst: "{{binary}}", perm: 0755}, @@ -187,7 +187,7 @@ var targets = map[string]target{ debname: "syncthing-relaypoolsrv", debdeps: []string{"libc6"}, description: "Syncthing Relay Pool Server", - buildPkg: "github.com/syncthing/syncthing/cmd/strelaypoolsrv", + buildPkgs: []string{"github.com/syncthing/syncthing/cmd/strelaypoolsrv"}, binaryName: "strelaypoolsrv", // .exe will be added automatically for Windows builds archiveFiles: []archiveFile{ {src: "{{binary}}", dst: "{{binary}}", perm: 0755}, @@ -217,6 +217,18 @@ var dependencyRepos = []dependencyRepo{ } func init() { + all := targets["all"] + pkgs, _ := filepath.Glob("cmd/*") + for _, pkg := range pkgs { + pkg = filepath.Base(pkg) + if strings.HasPrefix(pkg, ".") { + // ignore dotfiles + continue + } + all.buildPkgs = append(all.buildPkgs, fmt.Sprintf("github.com/syncthing/syncthing/cmd/%s", pkg)) + } + targets["all"] = all + // The "syncthing" target includes a few more files found in the "etc" // and "extra" dirs. syncthingPkg := targets["syncthing"] @@ -382,9 +394,6 @@ func install(target target, tags []string) { } os.Setenv("GOBIN", filepath.Join(cwd, "bin")) - args := []string{"install", "-v"} - args = appendParameters(args, tags, target) - os.Setenv("GOOS", goos) os.Setenv("GOARCH", goarch) os.Setenv("CC", cc) @@ -400,19 +409,20 @@ func install(target target, tags []string) { defer shouldCleanupSyso(sysoPath) } - runPrint(goCmd, args...) + for _, pkg := range target.buildPkgs { + args := []string{"install", "-v"} + args = appendParameters(args, tags, pkg) + + runPrint(goCmd, args...) + } } func build(target target, tags []string) { lazyRebuildAssets() - tags = append(target.tags, tags...) rmr(target.BinaryName()) - args := []string{"build", "-v"} - args = appendParameters(args, tags, target) - os.Setenv("GOOS", goos) os.Setenv("GOARCH", goarch) os.Setenv("CC", cc) @@ -432,10 +442,15 @@ func build(target target, tags []string) { defer shouldCleanupSyso(sysoPath) } - runPrint(goCmd, args...) + for _, pkg := range target.buildPkgs { + args := []string{"build", "-v"} + args = appendParameters(args, tags, pkg) + + runPrint(goCmd, args...) + } } -func appendParameters(args []string, tags []string, target target) []string { +func appendParameters(args []string, tags []string, pkg string) []string { if pkgdir != "" { args = append(args, "-pkgdir", pkgdir) } @@ -451,7 +466,7 @@ func appendParameters(args []string, tags []string, target target) []string { if !debugBinary { // Regular binaries get version tagged and skip some debug symbols - args = append(args, "-ldflags", ldflags()) + args = append(args, "-ldflags", ldflags(path.Base(pkg))) } else { // -gcflags to disable optimizations and inlining. Skip -ldflags // because `Could not launch program: decoding dwarf section info at @@ -460,7 +475,7 @@ func appendParameters(args []string, tags []string, target target) []string { args = append(args, "-gcflags", "-N -l") } - return append(args, target.buildPkg) + return append(args, pkg) } func buildTar(target target) { @@ -708,6 +723,7 @@ func listFiles(dir string) []string { if err != nil { return err } + if fi.Mode().IsRegular() { res = append(res, path) } @@ -789,7 +805,7 @@ func transifex() { runPrint(goCmd, "run", "../../../../script/transifexdl.go") } -func ldflags() string { +func ldflags(program string) string { sep := '=' if goVersion > 0 && goVersion < 1.5 { sep = ' ' @@ -801,8 +817,9 @@ func ldflags() string { fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Stamp%c%d", sep, buildStamp()) fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.User%c%s", sep, buildUser()) fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Host%c%s", sep, buildHost()) + fmt.Fprintf(b, " -X github.com/syncthing/syncthing/lib/build.Program%c%s", sep, program) if v := os.Getenv("EXTRA_LDFLAGS"); v != "" { - fmt.Fprintf(b, " %s", v); + fmt.Fprintf(b, " %s", v) } return b.String() } diff --git a/cmd/stcli/main.go b/cmd/stcli/main.go index 50daf79c9..6659c6a8f 100644 --- a/cmd/stcli/main.go +++ b/cmd/stcli/main.go @@ -14,7 +14,6 @@ import ( "log" "os" "reflect" - "strings" "github.com/AudriusButkevicius/recli" "github.com/flynn-archive/go-shlex" @@ -128,7 +127,7 @@ func main() { app.HelpName = app.Name app.Author = "The Syncthing Authors" app.Usage = "Syncthing command line interface" - app.Version = strings.Replace(build.LongVersion, "syncthing", app.Name, 1) + app.Version = build.Version app.Flags = fakeFlags app.Metadata = map[string]interface{}{ "client": client, diff --git a/cmd/stdiscosrv/main.go b/cmd/stdiscosrv/main.go index 8ef7ace17..ab7d1de9c 100644 --- a/cmd/stdiscosrv/main.go +++ b/cmd/stdiscosrv/main.go @@ -9,17 +9,15 @@ package main import ( "crypto/tls" "flag" - "fmt" "log" "net" "net/http" "os" - "runtime" - "strconv" "strings" "time" "github.com/prometheus/client_golang/prometheus/promhttp" + "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/tlsutil" "github.com/syndtr/goleveldb/leveldb/opt" @@ -65,24 +63,6 @@ var levelDBOptions = &opt.Options{ WriteBuffer: 32 << 20, // default 4<<20 } -var ( - Version string - BuildStamp string - BuildUser string - BuildHost string - - BuildDate time.Time - LongVersion string -) - -func init() { - stamp, _ := strconv.Atoi(BuildStamp) - BuildDate = time.Unix(int64(stamp), 0) - - date := BuildDate.UTC().Format("2006-01-02 15:04:05 MST") - LongVersion = fmt.Sprintf(`stdiscosrv %s (%s %s-%s) %s@%s %s`, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, BuildUser, BuildHost, date) -} - var ( debug = false ) @@ -109,9 +89,13 @@ func main() { flag.StringVar(&metricsListen, "metrics-listen", "", "Metrics listen address") flag.StringVar(&replicationPeers, "replicate", "", "Replication peers, id@address, comma separated") flag.StringVar(&replicationListen, "replication-listen", ":19200", "Replication listen address") + showVersion := flag.Bool("version", false, "Show version") flag.Parse() - log.Println(LongVersion) + log.Println(build.LongVersion) + if *showVersion { + return + } cert, err := tls.LoadX509KeyPair(certFile, keyFile) if err != nil { diff --git a/cmd/strelaysrv/main.go b/cmd/strelaysrv/main.go index c5b564698..09c72875d 100644 --- a/cmd/strelaysrv/main.go +++ b/cmd/strelaysrv/main.go @@ -14,12 +14,12 @@ import ( "os/signal" "path/filepath" "runtime" - "strconv" "strings" "sync/atomic" "syscall" "time" + "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/relay/protocol" @@ -34,24 +34,6 @@ import ( syncthingprotocol "github.com/syncthing/syncthing/lib/protocol" ) -var ( - Version string - BuildStamp string - BuildUser string - BuildHost string - - BuildDate time.Time - LongVersion string -) - -func init() { - stamp, _ := strconv.Atoi(BuildStamp) - BuildDate = time.Unix(int64(stamp), 0) - - date := BuildDate.UTC().Format("2006-01-02 15:04:05 MST") - LongVersion = fmt.Sprintf(`strelaysrv %s (%s %s-%s) %s@%s %s`, Version, runtime.Version(), runtime.GOOS, runtime.GOARCH, BuildUser, BuildHost, date) -} - var ( listen string debug bool @@ -117,8 +99,14 @@ func main() { flag.IntVar(&natTimeout, "nat-timeout", 10, "NAT discovery timeout in seconds") flag.BoolVar(&pprofEnabled, "pprof", false, "Enable the built in profiling on the status server") flag.IntVar(&networkBufferSize, "network-buffer", 2048, "Network buffer size (two of these per proxied connection)") + showVersion := flag.Bool("version", false, "Show version") flag.Parse() + if *showVersion { + fmt.Println(build.LongVersion) + return + } + if extAddress == "" { extAddress = listen } @@ -147,7 +135,7 @@ func main() { } } - log.Println(LongVersion) + log.Println(build.LongVersion) maxDescriptors, err := osutil.MaximizeOpenFileLimit() if maxDescriptors > 0 { diff --git a/cmd/strelaysrv/status.go b/cmd/strelaysrv/status.go index a9aaca8ae..8079af572 100644 --- a/cmd/strelaysrv/status.go +++ b/cmd/strelaysrv/status.go @@ -10,6 +10,8 @@ import ( "runtime" "sync/atomic" "time" + + "github.com/syncthing/syncthing/lib/build" ) var rc *rateCalculator @@ -40,10 +42,10 @@ func getStatus(w http.ResponseWriter, r *http.Request) { sessionMut.Lock() // This can potentially be double the number of pending sessions, as each session has two keys, one for each side. - status["version"] = Version - status["buildHost"] = BuildHost - status["buildUser"] = BuildUser - status["buildDate"] = BuildDate + status["version"] = build.Version + status["buildHost"] = build.Host + status["buildUser"] = build.User + status["buildDate"] = build.Date status["startTime"] = rc.startTime status["uptimeSeconds"] = time.Since(rc.startTime) / time.Second status["numPendingSessionKeys"] = len(pendingSessions) diff --git a/cmd/todos/main.go b/cmd/todos/main.go deleted file mode 100644 index 16bf090e6..000000000 --- a/cmd/todos/main.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2014 The Syncthing Authors. -// -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this file, -// You can obtain one at https://mozilla.org/MPL/2.0/. - -// +build ignore - -package main - -import ( - "bytes" - "fmt" - "io" - "os" -) - -func main() { - buf := make([]byte, 4096) - var err error - for err == nil { - n, err := io.ReadFull(os.Stdin, buf) - if n > 0 { - buf = buf[:n] - repl := bytes.Replace(buf, []byte("\n"), []byte("\r\n"), -1) - _, err = os.Stdout.Write(repl) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - } - if err == io.EOF { - return - } - buf = buf[:cap(buf)] - } - fmt.Println(err) - os.Exit(1) -} diff --git a/lib/build/build.go b/lib/build/build.go index e353f45e7..733fb9137 100644 --- a/lib/build/build.go +++ b/lib/build/build.go @@ -18,10 +18,11 @@ import ( var ( // Injected by build script + Program = "syncthing" Version = "unknown-dev" - Host = "unknown" // Set by build script - User = "unknown" // Set by build script - Stamp = "0" // Set by build script + Host = "unknown" + User = "unknown" + Stamp = "0" // Static Codename = "Fermium Flea" @@ -73,7 +74,7 @@ func setBuildData() { Date = time.Unix(int64(stamp), 0) date := Date.UTC().Format("2006-01-02 15:04:05 MST") - LongVersion = fmt.Sprintf(`syncthing %s "%s" (%s %s-%s) %s@%s %s`, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date) + LongVersion = fmt.Sprintf(`%s %s "%s" (%s %s-%s) %s@%s %s`, Program, Version, Codename, runtime.Version(), runtime.GOOS, runtime.GOARCH, User, Host, date) if len(Tags) > 0 { LongVersion = fmt.Sprintf("%s [%s]", LongVersion, strings.Join(Tags, ", "))