Multiplatform Builds
Building for Multiple Platforms
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.
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.
Defining Platform Selectors
You can specify platform requirements using the platform
field:
default_platform: os: - linux arch: - amd64
targets: # default_platform 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_platform platform: os: - darwin arch: - arm64 inputs: - cmd/**/*.go - go.mod outputs: - dist/myapp-darwin-arm64
In this example:
build_linux_amd64
will only run on Linux AMD64 hostsbuild_darwin_arm64
will only run on macOS ARM64 hosts (like Apple Silicon Macs)
Platform Selector Behavior
When you run grog build
, Grog will:
- Check each target’s platform selector against the os and arch the grog binary was built for
- Skip targets that don’t match the current platform
- 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.
Skipping platform selectors
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
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 platform: os: - linux outputs: - dist/myapp-linux
- name: package_linux dependencies: - :linux_binary command: tar -czf dist/myapp-linux.tar.gz dist/myapp-linux platform: os: - linux 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.