maerics
maerics

Reputation: 156682

Makefile environment variable for specific target

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.

Upvotes: 3

Views: 3818

Answers (3)

Beta
Beta

Reputation: 99172

I would do it this way:

test: export GOOS=
test: export GOARCH=
test: fmt vet
    go test ./...

Upvotes: 5

maerics
maerics

Reputation: 156682

I just realized that I can unset the env vars in the troublesome test command itself:

test: fmt vet
    GOOS= GOARCH= go test ./...

This way the "go test" command will always use the local system OS and architecture (as it should!).

Thx @KonstantinItskov for the inspiration

Upvotes: 0

Konstantin Itskov
Konstantin Itskov

Reputation: 145

Why not do something like this? It will just set those environment variables during that go execution.

    # Generates a container release artifact.
release: build
  docker build ...

# Generates an executable for local use.
build: test
  GOOS=linux GOARCH=amd64 go build ...

test: fmt vet
  go test ./...

...

Upvotes: 2

Related Questions