github.com/ratrocket/u-root@v0.0.0-20180201221235-1cf9f48ee2cf/pkg/uroot/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 uroot
     6  
     7  import (
     8  	"path/filepath"
     9  	"sync"
    10  
    11  	"github.com/u-root/u-root/pkg/golang"
    12  )
    13  
    14  // BinaryBuild builds all given packages as separate binaries and includes them
    15  // in the archive.
    16  func BinaryBuild(opts BuildOpts) (ArchiveFiles, error) {
    17  	af := NewArchiveFiles()
    18  
    19  	result := make(chan error, len(opts.Packages))
    20  	var wg sync.WaitGroup
    21  
    22  	for _, pkg := range opts.Packages {
    23  		wg.Add(1)
    24  		go func(p string) {
    25  			defer wg.Done()
    26  			result <- opts.Env.Build(
    27  				p,
    28  				filepath.Join(opts.TempDir, "bin", filepath.Base(p)),
    29  				golang.BuildOpts{})
    30  		}(pkg)
    31  	}
    32  
    33  	wg.Wait()
    34  	close(result)
    35  
    36  	for err := range result {
    37  		if err != nil {
    38  			return ArchiveFiles{}, err
    39  		}
    40  	}
    41  
    42  	// Add bin directory to archive.
    43  	if err := af.AddFile(opts.TempDir, ""); err != nil {
    44  		return ArchiveFiles{}, err
    45  	}
    46  	return af, nil
    47  }