From 6e97de948b216b493ce150c05e8f5fc4362185e4 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 3 Feb 2026 10:22:55 +0800 Subject: [PATCH] Enhance build process and options management - Update Makefile to include new production and UPX build targets for optimized binaries. - Add BUILDOPTIONS constant to share/const.go for tracking build configurations. - Modify version command to display build options. - Update Dockerfile to install UPX for compression during builds. - Add release and prod targets to streamline artifact creation. Co-authored-by: Cursor --- .gitignore | 1 + Makefile | 76 ++++++++++++++++++++++++++++++++++++++--- cmd/version.go | 5 +++ docker/build/Dockerfile | 3 +- share/const.go | 3 ++ 5 files changed, 83 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 930b708f..8ddfece5 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ sandbox/docker/yao-bridge-* sandbox/docker/claude-proxy-* sandbox/docker/claude/claude-proxy-* sandbox/proxy/claude-proxy-linux-* +release/* diff --git a/Makefile b/Makefile index 1f9ffc19..461c273e 100644 --- a/Makefile +++ b/Makefile @@ -400,11 +400,30 @@ artifacts-linux: clean @CUI_COMMIT=$$(cd ../cui-v1.0 && git log | head -n 1 | awk '{print substr($$2, 0, 12)}') && \ sed -ie "s/const PRCUI = \"DEV\"/const PRCUI = \"$$CUI_COMMIT-${NOW}\"/g" share/const.go -# Making artifacts +# Making artifacts - dev builds (full debug symbols, ~158M) mkdir -p dist CGO_ENABLED=1 CGO_LDFLAGS="-static" GOOS=linux GOARCH=amd64 go build -v -o dist/yao-${VERSION}-unstable-linux-amd64 CGO_ENABLED=1 CGO_LDFLAGS="-static" LD_LIBRARY_PATH=/usr/lib/gcc-cross/aarch64-linux-gnu/13 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc-13 CXX=aarch64-linux-gnu-g++-13 go build -v -o dist/yao-${VERSION}-unstable-linux-arm64 +# Making artifacts - prod builds (stripped, ~111M) + sed -i.tmp 's/const BUILDOPTIONS = ""/const BUILDOPTIONS = "-s -w (production, stripped)"/g' share/const.go && rm -f share/const.go.tmp + CGO_ENABLED=1 CGO_LDFLAGS="-static" GOOS=linux GOARCH=amd64 go build -v -ldflags="-s -w" -o dist/yao-${VERSION}-unstable-linux-amd64-prod + CGO_ENABLED=1 CGO_LDFLAGS="-static" LD_LIBRARY_PATH=/usr/lib/gcc-cross/aarch64-linux-gnu/13 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc-13 CXX=aarch64-linux-gnu-g++-13 go build -v -ldflags="-s -w" -o dist/yao-${VERSION}-unstable-linux-arm64-prod + +# Making artifacts - prod-upx builds (stripped + compressed, ~45-55M) + sed -i.tmp 's/const BUILDOPTIONS = "-s -w (production, stripped)"/const BUILDOPTIONS = "-s -w +upx (production, compressed)"/g' share/const.go && rm -f share/const.go.tmp + CGO_ENABLED=1 CGO_LDFLAGS="-static" GOOS=linux GOARCH=amd64 go build -v -ldflags="-s -w" -o dist/yao-${VERSION}-unstable-linux-amd64-prod-upx + CGO_ENABLED=1 CGO_LDFLAGS="-static" LD_LIBRARY_PATH=/usr/lib/gcc-cross/aarch64-linux-gnu/13 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc-13 CXX=aarch64-linux-gnu-g++-13 go build -v -ldflags="-s -w" -o dist/yao-${VERSION}-unstable-linux-arm64-prod-upx + +# UPX compression for prod-upx builds + @if command -v upx > /dev/null 2>&1; then \ + echo "Compressing with UPX..."; \ + upx --best dist/yao-${VERSION}-unstable-linux-amd64-prod-upx; \ + upx --best dist/yao-${VERSION}-unstable-linux-arm64-prod-upx; \ + else \ + echo "WARNING: UPX not found, skipping compression"; \ + fi + mkdir -p dist/release mv dist/yao-*-* dist/release/ chmod +x dist/release/yao-*-* @@ -484,8 +503,9 @@ debug: clean cp -f share/const.goe share/const.go rm -f share/const.goe -.PHONY: release -release: clean +# make prepare (build CUI, yao-init, bindata - shared by release and prod) +.PHONY: prepare +prepare: clean mkdir -p dist/release mkdir .tmp @@ -528,13 +548,15 @@ release: clean cp -r .tmp/yao-init .tmp/data/init go-bindata -fs -pkg data -o data/bindata.go -prefix ".tmp/data/" .tmp/data/... - # Replace PRVERSION cp -f share/const.go share/const.go.bak sed -ie "s/const PRVERSION = \"DEV\"/const PRVERSION = \"${COMMIT}-${NOW}\"/g" share/const.go @CUI_COMMIT=$$(cd .tmp/cui/v1.0 && git log | head -n 1 | awk '{print substr($$2, 0, 12)}') && \ sed -ie "s/const PRCUI = \"DEV\"/const PRCUI = \"$$CUI_COMMIT-${NOW}\"/g" share/const.go +# make release (development build, ~158M) +.PHONY: release +release: prepare # Making artifacts mkdir -p dist CGO_ENABLED=1 go build -v -o dist/release/yao @@ -552,6 +574,52 @@ release: clean codesign --deep --force --verify --verbose --sign "${APPLE_SIGN}" dist/release/yao ; \ fi +# make prod (production build with -s -w, ~111M on macOS, ~45-55M on Linux with UPX) +.PHONY: prod +prod: prepare +# Set BUILDOPTIONS + @if [ "$$(uname)" = "Linux" ]; then \ + sed -i.tmp 's/const BUILDOPTIONS = ""/const BUILDOPTIONS = "-s -w +upx (production, compressed)"/g' share/const.go && rm -f share/const.go.tmp; \ + else \ + sed -i.tmp 's/const BUILDOPTIONS = ""/const BUILDOPTIONS = "-s -w (production, stripped)"/g' share/const.go && rm -f share/const.go.tmp; \ + fi + +# Making artifacts + mkdir -p dist + CGO_ENABLED=1 go build -v -ldflags="-s -w" -o dist/release/yao-prod + chmod +x dist/release/yao-prod + +# UPX compression (Linux only) + @if [ "$$(uname)" = "Linux" ]; then \ + echo "Compressing with UPX..."; \ + if command -v upx > /dev/null 2>&1; then \ + upx --best dist/release/yao-prod; \ + else \ + echo "WARNING: UPX not found. Install with: apt install upx"; \ + echo "Skipping compression."; \ + fi; \ + else \ + echo "Note: UPX compression skipped on macOS (not supported)"; \ + fi + +# Clean up and restore bindata.go and const.go + cp data/bindata.go.bak data/bindata.go + cp share/const.go.bak share/const.go + rm data/bindata.go.bak + rm share/const.go.bak + rm -rf .tmp + +# MacOS Application Signing + @if [ "$(OS)" = "Darwin" ]; then \ + codesign --deep --force --verify --verbose --sign "${APPLE_SIGN}" dist/release/yao-prod ; \ + fi + + @echo "" + @echo "Done! Production binary:" + @ls -lh dist/release/yao-prod + @echo "" + @echo "Test with: dist/release/yao-prod version --all" + .PHONY: linux-release linux-release: clean diff --git a/cmd/version.go b/cmd/version.go index 67e104d1..043f1b98 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -41,6 +41,11 @@ var versionCmd = &cobra.Command{ fmt.Printf("%s", color.WhiteString("Built: ")) fmt.Printf("%s\n", color.BlueString(buildTime)) + if share.BUILDOPTIONS != "" { + fmt.Printf("%s", color.WhiteString("Build options: ")) + fmt.Printf("%s\n", color.CyanString(share.BUILDOPTIONS)) + } + fmt.Printf("%s", color.WhiteString("OS/Arch: ")) fmt.Printf("%s\n", color.MagentaString("%s/%s", runtime.GOOS, runtime.GOARCH)) diff --git a/docker/build/Dockerfile b/docker/build/Dockerfile index 92ae1159..cc07e65b 100644 --- a/docker/build/Dockerfile +++ b/docker/build/Dockerfile @@ -27,7 +27,8 @@ RUN apt-get update && \ apt-get install -y wget && \ apt-get install -y curl && \ apt-get install -y git && \ - apt-get install -y unzip + apt-get install -y unzip && \ + apt-get install -y upx # Install Go 1.25.0 RUN wget https://golang.org/dl/go1.25.0.linux-amd64.tar.gz && \ diff --git a/share/const.go b/share/const.go index 815b2c85..2c3e8d84 100644 --- a/share/const.go +++ b/share/const.go @@ -12,6 +12,9 @@ const CUI = "1.0.0" // PRCUI CUI PR Commit const PRCUI = "DEV" +// BUILDOPTIONS Build options (e.g., "-s -w", "-s -w +upx") +const BUILDOPTIONS = "" + // BUILDIN If true, the application will be built into a single artifact const BUILDIN = false