Skip to content

Commit

Permalink
Update build.go to be generic
Browse files Browse the repository at this point in the history
  • Loading branch information
200sc committed May 21, 2017
1 parent 3fd7331 commit de0c407
Showing 1 changed file with 98 additions and 15 deletions.
113 changes: 98 additions & 15 deletions demo/bin/build.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,126 @@
package main

// Build cross-compiles the demo package on a small set of
// OS and architecture pairs. It should be generalized to
// take in variable output and package names, and varible
// sets of os-arch pairs, then split off into a separate
// project.
// Build cross-compiles packages on set of
// OS and architecture pairs.

import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
)

var (
osArchPairs = [][2]string{
// Defaults
osxPairs = [][2]string{
{"darwin", "386"},
{"darwin", "amd64"},
{"darwin", "arm"},
{"darwin", "arm64"},
}
linuxPairs = [][2]string{
{"linux", "386"},
{"linux", "amd64"},
{"linux", "arm"},
{"linux", "arm64"},
}
winPairs = [][2]string{
{"windows", "386"},
{"windows", "amd64"},
}
packageName = "github.com/200sc/go-compgeo/demo"
outputName = "pl-demo"
verbose = true
// End Defaults
android = [][2]string{
{"android", "arm"},
}

// These are grouped together because, from my perspective, they
// are less often used by themselves. If there are valid use cases
// to split them up into their own boolean flags then this can change.
// I admit this is mostly because I can't think of what computer would
// use these and would also be used for a generic program.
nonDefaultPairs = [][2]string{
{"dragonfly", "amd64"},
{"freebsd", "386"},
{"freebsd", "amd64"},
{"freebsd", "arm"},
{"linux", "ppc64"},
{"linux", "ppc64le"},
{"linux", "mips"},
{"linux", "mipsle"},
{"linux", "mips64"},
{"linux", "mips64le"},
{"netbsd", "386"},
{"netbsd", "amd64"},
{"netbsd", "arm"},
{"openbsd", "386"},
{"openbsd", "amd64"},
{"openbsd", "arm"},
{"plan9", "386"},
{"plan9", "amd64"},
{"solaris", "amd64"},
}

osArchPairs [][2]string

packageName string
outputName string
verbose bool
useosx bool
usewin bool
uselinux bool
usedroid bool
useall bool
)

func init() {
flag.BoolVar(&verbose, "v", false, "print build commands as they are run")
flag.StringVar(&outputName, "o", "out", "output executable name")
flag.BoolVar(&useosx, "osx", true, "build darwin executables")
flag.BoolVar(&uselinux, "nix", true, "build linux exectuables")
flag.BoolVar(&usewin, "win", true, "build windows exectuables")
flag.BoolVar(&usedroid, "android", false, "build android executables")
flag.BoolVar(&useall, "all", false, "build all executables")
}

func main() {
goos := os.Getenv("GOOS")
goarch := os.Getenv("GOARCH")
if len(os.Args) < 2 {
fmt.Println("Usage: go run cross.go <flags> <package>")
}
flag.Parse()
packageName = os.Args[len(os.Args)-1]
if useall {
useosx = true
usewin = true
usedroid = true
usewin = true
osArchPairs = nonDefaultPairs
}
if useosx {
osArchPairs = append(osArchPairs, osxPairs...)
}
if uselinux {
osArchPairs = append(osArchPairs, linuxPairs...)
}
if usedroid {
osArchPairs = append(osArchPairs, android...)
}
if usewin {
osArchPairs = append(osArchPairs, winPairs...)
}

initOs := os.Getenv("GOOS")
initArch := os.Getenv("GOARCH")

for _, pair := range osArchPairs {
os.Setenv("GOOS", pair[0])
os.Setenv("GOARCH", pair[1])
buildName := outputName + "_" + pair[0] + pair[1]
var out bytes.Buffer
if verbose {
fmt.Println("Running: go build -o", outputName+"_"+pair[0]+pair[1], packageName)
fmt.Println("Running: go build -o", buildName, packageName)
}
cmd := exec.Command("go", "build", "-o", outputName+"_"+pair[0]+pair[1], packageName)
cmd := exec.Command("go", "build", "-o", buildName, packageName)
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
Expand All @@ -48,6 +131,6 @@ func main() {
fmt.Printf("%s\n", out.String())
}
}
os.Setenv("GOOS", goos)
os.Setenv("GOARCH", goarch)
os.Setenv("GOOS", initOs)
os.Setenv("GOARCH", initArch)
}

0 comments on commit de0c407

Please sign in to comment.