-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathcompiler.go
214 lines (175 loc) · 5.56 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package builder
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"syscall"
"time"
"github.com/x1unix/go-playground/internal/builder/storage"
"github.com/x1unix/go-playground/pkg/util/osutil"
"go.uber.org/zap"
)
// defaultGoModName is default module name that will be set if no go.mod provided.
const defaultGoModName = "app"
// predefinedBuildVars is list of environment vars which contain build values
var predefinedBuildVars = osutil.EnvironmentVariables{
"CGO_ENABLED": "0",
"GOOS": "js",
"GOARCH": "wasm",
"HOME": os.Getenv("HOME"),
}
// Result is WASM build result
type Result struct {
// FileName is artifact file name
FileName string
// IsTest indicates whether binary is a test file
IsTest bool
// HasBenchmark indicates whether test contains benchmarks.
HasBenchmark bool
// HasFuzz indicates whether test has fuzzing tests.
HasFuzz bool
}
// BuildEnvironmentConfig is BuildService environment configuration.
type BuildEnvironmentConfig struct {
// IncludedEnvironmentVariables is a list included environment variables for build.
IncludedEnvironmentVariables osutil.EnvironmentVariables
// KeepGoModCache disables Go modules cache cleanup.
KeepGoModCache bool
}
// BuildService is WASM build service
type BuildService struct {
log *zap.Logger
config BuildEnvironmentConfig
storage storage.StoreProvider
cmdRunner CommandRunner
}
// NewBuildService is BuildService constructor
func NewBuildService(log *zap.Logger, cfg BuildEnvironmentConfig, store storage.StoreProvider) BuildService {
return BuildService{
log: log.Named("builder"),
config: cfg,
storage: store,
cmdRunner: OSCommandRunner{},
}
}
func (s BuildService) getEnvironmentVariables() []string {
if len(s.config.IncludedEnvironmentVariables) == 0 {
return predefinedBuildVars.Join()
}
return s.config.IncludedEnvironmentVariables.Concat(predefinedBuildVars).Join()
}
// GetArtifact returns artifact by id
func (s BuildService) GetArtifact(id storage.ArtifactID) (storage.ReadCloseSizer, error) {
return s.storage.GetItem(id)
}
// Build compiles Go source to WASM and returns result
func (s BuildService) Build(ctx context.Context, files map[string][]byte) (*Result, error) {
projInfo, err := detectProjectType(files)
if err != nil {
return nil, err
}
// Go module is required to build project
if _, ok := files["go.mod"]; !ok {
files["go.mod"] = generateGoMod(defaultGoModName)
}
aid, err := storage.GetArtifactID(files)
if err != nil {
return nil, err
}
result := &Result{
FileName: aid.Ext(storage.ExtWasm),
IsTest: projInfo.projectType == projectTypeTest,
HasBenchmark: projInfo.hasBenchmark,
HasFuzz: projInfo.hasFuzz,
}
isCached, err := s.storage.HasItem(aid)
if err != nil {
s.log.Error("failed to check cache", zap.Stringer("artifact", aid), zap.Error(err))
return nil, err
}
if isCached {
// Just return precompiled result if data is cached already
s.log.Debug("build cached, returning cached file", zap.Stringer("artifact", aid))
return result, nil
}
workspace, err := s.storage.CreateWorkspace(aid, files)
if err != nil {
if errors.Is(err, syscall.ENOSPC) {
// Immediately schedule cleanup job!
s.handleNoSpaceLeft()
}
return nil, err
}
err = s.buildSource(ctx, projInfo, workspace)
return result, err
}
func (s BuildService) buildSource(ctx context.Context, projInfo projectInfo, workspace *storage.Workspace) error {
// Populate go.mod and go.sum files.
if err := s.runGoTool(ctx, workspace.WorkDir, "mod", "tidy"); err != nil {
return err
}
if projInfo.projectType == projectTypeProgram {
return s.runGoTool(ctx, workspace.WorkDir, "build", "-o", workspace.BinaryPath, ".")
}
args := []string{"test"}
if projInfo.hasBenchmark {
args = append(args, "-bench=.")
}
if projInfo.hasFuzz {
args = append(args, "-fuzz=.")
}
args = append(args, "-c", "-o", workspace.BinaryPath)
return s.runGoTool(ctx, workspace.WorkDir, args...)
}
func (s BuildService) handleNoSpaceLeft() {
s.log.Warn("no space left on device, immediate clean triggered!")
ctx, cancelFn := context.WithTimeout(context.Background(), time.Minute)
defer cancelFn()
if err := s.storage.Clean(ctx); err != nil {
s.log.Error("failed to clear storage", zap.Error(err))
}
if err := s.Clean(ctx); err != nil {
s.log.Error("failed to clear Go cache", zap.Error(err))
}
}
func (s BuildService) runGoTool(ctx context.Context, workDir string, args ...string) error {
cmd := newGoToolCommand(ctx, args...)
cmd.Dir = workDir
cmd.Env = s.getEnvironmentVariables()
buff := &bytes.Buffer{}
cmd.Stderr = buff
s.log.Debug(
"starting go command", zap.Strings("command", cmd.Args), zap.Strings("env", cmd.Env),
)
if err := s.cmdRunner.RunCommand(cmd); err != nil {
s.log.Debug(
"build failed",
zap.Error(err), zap.Strings("cmd", cmd.Args), zap.Stringer("stderr", buff),
)
return formatBuildError(ctx, err, buff)
}
return nil
}
// CleanJobName implements' builder.Cleaner interface.
func (s BuildService) CleanJobName() string {
return "gocache"
}
// Clean implements' builder.Cleaner interface.
//
// Cleans go build and modules cache.
func (s BuildService) Clean(ctx context.Context) error {
if s.config.KeepGoModCache {
s.log.Info("go mod cache cleanup is disabled, skip")
return nil
}
cmd := newGoToolCommand(ctx, "clean", "-modcache", "-cache", "-testcache", "-fuzzcache")
cmd.Env = s.getEnvironmentVariables()
buff := &bytes.Buffer{}
cmd.Stderr = buff
if err := s.cmdRunner.RunCommand(cmd); err != nil {
return fmt.Errorf("process returned error: %s. Stderr: %s", err, buff.String())
}
return nil
}