2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-06 11:00:48 +00:00

build.go: Support cross-compilation via new --goos and --goarch flags

This commit is contained in:
mappu 2016-07-16 09:42:41 +12:00
parent f967e90a96
commit c9400d5c61

View File

@ -156,6 +156,8 @@ func showUsage(output io.Writer) {
fmt.Fprintf(output, " -t --tags specify additional build tags\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, " -k --keep-gopath do not remove the GOPATH after build\n")
fmt.Fprintf(output, " -T --test run tests\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{}) { func verbosePrintf(message string, args ...interface{}) {
@ -181,10 +183,10 @@ 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, gopath string, args ...string) error { func build(cwd, goos, goarch, gopath string, args ...string) error {
args = append([]string{"build"}, args...) args = append([]string{"build"}, args...)
cmd := exec.Command("go", 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.Dir = cwd
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -281,6 +283,10 @@ func main() {
skipNext := false skipNext := false
params := os.Args[1:] params := os.Args[1:]
targetGOOS := runtime.GOOS
targetGOARCH := runtime.GOARCH
for i, arg := range params { for i, arg := range params {
if skipNext { if skipNext {
skipNext = false skipNext = false
@ -297,6 +303,12 @@ func main() {
buildTags = strings.Split(params[i+1], " ") buildTags = strings.Split(params[i+1], " ")
case "-T", "--test": case "-T", "--test":
runTests = true runTests = true
case "--goos":
skipNext = true
targetGOOS = params[i+1]
case "--goarch":
skipNext = true
targetGOARCH = params[i+1]
case "-h": case "-h":
showUsage(os.Stdout) showUsage(os.Stdout)
return return
@ -354,7 +366,7 @@ func main() {
}() }()
outputFilename := "restic" outputFilename := "restic"
if runtime.GOOS == "windows" { if targetGOOS == "windows" {
outputFilename = "restic.exe" outputFilename = "restic.exe"
} }
@ -379,7 +391,7 @@ func main() {
"-o", output, "cmds/restic", "-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 { if err != nil {
die("build failed: %v\n", err) die("build failed: %v\n", err)
} }