autorestic/build/build.go

133 lines
3.1 KiB
Go
Raw Permalink Normal View History

2021-04-11 11:03:47 +00: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 (
2023-03-05 16:22:03 +00:00
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
2021-04-11 11:03:47 +00:00
"fmt"
2023-03-05 16:22:03 +00:00
"io/ioutil"
2021-04-11 11:03:47 +00:00
"os"
"os/exec"
"path"
"path/filepath"
2021-04-26 11:29:26 +00:00
"sync"
2021-04-11 11:03:47 +00:00
"github.com/cupcakearmy/autorestic/internal"
)
var DIR, _ = filepath.Abs("./dist")
var targets = map[string][]string{
// "aix": {"ppc64"}, // Not supported by fsnotify
2021-04-26 11:15:58 +00:00
"darwin": {"amd64", "arm64"},
2021-04-11 11:03:47 +00:00
"freebsd": {"386", "amd64", "arm"},
"linux": {"386", "amd64", "arm", "arm64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "s390x"},
2021-04-11 11:03:47 +00:00
"netbsd": {"386", "amd64"},
"openbsd": {"386", "amd64"},
// "windows": {"386", "amd64"}, // Not supported by autorestic
"solaris": {"amd64"},
2021-04-11 11:03:47 +00:00
}
type buildOptions struct {
Target, Arch, Version string
}
2023-03-05 16:22:03 +00:00
const (
CHECKSUM_MD5 = "MD5SUMS"
CHECKSUM_SHA_1 = "SHA1SUMS"
CHECKSUM_SHA_256 = "SHA256SUMS"
)
type Checksums struct {
filename, md5, sha1, sha256 string
}
func writeChecksums(checksums *[]Checksums) {
FILE_MD5, _ := os.OpenFile(path.Join(DIR, CHECKSUM_MD5), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
defer FILE_MD5.Close()
FILE_SHA1, _ := os.OpenFile(path.Join(DIR, CHECKSUM_SHA_1), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
defer FILE_SHA1.Close()
FILE_SHA256, _ := os.OpenFile(path.Join(DIR, CHECKSUM_SHA_256), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
defer FILE_SHA256.Close()
for _, checksum := range *checksums {
fmt.Fprintf(FILE_MD5, "%s %s\n", checksum.md5, checksum.filename)
fmt.Fprintf(FILE_SHA1, "%s %s\n", checksum.sha1, checksum.filename)
fmt.Fprintf(FILE_SHA256, "%s %s\n", checksum.sha256, checksum.filename)
}
}
func build(options buildOptions, wg *sync.WaitGroup, checksums *[]Checksums) {
defer wg.Done()
fmt.Printf("Building: %s %s\n", options.Target, options.Arch)
2021-04-11 11:03:47 +00:00
out := fmt.Sprintf("autorestic_%s_%s_%s", options.Version, options.Target, options.Arch)
out = path.Join(DIR, 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 11:29:26 +00:00
panic(err)
2021-04-11 11:03:47 +00: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 11:29:26 +00:00
panic(err)
2021-04-11 11:03:47 +00:00
}
}
2023-03-05 16:22:03 +00:00
// Checksum
{
file := out + ".bz2"
content, _ := ioutil.ReadFile(file)
*checksums = append(*checksums, Checksums{
filename: path.Base(file),
md5: fmt.Sprintf("%x", md5.Sum(content)),
sha1: fmt.Sprintf("%x", sha1.Sum(content)),
sha256: fmt.Sprintf("%x", sha256.Sum256(content)),
})
}
fmt.Printf("Built: %s\n", path.Base(out))
2021-04-11 11:03:47 +00:00
}
func main() {
os.RemoveAll(DIR)
v := internal.VERSION
2023-03-05 16:22:03 +00:00
checksums := []Checksums{}
// Build async
2021-04-26 11:29:26 +00:00
var wg sync.WaitGroup
2021-04-11 11:03:47 +00:00
for target, archs := range targets {
for _, arch := range archs {
2021-04-26 11:29:26 +00:00
wg.Add(1)
2023-03-05 16:22:03 +00:00
go build(buildOptions{
2021-04-11 11:03:47 +00:00
Target: target,
Arch: arch,
Version: v,
2023-03-05 16:22:03 +00:00
}, &wg, &checksums)
2021-04-11 11:03:47 +00:00
}
}
2021-04-26 11:29:26 +00:00
wg.Wait()
2023-03-05 16:22:03 +00:00
writeChecksums(&checksums)
2021-04-11 11:03:47 +00:00
}