Issue #25: feat: クロスプラットフォーム対応・ビルドシステム最適化

Opened 2025/7/12 by @nyasuto Open
priority: low type: enhancement

Description

🎯 feat: クロスプラットフォーム対応・ビルドシステム最適化

Priority: MEDIUM

Impact: 幅広いプラットフォーム対応、ビルド効率向上、配布性向上

Component: ビルドシステム、クロスコンパイル、最適化

Files: Makefile, scripts/build/, .goreleaser.yml

Problem Description

Issue #5で開発環境標準化が設計されましたが、異なるOS・アーキテクチャでの動作保証とビルド最適化が必要です。Windows/macOS/Linux、AMD64/ARM64対応、GPU環境の差異対応、配布用バイナリ最適化を実現する必要があります。

Recommended Solution

クロスプラットフォーム対応システム

  1. Multi-Platform Build System (Makefile拡張)

    # Cross-platform build targets
    PLATFORMS := windows/amd64 linux/amd64 linux/arm64 darwin/amd64 darwin/arm64
    BUILD_DIR := bin
    VERSION := $(shell git describe --tags --always --dirty)
    
    .PHONY: build-all-platforms
    build-all-platforms: ## Build for all supported platforms
    	@echo "🔨 Building for all platforms..."
    	@for platform in $(PLATFORMS); do \
    		OS=$$(echo $$platform | cut -d'/' -f1); \
    		ARCH=$$(echo $$platform | cut -d'/' -f2); \
    		echo "Building for $$OS/$$ARCH..."; \
    		GOOS=$$OS GOARCH=$$ARCH make build-platform OS=$$OS ARCH=$$ARCH; \
    	done
    
    .PHONY: build-platform
    build-platform: ## Build for specific platform
    	@mkdir -p $(BUILD_DIR)/$(OS)-$(ARCH)
    	@GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build \
    		-ldflags="-s -w -X main.version=$(VERSION)" \
    		-o $(BUILD_DIR)/$(OS)-$(ARCH)/bee$(if $(filter windows,$(OS)),.exe) \
    		./cmd/bee
    
    .PHONY: build-gpu
    build-gpu: ## Build with GPU support
    	@echo "🚀 Building with GPU support..."
    	@CGO_ENABLED=1 go build \
    		-tags="gpu cuda" \
    		-ldflags="-s -w -X main.version=$(VERSION)" \
    		-o $(BUILD_DIR)/bee-gpu \
    		./cmd/bee
    
  2. GoReleaser Configuration (.goreleaser.yml)

    project_name: bee
    
    before:
      hooks:
        - go mod tidy
        - make test
        - make benchmark-quick
    
    builds:
      - id: bee-cpu
        binary: bee
        main: ./cmd/bee
        env:
          - CGO_ENABLED=0
        goos:
          - linux
          - windows
          - darwin
        goarch:
          - amd64
          - arm64
        ldflags:
          - -s -w
          - -X main.version={{.Version}}
          - -X main.commit={{.Commit}}
          - -X main.date={{.Date}}
          
      - id: bee-gpu
        binary: bee-gpu
        main: ./cmd/bee
        env:
          - CGO_ENABLED=1
        tags:
          - gpu
          - cuda
        goos:
          - linux
        goarch:
          - amd64
        ldflags:
          - -s -w
          - -X main.version={{.Version}}
          - -X main.commit={{.Commit}}
    
    archives:
      - id: default
        builds:
          - bee-cpu
        name_template: 'bee_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
        format_overrides:
          - goos: windows
            format: zip
    
    release:
      github:
        owner: nyasuto
        name: bee
      name_template: "Release {{ .Version }}"
    
  3. Platform-Specific Build Scripts (scripts/build/)

    #\!/bin/bash
    # scripts/build/build-optimized.sh
    
    set -e
    
    PLATFORM=${1:-$(go env GOOS)/$(go env GOARCH)}
    OS=$(echo $PLATFORM | cut -d'/' -f1)
    ARCH=$(echo $PLATFORM | cut -d'/' -f2)
    
    echo "🔨 Building optimized binary for $OS/$ARCH..."
    
    # Platform-specific optimizations
    case "$OS-$ARCH" in
        "linux-amd64")
            # Linux AMD64 optimizations
            LDFLAGS="-s -w -linkmode external -extldflags '-static'"
            TAGS="netgo osusergo"
            ;;
        "linux-arm64")
            # ARM64 optimizations
            LDFLAGS="-s -w"
            TAGS="netgo"
            ;;
        "darwin-amd64"|"darwin-arm64")
            # macOS optimizations
            LDFLAGS="-s -w"
            TAGS=""
            ;;
        "windows-amd64")
            # Windows optimizations
            LDFLAGS="-s -w -H=windowsgui"
            TAGS=""
            ;;
    esac
    
    # Build with optimizations
    GOOS=$OS GOARCH=$ARCH CGO_ENABLED=0 go build \
        -tags="$TAGS" \
        -ldflags="$LDFLAGS -X main.version=$(git describe --tags)" \
        -trimpath \
        -o bin/bee-$OS-$ARCH$(if [ "$OS" = "windows" ]; then echo ".exe"; fi) \
        ./cmd/bee
    
    echo "✅ Build complete: bin/bee-$OS-$ARCH"
    

