-
Notifications
You must be signed in to change notification settings - Fork 12
/
builder.go
176 lines (168 loc) · 4.71 KB
/
builder.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
package goxz
import (
"bytes"
"compress/flate"
"compress/gzip"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mholt/archiver/v3"
"github.com/pkg/errors"
)
type builder struct {
name, version string
platform *platform
output string
buildLdFlags, buildTags, buildInstallSuffix string
pkgs []string
workDirBase string
zipAlways, static, trimpath bool
resources []string
projDir string
}
func (bdr *builder) build() (string, error) {
dirStuff := []string{bdr.name}
if bdr.version != "" {
dirStuff = append(dirStuff, bdr.version)
}
dirStuff = append(dirStuff, bdr.platform.os, bdr.platform.arch)
dirname := strings.Join(dirStuff, "_")
workDir := filepath.Join(bdr.workDirBase, dirname)
if err := os.Mkdir(workDir, 0755); err != nil {
return "", err
}
for _, pkg := range bdr.pkgs {
log.Printf("Building %s for %s/%s\n", pkg, bdr.platform.os, bdr.platform.arch)
var stdout, stderr bytes.Buffer
cmd := exec.Command("go", "list", "-f", "{{.Name}}", pkg)
cmd.Stdout, cmd.Stderr = &stdout, &stderr
if err := cmd.Run(); err != nil {
return "", errors.Errorf("go list failed with following output: %q", stderr.String())
}
pkgName := strings.TrimSpace(stdout.String())
if pkgName != "main" {
return "", errors.Errorf("can't build artifact for non main package: %q", pkgName)
}
output := bdr.output
if output == "" {
output = filepath.Base(pkg)
if output == "." {
wd, err := os.Getwd()
if err != nil {
return "", err
}
output = filepath.Base(wd)
}
if bdr.platform.os == "windows" {
output += ".exe"
}
}
cmdArgs := []string{"build", "-o", filepath.Join(workDir, output)}
// ref. https://github.com/golang/go/issues/26492#issuecomment-435462350
if bdr.buildLdFlags != "" || bdr.static {
var flags string
if bdr.static {
switch bdr.platform.os {
case "freebsd", "netbsd", "linux", "windows":
flags = `-extldflags "-static"`
case "darwin":
flags = `-s -extldflags "-sectcreate __TEXT __info_plist Info.plist"`
case "android":
flags = `-s`
}
}
if bdr.buildLdFlags != "" {
if flags == "" {
flags = bdr.buildLdFlags
} else {
flags += " " + bdr.buildLdFlags
}
}
if flags != "" {
cmdArgs = append(cmdArgs, "-ldflags", flags)
}
}
if bdr.buildTags != "" || bdr.static {
var tags string
if bdr.static {
switch bdr.platform.os {
case "windows", "freebsd", "netbsd":
tags = "netgo"
case "linux":
tags = "netgo osusergo"
}
}
if bdr.buildTags != "" {
if tags == "" {
tags = bdr.buildTags
} else {
tags += " " + bdr.buildTags
}
}
if tags != "" {
cmdArgs = append(cmdArgs, "-tags", tags)
}
}
if bdr.trimpath {
cmdArgs = append(cmdArgs, "-trimpath")
}
if bdr.buildInstallSuffix != "" {
cmdArgs = append(cmdArgs, "-installsuffix", bdr.buildInstallSuffix)
}
cmdArgs = append(cmdArgs, pkg)
cmd = exec.Command("go", cmdArgs...)
cmd.Env = append(os.Environ(), "GOOS="+bdr.platform.os, "GOARCH="+bdr.platform.arch)
bs, err := cmd.CombinedOutput()
if err != nil {
return "", errors.Wrapf(err,
"go build failed while building %q for %s/%s with following output:\n%s",
pkg, bdr.platform.os, bdr.platform.arch, string(bs))
}
}
files, err := ioutil.ReadDir(workDir)
if err != nil {
return "", err
}
if len(files) == 0 {
return "", errors.Errorf("No binaries are built from [%s] for %s/%s",
strings.Join(bdr.pkgs, " "), bdr.platform.os, bdr.platform.arch)
}
for _, rc := range bdr.resources {
rel, _ := filepath.Rel(bdr.projDir, rc)
dest := filepath.Join(workDir, rel)
if err := os.MkdirAll(filepath.Dir(dest), 0755); err != nil {
return "", err
}
if err := os.Link(rc, dest); err != nil {
return "", err
}
}
var arch archiver.Archiver
var archiveFilePath string
if bdr.zipAlways || bdr.platform.os == "windows" || bdr.platform.os == "darwin" {
arch = &archiver.Zip{
CompressionLevel: flate.DefaultCompression,
MkdirAll: true,
SelectiveCompression: true,
FileMethod: archiver.Deflate,
}
archiveFilePath = workDir + ".zip"
} else {
arch = &archiver.TarGz{
CompressionLevel: gzip.DefaultCompression,
Tar: &archiver.Tar{
MkdirAll: true,
},
}
archiveFilePath = workDir + ".tar.gz"
}
log.Printf("Archiving %s\n", filepath.Base(archiveFilePath))
err = arch.Archive([]string{workDir}, archiveFilePath)
if err != nil {
return "", err
}
return archiveFilePath, nil
}