Skip to content

Instantly share code, notes, and snippets.

@Dapacruz
Last active August 19, 2021 16:47
Show Gist options
  • Save Dapacruz/58b6efa1f38ccf4a06f3236d3876779b to your computer and use it in GitHub Desktop.
Save Dapacruz/58b6efa1f38ccf4a06f3236d3876779b to your computer and use it in GitHub Desktop.
Compile Go Apps for Multiple Platforms
<#
.Synopsis
Compile Go Apps for Multiple Platforms
.Parameter Platform
Platforms to compile for
.Notes
Developed by David Cruz
.Example
$ Build-GoSource.ps1 -Platform Windows-AMD64, Darwin-AMD64, Linux-AMD64
#>
[CmdletBinding()]
Param (
[Parameter()]
[ValidateSet('Aix-PPC64', 'Android-386', 'Android-AMD64', 'Android-ARM', 'Android-ARM64', 'Darwin-386',
'Darwin-AMD64', 'Darwin-ARM', 'Darwin-ARM64', 'Dragonfly-AMD64', 'Freebsd-386', 'Freebsd-AMD64',
'Freebsd-ARM', 'Illumos-AMD64', 'Js-WASM', 'Linux-386', 'Linux-AMD64', 'Linux-ARM', 'Linux-ARM64',
'Linux-MIPS', 'Linux-MIPS64', 'Linux-MIPS64LE', 'Linux-MIPSLE', 'Linux-PPC64', 'Linux-PPC64LE',
'Linux-S390X', 'Nacl-386', 'Nacl-AMD64P32', 'Nacl-ARM', 'Netbsd-386', 'Netbsd-AMD64', 'Netbsd-ARM',
'Netbsd-ARM64', 'Openbsd-386', 'Openbsd-AMD64', 'Openbsd-ARM', 'Openbsd-ARM64', 'Plan9-386',
'Plan9-AMD64', 'Plan9-ARM', 'Solaris-AMD64', 'Windows-386', 'Windows-AMD64', 'Windows-ARM')]
[String[]]$Platform = 'Windows-AMD64'
)
Begin {
$Platforms = @{
'Current' = @{ GOOS = $Env:GOOS; GOARCH = $Env:GOARCH }
'Aix-PPC64' = @{ GOOS = 'aix'; GOARCH = 'ppc64' }
'Android-386' = @{ GOOS = 'android'; GOARCH = '386' }
'Android-AMD64' = @{ GOOS = 'android'; GOARCH = 'amd64' }
'Android-ARM' = @{ GOOS = 'android'; GOARCH = 'arm' }
'Android-ARM64' = @{ GOOS = 'android'; GOARCH = 'arm64' }
'Darwin-386' = @{ GOOS = 'darwin'; GOARCH = '386' }
'Darwin-AMD64' = @{ GOOS = 'darwin'; GOARCH = 'amd64' }
'Darwin-ARM' = @{ GOOS = 'darwin'; GOARCH = 'arm' }
'Darwin-ARM64' = @{ GOOS = 'darwin'; GOARCH = 'arm64' }
'Dragonfly-AMD64' = @{ GOOS = 'dragonfly'; GOARCH = 'amd64' }
'Freebsd-386' = @{ GOOS = 'freebsd'; GOARCH = '386' }
'Freebsd-AMD64' = @{ GOOS = 'freebsd'; GOARCH = 'amd64' }
'Freebsd-ARM' = @{ GOOS = 'freebsd'; GOARCH = 'arm' }
'Illumos-AMD64' = @{ GOOS = 'illumos'; GOARCH = 'amd64' }
'Js-WASM' = @{ GOOS = 'js'; GOARCH = 'wasm' }
'Linux-386' = @{ GOOS = 'linux'; GOARCH = '386' }
'Linux-AMD64' = @{ GOOS = 'linux'; GOARCH = 'amd64' }
'Linux-ARM' = @{ GOOS = 'linux'; GOARCH = 'arm' }
'Linux-ARM64' = @{ GOOS = 'linux'; GOARCH = 'arm64' }
'Linux-MIPS' = @{ GOOS = 'linux'; GOARCH = 'mips' }
'Linux-MIPS64' = @{ GOOS = 'linux'; GOARCH = 'mips64' }
'Linux-MIPS64LE' = @{ GOOS = 'linux'; GOARCH = 'mips64le' }
'Linux-MIPSLE' = @{ GOOS = 'linux'; GOARCH = 'mipsle' }
'Linux-PPC64' = @{ GOOS = 'linux'; GOARCH = 'ppc64' }
'Linux-PPC64LE' = @{ GOOS = 'linux'; GOARCH = 'ppc64le' }
'Linux-S390X' = @{ GOOS = 'linux'; GOARCH = 's390x' }
'Nacl-386' = @{ GOOS = 'nacl'; GOARCH = '386' }
'Nacl-AMD64P32' = @{ GOOS = 'nacl'; GOARCH = 'amd64p32' }
'Nacl-ARM' = @{ GOOS = 'nacl'; GOARCH = 'arm' }
'Netbsd-386' = @{ GOOS = 'netbsd'; GOARCH = '386' }
'Netbsd-AMD64' = @{ GOOS = 'netbsd'; GOARCH = 'amd64' }
'Netbsd-ARM' = @{ GOOS = 'netbsd'; GOARCH = 'arm' }
'Netbsd-ARM64' = @{ GOOS = 'netbsd'; GOARCH = 'arm64' }
'Openbsd-386' = @{ GOOS = 'openbsd'; GOARCH = '386' }
'Openbsd-AMD64' = @{ GOOS = 'openbsd'; GOARCH = 'amd64' }
'Openbsd-ARM' = @{ GOOS = 'openbsd'; GOARCH = 'arm' }
'Openbsd-ARM64' = @{ GOOS = 'openbsd'; GOARCH = 'arm64' }
'Plan9-386' = @{ GOOS = 'plan9'; GOARCH = '386' }
'Plan9-AMD64' = @{ GOOS = 'plan9'; GOARCH = 'amd64' }
'Plan9-ARM' = @{ GOOS = 'plan9'; GOARCH = 'arm' }
'Solaris-AMD64' = @{ GOOS = 'solaris'; GOARCH = 'amd64' }
'Windows-386' = @{ GOOS = 'windows'; GOARCH = '386' }
'Windows-AMD64' = @{ GOOS = 'windows'; GOARCH = 'amd64' }
'Windows-ARM' = @{ GOOS = 'windows'; GOARCH = 'arm' }
}
}
Process {
foreach ($p in $Platform) {
$Env:GOOS = $Platforms.$p.GOOS
$Env:GOARCH = $Platforms.$p.GOARCH
$output_path = "output/$($Platforms.$p.GOOS)-$($Platforms.$p.GOARCH)"
$current_fg_color = [console]::ForegroundColor
# Create output folder
New-Item -Path $output_path -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
Write-Host "Compiling for $p ... " -NoNewline
# Set console foreground color to red for error output
[console]::ForegroundColor = 'Red'
# Execute Go build process
$result = Start-Process -FilePath 'C:\Program Files\Go\bin\go.exe' -ArgumentList "build -o $output_path" -Wait -NoNewWindow -PassThru
# Reset console foreground color
[console]::ForegroundColor = $current_fg_color
if (-not $result.ExitCode) {
Write-Host 'success' -ForegroundColor Green
}
}
}
End {
$Env:GOOS = $Platforms.Current.GOOS
$Env:GOARCH = $Platforms.Current.GOARCH
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment