是否可以创建一个 cmake 脚本来生成一个构建来处理构建时生成的未知文件?

发布于 2025-01-02 07:08:30 字数 195 浏览 2 评论 0原文

我有一个系统,可以从规范文档生成生成的代码,该文档可以随时更改。因此,生成的文件列表不能是静态的,并且必须能够在构建时动态处理。

据我所知,典型的 CMakeLists.txt 设置为在 cmake 生成时为每个文件定义规则。

有没有办法让 CMake 编写通用规则,以便可以在构建时设置目标?

如果没有,可能的解决方法是什么?

I have a system that produces generated code from a spec document which can change at any time. As such, the list of files being generated cannot be static, and must be able to be handled dynamically at build time.

From what I can tell the typical CMakeLists.txt is set up to define a rule for each file at cmake generation time.

Is there a way to get CMake to write generic rules so that the targets can be set at build time?

If not, what are the possible work-arounds?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱娆 2025-01-09 07:08:30

首先,你可以将你的代码生成过程以目标(add_custom_command)的形式放入CMakeLists.txt中,然后让你的目标依赖于这个命令。这是代码生成步骤,每次您发出 make 时都会运行。

或者,这里有一个 hack:

add_custom_target(cmake_regen ALL
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/CMakeFiles/Makefile.cmake)

还有一个不太 hackish 的变体,它应该保留已经构建的目标:

add_custom_target(cmake_regen ALL
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target rebuild_cache)

将此类代码添加到 CMakeLists.txt 中会强制 CMake 在每次运行时重新生成 makefile,而无需运行完整的配置过程。

First, you can put your code generation process into CMakeLists.txt in a form of a target (add_custom_command), and then make your target depends on this command. This was code generation steps will be run every time you issue make.

Alternatively, here is a hack:

add_custom_target(cmake_regen ALL
    COMMAND ${CMAKE_COMMAND} -E remove ${CMAKE_BINARY_DIR}/CMakeFiles/Makefile.cmake)

And a less hackish variant, which should preserve already built targets:

add_custom_target(cmake_regen ALL
    COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target rebuild_cache)

Adding such code into CMakeLists.txt forces CMake to regenerate makefiles on each run without running full configuration process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文