refactor: build.sh

+ fix command exit status check under set -e
+ fix crashes when $HY_APP_PLATFORMS contains invalid values
+ fix final binary size report error under UTF-8 locales
+ reduce global variables
This commit is contained in:
Haruue Icymoon 2022-12-10 12:29:39 +08:00
parent 455f36734c
commit 432a29ff9a
No known key found for this signature in database
GPG Key ID: F6083B28CBCBC148

105
build.sh
View File

@ -8,12 +8,36 @@ set -e
# - HY_APP_COMMIT: App commit hash # - HY_APP_COMMIT: App commit hash
# - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64") # - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64")
export LC_ALL=C
export LC_DATE=C
has_command() {
local cmd="$1"
type -P "$cmd" > /dev/null 2>&1
}
if ! has_command go; then
echo 'Error: go is not installed.' >&2
exit 1
fi
if ! has_command git; then
echo 'Error: git is not installed.' >&2
exit 1
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo 'Error: not in a git repository.' >&2
exit 1
fi
platform_to_env() { platform_to_env() {
local os=$1 local os="$1"
local arch=$2 local arch="$2"
local env="GOOS=$os GOARCH=$arch CGO_ENABLED=0" local env="GOOS=$os GOARCH=$arch CGO_ENABLED=0"
case $arch in case "$arch" in
arm) arm)
env+=" GOARM= GOARCH=arm" env+=" GOARM= GOARCH=arm"
;; ;;
@ -43,39 +67,54 @@ platform_to_env() {
;; ;;
esac esac
echo $env echo "$env"
} }
if ! [ -x "$(command -v go)" ]; then make_ldflags() {
echo 'Error: go is not installed.' >&2 local ldflags="-s -w -X 'main.appDate=$(date -u '+%F %T')'"
exit 1 if [ -n "$HY_APP_VERSION" ]; then
fi
if ! [ -x "$(command -v git)" ]; then
echo 'Error: git is not installed.' >&2
exit 1
fi
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo 'Error: not in a git repository.' >&2
exit 1
fi
ldflags="-s -w -X 'main.appDate=$(date -u '+%F %T')'"
if [ -n "$HY_APP_VERSION" ]; then
ldflags="$ldflags -X 'main.appVersion=$HY_APP_VERSION'" ldflags="$ldflags -X 'main.appVersion=$HY_APP_VERSION'"
else else
ldflags="$ldflags -X 'main.appVersion=$(git describe --tags --always)'" ldflags="$ldflags -X 'main.appVersion=$(git describe --tags --always --match 'v*')'"
fi fi
if [ -n "$HY_APP_COMMIT" ]; then if [ -n "$HY_APP_COMMIT" ]; then
ldflags="$ldflags -X 'main.appCommit=$HY_APP_COMMIT'" ldflags="$ldflags -X 'main.appCommit=$HY_APP_COMMIT'"
else else
ldflags="$ldflags -X 'main.appCommit=$(git rev-parse HEAD)'" ldflags="$ldflags -X 'main.appCommit=$(git rev-parse HEAD)'"
fi fi
echo "$ldflags"
}
build_for_platform() {
local platform="$1"
local ldflags="$2"
local GOOS="${platform%/*}"
local GOARCH="${platform#*/}"
if [[ -z "$GOOS" || -z "$GOARCH" ]]; then
echo "Invalid platform $platform" >&2
return 1
fi
echo "Building $GOOS/$GOARCH"
local output="build/hysteria-$GOOS-$GOARCH"
if [[ "$GOOS" = "windows" ]]; then
output="$output.exe"
fi
local envs="$(platform_to_env "$GOOS" "$GOARCH")"
local exit_val=0
env $envs go build -o "$output" -tags=gpl -ldflags "$ldflags" -trimpath ./app/cmd/ || exit_val=$?
if [[ "$exit_val" -ne 0 ]]; then
echo "Error: failed to build $GOOS/$GOARCH" >&2
return $exit_val
fi
}
if [ -z "$HY_APP_PLATFORMS" ]; then if [ -z "$HY_APP_PLATFORMS" ]; then
HY_APP_PLATFORMS="$(go env GOOS)/$(go env GOARCH)" HY_APP_PLATFORMS="$(go env GOOS)/$(go env GOARCH)"
fi fi
platforms=(${HY_APP_PLATFORMS//,/ }) platforms=(${HY_APP_PLATFORMS//,/ })
ldflags="$(make_ldflags)"
mkdir -p build mkdir -p build
rm -rf build/* rm -rf build/*
@ -83,19 +122,7 @@ rm -rf build/*
echo "Starting build..." echo "Starting build..."
for platform in "${platforms[@]}"; do for platform in "${platforms[@]}"; do
GOOS=${platform%/*} build_for_platform "$platform" "$ldflags"
GOARCH=${platform#*/}
echo "Building $GOOS/$GOARCH"
output="build/hysteria-$GOOS-$GOARCH"
if [ $GOOS = "windows" ]; then
output="$output.exe"
fi
envs=$(platform_to_env $GOOS $GOARCH)
env $envs go build -o $output -tags=gpl -ldflags "$ldflags" -trimpath ./app/cmd/
if [ $? -ne 0 ]; then
echo "Error: failed to build $GOOS/$GOARCH"
exit 1
fi
done done
echo "Build complete." echo "Build complete."