Skip to content

Cross Compilation

The Go Driver can be cross-compiled to different platforms and will require different steps depending on the target platform.

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.

  • Docker installed on your system
  • Your Go application using Valkey GLIDE

The official Golang Docker image includes the necessary tools for cross-compiling to Linux targets:

Terminal window
# Build for Linux AMD64
docker 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 ARM64
docker 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 ./..."

Due to Apple’s platform restrictions, cross-compiling CGO code for macOS requires building on a macOS system:

Terminal window
# On a macOS system
CGO_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:

  1. Runs on both push to main and pull requests
  2. Creates a build matrix for all four supported platforms
  3. Uses the appropriate runner for each OS
  4. Sets the correct build flags for each platform
  5. 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.

If you encounter issues during cross-compilation:

  1. Missing Libraries: Ensure Valkey GLIDE is properly installed and its pre-compiled libraries are available
  2. Linker Errors: Check that CGO is enabled and the correct cross-compiler is being used
  3. Architecture Mismatch: Verify that you’re using the correct GOARCH value for your target platform
  4. 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.