GPU環境対応

  1. Conditional GPU Support (pkg/gpu/)

    // pkg/gpu/detection.go
    //go:build gpu
    
    package gpu
    
    import (
        "log"
        "runtime"
    )
    
    // GPUInfo represents available GPU information
    type GPUInfo struct {
        Available bool
        Type      string  // "cuda", "opencl", "metal", "none"
        Memory    uint64  // Available GPU memory in bytes
        Cores     int     // Number of cores/SMs
    }
    
    // DetectGPU detects available GPU capabilities
    func DetectGPU() GPUInfo {
        switch runtime.GOOS {
        case "linux", "windows":
            return detectCUDA()
        case "darwin":
            return detectMetal()
        default:
            return GPUInfo{Available: false, Type: "none"}
        }
    }
    
    func detectCUDA() GPUInfo {
        // CUDA detection implementation
        // Use CGO to call CUDA runtime API
        return GPUInfo{
            Available: true,
            Type:      "cuda",
            Memory:    getGPUMemory(),
            Cores:     getGPUCores(),
        }
    }
    
  2. Fallback Implementation (pkg/gpu/cpu_fallback.go)

    //go:build \!gpu
    
    package gpu
    
    // CPU-only fallback implementation
    func DetectGPU() GPUInfo {
        return GPUInfo{
            Available: false,
            Type:      "none",
            Memory:    0,
            Cores:     0,
        }
    }
    
    // Use CPU-optimized implementations
    func MatMul(a, b, c [][]float64) {
        // Optimized CPU matrix multiplication
        matMulCPU(a, b, c)
    }
    

パフォーマンス最適化

  1. Compiler Optimization Flags (scripts/build/optimize.sh)

    #\!/bin/bash
    # Advanced Go compiler optimizations
    
    # Profile-guided optimization (PGO)
    if [ -f "cpu.prof" ]; then
        echo "🎯 Using Profile-Guided Optimization..."
        go build -pgo=cpu.prof -o bin/bee-pgo ./cmd/bee
    fi
    
    # Architecture-specific optimizations
    case "$(go env GOARCH)" in
        "amd64")
            # Intel/AMD 64-bit optimizations
            export GOAMD64=v3  # AVX2 support
            ;;
        "arm64")
            # ARM64 optimizations
            export GOARM64=v8.2  # Advanced SIMD
            ;;
    esac
    
    # Memory optimizations
    export GOGC=50  # More aggressive GC for better latency
    export GOMEMLIMIT=1GiB  # Memory limit for GC tuning
    
  2. Static Analysis & Optimization (scripts/analyze/)

    #\!/bin/bash
    # scripts/analyze/performance.sh
    
    echo "📊 Running performance analysis..."
    
    # Escape analysis
    go build -gcflags="-m -m" ./... 2> escape-analysis.log
    
    # Inline analysis  
    go build -gcflags="-d=ssa/check_bce/debug=1" ./... 2> bounds-check.log
    
    # Assembly output for critical functions
    go tool compile -S cmd/bee/main.go > assembly.out
    
    # Generate optimization report
    python3 scripts/analyze/generate-opt-report.py
    

