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.
1.6 KiB
Installation from source
-
Verify that you have Go 1.13+ installed
$ go versionIf
gois not installed, follow instructions on the Go website. -
Clone this repository
$ git clone https://github.com/cli/cli.git gh-cli $ cd gh-cli -
Build and install
Unix-like systems
# installs to '/usr/local' by default; sudo may be required $ make install # or, install to a different location $ make install prefix=/path/to/ghWindows
# build the `bin\gh.exe` binary > go run script\build.goThere is no install step available on Windows.
-
Run
gh versionto check if it worked.Windows
Run
bin\gh versionto 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:
# on a Unix-like system:
$ GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 make clean bin/gh
# 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.