开发者

How to tell CMake that install directory depends on phony target?

开发者 https://www.devze.com 2023-04-10 14:34 出处:网络
How do I achieve the following in CMake (using version 2.8.5)? I have doc开发者_JAVA技巧umentation generated by a custom target named doc, the output of which I would like to include when installing o

How do I achieve the following in CMake (using version 2.8.5)? I have doc开发者_JAVA技巧umentation 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!


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()


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号