Lukas Schmelzeisen
Lukas Schmelzeisen

Reputation: 3182

Only install something in CMake if custom target is built

I'm currently trying to setup CMake to automatically generate my Doxygen documentation. I currently use the following code.

find_package(Doxygen)
if (DOXYGEN_FOUND)
    configure_file("docs/Doxyfile.in" "${PROJECT_BINARY_DIR}/Doxyfile")
    add_custom_target(docs
                      COMMAND ${DOXYGEN_EXECUTABLE}
                              "${PROJECT_BINARY_DIR}/Doxyfile"
                      SOURCES "${PROJECT_BINARY_DIR}/Doxyfile")
    install(DIRECTORY "${PROJECT_BINARY_DIR}/docs/"
            DESTINATION "docs")
endif()

It works fine in when you enter make docs it generates the documentation in PROJECT_BINARY_DIR/docs. When you enter make install it copies the docs subdirectory to CMAKE_INSTALL_PREFIX. However when the user does not wish to generate documentation and just types make install the following error occurs:

CMake Error at cmake_install.cmake:36 (FILE):
  file INSTALL cannot find "/home/lukas/workspace/TheGame/build/docs".

How can one instruct the install command to only be executed if a custom target (docs) is built (or if the subdirectory docs exists in PROJECT_BINARY_DIR)?

Upvotes: 6

Views: 3297

Answers (1)

sakra
sakra

Reputation: 65981

Have you tried using the OPTIONAL flag?

install(DIRECTORY "${PROJECT_BINARY_DIR}/docs/" DESTINATION "docs" OPTIONAL)

Upvotes: 10

Related Questions