cli/docs/source.md
Mislav Marohnić d43720620e Tweak build scripts to enable cross-compiling
The main build script for this project is `script/build.go` which
implements Makefile-like building of the `gh` binary and associated man
pages. Our Makefile defers to the Go script.

However, when setting GOOS, GOARCH, and other environment variables to
modify the target for the resulting binary, these environment variables
would affect the execution of `build.go` as well, which was unintended.

This tweaks our Makefile to reset variables like GOOS and GOARCH when
building the `build.go` script itself, ensuring that the built script
runs on the same platform, and adds the ability to pass environment
variables as arguments to `go run script/build.go`. This allows the
following usage on platforms without `make`:

    go run script/build.go GOOS=linux

With this style of invocation, the GOOS setting does not actually affect
`go run` itself; just the `go build` that is executed in a child process.
2021-04-09 15:48:12 +02:00

59 lines
1.6 KiB
Markdown

# Installation from source
0. Verify that you have Go 1.13+ installed
```sh
$ go version
```
If `go` is not installed, follow instructions on [the Go website](https://golang.org/doc/install).
1. Clone this repository
```sh
$ git clone https://github.com/cli/cli.git gh-cli
$ cd gh-cli
```
2. Build and install
#### Unix-like systems
```sh
# installs to '/usr/local' by default; sudo may be required
$ make install
# or, install to a different location
$ make install prefix=/path/to/gh
```
#### Windows
```pwsh
# build the `bin\gh.exe` binary
> go run script\build.go
```
There is no install step available on Windows.
3. Run `gh version` to check if it worked.
#### Windows
Run `bin\gh version` to check if it worked.
## Cross-compiling binaries for different platforms
You can use any platform with Go installed to build a binary that is intended for another platform
or CPU architecture. This is achieved by setting environment variables such as GOOS and GOARCH.
For example, to compile the `gh` binary for the 32-bit Raspberry Pi OS:
```sh
# on a Unix-like system:
$ GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 make clean bin/gh
```
```pwsh
# on Windows, pass environment variables as arguments to the build script:
> go run script\build.go clean bin\gh GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0
```
Run `go tool dist list` to list all supported values of GOOS/GOARCH.
Tip: to reduce the size of the resulting binary, you can use `GO_LDFLAGS="-s -w"`. This omits
symbol tables used for debugging. See the list of [supported linker flags](https://golang.org/cmd/link/).