Skip to content

Commit

Permalink
Merge pull request #543 from mappu/master
Browse files Browse the repository at this point in the history
Updates for build.go
  • Loading branch information
fd0 committed Jul 17, 2016
2 parents 8b47ca5 + 983e509 commit b8c7622
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,12 @@ func showUsage(output io.Writer) {
fmt.Fprintf(output, "USAGE: go run build.go OPTIONS\n")
fmt.Fprintf(output, "\n")
fmt.Fprintf(output, "OPTIONS:\n")
fmt.Fprintf(output, " -v --verbose output more messages\n")
fmt.Fprintf(output, " -t --tags specify additional build tags\n")
fmt.Fprintf(output, " -k --keep-gopath do not remove the GOPATH after build\n")
fmt.Fprintf(output, " -T --test run tests\n")
fmt.Fprintf(output, " -v --verbose output more messages\n")
fmt.Fprintf(output, " -t --tags specify additional build tags\n")
fmt.Fprintf(output, " -k --keep-gopath do not remove the GOPATH after build\n")
fmt.Fprintf(output, " -T --test run tests\n")
fmt.Fprintf(output, " --goos value set GOOS for cross-compilation\n")
fmt.Fprintf(output, " --goarch value set GOARCH for cross-compilation\n")
}

func verbosePrintf(message string, args ...interface{}) {
Expand All @@ -181,10 +183,10 @@ func cleanEnv() (env []string) {
}

// build runs "go build args..." with GOPATH set to gopath.
func build(cwd, gopath string, args ...string) error {
func build(cwd, goos, goarch, gopath string, args ...string) error {
args = append([]string{"build"}, args...)
cmd := exec.Command("go", args...)
cmd.Env = append(cleanEnv(), "GOPATH="+gopath)
cmd.Env = append(cleanEnv(), "GOPATH="+gopath, "GOARCH="+goarch, "GOOS="+goos)
cmd.Dir = cwd
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down Expand Up @@ -263,7 +265,7 @@ func (cs Constants) LDFlags() string {
l := make([]string, 0, len(cs))

v := runtime.Version()
if strings.HasPrefix(v, "go1.5") || strings.HasPrefix(v, "go1.6") {
if strings.HasPrefix(v, "go1.5") || strings.HasPrefix(v, "go1.6") || strings.HasPrefix(v, "go1.7") {
for k, v := range cs {
l = append(l, fmt.Sprintf(`-X "%s=%s"`, k, v))
}
Expand All @@ -281,6 +283,10 @@ func main() {

skipNext := false
params := os.Args[1:]

targetGOOS := runtime.GOOS
targetGOARCH := runtime.GOARCH

for i, arg := range params {
if skipNext {
skipNext = false
Expand All @@ -297,6 +303,12 @@ func main() {
buildTags = strings.Split(params[i+1], " ")
case "-T", "--test":
runTests = true
case "--goos":
skipNext = true
targetGOOS = params[i+1]
case "--goarch":
skipNext = true
targetGOARCH = params[i+1]
case "-h":
showUsage(os.Stdout)
return
Expand Down Expand Up @@ -354,7 +366,7 @@ func main() {
}()

outputFilename := "restic"
if runtime.GOOS == "windows" {
if targetGOOS == "windows" {
outputFilename = "restic.exe"
}

Expand All @@ -370,7 +382,7 @@ func main() {
if version != "" {
constants["main.version"] = version
}
ldflags := "-s " + constants.LDFlags()
ldflags := "-s -w " + constants.LDFlags()
verbosePrintf("ldflags: %s\n", ldflags)

args := []string{
Expand All @@ -379,7 +391,7 @@ func main() {
"-o", output, "cmds/restic",
}

err = build(filepath.Join(gopath, "src"), gopath, args...)
err = build(filepath.Join(gopath, "src"), targetGOOS, targetGOARCH, gopath, args...)
if err != nil {
die("build failed: %v\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion run_integration_tests.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// xbuild ignore
// +build ignore

package main

Expand Down

0 comments on commit b8c7622

Please sign in to comment.