github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/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/uroot/initramfs"
    12  )
    13  
    14  // BinaryBuilder builds each Go command as a separate binary.
    15  //
    16  // BinaryBuilder is an implementation of Builder.
    17  type BinaryBuilder struct{}
    18  
    19  // DefaultBinaryDir implements Builder.DefaultBinaryDir.
    20  //
    21  // "bin" is the default initramfs binary directory for these binaries.
    22  func (BinaryBuilder) DefaultBinaryDir() string {
    23  	return "bin"
    24  }
    25  
    26  // Build implements Builder.Build.
    27  func (BinaryBuilder) Build(af *initramfs.Files, opts Opts) error {
    28  	result := make(chan error, len(opts.Packages))
    29  	var wg sync.WaitGroup
    30  
    31  	for _, pkg := range opts.Packages {
    32  		wg.Add(1)
    33  		go func(p string) {
    34  			defer wg.Done()
    35  			result <- opts.Env.Build(
    36  				p, filepath.Join(opts.TempDir, opts.BinaryDir, filepath.Base(p)))
    37  		}(pkg)
    38  	}
    39  
    40  	wg.Wait()
    41  	close(result)
    42  
    43  	for err := range result {
    44  		if err != nil {
    45  			return err
    46  		}
    47  	}
    48  
    49  	// Add bin directory to archive.
    50  	return af.AddFile(opts.TempDir, "")
    51  }