From 4b818a50d40f7be2266e8cdab00da99121c59f77 Mon Sep 17 00:00:00 2001 From: David Bariod Date: Tue, 10 Nov 2020 14:42:46 +0100 Subject: [PATCH] migrate cross build target from bash script to mage --- .travis.yml | 2 +- go.mod | 1 + go.sum | 2 ++ mage.go | 10 +++++++++ magefile.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 mage.go create mode 100644 magefile.go diff --git a/.travis.yml b/.travis.yml index 996f57a..57845ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ os: linux install: - ./travis/install.sh script: - - ./travis/cross_build.sh + - go run mage.go -v crossBuild - ./travis/lint.sh - export GOMAXPROCS=4 - export GORACE=halt_on_error=1 diff --git a/go.mod b/go.mod index b3919d5..37004ff 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ module github.com/sirupsen/logrus require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/magefile/mage v1.10.0 github.com/pmezard/go-difflib v1.0.0 // indirect github.com/stretchr/testify v1.2.2 golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 diff --git a/go.sum b/go.sum index 1edc143..bce26a1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g= +github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= diff --git a/mage.go b/mage.go new file mode 100644 index 0000000..4273031 --- /dev/null +++ b/mage.go @@ -0,0 +1,10 @@ +// +build ignore + +package main + +import ( + "github.com/magefile/mage/mage" + "os" +) + +func main() { os.Exit(mage.Main()) } diff --git a/magefile.go b/magefile.go new file mode 100644 index 0000000..78f9103 --- /dev/null +++ b/magefile.go @@ -0,0 +1,60 @@ +// +build mage + +package main + +import ( + "encoding/json" + "fmt" + + "github.com/magefile/mage/mg" + "github.com/magefile/mage/sh" +) + +// GenBuildMatrix regenerates the build matrix from the current version of the go compiler +func getBuildMatrix() (map[string][]string, error) { + jsonData, err := sh.Output("go", "tool", "dist", "list", "-json") + if err != nil { + return nil, err + } + var data []struct { + Goos string + Goarch string + } + if err := json.Unmarshal([]byte(jsonData), &data); err != nil { + return nil, err + } + + matrix := map[string][]string{} + for _, v := range data { + if val, ok := matrix[v.Goos]; ok { + matrix[v.Goos] = append(val, v.Goarch) + } else { + matrix[v.Goos] = []string{v.Goarch} + } + } + + return matrix, nil +} + +func CrossBuild() error { + matrix, err := getBuildMatrix() + if err != nil { + return err + } + + for os, arches := range matrix { + for _, arch := range arches { + env := map[string]string{ + "GOOS": os, + "GOARCH": arch, + } + if mg.Verbose() { + fmt.Printf("Building for GOOS=%s GOARCH=%s\n", os, arch) + } + if err := sh.RunWith(env, "go", "build", "./..."); err != nil { + return err + } + } + } + return nil +}