运行自定义目标
你还可以创建要在执行特定任务时运行的自定义目标。这些通常是你运行以执行不同操作的可执行文件。可能特别有用的是运行 Doxygen 为你的项目生成文档。为此,你可以在 CMakeLists.txt
中执行以下操作(为简单起见,我们将继续我们的 Qt5 项目示例):
cmake_minimum_required(VERSION 3.0)
project(MyQtProj LANGUAGES C CXX)
find_package(Qt5 COMPONENTS Core Gui Widgets)
#...set up your project
add_executable(MyQtProj ${PROJ_SOURCES} ${PROJ_HEADERS})
add_custom_command(TARGET MyQtProj POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Core> $<TARGET_FILE_DIR:MyQtProj>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Gui> $<TARGET_FILE_DIR:MyQtProj>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Widgets> $<TARGET_FILE_DIR:MyQtProj>
)
#Add target to build documents from visual studio.
set(DOXYGEN_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile)
#set the output directory of the documentation
set(DOXYGEN_OUTPUT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/docs)
# sanity check...
message("Doxygen Output ${DOXYGEN_OUTPUT_DIR}")
find_package(Doxygen)
if(DOXYGEN_FOUND)
# create the output directory where the documentation will live
file(MAKE_DIRECTORY ${DOXYGEN_OUTPUT_DIR})
# configure our Doxygen configuration file. This will be the input to the doxygen
# executable
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in
${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
# now add the custom target. This will create a build target called 'DOCUMENTATION'
# in your project
ADD_CUSTOM_TARGET(DOCUMENTATION
COMMAND ${CMAKE_COMMAND} -E echo_append "Building API Documentation..."
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOXYGEN_OUTPUT_DIR}
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMAND ${CMAKE_COMMAND} -E echo "Done."
WORKING_DIRECTORY ${DOXYGEN_OUTPUT_DIR})
endif(DOXYGEN_FOUND)
现在,当我们创建我们的解决方案时(再次假设你使用的是 Visual Studio),你将拥有一个名为 DOCUMENTATION
的构建目标,你可以构建该目标以重新生成项目的文档。