|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set +e +x |
3 | | -cat << EOF |
4 | | -GO Cross Platform Compilation |
5 | | -The valid combinations of \$GOOS and \$GOARCH are: |
6 | | -https://golang.org/doc/install/source#environment |
7 | | -https://golang.google.cn/doc/install/source#environment |
8 | | -
|
9 | | -EOF |
10 | | - |
11 | | -GOOS_GOARCH_array=( aix_ppc64 android_386 android_amd64 android_arm android_arm64 darwin_386 darwin_amd64 darwin_arm darwin_arm64 dragonfly_amd64 freebsd_386 freebsd_amd64 freebsd_arm illumos_amd64 js_wasm linux_386 linux_amd64 linux_arm linux_arm64 linux_ppc64 linux_ppc64le linux_mips linux_mipsle linux_mips64 linux_mips64le linux_s390x netbsd_386 netbsd_amd64 netbsd_arm openbsd_386 openbsd_amd64 openbsd_arm openbsd_arm64 plan9_386 plan9_amd64 plan9_arm solaris_amd64 windows_386 windows_amd64 ) |
12 | | -appname=`\basename $(\pwd)` |
13 | | -outputpath=./bin |
14 | | -package='' |
| 3 | +myversion='GO Cross Platform Compilation v2020.1.6' |
| 4 | +targets=(aix_ppc64 android_386 android_amd64 android_arm android_arm64 darwin_386 darwin_amd64 darwin_arm darwin_arm64 dragonfly_amd64 freebsd_386 freebsd_amd64 freebsd_arm illumos_amd64 js_wasm linux_386 linux_amd64 linux_arm linux_arm64 linux_ppc64 linux_ppc64le linux_mips linux_mipsle linux_mips64 linux_mips64le linux_s390x netbsd_386 netbsd_amd64 netbsd_arm openbsd_386 openbsd_amd64 openbsd_arm openbsd_arm64 plan9_386 plan9_amd64 plan9_arm solaris_amd64 windows_386 windows_amd64) |
| 5 | +appname=$(\basename $(\pwd)) |
| 6 | +outputpath=bin |
| 7 | +packages='' |
| 8 | +tags='' |
15 | 9 | declare -i verbose=0 |
| 10 | +declare -i nopause=0 |
| 11 | +declare -i succeeded=0 |
| 12 | +declare -i failed=0 |
16 | 13 |
|
17 | 14 | until [ $# -eq 0 ]; do |
18 | | - case "$1" in |
19 | | - --help|-h) |
20 | | - printf "usage: bash ${0##*/} [--verbose|-v] [--package|-p <package-name>] [--name|-n <app-name>] [--output|-o <output-path>]\n" |
21 | | - exit 0 |
22 | | - ;; |
23 | | - --verbose|-v) |
24 | | - verbose=1 |
25 | | - ;; |
26 | | - --package|-n) |
27 | | - shift |
28 | | - if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
29 | | - printf "ERROR: package argument error: $1\n" |
30 | | - exit 1 |
31 | | - fi |
32 | | - package=$1 |
33 | | - ;; |
34 | | - --name|-n) |
35 | | - shift |
36 | | - if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
37 | | - printf "ERROR: name argument error: $1\n" |
38 | | - exit 1 |
39 | | - fi |
40 | | - appname=$1 |
41 | | - ;; |
42 | | - --output|-o) |
43 | | - shift |
44 | | - if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
45 | | - printf "ERROR: output argument error: $1\n" |
46 | | - exit 1 |
47 | | - fi |
48 | | - outputpath=$1 |
49 | | - ;; |
50 | | - *) |
51 | | - printf "ERROR: arguments error. please run \"bash ${0##*/} --help\" to get usage\n" |
52 | | - exit 1 |
53 | | - ;; |
54 | | - esac |
| 15 | + case "$1" in |
| 16 | + --help | -h) |
| 17 | + cat <<EOF |
| 18 | +usage: bash ${0##*/} [options] [packages] |
| 19 | +
|
| 20 | +packages |
| 21 | + will append to "go build" command |
| 22 | +
|
| 23 | +options |
| 24 | + -v, --verbose print verbose text |
| 25 | + -n, --name <app-name> the name for format the executable file name |
| 26 | + like "name_os_arch", default is the directory name |
| 27 | + -o, --output <output-path> the output path, default is "bin" |
| 28 | + -t, --targets "OS_ARCH ..." the target OS_ARCH list. |
| 29 | + like "linux_amd64 darwin_amd64 windows_amd64 windows_386" |
| 30 | + default is all OS_ARCH |
| 31 | + from https://golang.org/doc/install/source#environment |
| 32 | + --tags tag,list the tags for "go build" command |
| 33 | + --no-pause do not pause when finish |
| 34 | + -h, --help display this help and exit |
| 35 | + --version display this script version and exit |
| 36 | +EOF |
| 37 | + exit 0 |
| 38 | + ;; |
| 39 | + --verbose | -v) |
| 40 | + verbose=1 |
| 41 | + ;; |
| 42 | + --name | -n) |
| 43 | + shift |
| 44 | + if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
| 45 | + printf "ERROR: name argument error: $1\n" |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + appname=$1 |
| 49 | + ;; |
| 50 | + --output | -o) |
| 51 | + shift |
| 52 | + if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
| 53 | + printf "ERROR: output argument error: $1\n" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | + outputpath=$1 |
| 57 | + ;; |
| 58 | + --targets | -t) |
| 59 | + shift |
| 60 | + if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
| 61 | + printf "ERROR: targets argument error: $1\n" |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + targets=() |
| 65 | + while IFS= read -r -d '' arg; do |
| 66 | + targets+=("$arg") |
| 67 | + done < <(xargs printf '%s\0' <<<"$1") |
| 68 | + ;; |
| 69 | + --tags) |
55 | 70 | shift |
| 71 | + if [ -z "$1" ] || [[ "$1" =~ ^\- ]]; then |
| 72 | + printf "ERROR: tags argument error: $1\n" |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | + tags="-tags $1" |
| 76 | + ;; |
| 77 | + --no-pause) |
| 78 | + nopause=1 |
| 79 | + ;; |
| 80 | + --version) |
| 81 | + printf "$myversion\n" |
| 82 | + exit 1 |
| 83 | + ;; |
| 84 | + --) |
| 85 | + shift |
| 86 | + packages="$@" |
| 87 | + break |
| 88 | + ;; |
| 89 | + -) |
| 90 | + printf "ERROR: arguments error. please run \"bash ${0##*/} --help\" to get usage\n" |
| 91 | + exit 1 |
| 92 | + ;; |
| 93 | + *) |
| 94 | + packages+=" $1" |
| 95 | + ;; |
| 96 | + esac |
| 97 | + shift |
56 | 98 | done |
57 | 99 |
|
58 | | -if [ -z "$appname" ]; then |
59 | | - printf "ERROR: please input an app-name by --name\n" |
| 100 | +if [ ! -z "$packages" ]; then |
| 101 | + declare -i err=0 |
| 102 | + while IFS= read -r -d '' arg; do |
| 103 | + if [ ! -e "$arg" ]; then |
| 104 | + let err+=1 |
| 105 | + printf "ERROR $err: cannot find the file $arg\n" |
| 106 | + fi |
| 107 | + done < <(xargs printf '%s\0' <<<"$packages") |
| 108 | + if [ $err -gt 0 ]; then |
60 | 109 | exit 2 |
| 110 | + fi |
| 111 | + # remove left 1 space, this space from first call packages+=" $1" |
| 112 | + packages=${packages:1} |
| 113 | +fi |
| 114 | + |
| 115 | +if [ -z "$appname" ]; then |
| 116 | + printf "ERROR: please input an app-name by --name\n" |
| 117 | + exit 2 |
61 | 118 | fi |
62 | 119 |
|
63 | 120 | if [ -z "$outputpath" ]; then |
64 | | - printf "ERROR: please input an output-path by --output\n" |
65 | | - exit 2 |
| 121 | + printf "ERROR: please input an output-path by --output\n" |
| 122 | + exit 2 |
66 | 123 | fi |
67 | 124 |
|
68 | 125 | mkdir -p "$outputpath" |
69 | 126 | if [ $? -ne 0 ]; then |
70 | | - printf "ERROR: create $outputpath failed: $?\n" |
71 | | - exit 2 |
| 127 | + printf "ERROR: create $outputpath failed: $?\n" |
| 128 | + exit 2 |
72 | 129 | fi |
73 | 130 |
|
74 | | -cat << EOF |
75 | | -Package: $package |
| 131 | +cat <<EOF |
| 132 | +$myversion |
| 133 | +The valid combinations of \$GOOS and \$GOARCH are: |
| 134 | +https://golang.org/doc/install/source#environment |
| 135 | +https://golang.google.cn/doc/install/source#environment |
| 136 | +
|
| 137 | +Packages: $packages |
76 | 138 | AppName: $appname |
77 | 139 | Output: $outputpath |
| 140 | +Targets: ${targets[@]} |
| 141 | +Tags: $tags |
78 | 142 | EOF |
79 | 143 |
|
80 | 144 | if [ ! -f go.mod ]; then |
81 | | - printf "\nCreating go.mod ...\n" |
82 | | - go mod init $appname |
83 | | - if [ $? -ne 0 ]; then |
84 | | - printf "Error: $?\n" |
85 | | - exit 2 |
86 | | - fi |
| 145 | + printf "\nCreating go.mod ...\n" |
| 146 | + go mod init $appname |
| 147 | + if [ $? -ne 0 ]; then |
| 148 | + printf "Error: $?\n" |
| 149 | + exit 2 |
| 150 | + fi |
87 | 151 | fi |
88 | 152 |
|
89 | | -declare -i count=${#GOOS_GOARCH_array[*]} |
| 153 | +declare -i count=${#targets[*]} |
90 | 154 | declare -i num=0 |
91 | | -for target in "${GOOS_GOARCH_array[@]}"; do |
92 | | - let num+=1 |
93 | | - output=$outputpath/${appname}_${target} |
94 | | - mygoos=${target%%_*} |
95 | | - mygoarch=${target##*_} |
96 | | - myverbose='' |
97 | | - mycgo='' |
98 | | - if [ $verbose -eq 1 ]; then |
99 | | - myverbose='-v' |
100 | | - fi |
101 | | - case "$mygoos" in |
102 | | - windows) |
103 | | - output="$output.exe" |
104 | | - ;; |
105 | | - android) |
106 | | - mycgo='CGO_ENABLED=1' |
107 | | - ;; |
108 | | - esac |
109 | | - printf "\n($num/$count) Building $output ...\n" |
110 | | - env GOOS=$mygoos GOARCH=$mygoarch `printf "$mycgo"` go build $myverbose -ldflags "-s -w" -o $output $package |
| 155 | +for target in "${targets[@]}"; do |
| 156 | + let num+=1 |
| 157 | + output=$outputpath/${appname}_${target} |
| 158 | + mygoos=${target%%_*} |
| 159 | + mygoarch=${target##*_} |
| 160 | + myverbose='' |
| 161 | + mycgo='' |
| 162 | + if [ $verbose -eq 1 ]; then |
| 163 | + myverbose='-v' |
| 164 | + fi |
| 165 | + case "$mygoos" in |
| 166 | + windows) |
| 167 | + output="$output.exe" |
| 168 | + ;; |
| 169 | + android) |
| 170 | + mycgo='CGO_ENABLED=1' |
| 171 | + ;; |
| 172 | + esac |
| 173 | + printf "\n($num/$count) Building $output ...\n" |
| 174 | + env GOOS=$mygoos GOARCH=$mygoarch $(printf "$mycgo") go build $myverbose $tags -ldflags "-s -w" -o $output $packages |
| 175 | + if [ $? -eq 0 ]; then |
| 176 | + let succeeded+=1 |
111 | 177 | file $output |
| 178 | + else |
| 179 | + let failed+=1 |
| 180 | + fi |
112 | 181 | done |
113 | 182 |
|
114 | | -if [ -t 1 ]; then |
115 | | - printf "\nDone. Press any key to exit..." |
116 | | - read -n1 |
117 | | - printf "\n" |
| 183 | +printf "\nDone. $succeeded succeeded, $failed failed, $(ls -1q $outputpath | wc -l | xargs echo) files in $outputpath\n" |
| 184 | +if [[ -t 1 && $nopause -eq 0 ]]; then |
| 185 | + printf 'Press any key to exit...' |
| 186 | + read -n1 |
| 187 | + printf "\n" |
118 | 188 | fi |
0 commit comments