Cross Compilation
The Go Driver can be cross-compiled to different platforms and will require different steps depending on the target platform.
Cross-Compiling with Docker
Section titled “Cross-Compiling with Docker”Valkey GLIDE uses CGO to interface with its Rust-based core, shipped as pre-built static libraries for each target platform, which can make cross-compilation challenging. Docker provides a convenient solution for cross-compiling applications that use Valkey GLIDE.
Prerequisites
Section titled “Prerequisites”- Docker installed on your system
- Your Go application using Valkey GLIDE
Cross-Compiling for Linux (AMD64/ARM64)
Section titled “Cross-Compiling for Linux (AMD64/ARM64)”The official Golang Docker image includes the necessary tools for cross-compiling to Linux targets:
# Build for Linux AMD64docker run --rm -v "$PWD":/app -w /app golang:1.22 \ bash -c "CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o myapp-linux-amd64 ./..."
# Build for Linux ARM64docker run --rm -v "$PWD":/app -w /app golang:1.22 \ bash -c "CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go build -o myapp-linux-arm64 ./..."Cross-Compiling for macOS
Section titled “Cross-Compiling for macOS”Due to Apple’s platform restrictions, cross-compiling CGO code for macOS requires building on a macOS system:
# On a macOS systemCGO_ENABLED=1 GOOS=darwin GOARCH=amd64 go build -o myapp-darwin-amd64 ./...CGO_ENABLED=1 GOOS=darwin GOARCH=arm64 go build -o myapp-darwin-arm64 ./...Setting Up GitHub Actions for Multi-Platform Builds
Section titled “Setting Up GitHub Actions for Multi-Platform Builds”You can use GitHub Actions to automatically build your application for all supported platforms. Here’s a sample workflow file:
name: Multi-Platform Build
on: push: branches: [ main ] pull_request: branches: [ main ]
jobs: build: name: Build ${{ matrix.os }}-${{ matrix.arch }} runs-on: ${{ matrix.runner }} strategy: fail-fast: false matrix: include: - os: linux arch: amd64 runner: ubuntu-latest - os: linux arch: arm64 runner: ubuntu-latest - os: darwin arch: amd64 runner: macos-latest - os: darwin arch: arm64 runner: macos-latest
steps: - uses: actions/checkout@v4
- name: Set up Go uses: actions/setup-go@v5 with: go-version: '1.22'
- name: Build Linux if: matrix.os == 'linux' run: | if [ "${{ matrix.arch }}" = "amd64" ]; then CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -o myapp-linux-amd64 ./... else CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go build -o myapp-linux-arm64 ./... fi
- name: Build macOS if: matrix.os == 'darwin' run: | CGO_ENABLED=1 GOOS=darwin GOARCH=${{ matrix.arch }} go build -o myapp-darwin-${{ matrix.arch }} ./...
- name: Upload artifacts uses: actions/upload-artifact@v4 with: name: myapp-${{ matrix.os }}-${{ matrix.arch }} path: myapp-${{ matrix.os }}-${{ matrix.arch }}This workflow:
- Runs on both push to main and pull requests
- Creates a build matrix for all four supported platforms
- Uses the appropriate runner for each OS
- Sets the correct build flags for each platform
- Uploads the compiled binaries as artifacts
You can customize this workflow to fit your specific application needs, such as adding tests, packaging steps, or deployment actions.
Troubleshooting Cross-Compilation
Section titled “Troubleshooting Cross-Compilation”If you encounter issues during cross-compilation:
- Missing Libraries: Ensure Valkey GLIDE is properly installed and its pre-compiled libraries are available
- Linker Errors: Check that CGO is enabled and the correct cross-compiler is being used
- Architecture Mismatch: Verify that you’re using the correct GOARCH value for your target platform
- Docker Issues: Make sure your Docker container has sufficient resources and access to the source code
For more complex cross-compilation scenarios or if you encounter specific issues, please open a GitHub issue for assistance.