build.sh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. set -e
  3. BINARY_NAME="opencode-monitor"
  4. VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
  5. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  6. PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  7. cd "$PROJECT_DIR"
  8. detect_platform() {
  9. local os arch suffix
  10. os=$(uname -s | tr '[:upper:]' '[:lower:]')
  11. arch=$(uname -m)
  12. case "$os" in
  13. linux*) os="linux" ;;
  14. darwin*) os="darwin" ;;
  15. *) os="windows" ;;
  16. esac
  17. case "$arch" in
  18. x86_64|amd64) arch="amd64" ;;
  19. aarch64|arm64) arch="arm64" ;;
  20. *) arch="amd64" ;;
  21. esac
  22. if [ "$os" = "windows" ]; then
  23. suffix=".exe"
  24. else
  25. suffix=""
  26. fi
  27. echo "${os}-${arch}${suffix}"
  28. }
  29. if [ "$1" = "--only-ble" ]; then
  30. echo "=== Building BLE relay only ==="
  31. python3 -m pip install pyinstaller bleak || python -m pip install pyinstaller bleak
  32. mkdir -p bin
  33. python3 -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py || python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py
  34. cp bin/ble_relay cmd/monitor/ble_relay
  35. echo ""
  36. echo "Done: bin/ble_relay + cmd/monitor/ble_relay"
  37. elif [ "$1" = "--ble" ]; then
  38. PLATFORM=$(detect_platform)
  39. echo "=== Building for ${PLATFORM} with BLE ==="
  40. echo "Step 1: Building BLE relay..."
  41. python3 -m pip install pyinstaller bleak || python -m pip install pyinstaller bleak
  42. mkdir -p bin
  43. python3 -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py || python -m PyInstaller --onefile --name ble_relay --distpath bin --specpath build --workpath build scripts/ble_relay.py
  44. cp bin/ble_relay cmd/monitor/ble_relay
  45. echo "Step 2: Building Go binary with BLE embedded..."
  46. go mod tidy
  47. mkdir -p dist
  48. go build -tags ble -ldflags "-X main.Version=${VERSION}" -o "dist/${BINARY_NAME}-${PLATFORM}" ./cmd/monitor
  49. rm -f cmd/monitor/ble_relay
  50. echo ""
  51. echo "Done: dist/${BINARY_NAME}-${PLATFORM}"
  52. else
  53. echo "=== Building all platforms (no BLE) ==="
  54. mkdir -p dist
  55. go mod tidy
  56. echo "Building for Linux (amd64)..."
  57. GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-linux-amd64 ./cmd/monitor
  58. echo "Building for Linux (arm64)..."
  59. GOOS=linux GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-linux-arm64 ./cmd/monitor
  60. echo "Building for macOS (amd64)..."
  61. GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-darwin-amd64 ./cmd/monitor
  62. echo "Building for macOS (arm64)..."
  63. GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-darwin-arm64 ./cmd/monitor
  64. echo "Building for Windows (amd64)..."
  65. GOOS=windows GOARCH=amd64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-windows-amd64.exe ./cmd/monitor
  66. echo "Building for Windows (arm64)..."
  67. GOOS=windows GOARCH=arm64 go build -ldflags "-X main.Version=${VERSION}" -o dist/${BINARY_NAME}-windows-arm64.exe ./cmd/monitor
  68. echo ""
  69. echo "Done! Binaries in dist/"
  70. ls -la dist/
  71. fi