Testing & Validation

  1. Cross-Platform Testing (scripts/test/)

    #\!/bin/bash
    # scripts/test/cross-platform.sh
    
    PLATFORMS="linux/amd64 darwin/amd64 windows/amd64"
    
    for platform in $PLATFORMS; do
        OS=$(echo $platform | cut -d'/' -f1)
        ARCH=$(echo $platform | cut -d'/' -f2)
        
        echo "🧪 Testing $OS/$ARCH build..."
        
        # Build for platform
        GOOS=$OS GOARCH=$ARCH make build-platform OS=$OS ARCH=$ARCH
        
        # Run platform-specific tests
        if [ "$OS" = "$(go env GOOS)" ]; then
            # Native platform - run full tests
            ./bin/$OS-$ARCH/bee test --quick
        else
            # Cross-compiled - basic validation only
            file ./bin/$OS-$ARCH/bee*
        fi
    done
    
  2. GPU Environment Testing (scripts/test/gpu-validation.sh)

    #\!/bin/bash
    # Validate GPU builds across different environments
    
    echo "🚀 Validating GPU environment support..."
    
    # Test CUDA availability
    if command -v nvidia-smi &> /dev/null; then
        echo "✅ NVIDIA GPU detected"
        make build-gpu
        ./bin/bee-gpu benchmark --gpu-test
    else
        echo "ℹ️  No GPU detected, testing CPU fallback"
        make build
        ./bin/bee benchmark --cpu-only
    fi
    

Package & Distribution

  1. Package Creation (scripts/package/)

    • .deb packages for Debian/Ubuntu
    • .rpm packages for Red Hat/CentOS
    • Homebrew formula for macOS
    • Chocolatey package for Windows
  2. Installation Scripts (scripts/install/)

    #\!/bin/bash
    # install.sh - Universal installation script
    
    set -e
    
    # Detect platform
    OS=$(uname -s | tr '[:upper:]' '[:lower:]')
    ARCH=$(uname -m)
    
    case $ARCH in
        x86_64) ARCH="amd64" ;;
        aarch64|arm64) ARCH="arm64" ;;
    esac
    
    # Download and install
    URL="https://github.com/nyasuto/bee/releases/latest/download/bee_${OS}_${ARCH}.tar.gz"
    curl -sSL $URL | tar xz -C /usr/local/bin/
    
    echo "✅ Bee installed successfully\!"
    echo "Run 'bee --help' to get started"
    

Acceptance Criteria

  • 5プラットフォーム対応ビルドシステム(Windows/Linux/macOS × AMD64/ARM64)
  • GPU対応・CPU fallback実装
  • GoReleaser設定・自動リリース機能
  • プラットフォーム固有最適化・コンパイラフラグ調整
  • クロスプラットフォームテスト・検証システム
  • パッケージ配布・インストールスクリプト
  • 性能プロファイリング・最適化分析ツール

あらゆるプラットフォームで最適な性能を発揮する配布可能なビルドシステム

Comments

コメント機能は現在実装されていません。
GitHub API の comments エンドポイントを統合する予定です。

🤖 AI分析

分類結果

🚀 パフォーマンス
🟢 低
63 スコア
カテゴリ 45
優先度 18
0

適用されたルール

Enhanced Performance Detection
• Body contains keyword: "performance"• Body contains keyword: "optimization"
performanceoptimizationmemory

Details

Assignees:

None

Milestone:

None

Created:

2025/7/12

Updated:

2025/7/13