autorestic/build/build.go

84 lines
1.6 KiB
Go
Raw Normal View History

2021-04-11 13:03:47 +02:00
// 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"
2021-04-26 13:29:26 +02:00
"sync"
2021-04-11 13:03:47 +02:00
"github.com/cupcakearmy/autorestic/internal"
)
var DIR, _ = filepath.Abs("./dist")
var targets = map[string][]string{
2021-04-26 13:15:58 +02:00
"darwin": {"amd64", "arm64"},
2021-04-11 13:03:47 +02:00
"freebsd": {"386", "amd64", "arm"},
"linux": {"386", "amd64", "arm", "arm64"},
"netbsd": {"386", "amd64"},
"openbsd": {"386", "amd64"},
}
type buildOptions struct {
Target, Arch, Version string
}
2021-04-26 13:29:26 +02:00
func build(options buildOptions, wg *sync.WaitGroup) {
2021-04-11 13:03:47 +02:00
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 {
2021-04-26 13:29:26 +02:00
panic(err)
2021-04-11 13:03:47 +02:00
}
}
// Compress
{
c := exec.Command("bzip2", out)
c.Dir = DIR
c.Stdout = os.Stdout
c.Stderr = os.Stderr
err := c.Run()
if err != nil {
2021-04-26 13:29:26 +02:00
panic(err)
2021-04-11 13:03:47 +02:00
}
}
2021-04-26 13:29:26 +02:00
wg.Done()
2021-04-11 13:03:47 +02:00
}
func main() {
os.RemoveAll(DIR)
v := internal.VERSION
2021-04-26 13:29:26 +02:00
var wg sync.WaitGroup
2021-04-11 13:03:47 +02:00
for target, archs := range targets {
for _, arch := range archs {
2021-04-26 13:29:26 +02:00
wg.Add(1)
build(buildOptions{
2021-04-11 13:03:47 +02:00
Target: target,
Arch: arch,
Version: v,
2021-04-26 13:29:26 +02:00
}, &wg)
2021-04-11 13:03:47 +02:00
}
}
2021-04-26 13:29:26 +02:00
wg.Wait()
2021-04-11 13:03:47 +02:00
}