-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
286 lines (269 loc) · 6.95 KB
/
main.go
File metadata and controls
286 lines (269 loc) · 6.95 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/mh-cbon/build-them-all/utils"
"github.com/urfave/cli"
)
var VERSION = "0.0.0"
func main() {
app := cli.NewApp()
app.Name = "build-them-all"
app.Version = VERSION
app.Usage = "Command line to build go programs to multiple targets"
app.UsageText = "build-them-all <cmd> <options>"
app.Commands = []cli.Command{
{
Name: "clean",
Usage: "Clean up directories",
UsageText: "build-them-all clean <options>",
Action: clean,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "dir, d",
Value: &cli.StringSlice{"build/"},
Usage: "Directoires to clean",
},
},
},
{
Name: "build",
Usage: "Build the binaries",
UsageText: "build-them-all build <options> <packages>",
Action: build,
Flags: []cli.Flag{
cli.StringFlag{
Name: "os",
Value: "major",
Usage: "OS selector",
},
cli.StringFlag{
Name: "arch",
Value: "major",
Usage: "Arch selector",
},
cli.StringFlag{
Name: "output, o",
Value: "",
Usage: "Output pattern",
},
cli.StringFlag{
Name: "gobin",
Value: "go",
Usage: "Go bin path",
},
cli.StringSliceFlag{
Name: "dir, d",
Value: &cli.StringSlice{"build/"},
Usage: "Directoires to clean",
},
cli.StringFlag{
Name: "wd",
Value: "",
Usage: "Working directory",
},
cli.BoolFlag{
Name: "i",
Usage: "Installs the packages that are dependencies of the target",
},
cli.BoolFlag{
Name: "a",
Usage: "Force rebuilding of packages that are already up-to-date",
},
cli.BoolFlag{
Name: "n",
Usage: "Print the commands but do not run them",
},
cli.StringFlag{
Name: "p",
Value: "",
Usage: "The number of programs that can be run in parallel",
},
cli.BoolFlag{
Name: "v",
Usage: "Print the names of packages as they are compiled",
},
cli.BoolFlag{
Name: "x",
Usage: "Print the commands",
},
cli.BoolFlag{
Name: "work",
Usage: "Print the name of the temporary work directory and do not delete it when exiting.",
},
cli.BoolFlag{
Name: "race",
Usage: "Enable data race detection",
},
cli.BoolFlag{
Name: "msan",
Usage: "Enable interoperation with memory sanitizer",
},
cli.StringFlag{
Name: "asmflags",
Value: "",
Usage: "Arguments to pass on each go tool asm invocation",
},
cli.StringFlag{
Name: "buildmode",
Value: "",
Usage: "Build mode to use. See 'go help buildmode' for more",
},
cli.StringFlag{
Name: "compiler",
Value: "",
Usage: "Name of compiler to use, as in runtime.Compiler (gccgo or gc)",
},
cli.StringFlag{
Name: "gccgoflags",
Value: "",
Usage: "Arguments to pass on each gccgo compiler/linker invocation",
},
cli.StringFlag{
Name: "gcflags",
Value: "",
Usage: "Arguments to pass on each go tool compile invocation",
},
cli.StringFlag{
Name: "installsuffix",
Value: "",
Usage: "A suffix to use in the name of the package installation directory",
},
cli.StringFlag{
Name: "ldflags",
Value: "",
Usage: "Arguments to pass on each go tool link invocation",
},
cli.StringFlag{
Name: "linkshared",
Value: "",
Usage: "Link against shared libraries previously created with -buildmode=shared",
},
cli.StringFlag{
Name: "pkgdir",
Value: "",
Usage: "Install and load all packages from dir instead of the usual locations",
},
cli.StringFlag{
Name: "tags",
Value: "",
Usage: "A list of build tags to consider satisfied during the build",
},
cli.StringFlag{
Name: "toolexec",
Value: "",
Usage: "A program to use to invoke toolchain programs like vet and asm",
},
},
},
}
app.Run(os.Args)
}
func clean(c *cli.Context) error {
errs := utils.CleanDirectories(c.StringSlice("dir"))
for _, err := range errs {
fmt.Println(err)
}
if len(errs) > 0 {
return cli.NewExitError("Some files were not properly deleted", 1)
}
fmt.Println("Cleaned directories")
for _, d := range c.StringSlice("dir") {
fmt.Println(string(d))
}
return nil
}
func build(c *cli.Context) error {
wantOs := c.String("os")
wantArch := c.String("arch")
output := c.String("output")
gobin := c.String("gobin")
wd := c.String("wd")
dirs := c.StringSlice("dir")
packages := make([]string, 0)
for _, arg := range c.Args() {
packages = append(packages, arg)
}
if len(packages) == 0 {
return cli.NewExitError("Packages list is required", 1)
}
gobin, err := exec.LookPath(gobin)
if err != nil {
return cli.NewExitError("Path to go bin '"+gobin+"' was not found", 1)
}
if len(wd) == 0 {
wd, err = os.Getwd()
if err != nil {
return cli.NewExitError("Working directory could not be determined", 1)
}
}
errs := utils.CleanDirectories(dirs)
for _, err := range errs {
fmt.Println(err)
}
if len(errs) > 0 {
return cli.NewExitError("Some files were not properly deleted", 1)
}
fmt.Println("wd=" + wd)
gotErr := false
selectedOsArch := utils.SelectOsArch(wantOs, wantArch)
for _, input := range packages {
pkg := utils.DetermineOutputName(input)
if len(pkg) == 0 {
fmt.Println("Could not determine output name for '" + input + "'")
gotErr = true
break
}
for cOs, archs := range selectedOsArch {
for _, cArch := range archs {
bCmd := utils.BuildCommand{
Install: c.Bool("i"),
RebuildAll: c.Bool("a"),
N: c.Bool("n"),
P: c.String("p"),
Race: c.Bool("race"),
Msan: c.Bool("msan"),
V: c.Bool("v"),
Work: c.Bool("work"),
X: c.Bool("x"),
Asmflags: c.String("asmflags"),
Buildmode: c.String("buildmode"),
Compiler: c.String("compiler"),
Gccgoflags: c.String("gccgoflags"),
Gcflags: c.String("gcflags"),
InstallSuffix: c.String("installsuffix"),
Ldflags: c.String("ldflags"),
Linkshared: c.String("linkshared"),
Pkgdir: c.String("pkgdir"),
Tags: c.String("tTags"),
Toolexec: c.String("toolexec"),
Output: utils.GenerateOutputName(output, pkg, cOs, cArch),
Wd: wd,
Gobin: gobin,
}
// build [-o output] [-i] [build flags] [packages]
oCmd := utils.GenerateBuildCommand(cOs, cArch, bCmd)
fmt.Println("> GOOS=" + cOs + " " + "GOARCH=" + cArch + " " + strings.Join(oCmd.Args, " "))
out, err := oCmd.CombinedOutput()
sOut := string(out)
if len(sOut) > 0 {
fmt.Println(sOut)
}
if err == nil {
fmt.Println("Success!")
} else {
fmt.Println("Failure!")
fmt.Println(err)
gotErr = true
}
fmt.Println("")
}
}
}
if gotErr {
return cli.NewExitError("There were errors during the build", 1)
}
return nil
}