如何在cmake中构建目标的开始和结束时调用功能
我正在寻找一种在启动和完成CMAKE目标构建时执行Shell代码的方法。最终目标是向数据跟踪工具发送消息,以指示何时构建开始和完成。
因此,例如,如果“制作”构建目标Alpha,Beta和Gamma,那么当Alpha成功构建目标Alpha时,我想致电foo_begin()。
这可能吗?
I'm looking for a way to execute shell code when starting and finishing the build of a target in cmake. The final goal is to send a message to a data tracking tool indicating when builds start and finish.
So for example, if "make" build targets alpha, beta and gamma, I'd like to call foo_begin() when alpha starts building and foo_end when target alpha is successfully built, and so on for all the targets.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不是可以在构建时间调用任意CMAKE功能。生成构建系统后,所有正常变量状态和功能定义都将丢弃(即它们不在缓存中)。
命令
参数到add_custom_command
获取shell命令,而不是cmake函数。但是,您可以使用Cmake的脚本模式,因此,如果您想要的是将CMake用作脚本语言只是为了实现一些构建钩子,则可以执行此操作:
让我们创建一个称为
proc_target.cmake.cmake.cmake
With With With这些内容:现在在cmakelists.txt中,我们可以写:
然后,当您运行此构建时,您会看到以下命令:
您可以看到
proc_target.cmake.cmake
被调用两次:一次调用foo
的链接器后。如果要在编译任何源之前运行proc_target
,则要编写:而不是上面的
pre_build
自定义命令。然后,您将获得:现在您可以看到自定义目标
pre-foo
是在foo
之前处理的。但是,这很可能不是您想要的。它是过度设计的。如果要为目标生成源文件,则应直接使用
add_custom_command(outption)
直接将输出连接到目标(即,当您调用add_executable 或通过
target_sources
)。It is not possible to call arbitrary CMake functions at build time. Once the build system is generated, all the normal variable state and function definitions are discarded (i.e. they are not in cache). The
COMMAND
argument toadd_custom_command
takes a shell command, not a CMake function.However, you can use CMake's script mode, so if what you want is to use CMake as a scripting language just to implement some build hooks, you can do this:
Let's create a file called
proc_target.cmake
with these contents:Now in the CMakeLists.txt we can write:
Then when you run this build, you'll see the following commands:
You can see how
proc_target.cmake
is called twice: once just before and once just after invoking the linker forfoo
. If you wantproc_target
to run before any of the sources are compiled, then you would want to write:instead of the
PRE_BUILD
custom command above. Then you would get:And now you can see that the custom target
pre-foo
is processed beforefoo
.This is most likely not what you want, however. It's over-engineered. If you want to generate a source file for a target, you should use
add_custom_command(OUTPUT)
directly and attach the output to the target as a source file (i.e. when you calladd_executable
or viatarget_sources
).PER @botje的评论,似乎我需要cmake的add_custom_command with Build Event指定。但是,我将需要pre_build,该文档告诉我,这仅适用于VS-Studio,而我正在建造CMAKE&海湾合作委员会因此,我想我有一个新问题:如何在CMAKE/GCC构建环境中复制PRE_BUILD的行为。
Per @botje's comment, it seems I need cmake's add_custom_command with build event specifies. I will however need PRE_BUILD, which the documentation informs me is only available for vs-studio, while I am building use cmake & gcc. So I guess I have a new question: how to duplicate the behavior of PRE_BUILD in a cmake/gcc build environment.