forked from lixiang12407/chubaofs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·332 lines (294 loc) · 9.74 KB
/
build.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
RootPath=$(cd $(dirname $0)/..; pwd)
BuildPath=${RootPath}/build
BuildOutPath=${BuildPath}/out
BuildBinPath=${BuildPath}/bin
VendorPath=${RootPath}/vendor
RM=$(find /bin /sbin /usr/bin /usr/local -name "rm" | head -1)
if [[ "-x$RM" == "-x" ]] ; then
RM=rm
fi
Version=$(git describe --abbrev=0 --tags 2>/dev/null)
BranchName=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
CommitID=$(git rev-parse HEAD 2>/dev/null)
BuildTime=$(date +%Y-%m-%d\ %H:%M)
LDFlags="-X github.com/chubaofs/chubaofs/proto.Version=${Version} \
-X github.com/chubaofs/chubaofs/proto.CommitID=${CommitID} \
-X github.com/chubaofs/chubaofs/proto.BranchName=${BranchName} \
-X 'github.com/chubaofs/chubaofs/proto.BuildTime=${BuildTime}'"
MODFLAGS=""
NPROC=$(nproc 2>/dev/null)
if [ -e /sys/fs/cgroup/cpu ] ; then
NPROC=4
fi
NPROC=${NPROC:-"1"}
GCC_LIBRARY_PATH="/lib /lib64 /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64"
cgo_cflags=""
cgo_ldflags="-lstdc++ -lm"
case $(uname -s | tr 'A-Z' 'a-z') in
"linux"|"darwin")
;;
*)
echo "Current platform $(uname -s) not support";
exit1;
;;
esac
CPUTYPE=${CPUTYPE} | tr 'A-Z' 'a-z'
build_zlib() {
ZlibSrcPath=${VendorPath}/dep/zlib-1.2.11
ZlibBuildPath=${BuildOutPath}/zlib
found=$(find ${ZlibBuildPath} -name libz.a 2>/dev/null | wc -l)
if [ ${found} -eq 0 ] ; then
if [ ! -d ${ZlibBuildPath} ] ; then
mkdir -p ${ZlibBuildPath}
cp -rf ${ZlibSrcPath}/* ${ZlibBuildPath}
fi
echo "build Zlib..."
pushd ${ZlibBuildPath} >/dev/null
[ "-$LUA_PATH" != "-" ] && unset LUA_PATH
./configure
make -j ${NPROC} && echo "build Zlib success" || { echo "build Zlib failed" ; exit 1; }
popd >/dev/null
fi
cgo_cflags="${cgo_cflags} -I${ZlibSrcPath}"
cgo_ldflags="${cgo_ldflags} -L${ZlibBuildPath} -lz"
LD_LIBRARY_PATH="${ZlibBuildPath}:${LD_LIBRARY_PATH}"
C_INCLUDE_PATH="${cgo_cflags}:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="${cgo_cflags}:$CPLUS_INCLUDE_PATH"
}
build_bzip2() {
Bzip2SrcPath=${VendorPath}/dep/bzip2-1.0.6
Bzip2BuildPath=${BuildOutPath}/bzip2
found=$(find ${Bzip2BuildPath} -name libz2.a 2>/dev/null | wc -l)
if [ ${found} -eq 0 ] ; then
if [ ! -d ${Bzip2BuildPath} ] ; then
mkdir -p ${Bzip2BuildPath}
cp -rf ${Bzip2SrcPath}/* ${Bzip2BuildPath}
fi
echo "build Bzip2..."
pushd ${Bzip2BuildPath} >/dev/null
[ "-$LUA_PATH" != "-" ] && unset LUA_PATH
make -j ${NPROC} bzip2 && echo "build Bzip2 success" || { echo "build Bzip2 failed" ; exit 1; }
popd >/dev/null
fi
cgo_cflags="${cgo_cflags} -I${Bzip2SrcPath}"
cgo_ldflags="${cgo_ldflags} -L${Bzip2BuildPath} -lbz2"
LD_LIBRARY_PATH="${Bzip2BuildPath}:${LD_LIBRARY_PATH}"
C_INCLUDE_PATH="${cgo_cflags}:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="${cgo_cflags}:$CPLUS_INCLUDE_PATH"
}
build_lz4() {
ZstdSrcPath=${VendorPath}/dep/lz4-1.9.2
ZstdBuildPath=${BuildOutPath}/lz4
found=$(find ${ZstdBuildPath}/lib -name liblz4.a 2>/dev/null | wc -l)
if [ ${found} -eq 0 ] ; then
if [ ! -d ${ZstdBuildPath} ] ; then
mkdir -p ${ZstdBuildPath}
cp -rf ${ZstdSrcPath}/* ${ZstdBuildPath}
fi
echo "build Zstd..."
pushd ${ZstdBuildPath} >/dev/null
[ "-$LUA_PATH" != "-" ] && unset LUA_PATH
make -j ${NPROC} && echo "build lz4 success" || { echo "build Zstd failed" ; exit 1; }
popd >/dev/null
fi
cgo_cflags="${cgo_cflags} -I${ZstdSrcPath}/programs"
cgo_ldflags="${cgo_ldflags} -L${ZstdBuildPath}/lib -llz4"
LD_LIBRARY_PATH="${ZstdBuildPath}/lib:${LD_LIBRARY_PATH}"
C_INCLUDE_PATH="${cgo_cflags}:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="${cgo_cflags}:$CPLUS_INCLUDE_PATH"
}
# dep zlib,bz2,lz4
build_zstd() {
ZstdSrcPath=${VendorPath}/dep/zstd-1.4.5
ZstdBuildPath=${BuildOutPath}/zstd
found=$(find ${ZstdBuildPath}/lib -name libzstd.a 2>/dev/null | wc -l)
if [ ${found} -eq 0 ] ; then
if [ ! -d ${ZstdBuildPath} ] ; then
mkdir -p ${ZstdBuildPath}
cp -rf ${ZstdSrcPath}/* ${ZstdBuildPath}
fi
echo "build Zstd..."
pushd ${ZstdBuildPath} >/dev/null
[ "-$LUA_PATH" != "-" ] && unset LUA_PATH
make -j ${NPROC} allzstd && echo "build Zstd success" || { echo "build Zstd failed" ; exit 1; }
popd >/dev/null
fi
cgo_cflags="${cgo_cflags} -I${ZstdSrcPath}/programs"
cgo_ldflags="${cgo_ldflags} -L${ZstdBuildPath}/lib -lzstd"
LD_LIBRARY_PATH="${ZstdBuildPath}/lib:${LD_LIBRARY_PATH}"
C_INCLUDE_PATH="${cgo_cflags}:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="${cgo_cflags}:$CPLUS_INCLUDE_PATH"
}
build_snappy() {
found=$(find ${GCC_LIBRARY_PATH} -name libsnappy.a -o -name libsnappy.so 2>/dev/null | wc -l)
if [ ${found} -gt 0 ] ; then
cgo_ldflags="${cgo_ldflags} -lsnappy"
return
fi
SnappySrcPath=${VendorPath}/snappy-1.1.7
SnappyBuildPath=${BuildOutPath}/snappy
found=$(find ${SnappyBuildPath} -name libsnappy.a 2>/dev/null | wc -l)
if [ ${found} -eq 0 ] ; then
mkdir -p ${SnappyBuildPath}
echo "build snappy..."
pushd ${SnappyBuildPath} >/dev/null
cmake ${SnappySrcPath} && make -j ${NPROC} && echo "build snappy success" || { echo "build snappy failed"; exit 1; }
popd >/dev/null
fi
cgo_cflags="${cgo_cflags} -I${SnappySrcPath}"
cgo_ldflags="${cgo_ldflags} -L${SnappyBuildPath} -lsnappy"
LD_LIBRARY_PATH="${SnappyBuildPath}:${LD_LIBRARY_PATH}"
C_INCLUDE_PATH="${cgo_cflags}:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="${cgo_cflags}:$CPLUS_INCLUDE_PATH"
}
build_rocksdb() {
found=$(find ${GCC_LIBRARY_PATH} -name librocksdb.a -o -name librocksdb.so 2>/dev/null | wc -l)
if [ ${found} -gt 0 ] ; then
cgo_ldflags="${cgo_ldflags} -lrocksdb"
return
fi
RocksdbSrcPath=${VendorPath}/rocksdb-5.9.2
RocksdbBuildPath=${BuildOutPath}/rocksdb
found=$(find ${RocksdbBuildPath} -name librocksdb.a 2>/dev/null | wc -l)
if [ ${found} -eq 0 ] ; then
if [ ! -d ${RocksdbBuildPath} ] ; then
mkdir -p ${RocksdbBuildPath}
cp -rf ${RocksdbSrcPath}/* ${RocksdbBuildPath}
fi
echo "build rocksdb..."
pushd ${RocksdbBuildPath} >/dev/null
[ "-$LUA_PATH" != "-" ] && unset LUA_PATH
make -j ${NPROC} static_lib && echo "build rocksdb success" || { echo "build rocksdb failed" ; exit 1; }
popd >/dev/null
fi
cgo_cflags="${cgo_cflags} -I${RocksdbSrcPath}/include"
cgo_ldflags="${cgo_ldflags} -L${RocksdbBuildPath} -lrocksdb"
LD_LIBRARY_PATH="${RocksdbBuildPath}:${LD_LIBRARY_PATH}"
C_INCLUDE_PATH="${cgo_cflags}:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="${cgo_cflags}:$CPLUS_INCLUDE_PATH"
}
init_gopath() {
export GO111MODULE=off
export GOPATH=$HOME/tmp/cfs/go
mkdir -p $GOPATH/src/github.com/chubaofs
SrcPath=$GOPATH/src/github.com/chubaofs/chubaofs
if [ -L "$SrcPath" ]; then
$RM -f $SrcPath
fi
if [ ! -e "$SrcPath" ] ; then
ln -s $RootPath $SrcPath 2>/dev/null
fi
}
pre_build_server() {
rocksdb_libs=( z bz2 lz4 zstd )
if [[ "$CPUTYPE" == arm64* ]];
then
build_zlib
build_bzip2
build_lz4
# build_zstd
else
for p in ${rocksdb_libs[*]} ; do
found=$(find /usr -name lib${p}.so 2>/dev/null | wc -l)
if [ ${found} -gt 0 ] ; then
cgo_ldflags="${cgo_ldflags} -l${p}"
fi
done
fi
build_snappy
build_rocksdb
export CGO_CFLAGS=${cgo_cflags}
export CGO_LDFLAGS="${cgo_ldflags}"
init_gopath
}
pre_build() {
export CGO_CFLAGS=""
export CGO_LDFLAGS=""
init_gopath
}
run_test() {
pre_build
pushd $SrcPath >/dev/null
go test ./...
ret=$?
popd >/dev/null
exit $ret
}
build_server() {
pre_build_server
pushd $SrcPath >/dev/null
echo -n "build cfs-server "
go build $MODFLAGS -ldflags "${LDFlags}" -o ${BuildBinPath}/cfs-server ${SrcPath}/cmd/*.go && echo "success" || echo "failed"
popd >/dev/null
}
build_client() {
pre_build
pushd $SrcPath >/dev/null
echo -n "build cfs-client "
go build $MODFLAGS -ldflags "${LDFlags}" -o ${BuildBinPath}/cfs-client ${SrcPath}/client/*.go && echo "success" || echo "failed"
popd >/dev/null
}
build_client2() {
pre_build
pushd $SrcPath >/dev/null
echo -n "build cfs-client2 "
go build $MODFLAGS -ldflags "${LDFlags}" -o ${BuildBinPath}/cfs-client2 ${SrcPath}/clientv2/*.go && echo "success" || echo "failed"
popd >/dev/null
}
build_authtool() {
pre_build
pushd $SrcPath >/dev/null
echo -n "build cfs-authtool "
go build $MODFLAGS -ldflags "${LDFlags}" -o ${BuildBinPath}/cfs-authtool ${SrcPath}/authtool/*.go && echo "success" || echo "failed"
popd >/dev/null
}
build_cli() {
#cli need gorocksdb too
pre_build_server
pushd $SrcPath >/dev/null
echo -n "build cfs-cli "
go build $MODFLAGS -ldflags "${LDFlags}" -o ${BuildBinPath}/cfs-cli ${SrcPath}/cli/*.go && echo "success" || echo "failed"
#sh cli/build.sh ${BuildBinPath}/cfs-cli && echo "success" || echo "failed"
popd >/dev/null
}
clean() {
$RM -rf ${BuildBinPath}
}
dist_clean() {
$RM -rf ${BuildBinPath}
$RM -rf ${BuildOutPath}
}
cmd=${1:-"all"}
case "$cmd" in
"all")
build_server
build_client
;;
"test")
run_test
;;
"server")
build_server
;;
"client")
build_client
;;
"client2")
build_client2
;;
"authtool")
build_authtool
;;
"cli")
build_cli
;;
"clean")
clean
;;
"dist_clean")
dist_clean
;;
*)
;;
esac