|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | | -default_combinations="linux,amd64 |
4 | | - darwin,amd64 |
5 | | - windows,amd64" |
6 | | - |
7 | | -all_combinations="$default_combinations |
8 | | - linux,386 |
9 | | - linux,arm64 |
10 | | - linux,mips64 |
11 | | - linux,mips64le |
12 | | - linux,mips |
13 | | - linux,mipsle |
14 | | - windows,386" |
| 3 | +# Get a list of apps to build by looking at the directories in cmd. |
| 4 | +mapfile -t apps < <(find cmd -maxdepth 1 -and -not -name cmd | sed -e 's#^cmd/##' | sort) |
| 5 | + |
| 6 | +default_combinations=( |
| 7 | + linux-amd64 |
| 8 | + darwin-amd64 |
| 9 | + windows-amd64 |
| 10 | +) |
| 11 | + |
| 12 | +all_combinations=("${default_combinations[@]}") |
| 13 | +all_combinations+=( |
| 14 | + linux-386 |
| 15 | + linux-arm64 |
| 16 | + linux-mips64 |
| 17 | + linux-mips64le |
| 18 | + linux-mips |
| 19 | + linux-mipsle |
| 20 | + windows-386 |
| 21 | +) |
15 | 22 |
|
16 | 23 | case "${1:-}" in |
17 | | - "") combinations="$default_combinations" ;; |
18 | | - default) combinations="$default_combinations" ;; |
19 | | - all) combinations="$all_combinations" ;; |
20 | | - *) echo "Invalid: $1" ; exit 1 ;; |
| 24 | + "") |
| 25 | + combinations=("${default_combinations[@]}") |
| 26 | + ;; |
| 27 | + default) |
| 28 | + combinations=("${default_combinations[@]}") |
| 29 | + ;; |
| 30 | + all) |
| 31 | + combinations=("${all_combinations[@]}") |
| 32 | + ;; |
| 33 | + *) |
| 34 | + echo "Invalid: $1" |
| 35 | + exit 1 |
| 36 | + ;; |
21 | 37 | esac |
22 | 38 |
|
23 | | -version="${DRONE_TAG:-$(git describe --tags 2>/dev/null)}" |
24 | | -version_hash="$(echo "${CI_COMMIT_SHA:-$(git rev-parse HEAD)}" | cut -b1-8)" |
25 | | -ldflags="-X main.CLIVersion=$version -X main.CLIVersionHash=$version_hash" |
26 | | - |
27 | | -pidsfile="$(mktemp)" |
28 | | -for combo in $combinations ; do |
29 | | - goos="$(echo "$combo" | cut -f1 -d,)" |
30 | | - goarch="$(echo "$combo" | cut -f2 -d,)" |
31 | | - case "$goos" in |
32 | | - windows) o="cmd/vega/vega-$goos-$goarch.exe" ;; |
33 | | - *) o="cmd/vega/vega-$goos-$goarch" ;; |
| 39 | +build_app() { |
| 40 | + local app |
| 41 | + app="${1:?}" |
| 42 | + |
| 43 | + local combo goarch goos ldflags o pidsfile |
| 44 | + |
| 45 | + case "$app" in |
| 46 | + vega) |
| 47 | + version="${DRONE_TAG:-$(git describe --tags 2>/dev/null)}" |
| 48 | + version_hash="$(echo "${CI_COMMIT_SHA:-$(git rev-parse HEAD)}" | cut -b1-8)" |
| 49 | + ldflags="-X main.CLIVersion=$version -X main.CLIVersionHash=$version_hash" |
| 50 | + ;; |
| 51 | + *) |
| 52 | + ldflags="" |
| 53 | + ;; |
34 | 54 | esac |
35 | | - env \ |
36 | | - CGO_ENABLED=0 \ |
37 | | - GOOS="$goos" \ |
38 | | - GOARCH="$goarch" \ |
39 | | - go build -o "$o" -ldflags "$ldflags" ./cmd/vega & |
40 | | - pid="$!" |
41 | | - echo "Building for OS=$goos arch=$goarch in subprocess $pid" |
42 | | - echo "$pid" >>"$pidsfile" |
43 | | -done |
44 | 55 |
|
45 | | -final=0 |
46 | | -while read -r pid ; do |
47 | | - echo -n "Waiting for subprocess $pid ..." |
48 | | - wait "$pid" |
49 | | - code="$?" |
50 | | - if test "$code" = 0 ; then |
51 | | - echo "OK" |
52 | | - else |
53 | | - echo "code $code" |
54 | | - final="$((final+1))" |
| 56 | + pidsfile="$(mktemp)" |
| 57 | + for combo in "${combinations[@]}" ; do |
| 58 | + goos="$(echo "$combo" | cut -f1 -d-)" |
| 59 | + goarch="$(echo "$combo" | cut -f2 -d-)" |
| 60 | + case "$goos" in |
| 61 | + windows) o="cmd/$app/$app-$goos-$goarch.exe" ;; |
| 62 | + *) o="cmd/$app/$app-$goos-$goarch" ;; |
| 63 | + esac |
| 64 | + env \ |
| 65 | + CGO_ENABLED=0 \ |
| 66 | + GOOS="$goos" \ |
| 67 | + GOARCH="$goarch" \ |
| 68 | + go build -o "$o" -ldflags "$ldflags" "./cmd/$app" & |
| 69 | + pid="$!" |
| 70 | + echo "Building $app for OS=$goos arch=$goarch in subprocess $pid" |
| 71 | + echo "$pid" >>"$pidsfile" |
| 72 | + done |
| 73 | + |
| 74 | + final=0 |
| 75 | + while read -r pid ; do |
| 76 | + echo -n "Waiting for subprocess $pid ..." |
| 77 | + wait "$pid" |
| 78 | + code="$?" |
| 79 | + if test "$code" = 0 ; then |
| 80 | + echo "OK" |
| 81 | + else |
| 82 | + echo "code $code" |
| 83 | + final="$((final+1))" |
| 84 | + fi |
| 85 | + done <"$pidsfile" |
| 86 | + rm -f "$pidsfile" |
| 87 | + |
| 88 | + if test "$final" -gt 0 ; then |
| 89 | + echo "Failed to build $app" |
| 90 | + exit "$final" |
55 | 91 | fi |
56 | | -done <"$pidsfile" |
57 | | -rm -f "$pidsfile" |
| 92 | +} |
58 | 93 |
|
59 | | -exit "$final" |
| 94 | +for app in "${apps[@]}" ; do |
| 95 | + build_app "$app" |
| 96 | +done |
0 commit comments