Multiplatform Builds
Building for Multiple Platforms
Section titled “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
Section titled “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
Section titled “Defining Platform Selectors”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-arm64In this example:
build_linux_amd64will only run on Linux AMD64 hostsbuild_darwin_arm64will 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.
Platform Selector Behavior
Section titled “Platform Selector Behavior”When you run grog build, Grog will:
- Check each target’s platform selectors against the os/arch platform 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
Section titled “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
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.gzIf 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.