ToolsSlide 83 of 86
83targetTools
Building for another platform
A build ends with the Go toolchain, so a Hive program is built for another platform the way any Go program is: by telling the toolchain which one. hive build takes a --target, a goos/goarch pair, and checks the pair before compiling anything. go tool dist list names every platform the toolchain knows; a pair it does not name is refused immediately, as a mistake in what was typed, rather than ten seconds later by a build failing in a language you did not write. The list is asked for, not kept, so a platform Go adds later needs no change in Hive.
- The output's name says what it is for. A host build of
main.hiveismain, and a cross build ismain-linux-arm64, so a cross build never quietly replaces the local one. The.exefollows the target, not the machine:--target windows/amd64writesmain-windows-amd64.exefrom any machine. - No C compiler is needed. The runtime is all Go, so a cross build runs with
CGO_ENABLED=0, and a build for this machine is given no environment at all: byte for byte the same build as without--target. - Only
buildandexporttake the flag, the two commands that write something for a platform.check,emit,testandcontainerrefuse it rather than quietly ignoring it. Withhive run, everything after the entrypoint belongs to the program, so a--targetwritten there is passed straight to it.
One machine, three platforms
$ hive build main.hive
Compiled main.hive -> main (1s)
$ hive build main.hive --target linux/arm64
Compiled main.hive -> main-linux-arm64 (2s)
$ hive build main.hive --target=windows/amd64
Compiled main.hive -> main-windows-amd64.exe (2s)The three ways it says no
$ hive build main.hive --target linux/sparc
hive: `--target linux/sparc` is not a platform the Go toolchain builds for. A target is a `goos/goarch` pair — `linux/arm64`, `darwin/arm64`, `windows/amd64` — and `go tool dist list` names every one it knows.
$ hive build main.hive --target
hive: `--target` was given no platform to build for. It takes a `goos/goarch` pair — `hive build <entrypoint.hive> --target linux/arm64`.
$ hive check main.hive --target linux/arm64
hive: `--target` is a flag of `hive build` and `hive export`. `check` builds nothing for another platform.