Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean copied files from external project

Tags:

cmake

As a part of my build process I'm building the webserver lighttpd.

The following CMakeLists.txt is working:

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
)

When I type make, the resulting binaries and libraries are in bin/ and lib/ respectively as done in the copy2bin step. However, when I type make clean the files are not cleared out.

How can I notify CMake to also clear out bin/ and lib/ lighttpd files when invoking clean?

Is there a better way I could be doing this? I basically modeled it after one I found on GitHub.

This is my top level 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

Edit:

I was able to get it working with:

set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    "${EXTERNAL_OUTPUT_PATH}"
    "${EXECUTABLE_OUTPUT_PATH}/lighttpd"
    "${EXECUTABLE_OUTPUT_PATH}/lighttpd-angel"
)
like image 584
Gillespie Avatar asked Nov 01 '25 16:11

Gillespie


1 Answers

set_directory_properties with ADDITIONAL_MAKE_CLEAN_FILES allows to add files to clean during the make clean stage:

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

I used separate calls to set_directory_properties because of its signature:

set_directory_properties(PROPERTIES prop1 value1 prop2 value2)

This allows to associate a property with only one value. An all-in-one solution would look like this:

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

I don't like the ADDITIONAL_MAKE_CLEAN_FILES repetition in the same call. Alternatively, you can use set_property:

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

Note:

When using a variable in a path, you should always write the path between quotes "": if the variable contains a space character, the resulting path may be considered as a list of value:

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

In the code above, ${EXECUTABLE_OUTPUT_PATH} without quotes will be interpreted as follows:

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

Here the property will not be applied to the path /path/with a/space as wanted, but to the paths /path/with and a/space.

like image 150
rgmt Avatar answered Nov 04 '25 10:11

rgmt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!