-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkodos.go
527 lines (458 loc) · 12.8 KB
/
kodos.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package kodos
import (
"fmt"
"go/build"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
)
// Context contains all build specific values.
type Context struct {
GOOS, GOARCH string
Workdir string
Pkgdir string
Bindir string
force bool // always force build, even if not stale
race bool // build a -race enabled binary
gcflags []string // -gcflags
ldflags []string // -ldflags
buildtags []string
}
func (c *Context) isCrossCompile() bool { return false }
func (c *Context) searchPaths() []string {
return []string{
c.Workdir,
c.Pkgdir,
}
}
// ctxString returns a string representation of the unique properties
// of the context.
func (c *Context) ctxString() string {
v := []string{
c.GOOS,
c.GOARCH,
}
v = append(v, c.buildtags...)
return strings.Join(v, "-")
}
// Package describes a set of Go files to be compiled.
type Package struct {
*Context
*build.Package
Imports []*Package
testScope bool // is a test scoped package
Main bool // this is a command
NotStale bool // this package _and_ all its dependencies are not stale
}
const debug = true
func debugf(format string, args ...interface{}) {
if !debug {
return
}
fmt.Fprintf(os.Stderr, format+"\n", args...)
}
// IsStale returns true if the source pkg is considered to be stale with
// respect to its installed version.
func (pkg *Package) IsStale() bool {
switch pkg.ImportPath {
case "C", "unsafe":
// synthetic packages are never stale
return false
}
if pkg.force {
return true
}
// tests are always stale, they are never installed
if pkg.testScope {
return true
}
// Package is stale if completely unbuilt.
var built time.Time
if fi, err := os.Stat(pkg.pkgpath()); err == nil {
built = fi.ModTime()
}
if built.IsZero() {
debugf("%s is missing", pkg.pkgpath())
return true
}
olderThan := func(file string) bool {
fi, err := os.Stat(file)
return err != nil || fi.ModTime().After(built)
}
newerThan := func(file string) bool {
fi, err := os.Stat(file)
return err != nil || fi.ModTime().Before(built)
}
// Package is stale if a dependency is newer.
for _, p := range pkg.Imports {
if p.Package.ImportPath == "C" || p.Package.ImportPath == "unsafe" {
continue // ignore stale imports of synthetic packages
}
if olderThan(p.pkgpath()) {
debugf("%s is older than %s", pkg.pkgpath(), p.pkgpath())
return true
}
}
// if the main package is up to date but _newer_ than the binary (which
// could have been removed), then consider it stale.
if pkg.Main && newerThan(pkg.Binfile()) {
debugf("%s is newer than %s", pkg.pkgpath(), pkg.Binfile())
return true
}
for _, src := range pkg.files() {
if olderThan(filepath.Join(pkg.Dir, src)) {
debugf("%s is older than %s", pkg.pkgpath(), filepath.Join(pkg.Dir, src))
return true
}
}
return false
}
// files returns all source files in scope
func (p *Package) files() []string {
return stringList(p.GoFiles)
}
// pkgpath returns the destination for object cached for this Package.
func (pkg *Package) pkgpath() string {
importpath := filepath.FromSlash(pkg.ImportPath) + ".a"
switch {
case pkg.isCrossCompile():
return filepath.Join(pkg.Pkgdir, importpath)
case pkg.race:
// race enabled standard lib
return filepath.Join(runtime.GOROOT(), "pkg", pkg.GOOS+"_"+pkg.GOARCH+"_race", importpath)
default:
return filepath.Join(pkg.Pkgdir, importpath)
}
}
// Binfile returns the destination of the compiled target of this command.
func (pkg *Package) Binfile() string {
// TODO(dfc) should have a check for package main, or should be merged in to objfile.
target := filepath.Join(pkg.Bindir, pkg.binname())
if pkg.testScope {
target = filepath.Join(pkg.Workdir, filepath.FromSlash(pkg.ImportPath), "_test", pkg.binname())
}
// if this is a cross compile or GOOS/GOARCH are both defined or there are build tags, add ctxString.
if pkg.isCrossCompile() || (os.Getenv("GOOS") != "" && os.Getenv("GOARCH") != "") {
target += "-" + pkg.ctxString()
} else if len(pkg.buildtags) > 0 {
target += "-" + strings.Join(pkg.buildtags, "-")
}
if pkg.GOOS == "windows" {
target += ".exe"
}
return target
}
func (pkg *Package) binname() string {
switch {
case pkg.testScope:
return pkg.name() + ".test"
case pkg.Main:
return filepath.Base(filepath.FromSlash(pkg.ImportPath))
default:
panic("binname called with non main package: " + pkg.ImportPath)
}
}
func (p *Package) complete() bool {
switch p.ImportPath {
case "bytes", "net", "os", "runtime/pprof", "sync", "time":
return false
default:
return len(p.SFiles) == 0 // no cgo or runtime code
}
}
func (p *Package) name() string { return filepath.FromSlash(p.ImportPath) }
func stringList(args ...[]string) []string {
var l []string
for _, arg := range args {
l = append(l, arg...)
}
return l
}
func (pkg *Package) Compile() error {
var gofiles []string
gofiles = append(gofiles, pkg.GoFiles...)
if len(gofiles) == 0 {
return fmt.Errorf("compile %q: no go files supplied", pkg.ImportPath)
}
ofiles := []string{pkg.objfile()}
run := func(dir, tool string, args ...string) error {
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "pkg", "tool", runtime.GOOS+"_"+runtime.GOARCH, tool), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = dir
fmt.Fprintf(os.Stderr, "+ %s\n", strings.Join(cmd.Args, " "))
return cmd.Run()
}
gc := func(pkg *Package) error {
args := append(pkg.gcflags, "-p", pkg.ImportPath)
args = append(args, "-o", ofiles[0])
for _, d := range pkg.searchPaths() {
args = append(args, "-I", d)
}
if pkg.ImportPath == "runtime" {
// runtime compiles with a special gc flag to emit
// additional reflect type data.
args = append(args, "-+")
}
switch {
case pkg.complete():
args = append(args, "-complete", "-pack")
default:
asmhdr := filepath.Join(filepath.Dir(pkg.objfile()), "go_asm.h")
args = append(args, "-asmhdr", asmhdr, "-pack")
}
return run(pkg.Dir, "compile", append(args, gofiles...)...)
}
asm := func(pkg *Package, ofile, sfile string) error {
ofiles = append(ofiles, ofile)
args := []string{"-o", ofile, "-D", "GOOS_" + runtime.GOOS, "-D", "GOARCH_" + runtime.GOARCH}
odir := filepath.Join(filepath.Dir(ofile))
includedir := filepath.Join(runtime.GOROOT(), "pkg", "include")
args = append(args, "-I", odir, "-I", includedir)
args = append(args, sfile)
return run(pkg.Dir, "asm", args...)
}
pack := func(pkg *Package, afiles ...string) error {
args := []string{"r"}
args = append(args, afiles...)
return run(pkg.Dir, "pack", args...)
}
if err := mkdir(filepath.Dir(pkg.pkgpath())); err != nil {
return err
}
if err := gc(pkg); err != nil {
return nil
}
for _, sfile := range pkg.SFiles {
if err := asm(pkg, filepath.Join(filepath.Dir(pkg.objfile()), strings.TrimSuffix(sfile, ".s")+".o"), sfile); err != nil {
return err
}
}
if len(ofiles) > 1 {
if err := pack(pkg, ofiles...); err != nil {
return err
}
}
return copyfile(pkg.installpath(), ofiles[0])
}
func (pkg *Package) Link() error {
// to ensure we don't write a partial binary, link the binary to a temporary file in
// in the target directory, then rename.
tmp, err := ioutil.TempFile(filepath.Dir(pkg.Binfile()), ".kang-link")
if err != nil {
return err
}
tmp.Close()
args := append(pkg.ldflags, "-o", tmp.Name())
for _, d := range pkg.searchPaths() {
args = append(args, "-L", d)
}
args = append(args, "-buildmode", "exe")
args = append(args, pkg.pkgpath())
cmd := exec.Command(filepath.Join(runtime.GOROOT(), "pkg", "tool", runtime.GOOS+"_"+runtime.GOARCH, "link"), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = pkg.Workdir
fmt.Fprintf(os.Stderr, "+ %s\n", strings.Join(cmd.Args, " "))
if err := cmd.Run(); err != nil {
os.Remove(tmp.Name()) // remove partial file
return err
}
if err := mkdir(filepath.Dir(pkg.Binfile())); err != nil {
os.Remove(tmp.Name()) // remove partial file
return err
}
return rename(tmp.Name(), pkg.Binfile())
}
// objfile returns the name of the object file for this package
func (pkg *Package) objfile() string {
return filepath.Join(pkg.Workdir, pkg.objname())
}
func (pkg *Package) objname() string {
return pkg.pkgname() + ".a"
}
func (pkg *Package) pkgname() string {
return filepath.Base(filepath.FromSlash(pkg.ImportPath))
}
// installpath returns the distination to cache this package's compiled .a file.
// pkgpath and installpath differ in that the former returns the location where you will find
// a previously cached .a file, the latter returns the location where an installed file
// will be placed.
//
// The difference is subtle. pkgpath must deal with the possibility that the file is from the
// standard library and is previously compiled. installpath will always return a path for the
// project's pkg/ directory in the case that the stdlib is out of date, or not compiled for
// a specific architecture.
func (pkg *Package) installpath() string {
if pkg.testScope {
panic("installpath called with test scope")
}
return filepath.Join(pkg.Pkgdir, filepath.FromSlash(pkg.ImportPath)+".a")
}
func mkdir(path string) error {
return os.MkdirAll(path, 0755)
}
func rename(from, to string) error {
fmt.Fprintf(os.Stderr, "+ mv %s %s\n", from, to)
return os.Rename(from, to)
}
// copyfile copies file to destination creating destination directory if needed
func copyfile(dst, src string) error {
err := mkdir(filepath.Dir(dst))
if err != nil {
return err
}
r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(dst)
if err != nil {
return err
}
defer w.Close()
fmt.Fprintln(os.Stderr, "+ cp", src, dst)
_, err = io.Copy(w, r)
return err
}
// Transform takes a slice of go/build.Package and returns the
// corresponding slice of kodos.Packages.
func (ctx *Context) Transform(v ...*build.Package) []*Package {
pkgs := transform(ctx, v...)
computeStale(pkgs...)
return pkgs
}
func transform(ctx *Context, v ...*build.Package) []*Package {
srcs := make(map[string]*build.Package)
for _, pkg := range v {
srcs[pkg.ImportPath] = pkg
}
seen := make(map[string]*Package)
var walk func(src *build.Package) *Package
walk = func(src *build.Package) *Package {
if pkg, ok := seen[src.ImportPath]; ok {
return pkg
}
// all binaries depend on runtime, even if they do not
// explicitly import it.
var deps []*Package
for _, i := range src.Imports {
pkg, ok := srcs[i]
if !ok {
panic(fmt.Sprintln("transform: pkg ", i, "is not loaded"))
}
deps = append(deps, walk(pkg))
}
pkg := &Package{
Context: ctx,
Package: src,
Main: src.Name == "main",
Imports: deps,
}
seen[src.ImportPath] = pkg
return pkg
}
var pkgs []*Package
for _, p := range v {
pkgs = append(pkgs, walk(p))
}
check := make(map[*Package]bool)
for _, p := range pkgs {
if check[p] {
panic(fmt.Sprintln(p.ImportPath, "present twice"))
}
check[p] = true
}
return pkgs
}
// computeStale sets the UpToDate flag on a set of package roots.
func computeStale(roots ...*Package) {
seen := make(map[*Package]bool)
var walk func(pkg *Package) bool
walk = func(pkg *Package) bool {
if seen[pkg] {
return pkg.NotStale
}
seen[pkg] = true
for _, i := range pkg.Imports {
if !walk(i) {
// a dep is stale so we are stale
return false
}
}
stale := pkg.IsStale()
pkg.NotStale = !stale
return !stale
}
for _, root := range roots {
walk(root)
}
}
func BuildPackages(pkgs ...*Package) (func() error, error) {
targets := make(map[string]func() error)
var deps []func() error
for _, pkg := range pkgs {
fn, err := buildPackage(targets, pkg)
if err != nil {
return nil, err
}
deps = append(deps, fn)
}
return func() error {
for _, fn := range deps {
if err := fn(); err != nil {
return err
}
}
return nil
}, nil
}
func buildPackage(targets map[string]func() error, pkg *Package) (func() error, error) {
// if this action is already present in the map, return an
// empty action
if fn, ok := targets[pkg.ImportPath]; ok {
return fn, nil
}
// step 0. are we stale ?
// if this package is not stale, then by definition none of its
// dependencies are stale, so ignore this whole tree.
if pkg.NotStale {
return func() error {
return nil
}, nil
}
// step 1. build dependencies
var deps []func() error
for _, pkg := range pkg.Imports {
fn, err := buildPackage(targets, pkg)
if err != nil {
return nil, err
}
deps = append(deps, fn)
}
// step 2. build this package
build := func() error {
for _, dep := range deps {
if err := dep(); err != nil {
return err
}
}
if err := pkg.Compile(); err != nil {
return err
}
if !pkg.Main {
return nil // we're done
}
return pkg.Link()
}
// record the final action as the action that represents
// building this package.
targets[pkg.ImportPath] = build
return build, nil
}