【问题标题】:Run setup before make target and "finally" afterwards?在制作目标之前运行设置并在之后“最终”运行?
【发布时间】:2020-10-16 10:38:51
【问题描述】:

我正在处理一个使用许多我无法控制的导入 Makefile 的项目。我只控制 repo 根目录下的单个 Makefile。其他 Makefile 之一中的目标对我正在处理的代码运行测试。但是,我的代码还有一个额外的设置和拆卸要求。

我可以将一个目标添加到我的本地 Makefile 作为公共目标的依赖项。这允许安装程序运行。但是,我也需要事后拆除。拆除需要始终进行,即使 make 目标失败。示例代码如下:

.PHONY: common/test local/setup local/teardown

# This includes the setup as needed
common/test: local/setup

local/setup:
        # Alter files to allow destruction of test resources

# But how do I ensure this runs? Depending on the user to run it is error prone...
# Also, this step needs to run EVEN IF the common/test target failed
local/teardown:
        # Revert files so they don't get committed to source control
        # because we need resource destruction disallowed in production

【问题讨论】:

  • “即使目标失败”是否包括失败场景,例如有人点击 ^C 或发送 SIGINT?还是您只担心common/test 配方是否以正常方式失败(以非 0 退出代码退出)?
  • 好问题,我主要关注常见/测试配方的失败。如果用户中断 make,他们可以期望清理部分状态。

标签: makefile


【解决方案1】:

遗憾的是,make 中没有拆卸工具。你能做的最好的就是把它放到同一个配方中,但是在失败时捕获退出代码,这样它仍然可以运行。

例如说common/test 的配方是这样的:

common/test: local/setup
        run my test

那么你必须使用:

common/test: local/setup
        run my test; ret=$$?; \
        perform teardown operations; \
        exit $$ret

【讨论】:

  • 这行得通。我实际上将编写一个“本地/测试”目标,其配方既可以设置也可以拆除,中间调用 common/test。捕获返回代码是确保拆卸步骤运行的缺失部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
  • 1970-01-01
相关资源
最近更新 更多