2018-08-30 18:53:42 +00:00
|
|
|
// Description
|
|
|
|
//
|
|
|
|
// This program aims to make building Go programs for end users easier by just
|
|
|
|
// calling it with `go run`, without having to setup a GOPATH.
|
|
|
|
//
|
|
|
|
// For Go < 1.11, it'll create a new GOPATH in a temporary directory, then run
|
|
|
|
// `go build` on the package configured as Main in the Config struct.
|
|
|
|
//
|
|
|
|
// For Go >= 1.11 if the file go.mod is present, it'll use Go modules and not
|
|
|
|
// setup a GOPATH. It builds the package configured as Main in the Config
|
|
|
|
// struct with `go build -mod=vendor` to use the vendored dependencies.
|
|
|
|
// The variable GOPROXY is set to `off` so that no network calls are made. All
|
|
|
|
// files are copied to a temporary directory before `go build` is called within
|
|
|
|
// that directory.
|
|
|
|
|
2018-02-11 10:34:03 +00:00
|
|
|
// BSD 2-Clause License
|
|
|
|
//
|
|
|
|
// Copyright (c) 2016-2018, Alexander Neumann <alexander@bumpern.de>
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This file has been copied from the repository at:
|
|
|
|
// https://github.com/fd0/build-go
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
// list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// +build ignore_build_go
|
2015-06-24 15:59:31 +00:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-08-14 13:08:07 +00:00
|
|
|
"runtime"
|
2018-01-12 21:06:42 +00:00
|
|
|
"strconv"
|
2015-06-24 15:59:31 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-02-11 10:34:03 +00:00
|
|
|
// config contains the configuration for the program to build.
|
|
|
|
var config = Config{
|
2018-12-20 15:39:48 +00:00
|
|
|
Name: "restic", // name of the program executable and directory
|
|
|
|
Namespace: "github.com/restic/restic", // subdir of GOPATH, e.g. "github.com/foo/bar"
|
|
|
|
Main: "./cmd/restic", // package name for the main package
|
|
|
|
DefaultBuildTags: []string{"selfupdate"}, // specify build tags which are always used
|
|
|
|
Tests: []string{"./..."}, // tests to run
|
2020-02-26 19:29:36 +00:00
|
|
|
MinVersion: GoVersion{Major: 1, Minor: 11, Patch: 0}, // minimum Go version supported
|
2016-12-03 10:14:30 +00:00
|
|
|
}
|
|
|
|
|
2018-02-11 10:34:03 +00:00
|
|
|
// Config configures the build.
|
|
|
|
type Config struct {
|
2018-08-12 15:10:04 +00:00
|
|
|
Name string
|
|
|
|
Namespace string
|
|
|
|
Main string
|
|
|
|
DefaultBuildTags []string
|
|
|
|
Tests []string
|
|
|
|
MinVersion GoVersion
|
2018-02-11 10:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
verbose bool
|
|
|
|
keepGopath bool
|
|
|
|
runTests bool
|
|
|
|
enableCGO bool
|
2018-05-11 07:50:10 +00:00
|
|
|
enablePIE bool
|
2018-08-30 18:53:42 +00:00
|
|
|
goVersion = ParseGoVersion(runtime.Version())
|
2018-02-11 10:34:03 +00:00
|
|
|
)
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
// copy all Go files in src to dst, creating directories on the fly, so calling
|
2015-06-24 15:59:31 +00:00
|
|
|
//
|
2018-08-30 18:53:42 +00:00
|
|
|
// copy("/tmp/gopath/src/github.com/restic/restic", "/home/u/restic")
|
2015-06-24 15:59:31 +00:00
|
|
|
//
|
|
|
|
// with "/home/u/restic" containing the file "foo.go" yields the following tree
|
|
|
|
// at "/tmp/gopath":
|
|
|
|
//
|
|
|
|
// /tmp/gopath
|
|
|
|
// └── src
|
|
|
|
// └── github.com
|
|
|
|
// └── restic
|
|
|
|
// └── restic
|
|
|
|
// └── foo.go
|
2018-08-30 18:53:42 +00:00
|
|
|
func copy(dst, src string) error {
|
|
|
|
verbosePrintf("copy contents of %v to %v\n", src, dst)
|
2015-06-24 15:59:31 +00:00
|
|
|
return filepath.Walk(src, func(name string, fi os.FileInfo, err error) error {
|
2017-07-21 19:51:01 +00:00
|
|
|
if name == src {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
if name == ".git" {
|
|
|
|
return filepath.SkipDir
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-21 19:51:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:59:31 +00:00
|
|
|
if fi.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
intermediatePath, err := filepath.Rel(src, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileSrc := filepath.Join(src, intermediatePath)
|
2018-08-30 18:53:42 +00:00
|
|
|
fileDst := filepath.Join(dst, intermediatePath)
|
2015-06-24 15:59:31 +00:00
|
|
|
|
|
|
|
return copyFile(fileDst, fileSrc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-12-03 10:14:30 +00:00
|
|
|
func directoryExists(dirname string) bool {
|
|
|
|
stat, err := os.Stat(dirname)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat.IsDir()
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
func fileExists(filename string) bool {
|
|
|
|
stat, err := os.Stat(filename)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat.Mode().IsRegular()
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:59:31 +00:00
|
|
|
// copyFile creates dst from src, preserving file attributes and timestamps.
|
|
|
|
func copyFile(dst, src string) error {
|
|
|
|
fi, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fsrc, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
|
|
|
|
fmt.Printf("MkdirAll(%v)\n", filepath.Dir(dst))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fdst, err := os.Create(dst)
|
|
|
|
if err != nil {
|
2018-08-30 18:53:42 +00:00
|
|
|
_ = fsrc.Close()
|
2015-06-24 15:59:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
_, err = io.Copy(fdst, fsrc)
|
|
|
|
if err != nil {
|
|
|
|
_ = fsrc.Close()
|
|
|
|
_ = fdst.Close()
|
2018-01-12 21:06:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
err = fdst.Close()
|
|
|
|
if err != nil {
|
|
|
|
_ = fsrc.Close()
|
|
|
|
return err
|
2018-01-12 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
err = fsrc.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
2018-01-12 21:06:42 +00:00
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
err = os.Chmod(dst, fi.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
return os.Chtimes(dst, fi.ModTime(), fi.ModTime())
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// die prints the message with fmt.Fprintf() to stderr and exits with an error
|
|
|
|
// code.
|
|
|
|
func die(message string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, message, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func showUsage(output io.Writer) {
|
|
|
|
fmt.Fprintf(output, "USAGE: go run build.go OPTIONS\n")
|
|
|
|
fmt.Fprintf(output, "\n")
|
|
|
|
fmt.Fprintf(output, "OPTIONS:\n")
|
2016-07-15 21:42:41 +00:00
|
|
|
fmt.Fprintf(output, " -v --verbose output more messages\n")
|
|
|
|
fmt.Fprintf(output, " -t --tags specify additional build tags\n")
|
2018-08-30 18:53:42 +00:00
|
|
|
fmt.Fprintf(output, " -k --keep-tempdir do not remove the temporary directory after build\n")
|
2016-07-15 21:42:41 +00:00
|
|
|
fmt.Fprintf(output, " -T --test run tests\n")
|
2017-01-12 18:51:08 +00:00
|
|
|
fmt.Fprintf(output, " -o --output set output file name\n")
|
2017-05-25 09:03:29 +00:00
|
|
|
fmt.Fprintf(output, " --enable-cgo use CGO to link against libc\n")
|
2018-05-11 07:50:10 +00:00
|
|
|
fmt.Fprintf(output, " --enable-pie use PIE buildmode\n")
|
2016-07-15 21:42:41 +00:00
|
|
|
fmt.Fprintf(output, " --goos value set GOOS for cross-compilation\n")
|
|
|
|
fmt.Fprintf(output, " --goarch value set GOARCH for cross-compilation\n")
|
2018-02-28 20:19:28 +00:00
|
|
|
fmt.Fprintf(output, " --goarm value set GOARM for cross-compilation\n")
|
2018-05-21 18:31:19 +00:00
|
|
|
fmt.Fprintf(output, " --tempdir dir use a specific directory for compilation\n")
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func verbosePrintf(message string, args ...interface{}) {
|
|
|
|
if !verbose {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:03:26 +00:00
|
|
|
fmt.Printf("build: "+message, args...)
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 13:44:13 +00:00
|
|
|
// cleanEnv returns a clean environment with GOPATH, GOBIN and GO111MODULE
|
|
|
|
// removed (if present).
|
2015-06-24 15:59:31 +00:00
|
|
|
func cleanEnv() (env []string) {
|
2018-08-12 13:44:13 +00:00
|
|
|
removeKeys := map[string]struct{}{
|
|
|
|
"GOPATH": struct{}{},
|
|
|
|
"GOBIN": struct{}{},
|
|
|
|
"GO111MODULE": struct{}{},
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:59:31 +00:00
|
|
|
for _, v := range os.Environ() {
|
2018-08-12 13:44:13 +00:00
|
|
|
data := strings.SplitN(v, "=", 2)
|
|
|
|
name := data[0]
|
|
|
|
|
|
|
|
if _, ok := removeKeys[name]; ok {
|
2015-06-24 15:59:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
env = append(env, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return env
|
|
|
|
}
|
|
|
|
|
|
|
|
// build runs "go build args..." with GOPATH set to gopath.
|
2018-08-30 18:53:42 +00:00
|
|
|
func build(cwd string, env map[string]string, args ...string) error {
|
2017-05-29 21:27:25 +00:00
|
|
|
a := []string{"build"}
|
2018-02-28 20:19:28 +00:00
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
if goVersion.AtLeast(GoVersion{1, 10, 0}) {
|
2018-02-28 20:19:28 +00:00
|
|
|
verbosePrintf("Go version is at least 1.10, using new syntax for -gcflags\n")
|
|
|
|
// use new prefix
|
2018-08-30 18:53:42 +00:00
|
|
|
a = append(a, "-asmflags", fmt.Sprintf("all=-trimpath=%s", cwd))
|
|
|
|
a = append(a, "-gcflags", fmt.Sprintf("all=-trimpath=%s", cwd))
|
2018-02-28 20:19:28 +00:00
|
|
|
} else {
|
2018-08-30 18:53:42 +00:00
|
|
|
a = append(a, "-asmflags", fmt.Sprintf("-trimpath=%s", cwd))
|
|
|
|
a = append(a, "-gcflags", fmt.Sprintf("-trimpath=%s", cwd))
|
2018-02-28 20:19:28 +00:00
|
|
|
}
|
2018-05-11 07:50:10 +00:00
|
|
|
if enablePIE {
|
|
|
|
a = append(a, "-buildmode=pie")
|
|
|
|
}
|
2018-02-28 20:19:28 +00:00
|
|
|
|
2017-05-29 21:27:25 +00:00
|
|
|
a = append(a, args...)
|
|
|
|
cmd := exec.Command("go", a...)
|
2018-08-30 18:53:42 +00:00
|
|
|
cmd.Env = append(cleanEnv(), "GOPROXY=off")
|
|
|
|
for k, v := range env {
|
|
|
|
cmd.Env = append(cmd.Env, k+"="+v)
|
2018-02-04 14:06:13 +00:00
|
|
|
}
|
2017-05-25 09:03:29 +00:00
|
|
|
if !enableCGO {
|
|
|
|
cmd.Env = append(cmd.Env, "CGO_ENABLED=0")
|
|
|
|
}
|
|
|
|
|
2016-02-14 16:39:51 +00:00
|
|
|
cmd.Dir = cwd
|
2015-06-24 15:59:31 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2018-08-30 18:53:42 +00:00
|
|
|
|
|
|
|
verbosePrintf("chdir %q\n", cwd)
|
|
|
|
verbosePrintf("go %q\n", a)
|
2015-06-24 15:59:31 +00:00
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2015-08-19 18:11:48 +00:00
|
|
|
// test runs "go test args..." with GOPATH set to gopath.
|
2018-08-30 18:53:42 +00:00
|
|
|
func test(cwd string, env map[string]string, args ...string) error {
|
|
|
|
args = append([]string{"test", "-count", "1"}, args...)
|
2015-08-19 18:11:48 +00:00
|
|
|
cmd := exec.Command("go", args...)
|
2018-08-30 18:53:42 +00:00
|
|
|
cmd.Env = append(cleanEnv(), "GOPROXY=off")
|
|
|
|
for k, v := range env {
|
|
|
|
cmd.Env = append(cmd.Env, k+"="+v)
|
|
|
|
}
|
|
|
|
if !enableCGO {
|
|
|
|
cmd.Env = append(cmd.Env, "CGO_ENABLED=0")
|
|
|
|
}
|
2016-02-14 16:39:51 +00:00
|
|
|
cmd.Dir = cwd
|
2015-08-19 18:11:48 +00:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2018-08-30 18:53:42 +00:00
|
|
|
|
|
|
|
verbosePrintf("chdir %q\n", cwd)
|
|
|
|
verbosePrintf("go %q\n", args)
|
2015-08-19 18:11:48 +00:00
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:13:00 +00:00
|
|
|
// getVersion returns the version string from the file VERSION in the current
|
|
|
|
// directory.
|
|
|
|
func getVersionFromFile() string {
|
2015-08-25 20:20:53 +00:00
|
|
|
buf, err := ioutil.ReadFile("VERSION")
|
|
|
|
if err != nil {
|
|
|
|
verbosePrintf("error reading file VERSION: %v\n", err)
|
2015-08-26 18:13:00 +00:00
|
|
|
return ""
|
2015-08-25 20:20:53 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 18:13:00 +00:00
|
|
|
return strings.TrimSpace(string(buf))
|
|
|
|
}
|
2015-08-25 20:20:53 +00:00
|
|
|
|
2015-08-26 18:13:00 +00:00
|
|
|
// getVersion returns a version string which is a combination of the contents
|
|
|
|
// of the file VERSION in the current directory and the version from git (if
|
|
|
|
// available).
|
2015-06-24 15:59:31 +00:00
|
|
|
func getVersion() string {
|
2015-08-26 18:13:00 +00:00
|
|
|
versionFile := getVersionFromFile()
|
|
|
|
versionGit := getVersionFromGit()
|
|
|
|
|
|
|
|
verbosePrintf("version from file 'VERSION' is %q, version from git %q\n",
|
|
|
|
versionFile, versionGit)
|
2015-08-25 20:20:53 +00:00
|
|
|
|
2015-08-26 18:13:00 +00:00
|
|
|
switch {
|
|
|
|
case versionFile == "":
|
|
|
|
return versionGit
|
|
|
|
case versionGit == "":
|
|
|
|
return versionFile
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 18:13:00 +00:00
|
|
|
return fmt.Sprintf("%s (%s)", versionFile, versionGit)
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 18:13:00 +00:00
|
|
|
// getVersionFromGit returns a version string that identifies the currently
|
|
|
|
// checked out git commit.
|
|
|
|
func getVersionFromGit() string {
|
2015-06-24 15:59:31 +00:00
|
|
|
cmd := exec.Command("git", "describe",
|
|
|
|
"--long", "--tags", "--dirty", "--always")
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
2015-08-19 04:31:14 +00:00
|
|
|
verbosePrintf("git describe returned error: %v\n", err)
|
|
|
|
return ""
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
version := strings.TrimSpace(string(out))
|
|
|
|
verbosePrintf("git version is %s\n", version)
|
|
|
|
return version
|
|
|
|
}
|
|
|
|
|
2015-08-26 18:03:16 +00:00
|
|
|
// Constants represents a set of constants that are set in the final binary to
|
|
|
|
// the given value via compiler flags.
|
2015-08-25 20:07:52 +00:00
|
|
|
type Constants map[string]string
|
|
|
|
|
|
|
|
// LDFlags returns the string that can be passed to go build's `-ldflags`.
|
|
|
|
func (cs Constants) LDFlags() string {
|
|
|
|
l := make([]string, 0, len(cs))
|
|
|
|
|
2016-12-01 18:59:09 +00:00
|
|
|
for k, v := range cs {
|
|
|
|
l = append(l, fmt.Sprintf(`-X "%s=%s"`, k, v))
|
2015-08-25 20:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(l, " ")
|
|
|
|
}
|
|
|
|
|
2018-01-12 21:06:42 +00:00
|
|
|
// GoVersion is the version of Go used to compile the project.
|
|
|
|
type GoVersion struct {
|
|
|
|
Major int
|
|
|
|
Minor int
|
|
|
|
Patch int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseGoVersion parses the Go version s. If s cannot be parsed, the returned GoVersion is null.
|
|
|
|
func ParseGoVersion(s string) (v GoVersion) {
|
|
|
|
if !strings.HasPrefix(s, "go") {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s[2:]
|
|
|
|
data := strings.Split(s, ".")
|
2018-02-28 20:19:28 +00:00
|
|
|
if len(data) < 2 || len(data) > 3 {
|
|
|
|
// invalid version
|
|
|
|
return GoVersion{}
|
2018-01-12 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 20:19:28 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
v.Major, err = strconv.Atoi(data[0])
|
2018-01-12 21:06:42 +00:00
|
|
|
if err != nil {
|
2018-02-28 20:19:28 +00:00
|
|
|
return GoVersion{}
|
2018-01-12 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 20:19:28 +00:00
|
|
|
// try to parse the minor version while removing an eventual suffix (like
|
|
|
|
// "rc2" or so)
|
|
|
|
for s := data[1]; s != ""; s = s[:len(s)-1] {
|
|
|
|
v.Minor, err = strconv.Atoi(s)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
2018-01-12 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 20:19:28 +00:00
|
|
|
if v.Minor == 0 {
|
|
|
|
// no minor version found
|
|
|
|
return GoVersion{}
|
2018-01-12 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 20:19:28 +00:00
|
|
|
if len(data) >= 3 {
|
|
|
|
v.Patch, err = strconv.Atoi(data[2])
|
|
|
|
if err != nil {
|
|
|
|
return GoVersion{}
|
|
|
|
}
|
2018-01-12 21:06:42 +00:00
|
|
|
}
|
2018-02-28 20:19:28 +00:00
|
|
|
|
2018-01-12 21:06:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// AtLeast returns true if v is at least as new as other. If v is empty, true is returned.
|
|
|
|
func (v GoVersion) AtLeast(other GoVersion) bool {
|
|
|
|
var empty GoVersion
|
|
|
|
|
|
|
|
// the empty version satisfies all versions
|
|
|
|
if v == empty {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Major < other.Major {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Minor < other.Minor {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Patch < other.Patch {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v GoVersion) String() string {
|
|
|
|
return fmt.Sprintf("Go %d.%d.%d", v.Major, v.Minor, v.Patch)
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:59:31 +00:00
|
|
|
func main() {
|
2018-08-30 18:53:42 +00:00
|
|
|
if !goVersion.AtLeast(config.MinVersion) {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s detected, this program requires at least %s\n", goVersion, config.MinVersion)
|
2017-03-09 10:29:24 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2018-08-12 15:10:04 +00:00
|
|
|
buildTags := config.DefaultBuildTags
|
2015-06-24 17:57:08 +00:00
|
|
|
|
|
|
|
skipNext := false
|
|
|
|
params := os.Args[1:]
|
2016-07-15 22:14:41 +00:00
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
goEnv := map[string]string{}
|
|
|
|
buildEnv := map[string]string{
|
|
|
|
"GOOS": runtime.GOOS,
|
|
|
|
"GOARCH": runtime.GOARCH,
|
|
|
|
"GOARM": "",
|
|
|
|
}
|
2016-07-15 22:14:41 +00:00
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
tempdir := ""
|
2018-02-27 20:56:42 +00:00
|
|
|
|
2017-01-12 18:51:08 +00:00
|
|
|
var outputFilename string
|
|
|
|
|
2015-06-24 17:57:08 +00:00
|
|
|
for i, arg := range params {
|
|
|
|
if skipNext {
|
|
|
|
skipNext = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:59:31 +00:00
|
|
|
switch arg {
|
|
|
|
case "-v", "--verbose":
|
|
|
|
verbose = true
|
|
|
|
case "-k", "--keep-gopath":
|
|
|
|
keepGopath = true
|
2015-06-24 17:57:08 +00:00
|
|
|
case "-t", "-tags", "--tags":
|
2016-12-03 10:14:30 +00:00
|
|
|
if i+1 >= len(params) {
|
|
|
|
die("-t given but no tag specified")
|
|
|
|
}
|
2015-06-24 17:57:08 +00:00
|
|
|
skipNext = true
|
2018-08-12 15:10:04 +00:00
|
|
|
buildTags = append(buildTags, strings.Split(params[i+1], " ")...)
|
2017-01-12 18:51:08 +00:00
|
|
|
case "-o", "--output":
|
|
|
|
skipNext = true
|
|
|
|
outputFilename = params[i+1]
|
2018-02-27 20:56:42 +00:00
|
|
|
case "--tempdir":
|
|
|
|
skipNext = true
|
2018-08-30 18:53:42 +00:00
|
|
|
tempdir = params[i+1]
|
2015-08-19 18:11:48 +00:00
|
|
|
case "-T", "--test":
|
|
|
|
runTests = true
|
2017-05-25 09:03:29 +00:00
|
|
|
case "--enable-cgo":
|
|
|
|
enableCGO = true
|
2018-05-11 07:50:10 +00:00
|
|
|
case "--enable-pie":
|
|
|
|
enablePIE = true
|
2016-07-15 21:42:41 +00:00
|
|
|
case "--goos":
|
|
|
|
skipNext = true
|
2018-08-30 18:53:42 +00:00
|
|
|
buildEnv["GOOS"] = params[i+1]
|
2016-07-15 21:42:41 +00:00
|
|
|
case "--goarch":
|
|
|
|
skipNext = true
|
2018-08-30 18:53:42 +00:00
|
|
|
buildEnv["GOARCH"] = params[i+1]
|
2018-02-04 14:06:13 +00:00
|
|
|
case "--goarm":
|
|
|
|
skipNext = true
|
2018-08-30 18:53:42 +00:00
|
|
|
buildEnv["GOARM"] = params[i+1]
|
2015-06-24 15:59:31 +00:00
|
|
|
case "-h":
|
|
|
|
showUsage(os.Stdout)
|
2016-02-14 16:39:51 +00:00
|
|
|
return
|
2015-06-24 15:59:31 +00:00
|
|
|
default:
|
|
|
|
fmt.Fprintf(os.Stderr, "Error: unknown option %q\n\n", arg)
|
|
|
|
showUsage(os.Stderr)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
verbosePrintf("detected Go version %v\n", goVersion)
|
2018-02-28 20:19:28 +00:00
|
|
|
|
2015-06-24 17:57:08 +00:00
|
|
|
for i := range buildTags {
|
|
|
|
buildTags[i] = strings.TrimSpace(buildTags[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
verbosePrintf("build tags: %s\n", buildTags)
|
|
|
|
|
2015-06-24 15:59:31 +00:00
|
|
|
root, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
die("Getwd(): %v\n", err)
|
|
|
|
}
|
|
|
|
|
2017-01-12 18:51:08 +00:00
|
|
|
if outputFilename == "" {
|
|
|
|
outputFilename = config.Name
|
2018-08-30 18:53:42 +00:00
|
|
|
if buildEnv["GOOS"] == "windows" {
|
2017-01-12 18:51:08 +00:00
|
|
|
outputFilename += ".exe"
|
|
|
|
}
|
2016-02-14 16:39:51 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 09:54:19 +00:00
|
|
|
output := outputFilename
|
|
|
|
if !filepath.IsAbs(output) {
|
2018-08-30 18:53:42 +00:00
|
|
|
output = filepath.Join(root, output)
|
2017-07-14 09:54:19 +00:00
|
|
|
}
|
2016-02-14 16:39:51 +00:00
|
|
|
|
2015-08-19 04:31:14 +00:00
|
|
|
version := getVersion()
|
2016-12-19 20:14:12 +00:00
|
|
|
constants := Constants{}
|
2015-08-19 04:31:14 +00:00
|
|
|
if version != "" {
|
2015-08-25 20:07:52 +00:00
|
|
|
constants["main.version"] = version
|
2015-08-19 04:31:14 +00:00
|
|
|
}
|
2016-07-15 21:42:22 +00:00
|
|
|
ldflags := "-s -w " + constants.LDFlags()
|
2015-08-26 18:03:16 +00:00
|
|
|
verbosePrintf("ldflags: %s\n", ldflags)
|
2015-08-25 20:07:52 +00:00
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
var (
|
|
|
|
buildArgs []string
|
|
|
|
testArgs []string
|
|
|
|
)
|
|
|
|
|
|
|
|
mainPackage := config.Main
|
|
|
|
if strings.HasPrefix(mainPackage, config.Namespace) {
|
|
|
|
mainPackage = strings.Replace(mainPackage, config.Namespace, "./", 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTarget := filepath.FromSlash(mainPackage)
|
|
|
|
buildCWD := ""
|
|
|
|
|
|
|
|
if goVersion.AtLeast(GoVersion{1, 11, 0}) && fileExists("go.mod") {
|
|
|
|
verbosePrintf("Go >= 1.11 and 'go.mod' found, building with modules\n")
|
|
|
|
buildCWD = root
|
|
|
|
|
|
|
|
buildArgs = append(buildArgs, "-mod=vendor")
|
|
|
|
testArgs = append(testArgs, "-mod=vendor")
|
2019-07-29 07:22:22 +00:00
|
|
|
|
|
|
|
goEnv["GO111MODULE"] = "on"
|
|
|
|
buildEnv["GO111MODULE"] = "on"
|
2018-08-30 18:53:42 +00:00
|
|
|
} else {
|
|
|
|
if tempdir == "" {
|
|
|
|
tempdir, err = ioutil.TempDir("", fmt.Sprintf("%v-build-", config.Name))
|
|
|
|
if err != nil {
|
|
|
|
die("TempDir(): %v\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
verbosePrintf("Go < 1.11 or 'go.mod' not found, create GOPATH at %v\n", tempdir)
|
|
|
|
targetdir := filepath.Join(tempdir, "src", filepath.FromSlash(config.Namespace))
|
|
|
|
if err = copy(targetdir, root); err != nil {
|
|
|
|
die("copying files from %v to %v/src failed: %v\n", root, tempdir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if !keepGopath {
|
|
|
|
verbosePrintf("remove %v\n", tempdir)
|
|
|
|
if err = os.RemoveAll(tempdir); err != nil {
|
|
|
|
die("remove GOPATH at %s failed: %v\n", tempdir, err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
verbosePrintf("leaving temporary GOPATH at %v\n", tempdir)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
buildCWD = targetdir
|
|
|
|
|
|
|
|
goEnv["GOPATH"] = tempdir
|
|
|
|
buildEnv["GOPATH"] = tempdir
|
|
|
|
}
|
|
|
|
|
|
|
|
verbosePrintf("environment:\n go: %v\n build: %v\n", goEnv, buildEnv)
|
|
|
|
|
|
|
|
buildArgs = append(buildArgs,
|
2015-06-24 17:57:08 +00:00
|
|
|
"-tags", strings.Join(buildTags, " "),
|
2015-08-19 04:31:14 +00:00
|
|
|
"-ldflags", ldflags,
|
2018-08-30 18:53:42 +00:00
|
|
|
"-o", output, buildTarget,
|
|
|
|
)
|
2015-08-14 13:08:07 +00:00
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
err = build(buildCWD, buildEnv, buildArgs...)
|
2015-06-24 15:59:31 +00:00
|
|
|
if err != nil {
|
2015-08-19 19:40:36 +00:00
|
|
|
die("build failed: %v\n", err)
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-19 18:11:48 +00:00
|
|
|
if runTests {
|
|
|
|
verbosePrintf("running tests\n")
|
|
|
|
|
2018-08-30 18:53:42 +00:00
|
|
|
testArgs = append(testArgs, config.Tests...)
|
|
|
|
|
|
|
|
err = test(buildCWD, goEnv, testArgs...)
|
2015-08-19 18:11:48 +00:00
|
|
|
if err != nil {
|
2015-08-19 19:40:36 +00:00
|
|
|
die("running tests failed: %v\n", err)
|
2015-06-24 15:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|