#!/bin/bash
set -e
CUR_DIR=$(dirname "$(readlink -f "$0")")
TOP_DIR=$(realpath "${CUR_DIR}"/..)
build_version="7.3.0"
version_file="${TOP_DIR}"/../../build/service_config.ini
if [ -f "$version_file" ]; then
line=$(sed -n '1p' "$version_file" 2>&1)
build_version="v"${line#*=}
fi
output_name="device-plugin"
os_type=$(arch)
build_type=build
if [ "$1" == "ci" ] || [ "$2" == "ci" ]; then
export GO111MODULE="on"
export GONOSUMDB="*"
build_type=ci
fi
function clean() {
rm -rf "${TOP_DIR}"/output/
mkdir -p "${TOP_DIR}"/output
}
function build_plugin() {
cd "${TOP_DIR}"
export CGO_ENABLED=1
export GONOSUMDB="*"
export CGO_CFLAGS="-fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -fPIC -ftrapv"
export CGO_CPPFLAGS="-fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -fPIC -ftrapv"
go mod tidy
go build -mod=mod -buildmode=pie -ldflags "-X main.BuildName=${output_name} \
-X main.BuildVersion=${build_version}_linux-${os_type} \
-buildid none \
-s \
-extldflags=-Wl,-z,relro,-z,now,-z,noexecstack" \
-o "${output_name}" \
-trimpath
ls "${output_name}"
if [ $? -ne 0 ]; then
echo "fail to find device-plugin"
exit 1
fi
}
function mv_file() {
mv "${TOP_DIR}/${output_name}" "${TOP_DIR}"/output
}
function change_mod() {
chmod 400 "$TOP_DIR"/output/*
chmod 500 "${TOP_DIR}/output/${output_name}"
}
function main() {
echo "------------------------ start build device plugin ------------------------"
clean
build_plugin
mv_file
change_mod
echo "------------------------ end build device plugin ------------------------"
}
main $1