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