gobuild

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2022 License: MIT Imports: 13 Imported by: 4

README

Go Build Tools

Go Report Card GitHub release (latest by date) PkgGoDev

Tools for building and distributing Go executables

Examples

Directly Use

example/purego

target := gobuild.Target{
    Source: "./example/payload",

    OutputName: fmt.Sprintf("payload-%s-%s-%s",
        gobuild.PlaceholderVersion,
        gobuild.PlaceholderOS,
        gobuild.PlaceholderArch),

    OutputPath:  "./output",
    CleanOutput: true,

    ExtraLdFlags: "-s -w",

    VersionPath: "main.Version",
    HashPath:    "main.Hash",

    Compress:  gobuild.CompressZip,
    Platforms: gobuild.PlatformCommon,
}

err := target.Build()
if err != nil {
    panic(err)
}
Config File

example/config

{
    "Go": "",
    "Source": "./example/payload",
    "OutputName": "payload-{Version}-{OS}-{Arch}",
    "OutputPath": "./output",
    "CleanOutput": true,
    "Cgo": false,
    "ExtraFlags": null,
    "ExtraLdFlags": "-s -w",
    "VersionPath": "main.Version",
    "HashPath": "main.Hash",
    "Compress": "auto",
    "PlatformShortcut": "common",
    "Platforms": [
        {
            "Arch": "riscv",
            "OS": "linux"
        }
    ]
}
target, err := gobuild.GetTargetFromJson("./config.json")
if err != nil {
    panic(err)
}
err = target.Build()
if err != nil {
    panic(err)
}
CGO
target = gobuild.Target{
    Platforms: []gobuild.Platform{
        {OS: gobuild.OSLinux, Arch: gobuild.ArchAmd64, CC: "gcc"},
        {OS: gobuild.OSWindows, Arch: gobuild.ArchAmd64, CC: "x86_64-w64-mingw32-gcc"},
    },
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PlatformWindows386   = Platform{OS: OSWindows, Arch: Arch386}
	PlatformWindowsAmd64 = Platform{OS: OSWindows, Arch: ArchAmd64}
	PlatformWindowsArm64 = Platform{OS: OSWindows, Arch: ArchArm64}
	PlatformWindowsArm5  = Platform{OS: OSWindows, Arch: ArchArm, GoArm: GoArm5}
	PlatformWindowsArm6  = Platform{OS: OSWindows, Arch: ArchArm, GoArm: GoArm6}
	PlatformWindowsArm7  = Platform{OS: OSWindows, Arch: ArchArm, GoArm: GoArm7}

	PlatformLinux386   = Platform{OS: OSLinux, Arch: Arch386}
	PlatformLinuxAmd64 = Platform{OS: OSLinux, Arch: ArchAmd64}
	PlatformLinuxArm64 = Platform{OS: OSLinux, Arch: ArchArm64}
	PlatformLinuxArm5  = Platform{OS: OSLinux, Arch: ArchArm, GoArm: GoArm5}
	PlatformLinuxArm6  = Platform{OS: OSLinux, Arch: ArchArm, GoArm: GoArm6}
	PlatformLinuxArm7  = Platform{OS: OSLinux, Arch: ArchArm, GoArm: GoArm7}

	PlatformDarwinAmd64     = Platform{OS: OSDarwin, Arch: ArchAmd64}
	PlatformDarwinArm64     = Platform{OS: OSDarwin, Arch: ArchArm64}
	PlatformDarwinUniversal = Platform{OS: OSDarwin, Arch: ArchUniversal}
)
View Source
var (
	// PlatformCommon includes the most used (~99.9%) platforms.
	//
	// Including OS: Windows, Linux, Darwin(MacOS).
	// Including Arch: 386, amd64, arm32(v5, v6, v7), arm64.
	PlatformCommon = []Platform{
		PlatformWindows386,
		PlatformWindowsAmd64,
		PlatformWindowsArm64,
		PlatformWindowsArm6,
		PlatformWindowsArm7,

		PlatformLinux386,
		PlatformLinuxAmd64,
		PlatformLinuxArm64,
		PlatformLinuxArm6,
		PlatformLinuxArm7,

		PlatformDarwinAmd64,
		PlatformDarwinArm64,
	}

	// PlatformNative contains the config for native platform
	PlatformNative = []Platform{
		{},
	}
)
View Source
var (
	OutputMode = 0755
)

Functions

func BuildMacOSApp added in v1.0.0

func BuildMacOSApp(output, name, exe, id, icon string, convertIcon bool) error

func Compress added in v1.1.0

func Compress(outputPath string, files map[string]string, format archiver.Archiver) error

Compress files to outputPath with format

func GetGitHash

func GetGitHash(path string) (string, error)

GetGitHash returns the current git commit hash, which is the result of `git rev-parse HEAD` run in `path`

func GetGitVersion

func GetGitVersion(path string) (string, error)

GetGitVersion returns the current git tag, which is the result of `git describe --tags` run in `path`

Types

type CompressType

type CompressType string

CompressType describe compress strategy.

const (
	// CompressRaw do not compress binaries, just copy them to output path.
	CompressRaw CompressType = "raw"

	// CompressTarGz compress all binaries into tar.gz format
	CompressTarGz CompressType = "tar.gz"

	// CompressZip compress all binaries into zip format
	CompressZip CompressType = "zip"
)

type Placeholder added in v0.2.0

type Placeholder string

Placeholder will be replaced when building.

const (
	// PlaceholderVersion will be replaced by output of `git describe --tags` on success
	PlaceholderVersion Placeholder = "{Version}"

	// PlaceholderArch will be replaced by GOARCH.
	PlaceholderArch Placeholder = "{Arch}"

	// PlaceholderOS will be replaced by GOOS.
	PlaceholderOS Placeholder = "{OS}"
)

