forked from xo/usql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-release.sh
executable file
·169 lines (151 loc) · 2.99 KB
/
build-release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
set -e
SRC=$(realpath $(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd))
VER=
BUILD=$SRC/build
STATIC=0
FORCE=0
OPTIND=1
while getopts "b:v:sfr" opt; do
case "$opt" in
b) BUILD=$OPTARG ;;
v) VER=$OPTARG ;;
s) STATIC=1 ;;
f) FORCE=1 ;;
r)
# get latest tag version
pushd $SRC &> /dev/null
VER=$(git tag -l|grep -E '^v[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?$'|sort -r -V|head -1||:)
popd &> /dev/null
;;
esac
done
# neither -v or -r specified, set FORCE and VER
if [ "$VER" = "" ]; then
VER=0.0.0-dev
FORCE=1
fi
PLATFORM=$(uname|sed -e 's/_.*//'|tr '[:upper:]' '[:lower:]'|sed -e 's/^\(msys\|mingw\).*/windows/')
ARCH=amd64
NAME=$(basename $SRC)
VER="${VER#v}"
EXT=tar.bz2
DIR=$BUILD/$PLATFORM/$VER
BIN=$DIR/$NAME
TAGS=(
most
sqlite_app_armor
sqlite_fts5
sqlite_introspect
sqlite_json1
sqlite_stat4
sqlite_userauth
sqlite_vtable
)
case $PLATFORM in
darwin|linux)
TAGS+=(no_adodb)
;;
windows)
EXT=zip
BIN=$BIN.exe
;;
esac
OUT=$DIR/$NAME-$VER-$PLATFORM-$ARCH.$EXT
LDFLAGS=(
-s
-w
-X github.com/xo/usql/text.CommandName=$NAME
-X github.com/xo/usql/text.CommandVersion=$VER
)
if [ "$STATIC" = "1" ]; then
OUT=$DIR/${NAME}_static-$VER-$PLATFORM-$ARCH.$EXT
BIN=$DIR/${NAME}_static
case $PLATFORM in
linux)
TAGS+=(
netgo
osusergo
)
EXTLDFLAGS=(
-static
-lm
-ldl
)
EXTLDFLAGS="${EXTLDFLAGS[@]}"
LDFLAGS+=(
-linkmode=external
-extldflags \'$EXTLDFLAGS\'
-extld g++
)
;;
*)
echo "error: fully static builds not currently supported for $PLATFORM"
exit 1
;;
esac
fi
# check not overwriting existing build artifacts
if [[ -e $OUT && "$FORCE" != "1" ]]; then
echo "error: $OUT exists and FORCE!=1 (try $0 -f)"
exit 1
fi
TAGS="${TAGS[@]}"
LDFLAGS="${LDFLAGS[@]}"
log() {
cat - | while read -r message; do
echo "$1$message"
done
}
echo "APP: $NAME/${VER} ($PLATFORM/$ARCH)"
if [ "$STATIC" = "1" ]; then
echo "STATIC: yes"
fi
echo "BUILD TAGS: $TAGS"
echo "LDFLAGS: $LDFLAGS"
pushd $SRC &> /dev/null
if [ -f $OUT ]; then
echo "REMOVING: $OUT"
rm -rf $OUT
fi
mkdir -p $DIR
echo "BUILDING: $BIN"
# build
echo "BUILD:"
(set -x;
go build \
-tags="$TAGS" \
-ldflags="$LDFLAGS" \
-o $BIN
) 2>&1 | log ' '
# strip
case $PLATFORM in
linux|windows|darwin)
echo "STRIPPING: $BIN"
strip $BIN
;;
esac
# compress
case $PLATFORM in
linux|windows|darwin)
COMPRESSED=$(upx -q -q $BIN|awk '{print $1 " -> " $3 " (" $4 ")"}')
echo "COMPRESSED: $COMPRESSED"
;;
esac
# check build
BUILT_VER=$($BIN --version)
if [ "$BUILT_VER" != "$NAME ${VER#v}" ]; then
echo -e "\n\nerror: expected $NAME --version to report '$NAME ${VER#v}', got: '$BUILT_VER'"
exit 1
fi
echo "REPORTED: $BUILT_VER"
case $EXT in
tar.bz2)
tar -C $DIR -cjf $OUT $(basename $BIN)
;;
zip)
zip $OUT -j $BIN
;;
esac
echo "PACKED: $OUT ($(du -sh $OUT|awk '{print $1}'))"
popd &> /dev/null