Skip to content

Building

rob edited this page Jul 12, 2024 · 3 revisions

The Go compiler supports cross-compilation with the GOOS and GOARCH environment variables.

To build your extension for Mac set GOOS=darwin.
I recommend building binaries for both amd64 and arm64 architectures to support both Intel and Apple silicon chips.
These can be combined into a universal binary using a tool such as lipo which can be installed with go install github.com/konoui/lipo@latest.

The following are example build scripts to build binaries for Windows, Mac and Linux using lipo to build the universal binary for Mac.

Bash

#!/bin/bash

name="YourExt"

echo "Building for Windows..."
GOOS=windows GOARCH=amd64 go build -ldflags="-s -w" -trimpath=1 -o ./bin/${name}_windows.exe .

echo "Building for Linux..."
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -trimpath=1 -o ./bin/${name}_linux .

for arch in amd64 arm64; do
  echo "Building for Mac ($arch)..."
  GOOS=darwin GOARCH=$arch go build -ldflags="-s -w" -trimpath=1 -o ./bin/${name}_mac_${arch} .
done
echo "Creating universal binary for Mac..."
lipo -create -output "bin/${name}_mac" bin/${name}_mac_{amd64,arm64} && rm bin/${name}_mac_{amd64,arm64}

Powershell

$name="YourExt"

echo "Building for Windows..."
$env:GOARCH="amd64"
$env:GOOS="windows"
go build -ldflags="-s -w" -trimpath=1 -o ./bin/${name}_windows.exe .

echo "Building for Linux..."
$env:GOOS="linux"
go build -ldflags="-s -w" -trimpath=1 -o ./bin/${name}_linux .

foreach ($arch in @("arm64","amd64")) {
  echo "Building for Mac ($arch)..."
  $env:GOOS="darwin"
  $env:GOARCH="$arch"
  go build -ldflags="-s -w" -trimpath=1 -o ./bin/${name}_mac_${arch} .
}
echo "Creating universal binary for Mac..."
lipo -create -output "bin/${name}_mac" bin/${name}_mac_arm64 bin/${name}_mac_amd64 && Remove-Item bin/* -Include "${name}_mac_*"
Clone this wiki locally