-
Notifications
You must be signed in to change notification settings - Fork 1
/
target_spec.go
411 lines (335 loc) · 6.97 KB
/
target_spec.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package specs
import (
"encoding/json"
"github.com/hephbuild/heph/exprs"
"github.com/hephbuild/heph/packages"
"github.com/hephbuild/heph/utils/ads"
"github.com/hephbuild/heph/utils/maps"
"github.com/hephbuild/heph/utils/xsync"
"go.starlark.net/syntax"
"os/exec"
"strings"
"time"
)
var (
FileEnvIgnore = "ignore"
FileEnvRelRoot = "rel_root"
FileEnvRelPkg = "rel_pkg"
FileEnvAbs = "abs"
FileEnvValues = []string{FileEnvIgnore, FileEnvRelRoot, FileEnvRelPkg, FileEnvAbs}
)
var (
HashFileContent = "content"
HashFileModTime = "mod_time"
HashFileValues = []string{HashFileContent, HashFileModTime}
)
var (
EntrypointExec = "exec"
EntrypointBash = "bash"
EntrypointSh = "sh"
EntrypointValues = []string{EntrypointExec, EntrypointSh, EntrypointBash}
)
var (
CodegenNone = ""
CodegenLink = "link"
CodegenCopy = "copy"
CodegenCopyNoExclude = "copy_noexclude"
CodegenValues = []string{CodegenNone, CodegenLink, CodegenCopy, CodegenCopyNoExclude}
)
type SrcEnv struct {
Default string
Named map[string]string
}
func (e SrcEnv) Get(name string) string {
if v, ok := e.Named[name]; ok {
return v
}
return e.Default
}
const SupportFilesOutput = "@support_files"
func SortOutputsForHashing(names []string) []string {
names = ads.Copy(names)
ads.SortFunc(names, func(a, b string) bool {
if a == SupportFilesOutput {
return true
}
return strings.Compare(a, b) < 0
})
return names
}
type Targets []Target
func (ts Targets) Addrs() []string {
return ads.Map(ts, func(t Target) string {
return t.Addr
})
}
func (ts Targets) Get(addr string) (Target, bool) {
for _, t := range ts {
if t.Addr == addr {
return t, true
}
}
return Target{}, false
}
type GenSource struct {
Addr string
Sources []Source
}
type Target struct {
Name string
Addr string
Package *packages.Package
Doc string
GenSources []string
Run []string
FileContent []byte // Used by special target `text_file`
Entrypoint string
Platforms []Platform
ConcurrentExecution bool
Dir string
PassArgs bool
Deps Deps
HashDeps Deps
RuntimeDeps Deps
Tools Tools
Out []OutFile
Cache Cache
RestoreCache RestoreCache
HasSupportFiles bool
Sandbox bool
OutInSandbox bool
Codegen string
Labels []string
Env map[string]string
PassEnv []string
RuntimePassEnv []string
RunInCwd bool
Gen []Matcher
Sources []Source
RuntimeEnv map[string]string
SrcEnv SrcEnv
OutEnv string
HashFile string
Transitive Transitive
Timeout time.Duration
GenDepsMeta bool
Annotations map[string]interface{}
Requests map[string]float64
}
func (t Target) MarshalJSON() ([]byte, error) {
type MTarget Target
type mtarget struct {
MTarget
Private bool
}
return json.Marshal(mtarget{
MTarget: MTarget(t),
Private: t.IsPrivate(),
})
}
type Specer interface {
Spec() Target
}
func (t Target) Spec() Target {
return t
}
func AsSpecers[T Specer](a []T) []Specer {
return ads.Map(a, func(t T) Specer {
return t
})
}
type Source struct {
CallFrames []SourceCallFrame
}
func (s Source) SourceLocation() string {
if len(s.CallFrames) < 1 {
return ""
}
return s.CallFrames[0].Pos.String()
}
func (s Source) SourceFile() string {
if len(s.CallFrames) < 1 {
return ""
}
return s.CallFrames[0].Pos.Filename()
}
type SourceCallFrame struct {
Name string
Pos SourceCallFramePosition
}
func (s SourceCallFrame) String() string {
return s.Name + " " + s.Pos.String()
}
type SourceCallFramePosition struct {
syntax.Position
}
func (u *SourceCallFramePosition) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
File string
Line int32
}{
File: u.Filename(),
Line: u.Line,
})
}
type Platform struct {
Labels map[string]string
Options map[string]interface{}
Default bool
}
type Transitive struct {
Deps Deps
HashDeps Deps
RuntimeDeps Deps
Tools Tools
Env map[string]string
PassEnv []string
RuntimePassEnv []string
RuntimeEnv map[string]string
Platforms []Platform
}
func (t Target) IsPrivate() bool {
return strings.HasPrefix(t.Name, "_")
}
func (t Target) AddrStruct() TargetAddr {
return TargetAddr{
Package: t.Package.Path,
Name: t.Name,
}
}
func (t Target) IsGroup() bool {
return len(t.Run) == 1 && t.Run[0] == "group"
}
func (t Target) IsTool() bool {
return len(t.Run) >= 1 && t.Run[0] == "heph_tool"
}
func (t Target) IsGen() bool {
return len(t.Gen) > 0
}
func (t Target) IsTextFile() bool {
return len(t.Run) == 2 && t.Run[0] == "text_file"
}
func (t Target) IsExecutable() bool {
return !t.IsGroup() && !t.IsTextFile()
}
func (t Target) HasDefaultPlatforms() bool {
return len(t.Platforms) == 1 && t.Platforms[0].Default
}
func (t Target) Json() []byte {
t.Package = nil
t.Sources = nil
b, err := json.Marshal(t)
if err != nil {
panic(err)
}
return b
}
func (t Target) Equal(spec Target) bool {
return t.equalStruct(spec)
}
func (t Target) equalJson(spec Target) bool {
tj := t.Json()
sj := spec.Json()
if len(tj) != len(sj) {
return false
}
for i := 0; i < len(tj); i++ {
if tj[i] != sj[i] {
return false
}
}
return true
}
func (t Target) SourceLocation() string {
if len(t.Sources) < 1 {
return ""
}
return t.Sources[0].SourceLocation()
}
type TargetTool struct {
Name string
Target string
Output string
}
type ExprTool struct {
Name string
Expr exprs.Expr
Output string
}
type HostTool struct {
Name string
BinName string
Path string
}
var binCache = maps.Map[string, xsync.Once[string]]{}
func (t HostTool) ResolvedPath() (string, error) {
if t.Path != "" {
return t.Path, nil
}
if t.BinName == "heph" {
panic("heph should be handled separately")
}
once := binCache.Get(t.BinName)
return once.Do(func() (string, error) {
return exec.LookPath(t.BinName)
})
}
type Deps struct {
Targets []DepTarget
Files []DepFile
Exprs []DepExpr
}
type Tools struct {
Targets []TargetTool
Hosts []HostTool
Exprs []ExprTool
}
type DepMode string
const (
DepModeCopy DepMode = "copy"
DepModeLink DepMode = "link"
)
var DepModes = []DepMode{
DepModeCopy,
DepModeLink,
}
type DepTarget struct {
Name string
Output string
Target string
Mode DepMode
}
type DepExpr struct {
Name string
Expr exprs.Expr
}
type DepFile struct {
Name string
Path string
}
type OutFile struct {
Name string
Path string
}
type Cache struct {
Enabled bool
Named []string
History int
}
func (c Cache) NamedEnabled(name string) bool {
if c.Named == nil {
return true
}
for _, n := range c.Named {
if n == name {
return true
}
}
return false
}
type RestoreCache struct {
Enabled bool
Key string
Paths []string
Env string
}