dropbox1111
dropbox1111
2019-05-06 15:15

特定目标的Makefile环境变量

已采纳

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongshi4078 dongshi4078 2年前

    I would do it this way:

    test: export GOOS=
    test: export GOARCH=
    test: fmt vet
        go test ./...
    
    点赞 评论 复制链接分享
  • donglu2523 donglu2523 2年前

    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

    点赞 评论 复制链接分享
  • dongposhi8677 dongposhi8677 2年前

    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 ./...
    
    ...
    
    点赞 评论 复制链接分享

为你推荐