gopkg.in/hugelgupf/u-root.v4@v4.0.0-20180831060141-1d761fb73d50/pkg/uroot/builder/binary.go (about)

     1  // Copyright 2015-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package builder
     6  
     7  import (
     8  	"path/filepath"
     9  	"sync"
    10  
    11  	"github.com/u-root/u-root/pkg/golang"
    12  	"github.com/u-root/u-root/pkg/uroot/initramfs"
    13  )
    14  
    15  type BinaryBuilder struct{}
    16  
    17  func (BinaryBuilder) DefaultBinaryDir() string {
    18  	return "bin"
    19  }
    20  
    21  // Build builds all given packages as separate binaries and includes them in
    22  // the archive.
    23  func (BinaryBuilder) Build(af initramfs.Files, opts Opts) error {
    24  	result := make(chan error, len(opts.Packages))
    25  	var wg sync.WaitGroup
    26  
    27  	for _, pkg := range opts.Packages {
    28  		wg.Add(1)
    29  		go func(p string) {
    30  			defer wg.Done()
    31  			result <- opts.Env.Build(
    32  				p,
    33  				filepath.Join(opts.TempDir, opts.BinaryDir, filepath.Base(p)),
    34  				golang.BuildOpts{})
    35  		}(pkg)
    36  	}
    37  
    38  	wg.Wait()
    39  	close(result)
    40  
    41  	for err := range result {
    42  		if err != nil {
    43  			return err
    44  		}
    45  	}
    46  
    47  	// Add bin directory to archive.
    48  	return af.AddFile(opts.TempDir, "")
    49  }