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