gopkg.in/hugelgupf/u-root.v4@v4.0.0-20180831060141-1d761fb73d50/pkg/uroot/builder/builder.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  	"fmt"
     9  
    10  	"github.com/u-root/u-root/pkg/golang"
    11  	"github.com/u-root/u-root/pkg/uroot/initramfs"
    12  )
    13  
    14  var Builders = map[string]Builder{
    15  	"bb":     BBBuilder{},
    16  	"source": SourceBuilder{},
    17  	"binary": BinaryBuilder{},
    18  }
    19  
    20  // Opts are arguments to the Builder.Build function.
    21  type Opts struct {
    22  	// Env is the Go compiler environment.
    23  	Env golang.Environ
    24  
    25  	// Packages are the Go packages to compile.
    26  	//
    27  	// Only an explicit list of Go import paths is accepted.
    28  	//
    29  	// E.g. cmd/go or github.com/u-root/u-root/cmds/ls.
    30  	Packages []string
    31  
    32  	// TempDir is a temporary directory where the compilation mode compiled
    33  	// binaries can be placed.
    34  	//
    35  	// TempDir should contain no files.
    36  	TempDir string
    37  
    38  	// BinaryDir is the initramfs directory for built binaries.
    39  	//
    40  	// BinaryDir must be specified.
    41  	BinaryDir string
    42  }
    43  
    44  // Builder builds Go packages and adds the binaries to an initramfs.
    45  //
    46  // The resulting files need not be binaries per se, but exec'ing the resulting
    47  // file should result in the Go program being executed.
    48  type Builder interface {
    49  	// Build uses the given options to build Go packages and adds its files
    50  	// to be included in the initramfs to the given ArchiveFiles.
    51  	Build(initramfs.Files, Opts) error
    52  
    53  	// DefaultBinaryDir is the initramfs' default directory for binaries
    54  	// built using this builder.
    55  	DefaultBinaryDir() string
    56  }
    57  
    58  // GetBuilder returns the Build function for the named build mode.
    59  func GetBuilder(name string) (Builder, error) {
    60  	build, ok := Builders[name]
    61  	if !ok {
    62  		return nil, fmt.Errorf("couldn't find builder %q", name)
    63  	}
    64  	return build, nil
    65  }