Skip to content

Instantly share code, notes, and snippets.

@dberstein
Last active April 30, 2023 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dberstein/7e27453ed551742c0136190391c268c9 to your computer and use it in GitHub Desktop.
Save dberstein/7e27453ed551742c0136190391c268c9 to your computer and use it in GitHub Desktop.
Golang os/arch, arch/os
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
const OsArch = "os_arch"
const ArchOs = "arch_os"
func makeList(content string) map[string]map[string][]string {
list := map[string]map[string][]string{
OsArch: map[string][]string{},
ArchOs: map[string][]string{},
}
scanner := bufio.NewScanner(strings.NewReader(content))
for scanner.Scan() {
parts := strings.Split(scanner.Text(), "/")
if _, ok := list[OsArch][parts[0]]; !ok {
list[OsArch][parts[0]] = []string{}
}
list[OsArch][parts[0]] = append(list[OsArch][parts[0]], parts[1])
if _, ok := list[ArchOs][parts[1]]; !ok {
list[ArchOs][parts[1]] = []string{}
}
list[ArchOs][parts[1]] = append(list[ArchOs][parts[1]], parts[0])
}
return list
}
func main() {
output, err := exec.Command("go", "tool", "dist", "list").Output()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(127)
}
list := makeList(string(output))
keys := []string{}
for k := range list[OsArch] {
for _, v := range list[OsArch][k] {
keys = append(keys, fmt.Sprintf("%s/%s", k, v))
}
}
fmt.Println("# TARGETS=$(shell go tool dist list)")
fmt.Println("TARGETS=" + strings.Join(keys, " "))
fmt.Println()
fmt.Println("$(TARGETS): %:")
fmt.Println("\t@GOOS=$(subst /, GOARCH=,$@) go build -o dist/$@/artifact ...")
fmt.Println()
for os := range list[OsArch] {
fmt.Printf("os/%s: ", os)
for _, arch := range list[OsArch][os] {
fmt.Printf("%s/%s ", os, arch)
}
fmt.Print("\n")
}
fmt.Println()
for arch := range list[ArchOs] {
fmt.Printf("arch/%s: ", arch)
for _, os := range list[ArchOs][arch] {
fmt.Printf("%s/%s ", os, arch)
}
fmt.Print("\n")
}
}
# TARGETS=$(shell go tool dist list)
TARGETS=freebsd/386 freebsd/amd64 freebsd/arm freebsd/arm64 freebsd/riscv64 js/wasm dragonfly/amd64 illumos/amd64 linux/386 linux/amd64 linux/arm linux/arm64 linux/loong64 linux/mips linux/mips64 linux/mips64le linux/mipsle linux/ppc64 linux/ppc64le linux/riscv64 linux/s390x openbsd/386 openbsd/amd64 openbsd/arm openbsd/arm64 openbsd/mips64 android/386 android/amd64 android/arm android/arm64 darwin/amd64 darwin/arm64 ios/amd64 ios/arm64 plan9/386 plan9/amd64 plan9/arm solaris/amd64 windows/386 windows/amd64 windows/arm windows/arm64 aix/ppc64 netbsd/386 netbsd/amd64 netbsd/arm netbsd/arm64
$(TARGETS): %:
@GOOS=$(subst /, GOARCH=,$@) go build -o dist/$@/artifact ...
os/dragonfly: dragonfly/amd64
os/illumos: illumos/amd64
os/linux: linux/386 linux/amd64 linux/arm linux/arm64 linux/loong64 linux/mips linux/mips64 linux/mips64le linux/mipsle linux/ppc64 linux/ppc64le linux/riscv64 linux/s390x
os/openbsd: openbsd/386 openbsd/amd64 openbsd/arm openbsd/arm64 openbsd/mips64
os/android: android/386 android/amd64 android/arm android/arm64
os/darwin: darwin/amd64 darwin/arm64
os/ios: ios/amd64 ios/arm64
os/plan9: plan9/386 plan9/amd64 plan9/arm
os/solaris: solaris/amd64
os/windows: windows/386 windows/amd64 windows/arm windows/arm64
os/aix: aix/ppc64
os/netbsd: netbsd/386 netbsd/amd64 netbsd/arm netbsd/arm64
os/freebsd: freebsd/386 freebsd/amd64 freebsd/arm freebsd/arm64 freebsd/riscv64
os/js: js/wasm
arch/loong64: linux/loong64
arch/ppc64le: linux/ppc64le
arch/s390x: linux/s390x
arch/ppc64: aix/ppc64 linux/ppc64
arch/arm: android/arm freebsd/arm linux/arm netbsd/arm openbsd/arm plan9/arm windows/arm
arch/riscv64: freebsd/riscv64 linux/riscv64
arch/wasm: js/wasm
arch/mips: linux/mips
arch/mips64: linux/mips64 openbsd/mips64
arch/amd64: android/amd64 darwin/amd64 dragonfly/amd64 freebsd/amd64 illumos/amd64 ios/amd64 linux/amd64 netbsd/amd64 openbsd/amd64 plan9/amd64 solaris/amd64 windows/amd64
arch/arm64: android/arm64 darwin/arm64 freebsd/arm64 ios/arm64 linux/arm64 netbsd/arm64 openbsd/arm64 windows/arm64
arch/mipsle: linux/mipsle
arch/386: android/386 freebsd/386 linux/386 netbsd/386 openbsd/386 plan9/386 windows/386
arch/mips64le: linux/mips64le
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment