-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorchitect.go
89 lines (75 loc) · 1.78 KB
/
gorchitect.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
package main
import (
"flag"
"fmt"
"github.com/rhobro/goutils/pkg/util"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
)
var path string
var out string
var nProcesses int
func init() {
// args and flags
wd, err := os.Getwd()
util.Check(err)
flag.StringVar(&out, "o", wd, "output path to which Go binaries should be compiled to")
flag.IntVar(&nProcesses, "n", runtime.GOMAXPROCS(0), "number of goroutines to use to concurrently compile")
flag.Parse()
path = flag.Arg(0)
semaphore = make(chan struct{}, nProcesses)
// mkdir out
util.Check(os.MkdirAll(out, os.ModePerm))
if path == "" {
log.Fatal("No path to Go program provided")
}
}
// semaphore to limit concurrency
var semaphore chan struct{}
func main() {
// get executable name
name := filepath.Base(path)
if strings.Contains(name, ".") {
name = name[:strings.LastIndex(name, ".")]
}
// get list of GOOSs and GOARCHes and compile for each
var wg sync.WaitGroup
rawList := strings.TrimSpace(execute(exec.Command("go", "tool", "dist", "list")))
for _, combo := range strings.Split(rawList, "\n") {
spl := strings.Split(combo, "/")
goOS, goArch := spl[0], spl[1]
out := filepath.Join(out, fmt.Sprintf("%s_%s_%s", name, goOS, goArch))
if goOS == "windows" {
out += ".exe"
}
if goArch == "wasm" {
out += ".wasm"
}
cmd := exec.Command("go", "build", "-o", out, path)
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "GOOS="+goOS)
cmd.Env = append(cmd.Env, "GOARCH="+goArch)
semaphore <- struct{}{}
wg.Add(1)
go func() {
_ = cmd.Run()
wg.Done()
<-semaphore
}()
}
wg.Wait()
}
func execute(c *exec.Cmd) string {
out, err := c.StdoutPipe()
util.Check(err)
util.Check(c.Start())
bd, _ := ioutil.ReadAll(out)
util.Check(c.Wait())
return string(bd)
}