-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.go
152 lines (141 loc) · 3.51 KB
/
build.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
// Build cross-compiles packages on set of
// OS and architecture pairs.
import (
"bytes"
"flag"
"fmt"
"os"
"os/exec"
)
var (
// 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"},
}
// 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
archPairFlags = map[[2]string][]string{
{"windows", "386"}: {"-ldflags=-H=windowsgui"},
{"windows", "amd64"}: {"-ldflags=-H=windowsgui"},
}
packageName string
outputName string
verbose bool
useosx bool
usewin bool
uselinux bool
usedroid bool
useall bool
help bool
)
func init() {
flag.BoolVar(&verbose, "v", true, "print build commands as they are run")
flag.StringVar(&outputName, "o", "agentBlue", "output executable name")
flag.BoolVar(&useosx, "osx", false, "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")
flag.StringVar(&packageName, "pkg", "github.com/oakmound/lowrez17", "package to build")
flag.BoolVar(&help, "h", false, "prints usage")
}
func main() {
if help {
fmt.Println("Usage: go run build.go <flags> -pkg <package>")
return
}
flag.Parse()
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]
if pair[0] == "windows" {
buildName += ".exe"
}
var out bytes.Buffer
toRun := []string{"build", "-o", buildName}
if flags, ok := archPairFlags[pair]; ok {
toRun = append(toRun, flags...)
}
toRun = append(toRun, packageName)
if verbose {
fmt.Println("Running: go ", toRun)
}
cmd := exec.Command("go", toRun...)
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
if verbose && out.Len() != 0 {
fmt.Printf("%s\n", out.String())
}
}
os.Setenv("GOOS", initOs)
os.Setenv("GOARCH", initArch)
}