-
Notifications
You must be signed in to change notification settings - Fork 0
/
mud.go
320 lines (273 loc) · 6.74 KB
/
mud.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
package main
import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"os"
slashpath "path"
"path/filepath"
"regexp"
"sort"
"strings"
"text/template"
"github.com/mutable/archive"
"github.com/mutable/base32"
"github.com/mutable/tempfile"
"go.uber.org/multierr"
"golang.org/x/tools/go/packages"
)
var tmpl = template.Must(template.New("external").Parse(`
# generator //tools/mud (DO NOT EDIT)
{ platform, pkgs, ... }:
platform.buildGo.external rec {
path = "{{.Path}}";
src = platform.lib.fetchGoModule {
inherit path;
version = "{{.Version}}";
sha256 = "{{.ModSHA256}}";
};
{{- with .Imports}}
deps = with platform.third_party; [
{{- range .}}
gopkgs.{{.NixAttr}}
{{- end}}
];
{{- end}}
}
`[1:]))
func main() {
if len(os.Args) > 1 {
fmt.Fprintln(os.Stderr, "mud takes no arguments")
os.Exit(1)
}
if _, err := os.Stat(".git"); os.IsNotExist(err) {
fmt.Fprintln(os.Stderr, "mud must be run from the repository root")
os.Exit(1)
}
roots := []string{"./..."}
{
pkgs, err := packages.Load(&packages.Config{
Mode: 0 |
packages.NeedName |
packages.NeedImports,
BuildFlags: []string{"-tags", "tools"},
}, "./tools")
if err != nil {
panic(err)
}
for _, pkg := range pkgs {
for dep := range pkg.Imports {
roots = append(roots, dep)
}
}
}
pkgs, err := packages.Load(&packages.Config{
Mode: 0 |
packages.NeedName |
packages.NeedDeps |
packages.NeedImports |
packages.NeedModule,
Tests: true,
}, roots...)
if err != nil {
panic(err)
}
// for each module, figure out what dependencies it has
// NOTE: these aren't necessarily *complete* dependencies,
// since we are just walking the packages we're transitively using,
// rather than $MODULE/...
modules := make(map[Path]*Module)
packages.Visit(pkgs,
func(pkg *packages.Package) bool {
return !isBuiltin(pkg)
},
func(pkg *packages.Package) {
if isBuiltin(pkg) {
return
}
if err := pkgErrors(pkg); err != nil {
panic(err)
}
if pkg.Module == nil {
if pkg.Name == "main" && strings.HasSuffix(pkg.PkgPath, ".test") {
return // test packages show up twice, once without pkg.Module set
}
if strings.HasSuffix(pkg.PkgPath, "_test") {
// _test packages don't have pkg.Module set
// their main package ends in .test instead
return
}
panic(fmt.Errorf("package without a module: %s", pkg.PkgPath))
}
mod := modules[Path(pkg.Module.Path)]
if mod == nil {
mod = &Module{
Path: Path(pkg.Module.Path),
Version: pkg.Module.Version,
Dir: pkg.Module.Dir,
Deps: make(map[*Module]PackageSet),
}
if pkg.Module.Replace != nil {
mod.ReplacePath = pkg.Module.Replace.Path
mod.Version = pkg.Module.Replace.Version
}
mod.Version = strings.TrimPrefix(mod.Version, "v")
modules[mod.Path] = mod
}
for _, dep := range pkg.Imports {
if isBuiltin(dep) {
continue
}
depMod := modules[Path(dep.Module.Path)]
if depMod.Path == mod.Path {
continue // ignore intra-module dependencies
}
depSet := mod.Dep(depMod)
depSet.Add(Path(dep.PkgPath))
}
},
)
var paths []Path
for path := range modules {
paths = append(paths, path)
}
sortPaths(paths)
var buffer bytes.Buffer
for _, path := range paths {
mod := modules[path]
if !mod.IsExternal() {
continue
}
buffer.Reset()
outDir := slashpath.Join("third_party/gopkgs", string(mod.Path))
if mod.ReplacePath != "" && mod.ReplacePath[0] == '.' {
if mod.ReplacePath != "./"+outDir {
fmt.Fprintf(os.Stderr, "replace points at //%v, expected it to point at //%v\n", mod.ReplacePath, outDir)
os.Exit(1)
}
// vendored packages don't use buildGo.external,
// so we don't generate a manifest for them.
// they are expected to have their own buildGo expressions,
// like any other in-tree code.
continue
}
if err := tmpl.Execute(&buffer, mod); err != nil {
panic(err)
}
if err := writeFile(outDir, "default.nix", buffer.Bytes()); err != nil {
panic(err)
}
}
}
func writeFile(dir, name string, data []byte) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
f, err := tempfile.Open(dir, name+".tmp", 0644)
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write(data); err != nil {
return err
}
// TODO(edef): this ought to use unix.Unlink,
// but that's a bit more caution and effort than a non-library function warrants
if err := os.Remove(f.Name()); err != nil && !os.IsNotExist(err) {
return err
}
if err := tempfile.Commit(f); err != nil {
return err
}
return os.Rename(f.Name(), filepath.Join(dir, name))
}
func pkgErrors(pkg *packages.Package) error {
var errs error
for _, err := range pkg.Errors {
multierr.AppendInto(&errs, err)
}
if pkg.Module != nil && pkg.Module.Error != nil {
multierr.AppendInto(&errs, errors.New(pkg.Module.Error.Err))
}
return errs
}
func isBuiltin(pkg *packages.Package) bool {
importPath := pkg.PkgPath
i := strings.IndexByte(importPath, '/')
if i != -1 {
importPath = importPath[:i]
}
return strings.IndexByte(importPath, '.') == -1
}
var nixIdentRe = regexp.MustCompile(`^[a-zA-Z\_][a-zA-Z0-9\_\'\-]*$`)
var nixKeyword = map[string]bool{
"if": true,
"then": true,
"else": true,
"assert": true,
"with": true,
"let": true,
"in": true,
"rec": true,
"inherit": true,
"or": true,
}
type Path string
func (p Path) NixAttr() string {
names := strings.Split(string(p), "/")
for i, name := range names {
if !nixIdentRe.MatchString(name) || nixKeyword[name] {
names[i] = fmt.Sprintf("%q", name)
}
}
return strings.Join(names, ".")
}
type Module struct {
Path Path
Version string
Dir string
// If not empty, the path that this module's source is located at in the repo
ReplacePath string
// Deps maps modules we depend on to the exact packages
// from that module we depend on
Deps map[*Module]PackageSet
}
func (m *Module) Imports() []Path {
var imports []Path
for _, dep := range m.Deps {
for pkg := range dep {
imports = append(imports, pkg)
}
}
sortPaths(imports)
return imports
}
func (m *Module) Dep(d *Module) PackageSet {
pkgs := m.Deps[d]
if pkgs == nil {
pkgs = make(PackageSet)
m.Deps[d] = pkgs
}
return pkgs
}
func (m *Module) ModSHA256() string {
if m.Dir == "" {
panic(fmt.Errorf("module without a dir: %s", m.Path))
}
h := sha256.New()
if err := archive.CopyPath(archive.WriteDump(h), m.Dir); err != nil {
panic(err)
}
return base32.Encode(h.Sum(nil))
}
func (m *Module) IsExternal() bool {
return !strings.HasPrefix(string(m.Path), "example.com/")
}
type PackageSet map[Path]struct{}
func (s PackageSet) Add(p Path) {
s[p] = struct{}{}
}
func sortPaths(xs []Path) {
sort.Slice(xs, func(i, j int) bool { return xs[i] < xs[j] })
}