2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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 http://mozilla.org/MPL/2.0/.
|
2014-08-18 19:57:10 +00:00
|
|
|
|
|
|
|
// +build ignore
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-04-15 07:26:25 +00:00
|
|
|
"syscall"
|
2014-10-13 18:13:42 +00:00
|
|
|
"time"
|
2014-08-18 19:38:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-08-30 18:50:07 +00:00
|
|
|
versionRe = regexp.MustCompile(`-[0-9]{1,3}-g[0-9a-f]{5,10}`)
|
|
|
|
goarch string
|
|
|
|
goos string
|
|
|
|
noupgrade bool
|
|
|
|
version string
|
|
|
|
goVersion float64
|
|
|
|
race bool
|
2016-06-08 16:15:45 +00:00
|
|
|
debug = os.Getenv("BUILDDEBUG") != ""
|
2014-08-18 19:38:22 +00:00
|
|
|
)
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
type target struct {
|
2016-09-12 12:21:07 +00:00
|
|
|
name string
|
|
|
|
buildPkg string
|
|
|
|
binaryName string
|
|
|
|
archiveFiles []archiveFile
|
|
|
|
installationFiles []archiveFile
|
|
|
|
tags []string
|
2016-04-06 21:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type archiveFile struct {
|
|
|
|
src string
|
|
|
|
dst string
|
|
|
|
perm os.FileMode
|
|
|
|
}
|
|
|
|
|
|
|
|
var targets = map[string]target{
|
|
|
|
"all": {
|
|
|
|
// Only valid for the "build" and "install" commands as it lacks all
|
|
|
|
// the archive creation stuff.
|
|
|
|
buildPkg: "./cmd/...",
|
2016-06-02 14:32:23 +00:00
|
|
|
tags: []string{"purego"},
|
2016-04-06 21:18:30 +00:00
|
|
|
},
|
|
|
|
"syncthing": {
|
|
|
|
// The default target for "build", "install", "tar", "zip", "deb", etc.
|
|
|
|
name: "syncthing",
|
|
|
|
buildPkg: "./cmd/syncthing",
|
|
|
|
binaryName: "syncthing", // .exe will be added automatically for Windows builds
|
|
|
|
archiveFiles: []archiveFile{
|
|
|
|
{src: "{{binary}}", dst: "{{binary}}", perm: 0755},
|
|
|
|
{src: "README.md", dst: "README.txt", perm: 0644},
|
|
|
|
{src: "LICENSE", dst: "LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
|
|
|
|
// All files from etc/ and extra/ added automatically in init().
|
|
|
|
},
|
2016-09-12 12:21:07 +00:00
|
|
|
installationFiles: []archiveFile{
|
2016-04-06 21:18:30 +00:00
|
|
|
{src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
|
|
|
|
{src: "README.md", dst: "deb/usr/share/doc/syncthing/README.txt", perm: 0644},
|
|
|
|
{src: "LICENSE", dst: "deb/usr/share/doc/syncthing/LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "deb/usr/share/doc/syncthing/AUTHORS.txt", perm: 0644},
|
|
|
|
{src: "man/syncthing.1", dst: "deb/usr/share/man/man1/syncthing.1", perm: 0644},
|
|
|
|
{src: "man/syncthing-config.5", dst: "deb/usr/share/man/man5/syncthing-config.5", perm: 0644},
|
|
|
|
{src: "man/syncthing-stignore.5", dst: "deb/usr/share/man/man5/syncthing-stignore.5", perm: 0644},
|
|
|
|
{src: "man/syncthing-device-ids.7", dst: "deb/usr/share/man/man7/syncthing-device-ids.7", perm: 0644},
|
|
|
|
{src: "man/syncthing-event-api.7", dst: "deb/usr/share/man/man7/syncthing-event-api.7", perm: 0644},
|
|
|
|
{src: "man/syncthing-faq.7", dst: "deb/usr/share/man/man7/syncthing-faq.7", perm: 0644},
|
|
|
|
{src: "man/syncthing-networking.7", dst: "deb/usr/share/man/man7/syncthing-networking.7", perm: 0644},
|
|
|
|
{src: "man/syncthing-rest-api.7", dst: "deb/usr/share/man/man7/syncthing-rest-api.7", perm: 0644},
|
|
|
|
{src: "man/syncthing-security.7", dst: "deb/usr/share/man/man7/syncthing-security.7", perm: 0644},
|
|
|
|
{src: "man/syncthing-versioning.7", dst: "deb/usr/share/man/man7/syncthing-versioning.7", perm: 0644},
|
|
|
|
{src: "etc/linux-systemd/system/syncthing@.service", dst: "deb/lib/systemd/system/syncthing@.service", perm: 0644},
|
|
|
|
{src: "etc/linux-systemd/system/syncthing-resume.service", dst: "deb/lib/systemd/system/syncthing-resume.service", perm: 0644},
|
|
|
|
{src: "etc/linux-systemd/user/syncthing.service", dst: "deb/usr/lib/systemd/user/syncthing.service", perm: 0644},
|
|
|
|
},
|
|
|
|
},
|
2016-07-04 10:51:22 +00:00
|
|
|
"stdiscosrv": {
|
|
|
|
name: "stdiscosrv",
|
|
|
|
buildPkg: "./cmd/stdiscosrv",
|
|
|
|
binaryName: "stdiscosrv", // .exe will be added automatically for Windows builds
|
2016-05-31 19:57:04 +00:00
|
|
|
archiveFiles: []archiveFile{
|
|
|
|
{src: "{{binary}}", dst: "{{binary}}", perm: 0755},
|
2016-07-04 10:51:22 +00:00
|
|
|
{src: "cmd/stdiscosrv/README.md", dst: "README.txt", perm: 0644},
|
|
|
|
{src: "cmd/stdiscosrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
|
2016-05-31 19:57:04 +00:00
|
|
|
{src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
|
|
|
|
},
|
2016-09-12 12:21:07 +00:00
|
|
|
installationFiles: []archiveFile{
|
2016-05-31 19:57:04 +00:00
|
|
|
{src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
|
2016-07-04 10:51:22 +00:00
|
|
|
{src: "cmd/stdiscosrv/README.md", dst: "deb/usr/share/doc/stdiscosrv/README.txt", perm: 0644},
|
|
|
|
{src: "cmd/stdiscosrv/LICENSE", dst: "deb/usr/share/doc/stdiscosrv/LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "deb/usr/share/doc/stdiscosrv/AUTHORS.txt", perm: 0644},
|
2016-07-27 15:00:10 +00:00
|
|
|
{src: "man/stdiscosrv.1", dst: "deb/usr/share/man/man1/stdiscosrv.1", perm: 0644},
|
2016-05-31 19:57:04 +00:00
|
|
|
},
|
2016-06-02 12:22:40 +00:00
|
|
|
tags: []string{"purego"},
|
2016-05-31 19:57:04 +00:00
|
|
|
},
|
2016-07-04 10:51:22 +00:00
|
|
|
"strelaysrv": {
|
|
|
|
name: "strelaysrv",
|
|
|
|
buildPkg: "./cmd/strelaysrv",
|
|
|
|
binaryName: "strelaysrv", // .exe will be added automatically for Windows builds
|
2016-06-02 12:07:29 +00:00
|
|
|
archiveFiles: []archiveFile{
|
|
|
|
{src: "{{binary}}", dst: "{{binary}}", perm: 0755},
|
2016-07-04 10:51:22 +00:00
|
|
|
{src: "cmd/strelaysrv/README.md", dst: "README.txt", perm: 0644},
|
|
|
|
{src: "cmd/strelaysrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
|
2016-06-02 12:07:29 +00:00
|
|
|
{src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
|
|
|
|
},
|
2016-09-12 12:21:07 +00:00
|
|
|
installationFiles: []archiveFile{
|
2016-06-02 12:07:29 +00:00
|
|
|
{src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
|
2016-07-04 10:51:22 +00:00
|
|
|
{src: "cmd/strelaysrv/README.md", dst: "deb/usr/share/doc/strelaysrv/README.txt", perm: 0644},
|
|
|
|
{src: "cmd/strelaysrv/LICENSE", dst: "deb/usr/share/doc/strelaysrv/LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "deb/usr/share/doc/strelaysrv/AUTHORS.txt", perm: 0644},
|
2016-07-27 15:00:10 +00:00
|
|
|
{src: "man/strelaysrv.1", dst: "deb/usr/share/man/man1/strelaysrv.1", perm: 0644},
|
2016-06-02 12:07:29 +00:00
|
|
|
},
|
|
|
|
},
|
2016-07-04 09:08:11 +00:00
|
|
|
"strelaypoolsrv": {
|
|
|
|
name: "strelaypoolsrv",
|
|
|
|
buildPkg: "./cmd/strelaypoolsrv",
|
|
|
|
binaryName: "strelaypoolsrv", // .exe will be added automatically for Windows builds
|
|
|
|
archiveFiles: []archiveFile{
|
|
|
|
{src: "{{binary}}", dst: "{{binary}}", perm: 0755},
|
|
|
|
{src: "cmd/strelaypoolsrv/README.md", dst: "README.txt", perm: 0644},
|
|
|
|
{src: "cmd/strelaypoolsrv/LICENSE", dst: "LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "AUTHORS.txt", perm: 0644},
|
|
|
|
},
|
2016-09-12 12:21:07 +00:00
|
|
|
installationFiles: []archiveFile{
|
2016-07-04 09:08:11 +00:00
|
|
|
{src: "{{binary}}", dst: "deb/usr/bin/{{binary}}", perm: 0755},
|
|
|
|
{src: "cmd/strelaypoolsrv/README.md", dst: "deb/usr/share/doc/relaysrv/README.txt", perm: 0644},
|
|
|
|
{src: "cmd/strelaypoolsrv/LICENSE", dst: "deb/usr/share/doc/relaysrv/LICENSE.txt", perm: 0644},
|
|
|
|
{src: "AUTHORS", dst: "deb/usr/share/doc/relaysrv/AUTHORS.txt", perm: 0644},
|
|
|
|
},
|
|
|
|
},
|
2016-04-06 21:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// The "syncthing" target includes a few more files found in the "etc"
|
|
|
|
// and "extra" dirs.
|
|
|
|
syncthingPkg := targets["syncthing"]
|
|
|
|
for _, file := range listFiles("etc") {
|
|
|
|
syncthingPkg.archiveFiles = append(syncthingPkg.archiveFiles, archiveFile{src: file, dst: file, perm: 0644})
|
|
|
|
}
|
|
|
|
for _, file := range listFiles("extra") {
|
|
|
|
syncthingPkg.archiveFiles = append(syncthingPkg.archiveFiles, archiveFile{src: file, dst: file, perm: 0644})
|
|
|
|
}
|
|
|
|
for _, file := range listFiles("extra") {
|
2016-09-12 12:21:07 +00:00
|
|
|
syncthingPkg.installationFiles = append(syncthingPkg.installationFiles, archiveFile{src: file, dst: "deb/usr/share/doc/syncthing/" + filepath.Base(file), perm: 0644})
|
2016-04-06 21:18:30 +00:00
|
|
|
}
|
|
|
|
targets["syncthing"] = syncthingPkg
|
|
|
|
}
|
|
|
|
|
2016-08-15 06:37:32 +00:00
|
|
|
const minGoVersion = 1.5
|
2014-08-19 09:04:20 +00:00
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
func main() {
|
|
|
|
log.SetOutput(os.Stdout)
|
|
|
|
log.SetFlags(0)
|
|
|
|
|
2016-06-08 16:15:45 +00:00
|
|
|
if debug {
|
|
|
|
t0 := time.Now()
|
|
|
|
defer func() {
|
|
|
|
log.Println("... build completed in", time.Since(t0))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
if os.Getenv("GOPATH") == "" {
|
2016-05-19 21:01:23 +00:00
|
|
|
setGoPath()
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
2016-03-31 07:33:09 +00:00
|
|
|
|
|
|
|
// We use Go 1.5+ vendoring.
|
|
|
|
os.Setenv("GO15VENDOREXPERIMENT", "1")
|
|
|
|
|
|
|
|
// Set path to $GOPATH/bin:$PATH so that we can for sure find tools we
|
|
|
|
// might have installed during "build.go setup".
|
2014-08-18 19:38:22 +00:00
|
|
|
os.Setenv("PATH", fmt.Sprintf("%s%cbin%c%s", os.Getenv("GOPATH"), os.PathSeparator, os.PathListSeparator, os.Getenv("PATH")))
|
|
|
|
|
2016-05-19 21:47:53 +00:00
|
|
|
parseFlags()
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-06-07 07:12:10 +00:00
|
|
|
checkArchitecture()
|
2015-08-06 07:02:24 +00:00
|
|
|
goVersion, _ = checkRequiredGoVersion()
|
2014-08-19 09:04:20 +00:00
|
|
|
|
2016-06-07 07:12:10 +00:00
|
|
|
// Invoking build.go with no parameters at all builds everything (incrementally),
|
2016-04-06 21:18:30 +00:00
|
|
|
// which is what you want for maximum error checking during development.
|
2014-08-18 19:38:22 +00:00
|
|
|
if flag.NArg() == 0 {
|
2016-06-07 07:12:10 +00:00
|
|
|
runCommand("install", targets["all"])
|
|
|
|
runCommand("vet", target{})
|
|
|
|
runCommand("lint", target{})
|
|
|
|
} else {
|
|
|
|
// with any command given but not a target, the target is
|
|
|
|
// "syncthing". So "go run build.go install" is "go run build.go install
|
|
|
|
// syncthing" etc.
|
|
|
|
targetName := "syncthing"
|
|
|
|
if flag.NArg() > 1 {
|
|
|
|
targetName = flag.Arg(1)
|
|
|
|
}
|
|
|
|
target, ok := targets[targetName]
|
|
|
|
if !ok {
|
|
|
|
log.Fatalln("Unknown target", target)
|
2014-11-17 15:02:41 +00:00
|
|
|
}
|
2015-04-28 20:32:10 +00:00
|
|
|
|
2016-06-07 07:12:10 +00:00
|
|
|
runCommand(flag.Arg(0), target)
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
2016-06-07 07:12:10 +00:00
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-06-07 07:12:10 +00:00
|
|
|
func checkArchitecture() {
|
|
|
|
switch goarch {
|
|
|
|
case "386", "amd64", "arm", "arm64", "ppc64", "ppc64le":
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
log.Printf("Unknown goarch %q; proceed with caution!", goarch)
|
2016-04-06 21:18:30 +00:00
|
|
|
}
|
2016-06-07 07:12:10 +00:00
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-06-07 07:12:10 +00:00
|
|
|
func runCommand(cmd string, target target) {
|
2016-04-06 21:18:30 +00:00
|
|
|
switch cmd {
|
|
|
|
case "setup":
|
|
|
|
setup()
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "install":
|
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
}
|
|
|
|
install(target, tags)
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "build":
|
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
}
|
|
|
|
build(target, tags)
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "test":
|
|
|
|
test("./lib/...", "./cmd/...")
|
2015-05-23 13:08:17 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "bench":
|
|
|
|
bench("./lib/...", "./cmd/...")
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "assets":
|
|
|
|
rebuildAssets()
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
case "proto":
|
|
|
|
proto()
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "translate":
|
|
|
|
translate()
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "transifex":
|
|
|
|
transifex()
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "tar":
|
|
|
|
buildTar(target)
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "zip":
|
|
|
|
buildZip(target)
|
2015-05-11 16:39:53 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "deb":
|
|
|
|
buildDeb(target)
|
2014-08-18 19:38:22 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "clean":
|
|
|
|
clean()
|
2015-04-28 20:32:10 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "vet":
|
2016-05-23 12:23:55 +00:00
|
|
|
vet("build.go")
|
2016-04-15 07:26:25 +00:00
|
|
|
vet("cmd", "lib")
|
2015-04-28 20:32:10 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
case "lint":
|
2016-05-23 12:23:55 +00:00
|
|
|
lint(".")
|
2016-04-15 07:26:25 +00:00
|
|
|
lint("./cmd/...")
|
2016-04-06 21:18:30 +00:00
|
|
|
lint("./lib/...")
|
2016-06-08 16:15:45 +00:00
|
|
|
|
|
|
|
case "metalint":
|
2016-05-23 21:19:08 +00:00
|
|
|
if isGometalinterInstalled() {
|
|
|
|
dirs := []string{".", "./cmd/...", "./lib/..."}
|
2016-07-04 10:40:29 +00:00
|
|
|
ok := gometalinter("deadcode", dirs, "test/util.go")
|
|
|
|
ok = gometalinter("structcheck", dirs) && ok
|
|
|
|
ok = gometalinter("varcheck", dirs) && ok
|
|
|
|
if !ok {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-05-23 21:19:08 +00:00
|
|
|
}
|
2016-04-06 21:18:30 +00:00
|
|
|
|
2016-09-06 10:54:40 +00:00
|
|
|
case "version":
|
|
|
|
fmt.Println(getVersion())
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
default:
|
|
|
|
log.Fatalf("Unknown command %q", cmd)
|
2014-09-28 11:07:13 +00:00
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 21:01:23 +00:00
|
|
|
// setGoPath sets GOPATH correctly with the assumption that we are
|
|
|
|
// in $GOPATH/src/github.com/syncthing/syncthing.
|
|
|
|
func setGoPath() {
|
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
gopath := filepath.Clean(filepath.Join(cwd, "../../../../"))
|
|
|
|
log.Println("GOPATH is", gopath)
|
|
|
|
os.Setenv("GOPATH", gopath)
|
|
|
|
}
|
|
|
|
|
2016-05-19 21:47:53 +00:00
|
|
|
func parseFlags() {
|
|
|
|
flag.StringVar(&goarch, "goarch", runtime.GOARCH, "GOARCH")
|
|
|
|
flag.StringVar(&goos, "goos", runtime.GOOS, "GOOS")
|
|
|
|
flag.BoolVar(&noupgrade, "no-upgrade", noupgrade, "Disable upgrade functionality")
|
|
|
|
flag.StringVar(&version, "version", getVersion(), "Set compiled in version string")
|
|
|
|
flag.BoolVar(&race, "race", race, "Use race detector")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2015-08-06 07:02:24 +00:00
|
|
|
func checkRequiredGoVersion() (float64, bool) {
|
2016-03-31 07:33:09 +00:00
|
|
|
re := regexp.MustCompile(`go(\d+\.\d+)`)
|
|
|
|
ver := runtime.Version()
|
|
|
|
if m := re.FindStringSubmatch(ver); len(m) == 2 {
|
2014-08-19 09:04:20 +00:00
|
|
|
vs := string(m[1])
|
|
|
|
// This is a standard go build. Verify that it's new enough.
|
|
|
|
f, err := strconv.ParseFloat(vs, 64)
|
|
|
|
if err != nil {
|
2014-12-01 09:24:13 +00:00
|
|
|
log.Printf("*** Couldn't parse Go version out of %q.\n*** This isn't known to work, proceed on your own risk.", vs)
|
2015-08-06 07:02:24 +00:00
|
|
|
return 0, false
|
2014-08-19 09:04:20 +00:00
|
|
|
}
|
2016-03-31 07:33:09 +00:00
|
|
|
if f < 1.5 {
|
|
|
|
log.Printf("*** Go version %.01f doesn't support the vendoring mechanism.\n*** Ensure correct dependencies in your $GOPATH.", f)
|
|
|
|
} else if f < minGoVersion {
|
2014-08-19 09:04:20 +00:00
|
|
|
log.Fatalf("*** Go version %.01f is less than required %.01f.\n*** This is known not to work, not proceeding.", f, minGoVersion)
|
|
|
|
}
|
2015-08-06 07:02:24 +00:00
|
|
|
return f, true
|
2014-08-19 09:04:20 +00:00
|
|
|
}
|
2015-08-06 07:02:24 +00:00
|
|
|
|
|
|
|
log.Printf("*** Unknown Go version %q.\n*** This isn't known to work, proceed on your own risk.", ver)
|
|
|
|
return 0, false
|
2014-08-19 09:04:20 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
func setup() {
|
2016-08-07 19:58:19 +00:00
|
|
|
runPrint("go", "get", "-v", "github.com/golang/lint/golint")
|
2014-11-29 23:17:00 +00:00
|
|
|
runPrint("go", "get", "-v", "golang.org/x/tools/cmd/cover")
|
|
|
|
runPrint("go", "get", "-v", "golang.org/x/net/html")
|
2016-03-05 20:01:58 +00:00
|
|
|
runPrint("go", "get", "-v", "github.com/FiloSottile/gvt")
|
2015-08-12 21:04:19 +00:00
|
|
|
runPrint("go", "get", "-v", "github.com/axw/gocov/gocov")
|
|
|
|
runPrint("go", "get", "-v", "github.com/AlekSi/gocov-xml")
|
2016-01-09 23:31:55 +00:00
|
|
|
runPrint("go", "get", "-v", "bitbucket.org/tebeka/go2xunit")
|
2016-05-23 21:19:08 +00:00
|
|
|
runPrint("go", "get", "-v", "github.com/alecthomas/gometalinter")
|
2016-05-31 12:40:30 +00:00
|
|
|
runPrint("go", "get", "-v", "github.com/mitchellh/go-wordwrap")
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 20:01:58 +00:00
|
|
|
func test(pkgs ...string) {
|
2016-03-28 10:03:13 +00:00
|
|
|
lazyRebuildAssets()
|
|
|
|
|
2016-02-15 10:33:24 +00:00
|
|
|
useRace := runtime.GOARCH == "amd64"
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin", "linux", "freebsd", "windows":
|
|
|
|
default:
|
|
|
|
useRace = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if useRace {
|
2016-06-02 14:39:17 +00:00
|
|
|
runPrint("go", append([]string{"test", "-short", "-race", "-timeout", "60s", "-tags", "purego"}, pkgs...)...)
|
2016-02-15 10:33:24 +00:00
|
|
|
} else {
|
2016-06-02 14:39:17 +00:00
|
|
|
runPrint("go", append([]string{"test", "-short", "-timeout", "60s", "-tags", "purego"}, pkgs...)...)
|
2016-02-15 10:33:24 +00:00
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 20:01:58 +00:00
|
|
|
func bench(pkgs ...string) {
|
2016-03-28 10:03:13 +00:00
|
|
|
lazyRebuildAssets()
|
2016-03-05 20:01:58 +00:00
|
|
|
runPrint("go", append([]string{"test", "-run", "NONE", "-bench", "."}, pkgs...)...)
|
2015-05-23 13:08:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
func install(target target, tags []string) {
|
2016-03-28 10:03:13 +00:00
|
|
|
lazyRebuildAssets()
|
|
|
|
|
2016-06-02 12:22:40 +00:00
|
|
|
tags = append(target.tags, tags...)
|
|
|
|
|
2015-10-20 13:29:33 +00:00
|
|
|
cwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
os.Setenv("GOBIN", filepath.Join(cwd, "bin"))
|
2014-11-17 15:01:16 +00:00
|
|
|
args := []string{"install", "-v", "-ldflags", ldflags()}
|
|
|
|
if len(tags) > 0 {
|
2016-06-06 11:39:08 +00:00
|
|
|
args = append(args, "-tags", strings.Join(tags, " "))
|
2014-11-17 15:01:16 +00:00
|
|
|
}
|
2014-11-29 21:17:54 +00:00
|
|
|
if race {
|
|
|
|
args = append(args, "-race")
|
|
|
|
}
|
2016-04-06 21:18:30 +00:00
|
|
|
args = append(args, target.buildPkg)
|
2016-03-31 07:33:09 +00:00
|
|
|
|
|
|
|
os.Setenv("GOOS", goos)
|
|
|
|
os.Setenv("GOARCH", goarch)
|
2014-11-17 15:01:16 +00:00
|
|
|
runPrint("go", args...)
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
func build(target target, tags []string) {
|
2016-03-28 10:03:13 +00:00
|
|
|
lazyRebuildAssets()
|
|
|
|
|
2016-06-02 12:22:40 +00:00
|
|
|
tags = append(target.tags, tags...)
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
rmr(target.binaryName)
|
2015-12-08 08:14:52 +00:00
|
|
|
args := []string{"build", "-i", "-v", "-ldflags", ldflags()}
|
2014-08-18 19:38:22 +00:00
|
|
|
if len(tags) > 0 {
|
2016-06-06 11:39:08 +00:00
|
|
|
args = append(args, "-tags", strings.Join(tags, " "))
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
2014-11-29 21:17:54 +00:00
|
|
|
if race {
|
|
|
|
args = append(args, "-race")
|
|
|
|
}
|
2016-04-06 21:18:30 +00:00
|
|
|
args = append(args, target.buildPkg)
|
2016-03-31 07:33:09 +00:00
|
|
|
|
|
|
|
os.Setenv("GOOS", goos)
|
|
|
|
os.Setenv("GOARCH", goarch)
|
2014-09-28 11:07:13 +00:00
|
|
|
runPrint("go", args...)
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
func buildTar(target target) {
|
|
|
|
name := archiveName(target)
|
|
|
|
filename := name + ".tar.gz"
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
name += "-noupgrade"
|
|
|
|
}
|
2015-08-21 07:31:46 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
build(target, tags)
|
2015-01-07 15:15:50 +00:00
|
|
|
|
2016-01-16 18:43:50 +00:00
|
|
|
if goos == "darwin" {
|
2016-04-06 21:18:30 +00:00
|
|
|
macosCodesign(target.binaryName)
|
2016-01-16 18:43:50 +00:00
|
|
|
}
|
2016-04-06 21:18:30 +00:00
|
|
|
|
|
|
|
for i := range target.archiveFiles {
|
|
|
|
target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.binaryName, 1)
|
|
|
|
target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.binaryName, 1)
|
2016-04-08 10:53:29 +00:00
|
|
|
target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
|
2016-04-06 21:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tarGz(filename, target.archiveFiles)
|
2014-08-18 19:38:22 +00:00
|
|
|
log.Println(filename)
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
func buildZip(target target) {
|
|
|
|
target.binaryName += ".exe"
|
|
|
|
|
|
|
|
name := archiveName(target)
|
|
|
|
filename := name + ".zip"
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
var tags []string
|
|
|
|
if noupgrade {
|
|
|
|
tags = []string{"noupgrade"}
|
|
|
|
name += "-noupgrade"
|
|
|
|
}
|
2015-08-21 07:31:46 +00:00
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
build(target, tags)
|
|
|
|
|
|
|
|
for i := range target.archiveFiles {
|
|
|
|
target.archiveFiles[i].src = strings.Replace(target.archiveFiles[i].src, "{{binary}}", target.binaryName, 1)
|
|
|
|
target.archiveFiles[i].dst = strings.Replace(target.archiveFiles[i].dst, "{{binary}}", target.binaryName, 1)
|
2016-04-08 10:53:29 +00:00
|
|
|
target.archiveFiles[i].dst = name + "/" + target.archiveFiles[i].dst
|
2015-01-07 15:15:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
zipFile(filename, target.archiveFiles)
|
2014-08-18 19:38:22 +00:00
|
|
|
log.Println(filename)
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
func buildDeb(target target) {
|
2015-05-11 16:39:53 +00:00
|
|
|
os.RemoveAll("deb")
|
|
|
|
|
2015-05-23 11:05:47 +00:00
|
|
|
// "goarch" here is set to whatever the Debian packages expect. We correct
|
|
|
|
// "it to what we actually know how to build and keep the Debian variant
|
|
|
|
// "name in "debarch".
|
|
|
|
debarch := goarch
|
|
|
|
switch goarch {
|
|
|
|
case "i386":
|
|
|
|
goarch = "386"
|
|
|
|
case "armel", "armhf":
|
|
|
|
goarch = "arm"
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
build(target, []string{"noupgrade"})
|
2015-05-11 16:39:53 +00:00
|
|
|
|
2016-09-12 12:21:07 +00:00
|
|
|
for i := range target.installationFiles {
|
|
|
|
target.installationFiles[i].src = strings.Replace(target.installationFiles[i].src, "{{binary}}", target.binaryName, 1)
|
|
|
|
target.installationFiles[i].dst = strings.Replace(target.installationFiles[i].dst, "{{binary}}", target.binaryName, 1)
|
2015-05-11 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 12:21:07 +00:00
|
|
|
for _, af := range target.installationFiles {
|
2015-05-11 16:39:53 +00:00
|
|
|
if err := copyFile(af.src, af.dst, af.perm); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 12:21:07 +00:00
|
|
|
maintainer := "Syncthing Release Management <release@syncthing.net>"
|
|
|
|
debver := version
|
|
|
|
if strings.HasPrefix(debver, "v") {
|
|
|
|
debver = debver[1:]
|
2016-01-19 17:49:41 +00:00
|
|
|
}
|
2016-09-12 12:21:07 +00:00
|
|
|
runPrint("fpm", "-t", "deb", "-s", "dir", "-C", "deb",
|
|
|
|
"-n", "syncthing", "-v", debver, "-a", debarch,
|
|
|
|
"--vendor", maintainer, "-m", maintainer,
|
|
|
|
"-d", "libc6",
|
|
|
|
"-d", "procps", // because postinst script
|
|
|
|
"--url", "https://syncthing.net/",
|
|
|
|
"--description", "Open Source Continuous File Synchronization",
|
|
|
|
"--after-upgrade", "script/post-upgrade",
|
|
|
|
"--license", "MPL-2")
|
2015-05-11 16:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(src, dst string, perm os.FileMode) error {
|
|
|
|
dstDir := filepath.Dir(dst)
|
|
|
|
os.MkdirAll(dstDir, 0755) // ignore error
|
|
|
|
srcFd, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFd.Close()
|
|
|
|
dstFd, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFd.Close()
|
|
|
|
_, err = io.Copy(dstFd, srcFd)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-24 12:48:52 +00:00
|
|
|
func listFiles(dir string) []string {
|
|
|
|
var res []string
|
|
|
|
filepath.Walk(dir, func(path string, fi os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if fi.Mode().IsRegular() {
|
|
|
|
res = append(res, path)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2016-03-28 10:03:13 +00:00
|
|
|
func rebuildAssets() {
|
2015-08-12 21:04:19 +00:00
|
|
|
runPipe("lib/auto/gui.files.go", "go", "run", "script/genassets.go", "gui")
|
2016-07-04 09:22:56 +00:00
|
|
|
runPipe("cmd/strelaypoolsrv/auto/gui.go", "go", "run", "script/genassets.go", "cmd/strelaypoolsrv/gui")
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-28 10:03:13 +00:00
|
|
|
func lazyRebuildAssets() {
|
2016-07-04 09:22:56 +00:00
|
|
|
if shouldRebuildAssets("lib/auto/gui.files.go", "gui") || shouldRebuildAssets("cmd/strelaypoolsrv/auto/gui.go", "cmd/strelaypoolsrv/auto/gui") {
|
2016-03-28 10:03:13 +00:00
|
|
|
rebuildAssets()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 09:22:56 +00:00
|
|
|
func shouldRebuildAssets(target, srcdir string) bool {
|
|
|
|
info, err := os.Stat(target)
|
2016-03-28 10:03:13 +00:00
|
|
|
if err != nil {
|
|
|
|
// If the file doesn't exist, we must rebuild it
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of the files in gui/ are newer than the asset file. If
|
|
|
|
// so we should rebuild it.
|
|
|
|
currentBuild := info.ModTime()
|
|
|
|
assetsAreNewer := false
|
2016-07-04 09:22:56 +00:00
|
|
|
filepath.Walk(srcdir, func(path string, info os.FileInfo, err error) error {
|
2016-03-28 10:03:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if assetsAreNewer {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
assetsAreNewer = info.ModTime().After(currentBuild)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return assetsAreNewer
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func proto() {
|
|
|
|
runPrint("go", "generate", "./lib/...")
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func translate() {
|
2016-01-10 15:37:31 +00:00
|
|
|
os.Chdir("gui/default/assets/lang")
|
2016-01-13 09:56:42 +00:00
|
|
|
runPipe("lang-en-new.json", "go", "run", "../../../../script/translate.go", "lang-en.json", "../../../")
|
2014-08-18 19:38:22 +00:00
|
|
|
os.Remove("lang-en.json")
|
|
|
|
err := os.Rename("lang-en-new.json", "lang-en.json")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-01-13 09:56:42 +00:00
|
|
|
os.Chdir("../../../..")
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func transifex() {
|
2016-01-10 15:37:31 +00:00
|
|
|
os.Chdir("gui/default/assets/lang")
|
2016-01-13 09:56:42 +00:00
|
|
|
runPrint("go", "run", "../../../../script/transifexdl.go")
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func clean() {
|
2016-03-05 20:01:58 +00:00
|
|
|
rmr("bin")
|
2014-08-18 19:38:22 +00:00
|
|
|
rmr(filepath.Join(os.Getenv("GOPATH"), fmt.Sprintf("pkg/%s_%s/github.com/syncthing", goos, goarch)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func ldflags() string {
|
2016-03-05 21:25:28 +00:00
|
|
|
sep := '='
|
|
|
|
if goVersion > 0 && goVersion < 1.5 {
|
|
|
|
sep = ' '
|
2015-08-06 07:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
2014-08-18 19:38:22 +00:00
|
|
|
b.WriteString("-w")
|
2015-08-06 07:02:24 +00:00
|
|
|
fmt.Fprintf(b, " -X main.Version%c%s", sep, version)
|
|
|
|
fmt.Fprintf(b, " -X main.BuildStamp%c%d", sep, buildStamp())
|
|
|
|
fmt.Fprintf(b, " -X main.BuildUser%c%s", sep, buildUser())
|
|
|
|
fmt.Fprintf(b, " -X main.BuildHost%c%s", sep, buildHost())
|
2014-08-18 19:38:22 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func rmr(paths ...string) {
|
|
|
|
for _, path := range paths {
|
2016-08-14 20:48:11 +00:00
|
|
|
if debug {
|
|
|
|
log.Println("rm -r", path)
|
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 10:17:18 +00:00
|
|
|
func getReleaseVersion() (string, error) {
|
|
|
|
fd, err := os.Open("RELEASE")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
bs, err := ioutil.ReadAll(fd)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(bytes.TrimSpace(bs)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getGitVersion() (string, error) {
|
2014-10-15 16:15:21 +00:00
|
|
|
v, err := runError("git", "describe", "--always", "--dirty")
|
2014-10-13 18:13:42 +00:00
|
|
|
if err != nil {
|
2015-03-12 10:17:18 +00:00
|
|
|
return "", err
|
2014-10-13 18:13:42 +00:00
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
v = versionRe.ReplaceAllFunc(v, func(s []byte) []byte {
|
|
|
|
s[0] = '+'
|
|
|
|
return s
|
|
|
|
})
|
2015-03-12 10:17:18 +00:00
|
|
|
return string(v), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getVersion() string {
|
|
|
|
// First try for a RELEASE file,
|
|
|
|
if ver, err := getReleaseVersion(); err == nil {
|
|
|
|
return ver
|
|
|
|
}
|
2015-03-13 09:10:13 +00:00
|
|
|
// ... then see if we have a Git tag.
|
2015-03-12 10:17:18 +00:00
|
|
|
if ver, err := getGitVersion(); err == nil {
|
2016-03-10 09:24:11 +00:00
|
|
|
if strings.Contains(ver, "-") {
|
|
|
|
// The version already contains a hash and stuff. See if we can
|
|
|
|
// find a current branch name to tack onto it as well.
|
|
|
|
return ver + getBranchSuffix()
|
|
|
|
}
|
2015-03-12 10:17:18 +00:00
|
|
|
return ver
|
|
|
|
}
|
|
|
|
// This seems to be a dev build.
|
|
|
|
return "unknown-dev"
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:24:11 +00:00
|
|
|
func getBranchSuffix() string {
|
|
|
|
bs, err := runError("git", "branch", "-a", "--contains")
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:37:13 +00:00
|
|
|
branches := strings.Split(string(bs), "\n")
|
2016-03-10 09:24:11 +00:00
|
|
|
if len(branches) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:37:13 +00:00
|
|
|
branch := ""
|
|
|
|
for i, candidate := range branches {
|
|
|
|
if strings.HasPrefix(candidate, "*") {
|
|
|
|
// This is the current branch. Select it!
|
|
|
|
branch = strings.TrimLeft(candidate, " \t*")
|
|
|
|
break
|
|
|
|
} else if i == 0 {
|
|
|
|
// Otherwise the first branch in the list will do.
|
|
|
|
branch = strings.TrimSpace(branch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if branch == "" {
|
|
|
|
return ""
|
|
|
|
}
|
2016-03-10 09:24:11 +00:00
|
|
|
|
|
|
|
// The branch name may be on the form "remotes/origin/foo" from which we
|
|
|
|
// just want "foo".
|
2016-03-10 09:37:13 +00:00
|
|
|
parts := strings.Split(branch, "/")
|
2016-03-10 09:24:11 +00:00
|
|
|
if len(parts) == 0 || len(parts[len(parts)-1]) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2016-03-10 09:37:13 +00:00
|
|
|
|
2016-03-10 09:47:15 +00:00
|
|
|
branch = parts[len(parts)-1]
|
2016-03-10 09:37:13 +00:00
|
|
|
if branch == "master" {
|
|
|
|
// master builds are the default.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:49:11 +00:00
|
|
|
validBranchRe := regexp.MustCompile(`^[a-zA-Z0-9_.-]+$`)
|
2016-03-10 09:47:15 +00:00
|
|
|
if !validBranchRe.MatchString(branch) {
|
|
|
|
// There's some odd stuff in the branch name. Better skip it.
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-03-10 09:37:13 +00:00
|
|
|
return "-" + branch
|
2016-03-10 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
func buildStamp() int64 {
|
2016-06-29 18:52:49 +00:00
|
|
|
// If SOURCE_DATE_EPOCH is set, use that.
|
|
|
|
if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to get the timestamp of the latest commit.
|
2014-10-15 16:15:21 +00:00
|
|
|
bs, err := runError("git", "show", "-s", "--format=%ct")
|
2014-10-13 18:13:42 +00:00
|
|
|
if err != nil {
|
2016-06-29 18:52:49 +00:00
|
|
|
// Fall back to "now".
|
2014-10-13 18:13:42 +00:00
|
|
|
return time.Now().Unix()
|
|
|
|
}
|
2016-06-29 18:52:49 +00:00
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
s, _ := strconv.ParseInt(string(bs), 10, 64)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildUser() string {
|
2016-07-27 21:27:45 +00:00
|
|
|
if v := os.Getenv("BUILD_USER"); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
u, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return "unknown-user"
|
|
|
|
}
|
|
|
|
return strings.Replace(u.Username, " ", "-", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildHost() string {
|
2016-07-27 21:27:45 +00:00
|
|
|
if v := os.Getenv("BUILD_HOST"); v != "" {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
h, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
return "unknown-host"
|
|
|
|
}
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildArch() string {
|
|
|
|
os := goos
|
|
|
|
if os == "darwin" {
|
|
|
|
os = "macosx"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s-%s", os, goarch)
|
|
|
|
}
|
|
|
|
|
2016-04-06 21:18:30 +00:00
|
|
|
func archiveName(target target) string {
|
|
|
|
return fmt.Sprintf("%s-%s-%s", target.name, buildArch(), version)
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
2014-10-15 16:15:21 +00:00
|
|
|
func runError(cmd string, args ...string) ([]byte, error) {
|
2016-06-08 16:15:45 +00:00
|
|
|
if debug {
|
|
|
|
t0 := time.Now()
|
|
|
|
log.Println("runError:", cmd, strings.Join(args, " "))
|
|
|
|
defer func() {
|
|
|
|
log.Println("... in", time.Since(t0))
|
|
|
|
}()
|
|
|
|
}
|
2014-10-15 16:15:21 +00:00
|
|
|
ecmd := exec.Command(cmd, args...)
|
|
|
|
bs, err := ecmd.CombinedOutput()
|
2015-04-28 20:32:10 +00:00
|
|
|
return bytes.TrimSpace(bs), err
|
2014-10-15 16:15:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 19:38:22 +00:00
|
|
|
func runPrint(cmd string, args ...string) {
|
2016-06-08 16:15:45 +00:00
|
|
|
if debug {
|
|
|
|
t0 := time.Now()
|
|
|
|
log.Println("runPrint:", cmd, strings.Join(args, " "))
|
|
|
|
defer func() {
|
|
|
|
log.Println("... in", time.Since(t0))
|
|
|
|
}()
|
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
ecmd := exec.Command(cmd, args...)
|
|
|
|
ecmd.Stdout = os.Stdout
|
|
|
|
ecmd.Stderr = os.Stderr
|
|
|
|
err := ecmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runPipe(file, cmd string, args ...string) {
|
2016-06-08 16:15:45 +00:00
|
|
|
if debug {
|
|
|
|
t0 := time.Now()
|
|
|
|
log.Println("runPipe:", cmd, strings.Join(args, " "))
|
|
|
|
defer func() {
|
|
|
|
log.Println("... in", time.Since(t0))
|
|
|
|
}()
|
|
|
|
}
|
2014-08-18 19:38:22 +00:00
|
|
|
fd, err := os.Create(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
ecmd := exec.Command(cmd, args...)
|
|
|
|
ecmd.Stdout = fd
|
|
|
|
ecmd.Stderr = os.Stderr
|
|
|
|
err = ecmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-08-23 20:19:29 +00:00
|
|
|
fd.Close()
|
2014-08-18 19:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func tarGz(out string, files []archiveFile) {
|
|
|
|
fd, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
gw := gzip.NewWriter(fd)
|
|
|
|
tw := tar.NewWriter(gw)
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
sf, err := os.Open(f.src)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := sf.Stat()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
h := &tar.Header{
|
|
|
|
Name: f.dst,
|
|
|
|
Size: info.Size(),
|
|
|
|
Mode: int64(info.Mode()),
|
|
|
|
ModTime: info.ModTime(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tw.WriteHeader(h)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(tw, sf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
sf.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
err = tw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = gw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func zipFile(out string, files []archiveFile) {
|
|
|
|
fd, err := os.Create(out)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
zw := zip.NewWriter(fd)
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
sf, err := os.Open(f.src)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := sf.Stat()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fh, err := zip.FileInfoHeader(info)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-06-20 09:49:19 +00:00
|
|
|
fh.Name = filepath.ToSlash(f.dst)
|
2014-08-18 19:38:22 +00:00
|
|
|
fh.Method = zip.Deflate
|
|
|
|
|
|
|
|
if strings.HasSuffix(f.dst, ".txt") {
|
|
|
|
// Text file. Read it and convert line endings.
|
|
|
|
bs, err := ioutil.ReadAll(sf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
bs = bytes.Replace(bs, []byte{'\n'}, []byte{'\n', '\r'}, -1)
|
|
|
|
fh.UncompressedSize = uint32(len(bs))
|
|
|
|
fh.UncompressedSize64 = uint64(len(bs))
|
|
|
|
|
|
|
|
of, err := zw.CreateHeader(fh)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
of.Write(bs)
|
|
|
|
} else {
|
|
|
|
// Binary file. Copy verbatim.
|
|
|
|
of, err := zw.CreateHeader(fh)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = io.Copy(of, sf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = zw.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 09:52:09 +00:00
|
|
|
|
2016-04-15 07:26:25 +00:00
|
|
|
func vet(dirs ...string) {
|
|
|
|
params := []string{"tool", "vet", "-all"}
|
|
|
|
params = append(params, dirs...)
|
|
|
|
bs, err := runError("go", params...)
|
|
|
|
|
|
|
|
if len(bs) > 0 {
|
|
|
|
log.Printf("%s", bs)
|
2015-04-28 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-15 07:26:25 +00:00
|
|
|
if err != nil {
|
|
|
|
if exitStatus(err) == 3 {
|
|
|
|
// Exit code 3, the "vet" tool is not installed
|
|
|
|
return
|
2015-08-06 07:02:24 +00:00
|
|
|
}
|
2016-04-15 07:26:25 +00:00
|
|
|
|
|
|
|
// A genuine error exit from the vet tool.
|
|
|
|
log.Fatal(err)
|
2015-04-28 20:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func lint(pkg string) {
|
|
|
|
bs, err := runError("golint", pkg)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(`- No golint, not linting. Try "go get -u github.com/golang/lint/golint".`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
analCommentPolicy := regexp.MustCompile(`exported (function|method|const|type|var) [^\s]+ should have comment`)
|
2016-07-04 10:40:29 +00:00
|
|
|
for _, line := range strings.Split(string(bs), "\n") {
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if analCommentPolicy.MatchString(line) {
|
2015-04-28 20:32:10 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
if strings.Contains(line, ".pb.go:") {
|
|
|
|
continue
|
2015-08-06 07:02:24 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
log.Println(line)
|
2015-04-28 20:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-16 18:43:50 +00:00
|
|
|
|
|
|
|
func macosCodesign(file string) {
|
|
|
|
if pass := os.Getenv("CODESIGN_KEYCHAIN_PASS"); pass != "" {
|
|
|
|
bs, err := runError("security", "unlock-keychain", "-p", pass)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Codesign: unlocking keychain failed:", string(bs))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if id := os.Getenv("CODESIGN_IDENTITY"); id != "" {
|
|
|
|
bs, err := runError("codesign", "-s", id, file)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Codesign: signing failed:", string(bs))
|
|
|
|
return
|
|
|
|
}
|
2016-01-16 18:59:01 +00:00
|
|
|
log.Println("Codesign: successfully signed", file)
|
2016-01-16 18:43:50 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 07:26:25 +00:00
|
|
|
|
|
|
|
func exitStatus(err error) int {
|
|
|
|
if err, ok := err.(*exec.ExitError); ok {
|
|
|
|
if ws, ok := err.ProcessState.Sys().(syscall.WaitStatus); ok {
|
|
|
|
return ws.ExitStatus()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1
|
|
|
|
}
|
2016-05-23 21:19:08 +00:00
|
|
|
|
|
|
|
func isGometalinterInstalled() bool {
|
|
|
|
if _, err := runError("gometalinter", "--disable-all"); err != nil {
|
|
|
|
log.Println("gometalinter is not installed")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func gometalinter(linter string, dirs []string, excludes ...string) bool {
|
2016-05-23 21:19:08 +00:00
|
|
|
params := []string{"--disable-all"}
|
|
|
|
params = append(params, fmt.Sprintf("--deadline=%ds", 60))
|
|
|
|
params = append(params, "--enable="+linter)
|
|
|
|
|
|
|
|
for _, exclude := range excludes {
|
|
|
|
params = append(params, "--exclude="+exclude)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dir := range dirs {
|
|
|
|
params = append(params, dir)
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
bs, _ := runError("gometalinter", params...)
|
2016-05-23 21:19:08 +00:00
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
nerr := 0
|
|
|
|
for _, line := range strings.Split(string(bs), "\n") {
|
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(line, ".pb.go:") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Println(line)
|
|
|
|
nerr++
|
2016-05-23 21:19:08 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
return nerr == 0
|
2016-05-23 21:19:08 +00:00
|
|
|
}
|