dropbox1111 2019-05-06 07: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 08:41
    关注

    I would do it this way:

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

报告相同问题?

悬赏问题

  • ¥15 来个会搭建付费网站的有偿
  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部