ToolsSlide 82 of 85
82targetTools
Building for another platform
The last step of a build is 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 the pair is checked before anything is compiled. go tool dist list names every platform the toolchain knows, and one it does not name is refused there and then, as a mistake in what was typed — not ten seconds later, by a build failing in a language you did not write. The list is asked for rather than kept, so a platform Go learns tomorrow needs no change here.
- What comes out 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 rather than the machine:--target windows/amd64writesmain-windows-amd64.exefrom anywhere. - 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 build it always was. - Only
buildandexporttake it, the two commands that write something for a platform.check,emit,testandcontainerrefuse the flag rather than quietly ignoring it. Underhive run, everything after the entrypoint belongs to the program, so a--targetwritten there is passed straight through 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.