从 git 拉取后,在构建之前让 cmake 运行 [英] Getting cmake to run before building after pulling from git

查看:40
本文介绍了从 git 拉取后,在构建之前让 cmake 运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个项目保存在一个 git 存储库中,我们使用 cmake 和 ninja 构建.我们正在使用 globbing 表达式/函数来收集所有要编译的源文件.这意味着每次添加/删除文件时,都必须调用 cmake 来重新解析目录.

There is this project kept in a git repository, which we build with cmake and ninja. We are using globbing expressions/functions to collect all the source files to be compiled. That means that every time a file is added/removed, cmake has to be called to re-parse the directories.

我们已经看到,当有人在推入一些新文件后拉动而不修改任何 cmake 文件时,这会带来一些时间损失.我说最后一件事是因为对任何 cmake 文件的修改都会触发对 cmake 的调用(由 ninja),一切都会好起来的.

We have seen this is bringing some time loss when somebody pulls after somebody has pushed some new file, without modifications to any of the cmake files. I say this last thing because a modification to any of the cmake files would trigger a call to cmake (by ninja), and everything would be fine.

如何在拉取后重新开始构建项目之前/时调用 cmake?(注意:cmake 的运行次数是否超出必要程度并不重要,只要并非总是如此)

How can I obtain that cmake is called before/when I start building again my project after pulling? (Note: it does not matter if cmake is run a little bit more than necessary, as long as it is not always)

我正在构建源代码,此外还使用多个构建目录,例如我测试不同的编译器.

I am building out of source, furthermore using several build directory where for example I test different compilers.

我正在探索一些解决方案.一个使用 git hooks 脚本,即 post-merge(但我如何保证我将检索 source/CMakeLists.txt 的路径到 touch 它?我可以提交脚本以便它为每个人运行吗?这不是一个公共项目).不知道有没有关系,我们主要是通过图形界面(TortoiseGit)使用git.

I am exploring some solutions. One using git hooks script, namely post-merge (but how can I guarantee I will retrieve the path to source/CMakeLists.txt to touch it? Can I commit the script so that it runs for everybody? It is not a public project). I don't know if it is relevant, we mainly use git through graphic interface (TortoiseGit).

另一种可能的解决方案是在 cmake 中使用依赖于 .git efsheads 目录内容的自定义目标,但我想不出真正可行的组合...

The other possible solution would be using in cmake a custom target dependent on the content of .git efsheads directory, but I cannot think of a combination that could really work...

一些链接:

  1. http://git-scm.com/book/en/自定义-Git-Git-Hooks
  2. https://www.kernel.org/pub/软件/scm/git/docs/githooks.html

CMake 命令:http://www.cmake.org/cmake/help/v2.8.11/cmake.html

推荐答案

post-merge 脚本文件放在 .git/hooks 中似乎可以解决问题:

Putting a post-merge script file in .git/hooks seems to do the trick:

#!/bin/sh
#

touch source/CMakeLists.txt

执行路径显然是项目的根目录,非常适合这种操作.我通过从项目的一个随机"子文件夹中调用 TartoiseGit 上下文菜单中的pull"来测试这一点.以下是有关如何测试脚本的一些说明.

The execution path is apparently the root directory of the project, which is ideal for this kind of operation. I tested this by calling "pull" in the TartoiseGit context menu from one "random" subfolder of the project. Here are some instructions on how one could test the script.

缺点:如果有人在他的编辑器中修改了 CMakeLists.txt,并且有一些未保存的修改,如果回答磁盘上的文件已更改..."提示太快,他可能会失去一些工作.

Drawback: if somebody is modifying the CMakeLists.txt in his editor, with some unsaved modifications, he might lose some work if too quick in answering to the prompt "the file has changed on the disk..."

然后,为了使这个脚本成为存储库的一部分,我找到了以下似乎对我们的(非公共)项目有用的解决方案.

Then, to make this script part of the repository, I found the following solution that seems to be good for our (non-public) project.

1) 为 git hook 脚本创建一个文件夹(例如 zz_gitHookScripts)并放置每个人都应该使用的文件.将其添加到存储库中.
2) 在 CMakeLists.txt 中添加类似这样的内容,以便第一次运行 cmake 时将文件放在有效目录中(并且,鉴于上述情况,这将发生在我们拉取后第一次构建时,也是第一次时间,因为 CMakeLists.txt 通过此编辑被修改):

1) Create a folder (e.g. zz_gitHookScripts) for git hook scripts and put the files everybody should be using. Add it to the repository.
2) Add something like this to the CMakeLists.txt, so that the files will be put in the effective directory first time cmake will be run (and, given the above, this will happen the first time we build after pulling, also the first time because CMakeLists.txt gets modified through this edit):

file(GLOB HOOK_SCRIPTS zz_gitHookScripts/*)
if (HOOK_SCRIPTS)
    file(COPY ${HOOK_SCRIPTS} DESTINATION ${CMAKE_SOURCE_DIR}/../.git/hooks) 
endif (HOOK_SCRIPTS)


在补丁的情况下,可以使用等效的 post-applypatch 钩子.

缺点:

  • 它不会在 stash (用户存储添加/删除文件的更改,必须仍然记得手动运行 cmake)的情况下触发,或者在补丁的情况下
  • 用户无法个性化钩子脚本
  • 删除一些我们不想再使用的钩子脚本可能很复杂
  • 一般来说,所有分支都必须共享相同的钩子

无论如何,这样对我们的需求有好处.

Anyway, like this it's good for our needs.

这篇关于从 git 拉取后,在构建之前让 cmake 运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