I'm working on a go project which has a Makefile like below. Note that the "release" target sets two environment variables so that the "build" target cross-compiles for a specific architecture (which is possibly not that of the build system).
# Generates a container release artifact.
release: export GOOS=linux
release: export GOARCH=amd64
release: build
docker build ...
# Generates an executable for local use.
build: test
go build ...
test: fmt vet
go test ./...
...
The problems is that the GOOS
and GOARCH
env vars are also detected by the "go test" command which causes that step to fail since cross-platform testing apparently is not supported.
To put it another way, it's like I want to clear the env vars for the "test" target, but the example below doesn't appear to unset the vars as expected:
unexport GOOS
unexport GOARCH
test: fmt vet
go test ./...
I could remove the "test" dependency for the "build" target and it works fine but that seems like the wrong approach since I wouldn't want to build or release the binary if the tests fail.
Can I somehow modify the Makefile so that the GOOS
and GOARCH
environment variables are set by the "release" target and used by the "build" target but not the dependent "test" target?
Is there perhaps some other way to cross compile for release and still run the tests as a dependency without otherwise complicating things (such as building via Docker, etc)? Note that the make targets are a bit more complicated than the example above due to several flags for the release and build targets.