3 ms·
> I think this post is well intentioned but misses the point entirely. > > GOOS=darwin GOARCH=arm64 go build -ldflags="-X 'main.Version=v1.2.3'" > > Execu
by nrclark 2y ago
> I think this post is well intentioned but misses the point entirely.
>
> GOOS=darwin GOARCH=arm64 go build -ldflags="-X 'main.Version=v1.2.3'"
>
> Executing this with variables for os/arch/version is not straightforward in a makefile. You might argue that it's a flaw with the `go build` command, but replace go with cargo/dotnet/maven and you have the same problem.
This is pretty easy in Make, especially since GOOS and GOARCH are environment variables. One way to do it could look like this:
export GOOS ?= darwin
export GOARCH ?= arm64
VERSION ?= 0.0.0+$(shell git describe --dirty --always)
build:
$(if $(VERSION),,$(error VERSION was not set))
go build -ldflags="-X 'main.Version=v$(VERSION)'"
you could run it as:
$ make build (uses darwin/arm64/0.0.0+<git ref>)
$ make build GOOS=linux VERSION=1.0.0 (uses linux/arm64/1.0.0)
$ GOOS=templeos GOARCH=sparc make build VERSION=1.1.0 (uses templeos/sparc/1.1.0)