forked from mitchellh/gox
-
Notifications
You must be signed in to change notification settings - Fork 2
/
go.go
233 lines (200 loc) · 5.32 KB
/
go.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
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"text/template"
)
type OutputTemplateData struct {
Dir string
OS string
Arch string
ARM string
}
type CompileOpts struct {
PackagePath string
Platform Platform
OutputTpl string
Ldflags string
Gcflags string
Tags string
Cgo bool
Rebuild bool
GoCmd string
}
// GoCrossCompile
func GoCrossCompile(opts *CompileOpts) error {
env := append(os.Environ(),
"GOOS="+opts.Platform.OS,
"GOARCH="+opts.Platform.Arch)
// If we're building for our own platform, then enable cgo always. We
// respect the CGO_ENABLED flag if that is explicitly set on the platform.
if !opts.Cgo && os.Getenv("CGO_ENABLED") != "0" {
opts.Cgo = runtime.GOOS == opts.Platform.OS &&
runtime.GOARCH == opts.Platform.Arch
}
// If cgo is enabled then set that env var
if opts.Cgo {
env = append(env, "CGO_ENABLED=1")
} else {
env = append(env, "CGO_ENABLED=0")
}
if len(opts.Platform.ARM) > 0 {
env = append(env, "GOARM="+opts.Platform.ARM)
}
var outputPath bytes.Buffer
tpl, err := template.New("output").Parse(opts.OutputTpl)
if err != nil {
return err
}
tplData := OutputTemplateData{
Dir: filepath.Base(opts.PackagePath),
OS: opts.Platform.OS,
Arch: opts.Platform.GetArch(),
ARM: opts.Platform.GetARMVersion(),
}
if err := tpl.Execute(&outputPath, &tplData); err != nil {
return err
}
if opts.Platform.OS == "windows" {
outputPath.WriteString(".exe")
}
// Determine the full path to the output so that we can change our
// working directory when executing go build.
outputPathReal := outputPath.String()
outputPathReal, err = filepath.Abs(outputPathReal)
if err != nil {
return err
}
// Go prefixes the import directory with '_' when it is outside
// the GOPATH.For this, we just drop it since we move to that
// directory to build.
chdir := ""
if opts.PackagePath[0] == '_' {
if runtime.GOOS == "windows" {
// We have to replace weird paths like this:
//
// _/c_/Users
//
// With:
//
// c:\Users
//
re := regexp.MustCompile("^/([a-zA-Z])_/")
chdir = re.ReplaceAllString(opts.PackagePath[1:], "$1:\\")
chdir = strings.Replace(chdir, "/", "\\", -1)
} else {
chdir = opts.PackagePath[1:]
}
opts.PackagePath = ""
}
args := []string{"build"}
if opts.Rebuild {
args = append(args, "-a")
}
args = append(args,
"-gcflags", opts.Gcflags,
"-ldflags", opts.Ldflags,
"-tags", opts.Tags,
"-o", outputPathReal,
opts.PackagePath)
_, err = execGo(opts.GoCmd, env, chdir, args...)
return err
}
// GoMainDirs returns the file paths to the packages that are "main"
// packages, from the list of packages given. The list of packages can
// include relative paths, the special "..." Go keyword, etc.
func GoMainDirs(packages []string, GoCmd string) ([]string, error) {
args := make([]string, 0, len(packages)+3)
args = append(args, "list", "-f", "{{.Name}}|{{.ImportPath}}")
args = append(args, packages...)
output, err := execGo(GoCmd, nil, "", args...)
if err != nil {
return nil, err
}
results := make([]string, 0, len(output))
for _, line := range strings.Split(output, "\n") {
if line == "" {
continue
}
parts := strings.SplitN(line, "|", 2)
if len(parts) != 2 {
log.Printf("Bad line reading packages: %s", line)
continue
}
if parts[0] == "main" {
results = append(results, parts[1])
}
}
return results, nil
}
// GoRoot returns the GOROOT value for the compiled `go` binary.
func GoRoot() (string, error) {
output, err := execGo("go", nil, "", "env", "GOROOT")
if err != nil {
return "", err
}
return strings.TrimSpace(output), nil
}
// GoVersion reads the version of `go` that is on the PATH. This is done
// instead of `runtime.Version()` because it is possible to run gox against
// another Go version.
func GoVersion() (string, error) {
// NOTE: We use `go run` instead of `go version` because the output
// of `go version` might change whereas the source is guaranteed to run
// for some time thanks to Go's compatibility guarantee.
td, err := ioutil.TempDir("", "gox")
if err != nil {
return "", err
}
defer os.RemoveAll(td)
// Write the source code for the program that will generate the version
sourcePath := filepath.Join(td, "version.go")
if err := ioutil.WriteFile(sourcePath, []byte(versionSource), 0644); err != nil {
return "", err
}
// Execute and read the version, which will be the only thing on stdout.
return execGo("go", nil, "", "run", sourcePath)
}
// GoVersionParts parses the version numbers from the version itself
// into major and minor: 1.5, 1.4, etc.
func GoVersionParts() (result [2]int, err error) {
version, err := GoVersion()
if err != nil {
return
}
_, err = fmt.Sscanf(version, "go%d.%d", &result[0], &result[1])
return
}
func execGo(GoCmd string, env []string, dir string, args ...string) (string, error) {
var stderr, stdout bytes.Buffer
cmd := exec.Command(GoCmd, args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if env != nil {
cmd.Env = env
}
if dir != "" {
cmd.Dir = dir
}
if err := cmd.Run(); err != nil {
err = fmt.Errorf("%s\nStderr: %s", err, stderr.String())
return "", err
}
return stdout.String(), nil
}
const versionSource = `package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Print(runtime.Version())
}`