从CMake脚本构建doxygen

时间:2016-01-19 13:38:52

标签: cmake doxygen

我在Web上找到了一个示例cmake文件,并将其放在我的项目的/doc子目录中,其中文件myproject.doxgen也位于其中,包含Doxygen配置。

我已经测试过运行doxygen.exe myproject.doxygen会产生有效输出。我只需要将其构建到CMake过程中。所以/doc/CMakeLists.txt是:

find_package(Doxygen)
option(BUILD_DOCUMENTATION "Create and install the HTML based API        
documentation (requires Doxygen)" ${DOXYGEN_FOUND})

if(BUILD_DOCUMENTATION)
    if(NOT DOXYGEN_FOUND)
         message(FATAL_ERROR "Doxygen is needed to build the documentation.")
    endif()

    set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/../doc/myproject.doxygen)
    set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/doxyfile)

    configure_file(${doxyfile_in} ${doxyfile} @ONLY)

    message("Doxygen build started.")

    add_custom_target(doc
                      COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile_in}
                      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc
                      COMMENT "Generating API documentation with Doxygen"
                      VERBATIM)

    #    install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION     share/doc)
endif()

它对我不起作用,它只将原始配置文件复制到/build/my/project/doc/中,而不再执行任何操作。

我想要的是在我的构建期间生成doxygen文档;理想情况下,仅在构建发布配置时。

1 个答案:

答案 0 :(得分:10)

设置了您显示的CMake文件的方式,它会创建一个名为doc的目标;构建该目标(例如运行make doc)会生成doxymentation。目标不是make all(或等效物)的一部分;为此,请将ALL添加到自定义目标创建中:

add_custom_target(
  doc ALL
  COMMAND #... everything else as before
)

如果您希望将此目标限制为仅在特定配置中构建(如您在评论中所述),则可以使用generator expressions

add_custom_target(
  doc ALL
  COMMAND $<$<CONFIG:Release>:${DOXYGEN_EXECUTABLE} ${doxyfile_in}>
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc
  COMMENT "Generating API documentation with Doxygen"
  VERBATIM
)

有些CMake生成器可能无法很好地应对空COMMAND。考虑到这一点,以下应该是故障安全的:

add_custom_target(
  doc ALL
  COMMAND
    $<$<CONFIG:Release>:${DOXYGEN_EXECUTABLE} ${doxyfile_in}>
    $<$<NOT:$<CONFIG:Release>>:${CMAKE_COMMAND} -E echo "Only done in Release builds">
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc
  COMMENT "Generating API documentation with Doxygen"
  VERBATIM
)