Slide 51 of 53

Building for another platform

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`. `check` builds nothing for another platform.
Because the last step is the Go toolchain, 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, and nothing else does.A target is a goos/goarch pair, and the pair is checked before anything is compiled. `go tool dist list` names every one the toolchain knows, and one it does not name is refused there and then rather than ten seconds later, by a build failing in a language you did not write. The list is asked for rather than kept anywhere, so a platform Go learns tomorrow needs no change here. And a platform that does not exist is a mistake in what was typed, so it is reported as one — beside the other command-line mistakes, rather than against a line of a program that has nothing wrong with it.What comes out says what it is for. A host build of main.hive is main, and a cross build is main-linux-arm64: two files that cannot run in the same places must not answer to one name, and a cross build that quietly replaced the local one would only be found out by running it. The .exe follows the target rather than the machine, so --target windows/amd64 writes main-windows-amd64.exe from anywhere.CGO_ENABLED=0 goes with a cross build and only with one. Everything the runtime is made of is Go, so no Hive program needs a C compiler — and a C cross-compiler is the one thing a machine with the Go toolchain on it cannot be assumed to have. A build for the machine it is running on is given no environment at all, and is therefore byte for byte the build it always was.--target is a flag of build, and of build alone. check and emit compile nothing; test and container are both about this machine; and each of the four refuses the flag rather than accepting it and quietly ignoring it. run is the exception that proves the rule: everything after the entrypoint there belongs to the program being run, so a --target written after it is passed straight through, unread.