如何使用CMake生成具有个性化命令的Makefile?

发布于 2024-09-05 18:50:53 字数 350 浏览 7 评论 0原文

我喜欢保持我的 Makefile 的灵活性和多功能性。我通常添加到 make 命令的任务之一是 tar,例如以下指令可以完成这项工作:

tar:
    tar -cvf $(PROGNAME).tar $(SRCS) Makefile

我的问题是:如何使用 CMake 来生成个性化命令,例如 tar
我想查看一些代码示例。
对于完整的功能,创建项目的组件并能够将它们用作参数将很有用。
示例:仅归档头文件或某些特定库)。

预先感谢您的回答!

I like to keep my Makefiles flexible and multifunctional. One of the tasks I usually add to make command is tar, for example the following instruction does the job:

tar:
    tar -cvf $(PROGNAME).tar $(SRCS) Makefile

My question is: How can CMake be used to generate personalized commands like tar?
I would like to see some code samples.
For the full functionality it would be useful to create project's components and be able to use them as parameters.
(Exempli gratia: archive only header files or some specific library).

Thanks in advance for your answers!

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

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

发布评论

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

评论(1

反目相谮 2024-09-12 18:50:53

tar 示例的字面翻译是:

ADD_CUSTOM_TARGET(tar
    tar -cvf ${CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.tar ${SRCS} Makefile
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

这会添加一个新的目标“tar”,每当将其作为命令行目标请求时,它总是执行给定的命令,即每当您运行 make tar 时,它都会创建一个新的 tar 文件。 WORKING_DIRECTORY 参数将确保从源目录中获取源文件,而 CMAKE_CURRENT_BINARY_DIR 确保输出位于当前构建目录中。

稍微好一点的迭代是将 tar 替换为 ${CMAKE_COMMAND} -E tar,因为这不依赖于命令行 tar程序可用。因此,当您运行 make tar 时,这样的操作会压缩所有头文件:

SET(HEADER_FILES my.h another.h)
SET(PROGNAME myprog)
ADD_CUSTOM_TARGET(tar ${CMAKE_COMMAND} -E tar -czvf
    ${CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.tar.gz ${HEADER_FILES}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

更好的迭代是使用 CPack 功能来创建源文件或二进制 tar 文件,但这需要更多工作无论如何,可能不是您所需要的。

The literal translation of your tar example would be:

ADD_CUSTOM_TARGET(tar
    tar -cvf ${CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.tar ${SRCS} Makefile
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

This adds a new target "tar" that always executes the given command whenever it is requested as a command line target, i.e. whenever you run make tar it will create a new tar file. The WORKING_DIRECTORY argument will ensure that the source files are taken from the source directory, while CMAKE_CURRENT_BINARY_DIR ensures the output goes in the current build directory.

A slightly better iteration would be to replace tar with ${CMAKE_COMMAND} -E tar, as this doesn't depend on the command line tar program being available. So something like this would tar up all the header files when you run make tar:

SET(HEADER_FILES my.h another.h)
SET(PROGNAME myprog)
ADD_CUSTOM_TARGET(tar ${CMAKE_COMMAND} -E tar -czvf
    ${CMAKE_CURRENT_BINARY_DIR}/${PROGNAME}.tar.gz ${HEADER_FILES}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})

An even better iteration would be to use the CPack features to create source or binary tar files, but that's quite a bit more work and may not be what you need anyway.

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