在 cmake 构建之前和之后执行 git patch 和 unpatch

发布于 2025-01-11 15:04:18 字数 761 浏览 0 评论 0原文

我有一个项目,我想在构建代码之前向 git 存储库应用补丁,然后在创建可执行文件之后取消应用该补丁。原因是我希望将补丁应用于构建,但我希望修补后的存储库在其主分支上保持干净。我注意到 add_custom_command 似乎具有此功能,带有 PRE_BUILDPOST_BUILD 说明符。我尝试了这样的事情:

add_executable(foo ...)

# foo requires a patch to some library
target_link_libraries(foo ...)

add_custom_command(TARGET foo PRE_BUILD COMMAND git apply somepatch.patch)
add_custom_command(TARGET foo POST_BUILD COMMAND git apply --reverse somepatch.patch)

但是,如果仔细查看 add_custom_command 的文档,他们会在有关 PRE_BUILD 说明符的部分中提到这一点:

在 Visual Studio 生成器上,在目标内执行任何其他规则之前运行。在其他生成器上,在 PRE_LINK 命令之前运行。

事实确实如此。该补丁仅在编译阶段之后和链接阶段之前应用。因此,代码是在应用补丁之前编译的。如何确保在编译代码之前执行补丁?

I have a project for which I want to apply a patch to a git repo before building the code, then unapply that patch after creating the executable. The reason being that I want the patch applied to the build but I want the repository that is patched to stay clean on its master branch. I noticed add_custom_command seemed to have this capability, with PRE_BUILD and POST_BUILD specifiers. I tried something like this:

add_executable(foo ...)

# foo requires a patch to some library
target_link_libraries(foo ...)

add_custom_command(TARGET foo PRE_BUILD COMMAND git apply somepatch.patch)
add_custom_command(TARGET foo POST_BUILD COMMAND git apply --reverse somepatch.patch)

However, if one looks closer at the documentation of add_custom_command they mention this in the section about the PRE_BUILD specifer:

On Visual Studio Generators, run before any other rules are executed within the target. On other generators, run just before PRE_LINK commands.

And indeed this is what happens. The patch is only getting applied after the compilation phase, and before the linking phase. So the code is being compiled before the patch is applied. How can I ensure the patch is performed before compiling the code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文