Debian Codegraph @

ci/magefile.go

raw symbols SCIP
1 //go:build mage
2
3 package main
4
5 import (
6 "encoding/json"
7 "fmt"
8 "os"
9 "path"
10 "sort"
11
12 "github.com/magefile/mage/mg"
13 "github.com/magefile/mage/sh"
14 )
15
16 func intersect(a, b []string) []string {
17 sort.Strings(a)
18 sort.Strings(b)
19
20 res := make([]string, 0, func() int {
21 if len(a) < len(b) {
22 return len(a)
23 }
24 return len(b)
25 }())
26
27 for _, v := range a {
28 idx := sort.SearchStrings(b, v)
29 if idx < len(b) && b[idx] == v {
30 res = append(res, v)
31 }
32 }
33 return res
34 }
35
36 // getBuildMatrix returns the build matrix from the current version of the go compiler
37 func getFullBuildMatrix() (map[string][]string, error) {
38 jsonData, err := sh.Output("go", "tool", "dist", "list", "-json")
39 if err != nil {
40 return nil, err
41 }
42 var data []struct {
43 Goos string
44 Goarch string
45 }
46 if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
47 return nil, err
48 }
49
50 matrix := map[string][]string{}
51 for _, v := range data {
52 if val, ok := matrix[v.Goos]; ok {
53 matrix[v.Goos] = append(val, v.Goarch)
54 } else {
55 matrix[v.Goos] = []string{v.Goarch}
56 }
57 }
58
59 return matrix, nil
60 }
61
62 func getBuildMatrix() (map[string][]string, error) {
63 minimalMatrix := map[string][]string{
64 "linux": []string{"amd64"},
65 "darwin": []string{"amd64", "arm64"},
66 "freebsd": []string{"amd64"},
67 "js": []string{"wasm"},
68 "solaris": []string{"amd64"},
69 "windows": []string{"amd64", "arm64"},
70 }
71
72 fullMatrix, err := getFullBuildMatrix()
73 if err != nil {
74 return nil, err
75 }
76
77 for os, arches := range minimalMatrix {
78 if fullV, ok := fullMatrix[os]; !ok {
79 delete(minimalMatrix, os)
80 } else {
81 minimalMatrix[os] = intersect(arches, fullV)
82 }
83 }
84 return minimalMatrix, nil
85 }
86
87 func CrossBuild() error {
88 matrix, err := getBuildMatrix()
89 if err != nil {
90 return err
91 }
92
93 for os, arches := range matrix {
94 for _, arch := range arches {
95 env := map[string]string{
96 "GOOS": os,
97 "GOARCH": arch,
98 }
99 if mg.Verbose() {
100 fmt.Printf("Building for GOOS=%s GOARCH=%s\n", os, arch)
101 }
102 if err := sh.RunWith(env, "go", "build", "./..."); err != nil {
103 return err
104 }
105 }
106 }
107 return nil
108 }
109
110 func Lint() error {
111 gopath := os.Getenv("GOPATH")
112 if gopath == "" {
113 return fmt.Errorf("cannot retrieve GOPATH")
114 }
115
116 return sh.Run(path.Join(gopath, "bin", "golangci-lint"), "run", "./...")
117 }
118
119 // Run the test suite
120 func Test() error {
121 return sh.RunWith(map[string]string{"GORACE": "halt_on_error=1"},
122 "go", "test", "-race", "-v", "./...")
123 }