Files
EdgeNode/build/build.sh

153 lines
3.8 KiB
Bash
Raw Normal View History

2020-09-09 18:53:53 +08:00
#!/usr/bin/env bash
function build() {
2020-10-14 14:46:34 +08:00
ROOT=$(dirname $0)
NAME="edge-node"
2022-06-29 15:42:50 +08:00
VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
2020-10-14 14:46:34 +08:00
DIST=$ROOT/"../dist/${NAME}"
2021-12-01 14:24:56 +08:00
MUSL_DIR="/usr/local/opt/musl-cross/bin"
2022-01-09 20:12:59 +08:00
GCC_X86_64_DIR="/usr/local/Cellar/x86_64-unknown-linux-gnu/10.3.0/bin"
GCC_ARM64_DIR="/usr/local/Cellar/aarch64-unknown-linux-gnu/10.3.0/bin"
2020-10-14 14:46:34 +08:00
OS=${1}
ARCH=${2}
2021-07-25 16:28:36 +08:00
TAG=${3}
2020-10-14 14:46:34 +08:00
2022-06-29 15:42:50 +08:00
if [ -z "$OS" ]; then
2020-10-14 14:46:34 +08:00
echo "usage: build.sh OS ARCH"
exit
fi
2022-06-29 15:42:50 +08:00
if [ -z "$ARCH" ]; then
2020-10-14 14:46:34 +08:00
echo "usage: build.sh OS ARCH"
2020-09-09 18:53:53 +08:00
exit
fi
2022-06-29 15:42:50 +08:00
if [ -z "$TAG" ]; then
2021-07-25 16:28:36 +08:00
TAG="community"
fi
2020-09-09 18:53:53 +08:00
echo "checking ..."
ZIP_PATH=$(which zip)
2022-06-29 15:42:50 +08:00
if [ -z "$ZIP_PATH" ]; then
2020-09-09 18:53:53 +08:00
echo "we need 'zip' command to compress files"
exit
fi
2021-07-25 16:28:36 +08:00
echo "building v${VERSION}/${OS}/${ARCH}/${TAG} ..."
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
2020-09-09 18:53:53 +08:00
echo "copying ..."
2022-06-29 15:42:50 +08:00
if [ ! -d "$DIST" ]; then
mkdir "$DIST"
mkdir "$DIST"/bin
mkdir "$DIST"/configs
mkdir "$DIST"/logs
mkdir "$DIST"/data
2022-03-26 11:47:25 +08:00
if [ "$TAG" = "plus" ]; then
2022-06-29 15:42:50 +08:00
mkdir "$DIST"/scripts
mkdir "$DIST"/scripts/js
2022-03-26 11:47:25 +08:00
fi
2020-09-09 18:53:53 +08:00
fi
2022-06-29 15:42:50 +08:00
cp "$ROOT"/configs/api.template.yaml "$DIST"/configs
cp "$ROOT"/configs/cluster.template.yaml "$DIST"/configs
2022-06-29 15:42:50 +08:00
cp -R "$ROOT"/www "$DIST"/
cp -R "$ROOT"/pages "$DIST"/
2020-09-09 18:53:53 +08:00
2020-12-03 10:17:28 +08:00
# we support TOA on linux/amd64 only
if [ "$OS" == "linux" ] && [ "$ARCH" == "amd64" ]
2020-12-03 10:17:28 +08:00
then
2022-06-29 15:42:50 +08:00
cp -R "$ROOT"/edge-toa "$DIST"
2020-12-03 10:17:28 +08:00
fi
2020-09-09 18:53:53 +08:00
echo "building ..."
2021-05-25 11:05:55 +08:00
CC_PATH=""
CXX_PATH=""
2022-01-09 20:12:59 +08:00
BUILD_TAG=$TAG
2021-05-25 11:05:55 +08:00
if [[ `uname -a` == *"Darwin"* && "${OS}" == "linux" ]]; then
if [ "${ARCH}" == "amd64" ]; then
2022-01-09 20:12:59 +08:00
# build with script support
if [ -d $GCC_X86_64_DIR ]; then
MUSL_DIR=$GCC_X86_64_DIR
CC_PATH="x86_64-unknown-linux-gnu-gcc"
CXX_PATH="x86_64-unknown-linux-gnu-g++"
if [ "$TAG" = "plus" ]; then
BUILD_TAG="plus,script"
fi
else
CC_PATH="x86_64-linux-musl-gcc"
CXX_PATH="x86_64-linux-musl-g++"
fi
2021-05-25 11:05:55 +08:00
fi
if [ "${ARCH}" == "386" ]; then
CC_PATH="i486-linux-musl-gcc"
CXX_PATH="i486-linux-musl-g++"
fi
if [ "${ARCH}" == "arm64" ]; then
2022-01-09 20:12:59 +08:00
# build with script support
if [ -d $GCC_ARM64_DIR ]; then
MUSL_DIR=$GCC_ARM64_DIR
CC_PATH="aarch64-unknown-linux-gnu-gcc"
CXX_PATH="aarch64-unknown-linux-gnu-g++"
if [ "$TAG" = "plus" ]; then
BUILD_TAG="plus,script"
fi
else
CC_PATH="aarch64-linux-musl-gcc"
CXX_PATH="aarch64-linux-musl-g++"
fi
2021-05-25 11:05:55 +08:00
fi
2021-07-20 19:01:49 +08:00
if [ "${ARCH}" == "arm" ]; then
CC_PATH="arm-linux-musleabi-gcc"
CXX_PATH="arm-linux-musleabi-g++"
fi
2021-05-25 11:05:55 +08:00
if [ "${ARCH}" == "mips64" ]; then
CC_PATH="mips64-linux-musl-gcc"
CXX_PATH="mips64-linux-musl-g++"
fi
if [ "${ARCH}" == "mips64le" ]; then
CC_PATH="mips64el-linux-musl-gcc"
CXX_PATH="mips64el-linux-musl-g++"
fi
fi
if [ ! -z $CC_PATH ]; then
2022-06-29 15:42:50 +08:00
env CC=$MUSL_DIR/$CC_PATH CXX=$MUSL_DIR/$CXX_PATH GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=1 go build -trimpath -tags $BUILD_TAG -o "$DIST"/bin/${NAME} -ldflags "-linkmode external -extldflags -static -s -w" "$ROOT"/../cmd/edge-node/main.go
2021-05-25 11:05:55 +08:00
else
if [[ `uname` == *"Linux"* ]] && [ "$OS" == "linux" ] && [[ "$ARCH" == "amd64" || "$ARCH" == "arm64" ]] && [ "$TAG" == "plus" ]; then
BUILD_TAG="plus,script"
fi
env GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=1 go build -trimpath -tags $BUILD_TAG -o "$DIST"/bin/${NAME} -ldflags="-s -w" "$ROOT"/../cmd/edge-node/main.go
2021-05-25 11:05:55 +08:00
fi
2020-09-09 18:53:53 +08:00
2020-11-13 18:22:56 +08:00
# delete hidden files
2022-06-29 15:42:50 +08:00
find "$DIST" -name ".DS_Store" -delete
find "$DIST" -name ".gitignore" -delete
2020-11-13 18:22:56 +08:00
2020-09-09 18:53:53 +08:00
echo "zip files"
cd "${DIST}/../" || exit
if [ -f "${ZIP}" ]; then
rm -f "${ZIP}"
fi
zip -r -X -q "${ZIP}" ${NAME}/
rm -rf ${NAME}
cd - || exit
echo "OK"
}
2020-10-14 14:46:34 +08:00
function lookup-version() {
FILE=$1
2022-06-29 15:42:50 +08:00
VERSION_DATA=$(cat "$FILE")
2020-10-14 14:46:34 +08:00
re="Version[ ]+=[ ]+\"([0-9.]+)\""
if [[ $VERSION_DATA =~ $re ]]; then
VERSION=${BASH_REMATCH[1]}
2022-06-29 15:42:50 +08:00
echo "$VERSION"
2020-10-14 14:46:34 +08:00
else
echo "could not match version"
exit
fi
}
2022-06-29 15:42:50 +08:00
build "$1" "$2" "$3"