dropbox1111 2019-05-06 15:15
浏览 288
已采纳

特定目标的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 2019-05-06 16:41
    关注

    I would do it this way:

    test: export GOOS=
    test: export GOARCH=
    test: fmt vet
        go test ./...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料