From 455f36734cd1d075bc02957701c2e4d91e426580 Mon Sep 17 00:00:00 2001 From: tobyxdd Date: Fri, 9 Dec 2022 13:44:35 -0800 Subject: [PATCH 1/2] ci: update build scripts to handle microarchitectures --- build.ps1 | 58 +++++++++++++++++++++++++++++++++++++++++++++++-------- build.sh | 41 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/build.ps1 b/build.ps1 index e97014c..aa7c3a9 100644 --- a/build.ps1 +++ b/build.ps1 @@ -5,6 +5,49 @@ # - HY_APP_COMMIT: App commit hash # - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64") +function PlatformToEnv($os, $arch) { + $env:CGO_ENABLED = 0 + $env:GOOS = $os + $env:GOARCH = $arch + + switch -Regex ($arch) { + "arm" { + $env:GOARM = "7" + } + "armv5" { + $env:GOARM = "5" + $env:GOARCH = "arm" + } + "armv6" { + $env:GOARM = "6" + $env:GOARCH = "arm" + } + "armv7" { + $env:GOARM = "7" + $env:GOARCH = "arm" + } + "mips(le)?" { + $env:GOMIPS = "" + } + "mips-sf" { + $env:GOMIPS = "softfloat" + $env:GOARCH = "mips" + } + "mipsle-sf" { + $env:GOMIPS = "softfloat" + $env:GOARCH = "mipsle" + } + "amd64" { + $env:GOAMD64 = "" + $env:GOARCH = "amd64" + } + "amd64-avx" { + $env:GOAMD64 = "v3" + $env:GOARCH = "amd64" + } + } +} + if (!(Get-Command go -ErrorAction SilentlyContinue)) { Write-Host "Error: go is not installed." -ForegroundColor Red exit 1 @@ -49,19 +92,18 @@ New-Item -ItemType Directory -Force -Path build Write-Host "Starting build..." -ForegroundColor Green -$env:CGO_ENABLED = 0 - foreach ($platform in $platforms) { - $env:GOOS = $platform.Split("/")[0] - $env:GOARCH = $platform.Split("/")[1] - Write-Host "Building $env:GOOS/$env:GOARCH" -ForegroundColor Green - $output = "build/hysteria-$env:GOOS-$env:GOARCH" - if ($env:GOOS -eq "windows") { + $os = $platform.Split("/")[0] + $arch = $platform.Split("/")[1] + PlatformToEnv $os $arch + Write-Host "Building $os/$arch" -ForegroundColor Green + $output = "build/hysteria-$os-$arch" + if ($os -eq "windows") { $output = "$output.exe" } go build -o $output -tags=gpl -ldflags $ldflags -trimpath ./app/cmd/ if ($LastExitCode -ne 0) { - Write-Host "Error: failed to build $env:GOOS/$env:GOARCH" -ForegroundColor Red + Write-Host "Error: failed to build $os/$arch" -ForegroundColor Red exit 1 } } diff --git a/build.sh b/build.sh index 1a4f5fa..ac33c6e 100755 --- a/build.sh +++ b/build.sh @@ -8,6 +8,44 @@ set -e # - HY_APP_COMMIT: App commit hash # - HY_APP_PLATFORMS: Platforms to build for (e.g. "windows/amd64,linux/amd64,darwin/amd64") +platform_to_env() { + local os=$1 + local arch=$2 + local env="GOOS=$os GOARCH=$arch CGO_ENABLED=0" + + case $arch in + arm) + env+=" GOARM= GOARCH=arm" + ;; + armv5) + env+=" GOARM=5 GOARCH=arm" + ;; + armv6) + env+=" GOARM=6 GOARCH=arm" + ;; + armv7) + env+=" GOARM=7 GOARCH=arm" + ;; + mips | mipsle) + env+=" GOMIPS=" + ;; + mips-sf) + env+=" GOMIPS=softfloat GOARCH=mips" + ;; + mipsle-sf) + env+=" GOMIPS=softfloat GOARCH=mipsle" + ;; + amd64) + env+=" GOAMD64= GOARCH=amd64" + ;; + amd64-avx) + env+=" GOAMD64=v3 GOARCH=amd64" + ;; + esac + + echo $env +} + if ! [ -x "$(command -v go)" ]; then echo 'Error: go is not installed.' >&2 exit 1 @@ -52,7 +90,8 @@ for platform in "${platforms[@]}"; do if [ $GOOS = "windows" ]; then output="$output.exe" fi - env GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go build -o $output -tags=gpl -ldflags "$ldflags" -trimpath ./app/cmd/ + 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 From 432a29ff9a7535137387518e18583884ec2c122f Mon Sep 17 00:00:00 2001 From: Haruue Icymoon Date: Sat, 10 Dec 2022 12:29:39 +0800 Subject: [PATCH 2/2] 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 --- build.sh | 107 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/build.sh b/build.sh index ac33c6e..0c75988 100755 --- a/build.sh +++ b/build.sh @@ -8,12 +8,36 @@ set -e # - HY_APP_COMMIT: App commit hash # - 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() { - local os=$1 - local arch=$2 + local os="$1" + local arch="$2" local env="GOOS=$os GOARCH=$arch CGO_ENABLED=0" - case $arch in + case "$arch" in arm) env+=" GOARM= GOARCH=arm" ;; @@ -43,39 +67,54 @@ platform_to_env() { ;; esac - echo $env + echo "$env" } -if ! [ -x "$(command -v go)" ]; then - echo 'Error: go is not installed.' >&2 - exit 1 -fi +make_ldflags() { + local ldflags="-s -w -X 'main.appDate=$(date -u '+%F %T')'" + if [ -n "$HY_APP_VERSION" ]; then + ldflags="$ldflags -X 'main.appVersion=$HY_APP_VERSION'" + else + ldflags="$ldflags -X 'main.appVersion=$(git describe --tags --always --match 'v*')'" + fi + if [ -n "$HY_APP_COMMIT" ]; then + ldflags="$ldflags -X 'main.appCommit=$HY_APP_COMMIT'" + else + ldflags="$ldflags -X 'main.appCommit=$(git rev-parse HEAD)'" + fi + echo "$ldflags" +} -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 +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 +} -ldflags="-s -w -X 'main.appDate=$(date -u '+%F %T')'" -if [ -n "$HY_APP_VERSION" ]; then - ldflags="$ldflags -X 'main.appVersion=$HY_APP_VERSION'" -else - ldflags="$ldflags -X 'main.appVersion=$(git describe --tags --always)'" -fi -if [ -n "$HY_APP_COMMIT" ]; then - ldflags="$ldflags -X 'main.appCommit=$HY_APP_COMMIT'" -else - ldflags="$ldflags -X 'main.appCommit=$(git rev-parse HEAD)'" -fi if [ -z "$HY_APP_PLATFORMS" ]; then HY_APP_PLATFORMS="$(go env GOOS)/$(go env GOARCH)" fi platforms=(${HY_APP_PLATFORMS//,/ }) +ldflags="$(make_ldflags)" mkdir -p build rm -rf build/* @@ -83,19 +122,7 @@ rm -rf build/* echo "Starting build..." for platform in "${platforms[@]}"; do - GOOS=${platform%/*} - 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 + build_for_platform "$platform" "$ldflags" done echo "Build complete."