Documentation ¶
Overview ¶
Package bb builds one busybox-like binary out of many Go command sources.
This allows you to take two Go commands, such as Go implementations of `sl` and `cowsay` and compile them into one binary, callable like `./bb sl` and `./bb cowsay`.
Which command is invoked is determined by `argv[0]` or `argv[1]` if `argv[0]` is not recognized.
Under the hood, bb implements a Go source-to-source transformation on pure Go code. This AST transformation does the following:
- Takes a Go command's source files and rewrites them into Go package files without global side effects.
- Writes a `main.go` file with a `main()` that calls into the appropriate Go command package based on `argv[0]`.
Principally, the AST transformation moves all global side-effects into callable package functions. E.g. `main` becomes `Main`, each `init` becomes `InitN`, and global variable assignments are moved into their own `InitN`.
Index ¶
- func BuildBusybox(env golang.Environ, pkgs []string, binaryPath string) error
- func CreateBBMainSource(fset *token.FileSet, astp *ast.Package, pkgs []string, destDir string) error
- func ParseAST(files []string) (*token.FileSet, *ast.Package, error)
- func RewritePackage(env golang.Environ, pkgPath, bbImportPath string, importer types.Importer) error
- func SrcFiles(p *build.Package) []string
- type Package
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildBusybox ¶
BuildBusybox builds a busybox of the given Go packages.
pkgs is a list of Go import paths. If nil is returned, binaryPath will hold the busybox-style binary.
func CreateBBMainSource ¶
func CreateBBMainSource(fset *token.FileSet, astp *ast.Package, pkgs []string, destDir string) error
CreateBBMainSource creates a bb Go command that imports all given pkgs.
p must be the bb template.
- For each pkg in pkgs, add import _ "pkg" to astp's first file.
- Write source file out to destDir.
func ParseAST ¶
ParseAST parses the given files for a package named main.
Only files with a matching package statement will be part of the AST returned.
func RewritePackage ¶
func RewritePackage(env golang.Environ, pkgPath, bbImportPath string, importer types.Importer) error
RewritePackage rewrites pkgPath to be bb-mode compatible, where destDir is the file system destination of the written files and bbImportPath is the Go import path of the bb package to register with.
Types ¶
type Package ¶
type Package struct { // Name is the command name. // // In the standard Go tool chain, this is usually the base name of the // directory containing its source files. Name string // contains filtered or unexported fields }
Package is a Go package.
It holds AST, type, file, and Go package information about a Go package.
func NewPackage ¶
func NewPackage(name string, pkgPath string, srcFiles []string, importer types.Importer) (*Package, error)
NewPackage gathers AST, type, and token information about a command.
The given importer is used to resolve dependencies.
Directories ¶
Path | Synopsis |
---|---|
Package bbmain is the package imported by all rewritten busybox command-packages to register themselves.
|
Package bbmain is the package imported by all rewritten busybox command-packages to register themselves. |
cmd
Package main is the busybox main.go template.
|
Package main is the busybox main.go template. |