forked from u-root/u-root
/
binary.go
49 lines (39 loc) · 1.01 KB
/
binary.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
// Copyright 2015-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package builder
import (
"path/filepath"
"sync"
"github.com/u-root/u-root/pkg/golang"
"github.com/u-root/u-root/pkg/uroot/initramfs"
)
type BinaryBuilder struct{}
func (BinaryBuilder) DefaultBinaryDir() string {
return "bin"
}
// Build builds all given packages as separate binaries and includes them in
// the archive.
func (BinaryBuilder) Build(af initramfs.Files, opts Opts) error {
result := make(chan error, len(opts.Packages))
var wg sync.WaitGroup
for _, pkg := range opts.Packages {
wg.Add(1)
go func(p string) {
defer wg.Done()
result <- opts.Env.Build(
p,
filepath.Join(opts.TempDir, opts.BinaryDir, filepath.Base(p)),
golang.BuildOpts{})
}(pkg)
}
wg.Wait()
close(result)
for err := range result {
if err != nil {
return err
}
}
// Add bin directory to archive.
return af.AddFile(opts.TempDir, "")
}