type Platform

type Platform struct {
	// Arch sets GOARCH.
	Arch PlatformArch

	// OS sets GOOS.
	OS PlatformOS

	// GoArm sets GOARM when Arch is `arm`.
	GoArm PlatformGoArm

	// CC sets C compiler when cgo is enabled.
	CC string

	// Envs set extra environment variables
	Envs map[string]string
}

Platform set the target platform

type PlatformArch

type PlatformArch string

PlatformArch is all available GOARCH.

const (
	Arch386         PlatformArch = "386"
	ArchAmd64       PlatformArch = "amd64"
	ArchAmd64p32    PlatformArch = "amd64p32"
	ArchArm         PlatformArch = "arm"
	ArchArmbe       PlatformArch = "armbe"
	ArchArm64       PlatformArch = "arm64"
	ArchArm64be     PlatformArch = "arm64be"
	ArchPpc64       PlatformArch = "ppc64"
	ArchPpc64le     PlatformArch = "ppc64le"
	ArchMips        PlatformArch = "mips"
	ArchMipsle      PlatformArch = "mipsle"
	ArchMips64      PlatformArch = "mips64"
	ArchMips64le    PlatformArch = "mips64le"
	ArchMips64p32   PlatformArch = "mips64p32"
	ArchMips64p32le PlatformArch = "mips64p32le"
	ArchPpc         PlatformArch = "ppc"
	ArchRiscv       PlatformArch = "riscv"
	ArchRiscv64     PlatformArch = "riscv64"
	ArchS390        PlatformArch = "s390"
	ArchS390x       PlatformArch = "s390x"
	ArchSparc       PlatformArch = "sparc"
	ArchSparc64     PlatformArch = "sparc64"
	ArchWasm        PlatformArch = "wasm"
	ArchUniversal   PlatformArch = "universal"
)

type PlatformGoArm

type PlatformGoArm string

PlatformGoArm is all available GOARM.

const (
	GoArm5 PlatformGoArm = "5"
	GoArm6 PlatformGoArm = "6"
	GoArm7 PlatformGoArm = "7"
)

type PlatformOS

type PlatformOS string

PlatformOS is all available GOOS.

const (
	OSAix       PlatformOS = "aix"
	OSAndroid   PlatformOS = "android"
	OSDarwin    PlatformOS = "darwin"
	OSDragonfly PlatformOS = "dragonfly"
	OSFreebsd   PlatformOS = "freebsd"
	OSHurd      PlatformOS = "hurd"
	OSIllumos   PlatformOS = "illumos"
	OSIos       PlatformOS = "ios"
	OSJs        PlatformOS = "js"
	OSLinux     PlatformOS = "linux"
	OSNacl      PlatformOS = "nacl"
	OSNetbsd    PlatformOS = "netbsd"
	OSOpenbsd   PlatformOS = "openbsd"
	OSPlan9     PlatformOS = "plan9"
	OSSolaris   PlatformOS = "solaris"
	OSWindows   PlatformOS = "windows"
	OSZos       PlatformOS = "zos"
)

type PlatformShortcut added in v0.3.0

type PlatformShortcut string

PlatformShortcut is type of shortcuts for platform settings.

const (
	// PlatformShortcutCommon indicate append PlatformCommon into Target.Platforms.
	PlatformShortcutCommon PlatformShortcut = "common"

	// PlatformShortcutCommon indicate append PlatformNative into Target.Platforms.
	PlatformShortcutNative PlatformShortcut = "native"
)

type Target

type Target struct {
	// Go executeable path.
	//
	// If empty, `go` will be used.
	Go string

	// Source is directory of source main package.
	Source string

	// OutputName is the base name of output.
	//
	// It may contains `Placeholder`s, which will be replaced when building.
	OutputName string

	// OutputPath is the directory where all outputs will be stored.
	OutputPath string

	// If CleanOutput is true, all contents of OutputPath will be removed before building.
	CleanOutput bool

	// If Cgo is true, enabled cgo, and Platform.CC is used as C compiler.
	//
	// If false, cgo is disabled.
	Cgo bool

	// ExtraFlags to be passed to go compiler.
	ExtraFlags []string

	// ExtraLdFlags to be passed to loader.
	//
	// Example: to behave like `go build -ldflags "-s -w"`, this field
	// should be set to "-s -w".
	ExtraLdFlags string

	// VersionPath is the path of a variable of your source package
	// where you want it to be set to output of `git describe --tags` when building.
	//
	// Example: you have a variable `Version` in package `main`,
	// set VersionPath to `main.Version`, and Version will be set to your git tag.
	VersionPath string

	// HashPath is the path of a variable where you want it to be the current git hash.
	HashPath string

	// Compress set the compress methods.
	Compress CompressType

	// Platforms is the target platforms.
	Platforms []Platform

	// Envs set extra environment variables
	Envs map[string]string
	// contains filtered or unexported fields
}

Target describe build configs

func GetTargetFromJson added in v0.3.0

func GetTargetFromJson(path string) (*Target, error)

GetTargetFromJson parse json file as TargetConfig.

Modifies set by Shortcuts will be applied to TargetConfig.Target.

func (*Target) Build

func (t *Target) Build() error

Build the target

type TargetConfig added in v0.3.0

type TargetConfig struct {
	// Target is the real target.
	Target

	// PlatformShortcut is shortcut for platform settings
	PlatformShortcut PlatformShortcut
}

TargetConfig is used to parse configs.

Some shortcuts are added here, so you will not have to write many redundant data.

Directories

Path Synopsis
example
cgo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL