-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
build.go
73 lines (63 loc) · 1.29 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
package commands
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"
)
type builder struct {
os, srcdir string
release bool
tags []string
}
func (b *builder) build() error {
goos := b.os
if goos == "" {
goos = targetOS()
}
args := []string{"build"}
env := os.Environ()
env = append(env, "CGO_ENABLED=1") // in case someone is trying to cross-compile...
if goos == "windows" {
if b.release {
args = append(args, "-ldflags", "-s -w -H=windowsgui")
} else {
args = append(args, "-ldflags", "-H=windowsgui")
}
} else {
if goos == "darwin" {
env = append(env, "CGO_CFLAGS=-mmacosx-version-min=10.11")
env = append(env, "CGO_LDFLAGS=-mmacosx-version-min=10.11")
}
if b.release {
args = append(args, "-ldflags", "-s -w")
}
}
// handle build tags
tags := b.tags
if b.release {
tags = append(tags, "release")
}
if len(tags) > 0 {
args = append(args, "-tags", strings.Join(tags, ","))
}
cmd := exec.Command("go", args...)
cmd.Dir = b.srcdir
if goos != "ios" && goos != "android" {
env = append(env, "GOOS="+goos)
}
cmd.Env = env
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", string(out))
}
return err
}
func targetOS() string {
osEnv, ok := os.LookupEnv("GOOS")
if ok {
return osEnv
}
return runtime.GOOS
}