如何告诉 CMake 安装目录依赖于虚假目标?

发布于 2024-12-08 09:47:07 字数 1278 浏览 0 评论 0原文

如何在 CMake 中实现以下功能(使用版本 2.8.5)?我有一个由名为 doc 的自定义目标生成的文档,我希望在安装或 CPack'ing 时包含该文档的输出。

add_custom_target( doc "${DOXYGEN_EXECUTABLE}" Doxyfile )
install( DIRECTORY ${CMAKE_BINARY_DIR}/doc DESTINATION doc )

Doxyfile 告诉 Doxygen 将文档放在 ${CMAKE_BINARY_DIR}/doc 中。

如果我在构建目录中执行此操作:

make doc
cpack

一切正常,因为第一行创建 install 目标所依赖的目录。

但是,如果我有一个新的构建(因此 ${CMAKE_BINARY_DIR}/doc 尚不存在),并且我直接调用 CPack:

cpack

那么它会抱怨找不到 ${CMAKE_BINARY_DIR}/ doc

CMake Error at <snip>/build/cmake_install.cmake:36 (FILE):
  file INSTALL cannot find "<snip>/build/doc".

我也尝试了以下操作:

add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/doc COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile )
install( DIRECTORY ${CMAKE_BINARY_DIR}/doc DESTINATION doc )

但我仍然遇到相同的 CPack 错误,并且在构建目录中执行 make doc 也不起作用。

因此,如果我在 cpack 之前手动执行 make doc ,它可以与本文顶部的配置配合使用,但是我如何告诉 cmake/cpack install 指令依赖于自定义目标 doc,以便在调用 cpackmake install 时自动生成文档?

谢谢!

How do I achieve the following in CMake (using version 2.8.5)? I have documentation generated by a custom target named doc, the output of which I would like to include when installing or CPack'ing.

add_custom_target( doc "${DOXYGEN_EXECUTABLE}" Doxyfile )
install( DIRECTORY ${CMAKE_BINARY_DIR}/doc DESTINATION doc )

The Doxyfile tells Doxygen to put out the documentation at ${CMAKE_BINARY_DIR}/doc.

If I do this in the build directory:

make doc
cpack

things works fine, because the first line creates the directory on which the install target depends.

However, if I have a fresh build (so ${CMAKE_BINARY_DIR}/doc does not exist yet), and I invoke CPack directly:

cpack

then it complains that it cannot find ${CMAKE_BINARY_DIR}/doc:

CMake Error at <snip>/build/cmake_install.cmake:36 (FILE):
  file INSTALL cannot find "<snip>/build/doc".

I have also tried the following:

add_custom_command( OUTPUT ${CMAKE_BINARY_DIR}/doc COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_BINARY_DIR}/Doxyfile )
install( DIRECTORY ${CMAKE_BINARY_DIR}/doc DESTINATION doc )

but I still get the same CPack error, and doing make doc in the build directory does not work either.

So if I do the make doc manually before cpack it works with the configuration at the top of this post, but how can I tell cmake/cpack that the install directive depends on the custom target doc, so that building the documentation happens automatically when calling cpack or make install?

Thanks!

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

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

发布评论

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

评论(2

后知后觉 2024-12-15 09:47:07

可能您需要 ALL 关键字:

add_custom_target( doc ALL "${DOXYGEN_EXECUTABLE}" Doxyfile )

更新:

目前 cmake 不提供向其内置目标添加自定义依赖项的选项(例如 all安装测试等)。而且似乎在不久的将来它不会提供此选项 - 请参阅 http:// public.kitware.com/Bug/view.php?id=8438

但是,仍然可以通过一些技巧/解决方法来实现所需的行为。例如,您可以在安装步骤开始时直接运行 make 工具。

因此使用风险自负

add_custom_target(hack ${CMAKE_COMMAND} -E echo "Hacking install target")
if("${CMAKE_GENERATOR}" MATCHES Make)
    install(CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} hack WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")")
endif()

Probably you need ALL keyword:

add_custom_target( doc ALL "${DOXYGEN_EXECUTABLE}" Doxyfile )

Update:

Currently cmake does not provide an option to add custom dependencies to its built-in targets (such as all, install, test, etc). And it seems that it will not provide this option in near future - see http://public.kitware.com/Bug/view.php?id=8438

However it is still possible to achieve desired behavior with some hacks/workarounds. For example you can directly run make tool at the beginning of install step.

So use on your own risk:

add_custom_target(hack ${CMAKE_COMMAND} -E echo "Hacking install target")
if("${CMAKE_GENERATOR}" MATCHES Make)
    install(CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} hack WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")")
endif()
习惯成性 2024-12-15 09:47:07

我在 CMakeList.txt 中对此进行了一些更改,以便在 Linux 和 Windows 上构建

install(CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} hack WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")") endif()

这是我的解决方案。仅当需要 doc 或安装目标时(在 Visual Studio 上,编译 INSTALL 项目时),它才不会始终构建“全部”文档。
这个解决方案并不完整。例如,它不适用于 MinGW,但是......

set( DOXYFILE_OUTPUT_DIR ${PROJECT_BINARY_DIR}/doc )
#-- Configure the Template Doxyfile
configure_file(Doxyfile.in ${PROJECT_BINARY_DIR}/Doxyfile  @ONLY IMMEDIATE)

#-- Add a custom target to run Doxygen
add_custom_target (doc
    COMMAND ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/Doxyfile
    SOURCES ${PROJECT_BINARY_DIR}/Doxyfile
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

if(WIN32)
    install(
        CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} /Build Debug ABIHex.sln /project doc.vcproj
                              WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")"
    )
else(WIN32)
    install(
        CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} --build --target doc
                              WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")"
    )
endif(WIN32)

希望它对某人有用。

问候,
亚历克斯

I have changed this a bit in my CMakeList.txt in order to build on both Linux and Windows

install(CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} hack WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")") endif()

This is my solution. It doesn't build the documentation always "ALL" only when doc or install target are required (On Visual Studio, whe INSTALL project is compiled).
This solution is not complete. It will not work with MinGW, for instance, but...

set( DOXYFILE_OUTPUT_DIR ${PROJECT_BINARY_DIR}/doc )
#-- Configure the Template Doxyfile
configure_file(Doxyfile.in ${PROJECT_BINARY_DIR}/Doxyfile  @ONLY IMMEDIATE)

#-- Add a custom target to run Doxygen
add_custom_target (doc
    COMMAND ${DOXYGEN_EXECUTABLE} ${PROJECT_BINARY_DIR}/Doxyfile
    SOURCES ${PROJECT_BINARY_DIR}/Doxyfile
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

if(WIN32)
    install(
        CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} /Build Debug ABIHex.sln /project doc.vcproj
                              WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")"
    )
else(WIN32)
    install(
        CODE "execute_process(COMMAND ${CMAKE_BUILD_TOOL} --build --target doc
                              WORKING_DIRECTORY \"${CMAKE_CURRENT_BINARY_DIR}\")"
    )
endif(WIN32)

Hope it will be useful for someone.

Regards,
Alex

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