You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
autorestic/build/build.go

87 lines
1.8 KiB

4 years ago
// Heavily inspired (copied) by the restic build file
// https://github.com/restic/restic/blob/aa0faa8c7d7800b6ba7b11164fa2d3683f7f78aa/helpers/build-release-binaries/main.go#L225
package main
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
4 years ago
"sync"
4 years ago
"github.com/cupcakearmy/autorestic/internal"
)
var DIR, _ = filepath.Abs("./dist")
var targets = map[string][]string{
// "aix": {"ppc64"}, // Not supported by fsnotify
"darwin": {"amd64", "arm64"},
4 years ago
"freebsd": {"386", "amd64", "arm"},
"linux": {"386", "amd64", "arm", "arm64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "s390x"},
4 years ago
"netbsd": {"386", "amd64"},
"openbsd": {"386", "amd64"},
// "windows": {"386", "amd64"}, // Not supported by autorestic
"solaris": {"amd64"},
4 years ago
}
type buildOptions struct {
Target, Arch, Version string
}
4 years ago
func build(options buildOptions, wg *sync.WaitGroup) {
4 years ago
fmt.Printf("Building %s %s\n", options.Target, options.Arch)
out := fmt.Sprintf("autorestic_%s_%s_%s", options.Version, options.Target, options.Arch)
out = path.Join(DIR, out)
out, _ = filepath.Abs(out)
fmt.Println(out)
// Build
{
c := exec.Command("go", "build", "-o", out, "./main.go")
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Env = append(os.Environ(),
"CGO_ENABLED=0",
"GOOS="+options.Target,
"GOARCH="+options.Arch,
)
err := c.Run()
if err != nil {
4 years ago
panic(err)
4 years ago
}
}
// Compress
{
c := exec.Command("bzip2", out)
c.Dir = DIR
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
4 years ago
panic(err)
4 years ago
}
}
4 years ago
wg.Done()
4 years ago
}
func main() {
os.RemoveAll(DIR)
v := internal.VERSION
4 years ago
var wg sync.WaitGroup
4 years ago
for target, archs := range targets {
for _, arch := range archs {
4 years ago
wg.Add(1)
build(buildOptions{
4 years ago
Target: target,
Arch: arch,
Version: v,
4 years ago
}, &wg)
4 years ago
}
}
4 years ago
wg.Wait()
4 years ago
}