Skip to content

Multiplatform Builds

Often you will want your code to run on multiple operating systems and architectures. For example, you might want to build binaries or docker images that run both on Linux amd64 and MacOS arm64. Grog has one built-in solution for this: platform selectors.

Grog’s platform selectors allow you to define targets that only run on specific platforms. When a target doesn’t match the current host’s platform, Grog will skip it automatically.

You can specify platform requirements using the platforms field:

default_platforms:
- linux/amd64
targets:
# default_platforms is applied here
- name: build_linux_amd64
command: go build -o dist/myapp-linux-amd64 ./cmd/myapp
inputs:
- cmd/**/*.go
- go.mod
outputs:
- dist/myapp-linux-amd64
- name: build_darwin_arm64
command: go build -o dist/myapp-darwin-arm64 ./cmd/myapp
# This overrides default_platforms
platforms:
- darwin/arm64
inputs:
- cmd/**/*.go
- go.mod
outputs:
- dist/myapp-darwin-arm64

In this example:

  • build_linux_amd64 will only run on Linux AMD64 hosts
  • build_darwin_arm64 will only run on macOS ARM64 hosts (like Apple Silicon Macs)

Each entry in platforms must be a full os/arch identifier. To support multiple variants for the same target, list each supported identifier.

When you run grog build, Grog will:

  1. Check each target’s platform selectors against the os/arch platform the grog binary was built for
  2. Skip targets that don’t match the current platform
  3. Build targets that do match the platform

This allows you to define all platform variants in a single build file, but only build the ones appropriate for the current environment.

There may be scenarios where you want to skip platform selectors. To do so, pass the --all-platforms, (shorthand: -a) flag to build and querying commands.

Handling Dependencies with Platform Selectors

Section titled “Handling Dependencies with Platform Selectors”

When a target depends on a platform-specific target, Grog enforces platform compatibility:

targets:
- name: linux_binary
command: go build -o dist/myapp-linux ./cmd/myapp
platforms:
- linux/amd64
outputs:
- dist/myapp-linux
- name: package_linux
dependencies:
- :linux_binary
command: tar -czf dist/myapp-linux.tar.gz dist/myapp-linux
platforms:
- linux/amd64
outputs:
- dist/myapp-linux.tar.gz

If you try to build package_linux on a non-Linux platform, Grog will fail with an error because the dependency linux_binary cannot be built on the current platform.