-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.go
195 lines (154 loc) · 3.65 KB
/
compiler.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
package bintest
import (
"crypto/sha1"
"errors"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
deadlock "github.com/sasha-s/go-deadlock"
)
const (
serverEnv = ``
clientSrc = `package main
import (
"github.com/buildkite/bintest/v3"
"os"
)
var (
debug string
server string
)
func main() {
c := bintest.NewClient(server)
if debug == "true" {
c.Debug = true
}
os.Exit(c.Run())
}
`
)
var (
compileCacheInstance *compileCache
compileLock deadlock.Mutex
)
func compile(dest string, src string, vars []string) error {
args := []string{
"build",
"-o", dest,
}
if len(vars) > 0 || Debug {
varsCopy := make([]string, len(vars))
copy(varsCopy, vars)
args = append(args, "-ldflags")
for idx, val := range varsCopy {
varsCopy[idx] = "-X " + val
}
if Debug {
varsCopy = append(varsCopy, "-X main.debug=true")
}
args = append(args, strings.Join(varsCopy, " "))
}
t := time.Now()
output, err := exec.Command("go", append(args, src)...).CombinedOutput()
if err != nil {
return fmt.Errorf("Compile of %s failed: %s", src, output)
}
debugf("[compiler] Compiled %s in %v", dest, time.Now().Sub(t))
return nil
}
func compileClient(dest string, vars []string) error {
serverLock.Lock()
defer serverLock.Unlock()
// first off we create a temp dir for caching
if compileCacheInstance == nil {
cci, err := newCompileCache()
if err != nil {
return err
}
compileCacheInstance = cci
}
cacheBinaryPath, err := compileCacheInstance.file(vars)
if err != nil {
return err
}
// if we can, symlink to an existing file in the compile cache
if compileCacheInstance.IsCached(vars) {
return replaceSymlink(cacheBinaryPath, dest)
}
// we create a temp subdir relative to current dir so that
// we can make use of gopath / vendor dirs
dir := fmt.Sprintf(`_bintest_%x`, sha1.Sum([]byte(clientSrc)))
f := filepath.Join(dir, `main.go`)
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
}
defer os.RemoveAll(dir)
if err := os.WriteFile(f, []byte(clientSrc), 0o500); err != nil {
return err
}
if err := compile(cacheBinaryPath, f, vars); err != nil {
return err
}
if err := os.RemoveAll(dir); err != nil {
return err
}
// Create a symlink to the binary.
return replaceSymlink(cacheBinaryPath, dest)
}
// To keep the old behaviour of overwriting what was in the destination path,
// replaceSymlink creates a symlink with a temporary name and then renames it
// over the destination path.
func replaceSymlink(oldname, newname string) error {
tempname := fmt.Sprintf("%s.%x", newname, rand.Int())
if err := os.Symlink(oldname, tempname); err != nil {
return err
}
return os.Rename(tempname, newname)
}
type compileCache struct {
Dir string
}
func newCompileCache() (*compileCache, error) {
cc := &compileCache{}
var err error
cc.Dir, err = os.MkdirTemp("", "binproxy")
if err != nil {
return nil, fmt.Errorf("Error creating temp dir: %v", err)
}
return cc, nil
}
func (c *compileCache) IsCached(vars []string) bool {
path, err := c.file(vars)
if err != nil {
panic(err)
}
_, err = os.Stat(path)
return err == nil
}
func (c *compileCache) Key(vars []string) (string, error) {
h := sha1.New()
// add the vars to the hash
for _, v := range vars {
if _, err := io.WriteString(h, v); err != nil {
return "", err
}
}
// factor in client source as well
_, _ = io.WriteString(h, clientSrc)
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
func (c *compileCache) file(vars []string) (string, error) {
if c.Dir == "" {
return "", errors.New("No compile cache dir set")
}
k, err := c.Key(vars)
if err != nil {
return "", err
}
return filepath.Join(c.Dir, k), nil
}