Update build.go from github.com/fd0/build-go

This commit is contained in:
Alexander Neumann 2018-02-28 21:19:28 +01:00
parent 1e868933c5
commit ab7a3a803d
1 changed files with 39 additions and 19 deletions

View File

@ -227,7 +227,7 @@ func showUsage(output io.Writer) {
fmt.Fprintf(output, " --enable-cgo use CGO to link against libc\n") fmt.Fprintf(output, " --enable-cgo use CGO to link against libc\n")
fmt.Fprintf(output, " --goos value set GOOS for cross-compilation\n") fmt.Fprintf(output, " --goos value set GOOS for cross-compilation\n")
fmt.Fprintf(output, " --goarch value set GOARCH for cross-compilation\n") fmt.Fprintf(output, " --goarch value set GOARCH for cross-compilation\n")
fmt.Fprintf(output, " --goarm value set GOARM for cross-compilation\n") fmt.Fprintf(output, " --goarm value set GOARM for cross-compilation\n")
} }
func verbosePrintf(message string, args ...interface{}) { func verbosePrintf(message string, args ...interface{}) {
@ -253,10 +253,19 @@ func cleanEnv() (env []string) {
} }
// build runs "go build args..." with GOPATH set to gopath. // build runs "go build args..." with GOPATH set to gopath.
func build(cwd, goos, goarch, goarm, gopath string, args ...string) error { func build(cwd string, ver GoVersion, goos, goarch, goarm, gopath string, args ...string) error {
a := []string{"build"} a := []string{"build"}
a = append(a, "-asmflags", fmt.Sprintf("-trimpath=%s", gopath))
a = append(a, "-gcflags", fmt.Sprintf("-trimpath=%s", gopath)) if ver.AtLeast(GoVersion{1, 10, 0}) {
verbosePrintf("Go version is at least 1.10, using new syntax for -gcflags\n")
// use new prefix
a = append(a, "-asmflags", fmt.Sprintf("all=-trimpath=%s", gopath))
a = append(a, "-gcflags", fmt.Sprintf("all=-trimpath=%s", gopath))
} else {
a = append(a, "-asmflags", fmt.Sprintf("-trimpath=%s", gopath))
a = append(a, "-gcflags", fmt.Sprintf("-trimpath=%s", gopath))
}
a = append(a, args...) a = append(a, args...)
cmd := exec.Command("go", a...) cmd := exec.Command("go", a...)
cmd.Env = append(cleanEnv(), "GOPATH="+gopath, "GOARCH="+goarch, "GOOS="+goos) cmd.Env = append(cleanEnv(), "GOPATH="+gopath, "GOARCH="+goarch, "GOOS="+goos)
@ -366,30 +375,39 @@ func ParseGoVersion(s string) (v GoVersion) {
s = s[2:] s = s[2:]
data := strings.Split(s, ".") data := strings.Split(s, ".")
if len(data) != 3 { if len(data) < 2 || len(data) > 3 {
return // invalid version
return GoVersion{}
} }
major, err := strconv.Atoi(data[0]) var err error
v.Major, err = strconv.Atoi(data[0])
if err != nil { if err != nil {
return return GoVersion{}
} }
minor, err := strconv.Atoi(data[1]) // try to parse the minor version while removing an eventual suffix (like
if err != nil { // "rc2" or so)
return for s := data[1]; s != ""; s = s[:len(s)-1] {
v.Minor, err = strconv.Atoi(s)
if err == nil {
break
}
} }
patch, err := strconv.Atoi(data[2]) if v.Minor == 0 {
if err != nil { // no minor version found
return return GoVersion{}
} }
v = GoVersion{ if len(data) >= 3 {
Major: major, v.Patch, err = strconv.Atoi(data[2])
Minor: minor, if err != nil {
Patch: patch, return GoVersion{}
}
} }
return return
} }
@ -487,6 +505,8 @@ func main() {
} }
} }
verbosePrintf("detected Go version %v\n", ver)
if len(buildTags) == 0 { if len(buildTags) == 0 {
verbosePrintf("adding build-tag release\n") verbosePrintf("adding build-tag release\n")
buildTags = []string{"release"} buildTags = []string{"release"}
@ -563,7 +583,7 @@ func main() {
"-o", output, config.Main, "-o", output, config.Main,
} }
err = build(filepath.Join(gopath, "src"), targetGOOS, targetGOARCH, targetGOARM, gopath, args...) err = build(filepath.Join(gopath, "src"), ver, targetGOOS, targetGOARCH, targetGOARM, gopath, args...)
if err != nil { if err != nil {
die("build failed: %v\n", err) die("build failed: %v\n", err)
} }