优化编译脚本

This commit is contained in:
GoEdgeLab
2022-06-29 15:42:50 +08:00
parent 155d1284f3
commit 18ebd8c712

View File

@@ -3,7 +3,7 @@
function build() { function build() {
ROOT=$(dirname $0) ROOT=$(dirname $0)
NAME="edge-node" NAME="edge-node"
VERSION=$(lookup-version $ROOT/../internal/const/const.go) VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
DIST=$ROOT/"../dist/${NAME}" DIST=$ROOT/"../dist/${NAME}"
MUSL_DIR="/usr/local/opt/musl-cross/bin" MUSL_DIR="/usr/local/opt/musl-cross/bin"
GCC_X86_64_DIR="/usr/local/Cellar/x86_64-unknown-linux-gnu/10.3.0/bin" GCC_X86_64_DIR="/usr/local/Cellar/x86_64-unknown-linux-gnu/10.3.0/bin"
@@ -13,21 +13,21 @@ function build() {
ARCH=${2} ARCH=${2}
TAG=${3} TAG=${3}
if [ -z $OS ]; then if [ -z "$OS" ]; then
echo "usage: build.sh OS ARCH" echo "usage: build.sh OS ARCH"
exit exit
fi fi
if [ -z $ARCH ]; then if [ -z "$ARCH" ]; then
echo "usage: build.sh OS ARCH" echo "usage: build.sh OS ARCH"
exit exit
fi fi
if [ -z $TAG ]; then if [ -z "$TAG" ]; then
TAG="community" TAG="community"
fi fi
echo "checking ..." echo "checking ..."
ZIP_PATH=$(which zip) ZIP_PATH=$(which zip)
if [ -z $ZIP_PATH ]; then if [ -z "$ZIP_PATH" ]; then
echo "we need 'zip' command to compress files" echo "we need 'zip' command to compress files"
exit exit
fi fi
@@ -36,28 +36,28 @@ function build() {
ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip" ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
echo "copying ..." echo "copying ..."
if [ ! -d $DIST ]; then if [ ! -d "$DIST" ]; then
mkdir $DIST mkdir "$DIST"
mkdir $DIST/bin mkdir "$DIST"/bin
mkdir $DIST/configs mkdir "$DIST"/configs
mkdir $DIST/logs mkdir "$DIST"/logs
mkdir $DIST/data mkdir "$DIST"/data
if [ "$TAG" = "plus" ]; then if [ "$TAG" = "plus" ]; then
mkdir $DIST/scripts mkdir "$DIST"/scripts
mkdir $DIST/scripts/js mkdir "$DIST"/scripts/js
fi fi
fi fi
cp $ROOT/configs/api.template.yaml $DIST/configs cp "$ROOT"/configs/api.template.yaml "$DIST"/configs
cp -R $ROOT/www $DIST/ cp -R "$ROOT"/www "$DIST"/
cp -R $ROOT/pages $DIST/ cp -R "$ROOT"/pages "$DIST"/
cp -R $ROOT/resources $DIST/ cp -R "$ROOT"/resources "$DIST"/
# we support TOA on linux/amd64 only # we support TOA on linux/amd64 only
if [ $OS == "linux" -a $ARCH == "amd64" ] if [ "$OS" == "linux" -a "$ARCH" == "amd64" ]
then then
cp -R $ROOT/edge-toa $DIST cp -R "$ROOT"/edge-toa "$DIST"
fi fi
echo "building ..." echo "building ..."
@@ -112,14 +112,14 @@ function build() {
fi fi
fi fi
if [ ! -z $CC_PATH ]; then if [ ! -z $CC_PATH ]; then
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 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
else else
env GOOS=${OS} GOARCH=${ARCH} CGO_ENABLED=1 go build -trimpath -tags $TAG -o $DIST/bin/${NAME} -ldflags="-s -w" $ROOT/../cmd/edge-node/main.go env GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=1 go build -trimpath -tags $TAG -o "$DIST"/bin/${NAME} -ldflags="-s -w" "$ROOT"/../cmd/edge-node/main.go
fi fi
# delete hidden files # delete hidden files
find $DIST -name ".DS_Store" -delete find "$DIST" -name ".DS_Store" -delete
find $DIST -name ".gitignore" -delete find "$DIST" -name ".gitignore" -delete
echo "zip files" echo "zip files"
cd "${DIST}/../" || exit cd "${DIST}/../" || exit
@@ -135,15 +135,15 @@ function build() {
function lookup-version() { function lookup-version() {
FILE=$1 FILE=$1
VERSION_DATA=$(cat $FILE) VERSION_DATA=$(cat "$FILE")
re="Version[ ]+=[ ]+\"([0-9.]+)\"" re="Version[ ]+=[ ]+\"([0-9.]+)\""
if [[ $VERSION_DATA =~ $re ]]; then if [[ $VERSION_DATA =~ $re ]]; then
VERSION=${BASH_REMATCH[1]} VERSION=${BASH_REMATCH[1]}
echo $VERSION echo "$VERSION"
else else
echo "could not match version" echo "could not match version"
exit exit
fi fi
} }
build $1 $2 $3 build "$1" "$2" "$3"