-
Notifications
You must be signed in to change notification settings - Fork 1
/
bootstrap.go
86 lines (69 loc) · 1.97 KB
/
bootstrap.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
package hbuiltin
import (
"github.com/hephbuild/heph/buildfiles"
"github.com/hephbuild/heph/config"
"github.com/hephbuild/heph/hroot"
"github.com/hephbuild/heph/packages"
"github.com/hephbuild/heph/specs"
"github.com/hephbuild/heph/utils"
"github.com/hephbuild/heph/utils/hash"
"go.starlark.net/starlark"
)
func buildConfigUniverse(cfg *config.Config) starlark.StringDict {
cfgd := &starlark.Dict{}
cfgd.SetKey(starlark.String("version"), starlark.String(cfg.Version.String))
profiles := starlark.NewList(nil)
for _, profile := range cfg.Profiles {
profiles.Append(starlark.String(profile))
}
cfgd.SetKey(starlark.String("profiles"), profiles)
for k, v := range cfg.Extras {
sv := utils.FromGo(v)
cfgd.SetKey(starlark.String(k), sv)
}
cfgd.Freeze()
return starlark.StringDict{
"CONFIG": cfgd,
}
}
func Bootstrap(opts Opts) buildfiles.RunOptions {
configUniverse := buildConfigUniverse(opts.Config)
predeclaredGlobalsOnce(configUniverse)
universe := predeclared(predeclaredGlobals, configUniverse)
return buildfiles.RunOptions{
ThreadModifier: func(thread *starlark.Thread, pkg *packages.Package) {
thread.SetLocal(optsKey, opts)
thread.SetLocal(pkgKey, pkg)
},
UniverseFactory: func() starlark.StringDict {
return universe
},
CacheDirPath: opts.Root.Home.Join("__BUILD").Abs(),
ProgramExtraHash: func(h hash.Hash) {
h.String(predeclaredHash)
},
RootPkg: opts.Pkgs.GetOrCreate(packages.Package{
Path: "",
Root: opts.Root.Root,
}),
}
}
const optsKey = "__heph_opts"
type Opts struct {
Pkgs *packages.Registry
Root *hroot.State
Config *config.Config
RegisterTarget func(t specs.Target) error
}
func getOpts(thread *starlark.Thread) Opts {
pkg := thread.Local(optsKey).(Opts)
return pkg
}
const pkgKey = "__heph_pkg"
func getPackage(thread *starlark.Thread) *packages.Package {
pkg := thread.Local(pkgKey).(*packages.Package)
if pkg == nil {
panic("pkg is nil, not supposed to happen")
}
return pkg
}