forked from apache/kvrocks-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
114 lines (105 loc) · 3.28 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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
set -e
BUILDER_IMAGE=${1:-none}
if test -z "$TARGET_OS"; then
uname_S=`uname -s`
case "$uname_S" in
Darwin)
TARGET_OS=darwin
;;
OpenBSD)
TARGET_OS=openbsd
;;
DragonFly)
TARGET_OS=dragonfly
;;
FreeBSD)
TARGET_OS=freebsd
;;
NetBSD)
TARGET_OS=netbsd
;;
SunOS)
TARGET_OS=solaris
;;
*)
TARGET_OS=linux
;;
esac
fi
if test -z "$TARGET_ARCH"; then
uname_M=`uname -m`
case "$uname_M" in
x86_64)
TARGET_ARCH=amd64
;;
arm32)
TARGET_ARCH=arm32
;;
arm64)
TARGET_ARCH=arm64
;;
ppc64*)
TARGET_ARCH=ppc64
;;
aarch64)
TARGET_ARCH=arm
;;
i386)
TARGET_ARCH=386
;;
*)
TARGET_ARCH=amd64
;;
esac
fi
GO_PROJECT=github.com/apache/kvrocks-controller
BUILD_DIR=./_build
VERSION=`cat VERSION.txt`
BUILD_DATE=`date -u +'%Y-%m-%dT%H:%M:%SZ'`
GIT_REVISION=`git rev-parse --short HEAD`
SERVER_TARGET_NAME=kvctl-server
CLIENT_TARGET_NAME=kvctl
for TARGET_NAME in "$SERVER_TARGET_NAME" "$CLIENT_TARGET_NAME"; do
if [[ "$TARGET_NAME" == "$SERVER_TARGET_NAME" ]]; then
CMD_PATH="${GO_PROJECT}/cmd/server"
else
CMD_PATH="${GO_PROJECT}/cmd/client"
fi
if [[ "$BUILDER_IMAGE" == "none" ]]; then
GOOS="$TARGET_OS" GOARCH="$TARGET_ARCH" CGO_ENABLED=0 go build -v -ldflags \
"-X $GO_PROJECT/version.Version=$VERSION -X $GO_PROJECT/version.BuildDate=$BUILD_DATE -X $GO_PROJECT/version.BuildCommit=$GIT_REVISION" \
-o ${TARGET_NAME} ${CMD_PATH}
else
docker run --rm --privileged -it -v $(pwd):/${TARGET_NAME} -w /${TARGET_NAME} \
-e GOOS="$TARGET_OS" -e GOARCH="$TARGET_ARCH" -e CGO_ENABLED=0 \
$BUILDER_IMAGE go build -v -ldflags \
"-X $GO_PROJECT/version.Version=$VERSION -X $GO_PROJECT/version.BuildDate=$BUILD_DATE -X $GO_PROJECT/version.BuildCommit=$GIT_REVISION" \
-o /${TARGET_NAME}/${TARGET_NAME} ${CMD_PATH}
fi
if [[ $? -ne 0 ]]; then
echo "Failed to build $TARGET_NAME"
exit 1
fi
echo "Build $TARGET_NAME, OS is $TARGET_OS, Arch is $TARGET_ARCH"
done
rm -rf ${BUILD_DIR}
mkdir -p ${BUILD_DIR}
mv $SERVER_TARGET_NAME ${BUILD_DIR}
mv $CLIENT_TARGET_NAME ${BUILD_DIR}