github.com/TeaOSLab/EdgeNode@v1.3.8/build/build.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  function build() {
     4  	ROOT=$(dirname $0)
     5  	NAME="edge-node"
     6  	VERSION=$(lookup-version "$ROOT"/../internal/const/const.go)
     7  	DIST=$ROOT/"../dist/${NAME}"
     8  	MUSL_DIR="/usr/local/opt/musl-cross/bin"
     9  	SRCDIR=$(realpath "$ROOT/..")
    10  
    11  	# for macOS users: precompiled gcc can be downloaded from https://github.com/messense/homebrew-macos-cross-toolchains
    12  	GCC_X86_64_DIR="/usr/local/gcc/x86_64-unknown-linux-gnu/bin"
    13  	GCC_ARM64_DIR="/usr/local/gcc/aarch64-unknown-linux-gnu/bin"
    14  
    15  	OS=${1}
    16  	ARCH=${2}
    17  	TAG=${3}
    18  
    19  	if [ -z "$OS" ]; then
    20  		echo "usage: build.sh OS ARCH"
    21  		exit
    22  	fi
    23  	if [ -z "$ARCH" ]; then
    24  		echo "usage: build.sh OS ARCH"
    25  		exit
    26  	fi
    27  	if [ -z "$TAG" ]; then
    28  		TAG="community"
    29  	fi
    30  
    31  	echo "checking ..."
    32  	ZIP_PATH=$(which zip)
    33  	if [ -z "$ZIP_PATH" ]; then
    34  		echo "we need 'zip' command to compress files"
    35  		exit
    36  	fi
    37  
    38  	echo "building v${VERSION}/${OS}/${ARCH}/${TAG} ..."
    39  	ZIP="${NAME}-${OS}-${ARCH}-${TAG}-v${VERSION}.zip"
    40  
    41  	echo "copying ..."
    42  	if [ ! -d "$DIST" ]; then
    43  		mkdir "$DIST"
    44  		mkdir "$DIST"/bin
    45  		mkdir "$DIST"/configs
    46  		mkdir "$DIST"/logs
    47  		mkdir "$DIST"/data
    48  
    49  		if [ "$TAG" = "plus" ]; then
    50  			mkdir "$DIST"/scripts
    51  			mkdir "$DIST"/scripts/js
    52  		fi
    53  	fi
    54  
    55  	cp "$ROOT"/configs/api_node.template.yaml "$DIST"/configs
    56  	cp "$ROOT"/configs/cluster.template.yaml "$DIST"/configs
    57  	cp -R "$ROOT"/www "$DIST"/
    58  	cp -R "$ROOT"/pages "$DIST"/
    59  
    60  	# we support TOA on linux only
    61  	if [ "$OS" == "linux" ] && [ -f "${ROOT}/edge-toa/edge-toa-${ARCH}" ]
    62  	then
    63  		if [ ! -d  "$DIST/edge-toa" ]
    64  		then
    65  			mkdir "$DIST/edge-toa"
    66  		fi
    67  		cp "${ROOT}/edge-toa/edge-toa-${ARCH}" "$DIST/edge-toa/edge-toa"
    68  	fi
    69  
    70  	echo "building ..."
    71  
    72  	CC_PATH=""
    73  	CXX_PATH=""
    74  	CGO_LDFLAGS=""
    75  	CGO_CFLAGS=""
    76  	BUILD_TAG=$TAG
    77  	if [[ `uname -a` == *"Darwin"* && "${OS}" == "linux" ]]; then
    78  		if [ "${ARCH}" == "amd64" ]; then
    79  			# build with script support
    80  			if [ -d $GCC_X86_64_DIR ]; then
    81  				MUSL_DIR=$GCC_X86_64_DIR
    82  				CC_PATH="x86_64-unknown-linux-gnu-gcc"
    83  				CXX_PATH="x86_64-unknown-linux-gnu-g++"
    84  				if [ "$TAG" = "plus" ]; then
    85  					BUILD_TAG="plus,script,packet"
    86  				fi
    87  			else
    88  				CC_PATH="x86_64-linux-musl-gcc"
    89  				CXX_PATH="x86_64-linux-musl-g++"
    90  			fi
    91  		fi
    92  		if [ "${ARCH}" == "386" ]; then
    93  			CC_PATH="i486-linux-musl-gcc"
    94  			CXX_PATH="i486-linux-musl-g++"
    95  		fi
    96  		if [ "${ARCH}" == "arm64" ]; then
    97  			# build with script support
    98  			if [ -d $GCC_ARM64_DIR ]; then
    99  				MUSL_DIR=$GCC_ARM64_DIR
   100  				CC_PATH="aarch64-unknown-linux-gnu-gcc"
   101  				CXX_PATH="aarch64-unknown-linux-gnu-g++"
   102  				if [ "$TAG" = "plus" ]; then
   103  					BUILD_TAG="plus,script,packet"
   104  				fi
   105  			else
   106  				CC_PATH="aarch64-linux-musl-gcc"
   107  				CXX_PATH="aarch64-linux-musl-g++"
   108  			fi
   109  		fi
   110  		if [ "${ARCH}" == "arm" ]; then
   111  			CC_PATH="arm-linux-musleabi-gcc"
   112  			CXX_PATH="arm-linux-musleabi-g++"
   113  		fi
   114  		if [ "${ARCH}" == "mips64" ]; then
   115  			CC_PATH="mips64-linux-musl-gcc"
   116  			CXX_PATH="mips64-linux-musl-g++"
   117  		fi
   118  		if [ "${ARCH}" == "mips64le" ]; then
   119  			CC_PATH="mips64el-linux-musl-gcc"
   120  			CXX_PATH="mips64el-linux-musl-g++"
   121  		fi
   122  	fi
   123  
   124  	# libpcap
   125  	if [ "$OS" == "linux" ] && [[ "$ARCH" == "amd64" || "$ARCH" == "arm64" ]] &&  [ "$TAG" == "plus" ]; then
   126  		CGO_LDFLAGS="-L${SRCDIR}/libs/libpcap/${ARCH} -lpcap -L${SRCDIR}/libs/libbrotli/${ARCH} -lbrotlienc -lbrotlidec -lbrotlicommon"
   127  		CGO_CFLAGS="-I${SRCDIR}/libs/libpcap/src/libpcap -I${SRCDIR}/libs/libpcap/src/libpcap/pcap -I${SRCDIR}/libs/libbrotli/src/brotli/c/include"
   128  	fi
   129  
   130  	if [ ! -z $CC_PATH ]; then
   131  		env CC=$MUSL_DIR/$CC_PATH \
   132  		 CXX=$MUSL_DIR/$CXX_PATH GOOS="${OS}" \
   133  		 GOARCH="${ARCH}" CGO_ENABLED=1 \
   134  		 CGO_LDFLAGS="${CGO_LDFLAGS}" \
   135  		 CGO_CFLAGS="${CGO_CFLAGS}" \
   136  		 go build -trimpath -tags $BUILD_TAG -o "$DIST"/bin/${NAME} -ldflags "-linkmode external -extldflags -static -s -w" "$ROOT"/../cmd/edge-node/main.go
   137  	else
   138  		if [[ `uname` == *"Linux"* ]] && [ "$OS" == "linux" ] && [[ "$ARCH" == "amd64" || "$ARCH" == "arm64" ]] &&  [ "$TAG" == "plus" ]; then
   139  			BUILD_TAG="plus,script,packet"
   140  		fi
   141  
   142  		env GOOS="${OS}" GOARCH="${ARCH}" CGO_ENABLED=1  CGO_LDFLAGS="${CGO_LDFLAGS}" CGO_CFLAGS="${CGO_CFLAGS}" go build -trimpath -tags $BUILD_TAG -o "$DIST"/bin/${NAME} -ldflags="-s -w" "$ROOT"/../cmd/edge-node/main.go
   143  	fi
   144  
   145  	if [ ! -f "${DIST}/bin/${NAME}" ]; then
   146  		echo "build failed!"
   147  		exit
   148  	fi
   149  
   150  	# delete hidden files
   151  	find "$DIST" -name ".DS_Store" -delete
   152  	find "$DIST" -name ".gitignore" -delete
   153  
   154  	echo "zip files"
   155  	cd "${DIST}/../" || exit
   156  	if [ -f "${ZIP}" ]; then
   157  		rm -f "${ZIP}"
   158  	fi
   159  	zip -r -X -q "${ZIP}" ${NAME}/
   160  	rm -rf ${NAME}
   161  	cd - || exit
   162  
   163  	echo "OK"
   164  }
   165  
   166  function lookup-version() {
   167  	FILE=$1
   168  	VERSION_DATA=$(cat "$FILE")
   169  	re="Version[ ]+=[ ]+\"([0-9.]+)\""
   170  	if [[ $VERSION_DATA =~ $re ]]; then
   171  		VERSION=${BASH_REMATCH[1]}
   172  		echo "$VERSION"
   173  	else
   174  		echo "could not match version"
   175  		exit
   176  	fi
   177  }
   178  
   179  build "$1" "$2" "$3"