从外部项目清除复制​​的文件

时间:2016-12-29 17:26:17

标签: cmake

作为构建过程的一部分,我正在构建网络服务器lighttpd

以下CMakeLists.txt正在运作:

include(ExternalProject)

ExternalProject_Add(
    lighttpd
    URL ${PROJECT_SOURCE_DIR}/ext/lighttpd.tar.gz
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_OUTPUT_PATH}/lighttpd
)

ExternalProject_Add_Step(
    lighttpd copy2bin
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${EXTERNAL_OUTPUT_PATH}/lighttpd/sbin ${EXECUTABLE_OUTPUT_PATH}
    COMMAND ${CMAKE_COMMAND} -E copy_directory ${EXTERNAL_OUTPUT_PATH}/lighttpd/lib/lighttpd ${LIBRARY_OUTPUT_PATH}
    DEPENDEES install
)

当我输入make时,生成的二进制文件和库分别位于bin/lib/copy2bin步骤中。但是,当我键入make clean时,文件不会被清除。

如何在调用bin/时通知CMake同时清除lib/clean lighttpd文件?

我有更好的方法吗?我基本上是在GitHub找到的那个之后对它进行了建模。

这是我的顶级CMakeLists:

# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.5)

# Project's name
project(CMakeTest)

# Set the output folder where your program will be created
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH    ${CMAKE_BINARY_DIR}/lib)
set(EXTERNAL_OUTPUT_PATH   ${CMAKE_BINARY_DIR}/ext_install)

add_subdirectory(ext) #Contains CMakeLists above

编辑:

我能够使用它:

set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    "${EXTERNAL_OUTPUT_PATH}"
    "${EXECUTABLE_OUTPUT_PATH}/lighttpd"
    "${EXECUTABLE_OUTPUT_PATH}/lighttpd-angel"
)

1 个答案:

答案 0 :(得分:1)

带有set_directory_properties

ADDITIONAL_MAKE_CLEAN_FILES允许在make clean阶段添加要清理的文件:

set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${EXECUTABLE_OUTPUT_PATH}")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${LIBRARY_OUTPUT_PATH}")

由于其签名,我使用了对set_directory_properties的单独调用:

set_directory_properties(PROPERTIES prop1 value1 prop2 value2)

这允许仅使用一个值关联属性。一体化解决方案如下所示:

set_directory_properties(PROPERTIES
    ADDITIONAL_MAKE_CLEAN_FILES "${EXECUTABLE_OUTPUT_PATH}"
    ADDITIONAL_MAKE_CLEAN_FILES "${LIBRARY_OUTPUT_PATH}"
)

我不喜欢同一个电话中的ADDITIONAL_MAKE_CLEAN_FILES重复。或者,您可以使用set_property

set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    "${EXECUTABLE_OUTPUT_PATH}"
    "${LIBRARY_OUTPUT_PATH}"
)

注意:

在路径中使用变量时,应始终在引号""之间写入路径:如果变量包含空格字符,则生成的路径可视为值列表:

set(EXECUTABLE_OUTPUT_PATH "/path/with a/space")
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    ${EXECUTABLE_OUTPUT_PATH}
)

在上面的代码中,没有引号的${EXECUTABLE_OUTPUT_PATH}将被解释如下:

set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    /path/with a/space
)

此处,该属性不会应用于所需的路径/path/with a/space,而是应用于路径/path/witha/space