make clean in CMake generated build system does not propagate into external projects

Viewed 674

I am currently setting up a build system using CMake but I have some struggles with "make clean" in conjunction with external projects: I can't figure out why "make clean" does not propagate down to the projects that I add via ExternalProject_Add.

My (minimalistic example) source structure looks like this:

/
|- test/
|  |- CMakeLists.txt
|  |- main.cpp
|  |- my_lib
|     |- CMakeLists.txt
|     |- lib_functions.cpp

In /test/CMakeLists.txt I use ExternalProject_Add to add my_lib:

cmake_minimum_required(VERSION 2.8)

# via external project
include(ExternalProject)
set(MY_LIB_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/my_lib)
ExternalProject_Add(ext_my_lib
    DOWNLOAD_COMMAND ""
    SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/my_lib
    BINARY_DIR ${MY_LIB_OUTPUT_DIR}
    INSTALL_COMMAND ""
    )
add_library(my_lib STATIC IMPORTED)
set_target_properties(my_lib PROPERTIES IMPORTED_LOCATION ${MY_LIB_OUTPUT_DIR}/libmy_lib.a)
add_dependencies(my_lib
    ext_my_lib
    )

set(PROJECT_NAME my_test)
project(${PROJECT_NAME})

add_executable(${CMAKE_PROJECT_NAME} main.cpp)
target_link_libraries(${CMAKE_PROJECT_NAME} my_lib)

And /test/my_lib/CMakeLists.txt is just a simple library:

cmake_minimum_required(VERSION 2.8)

set(PROJECT_NAME my_lib)
project(${PROJECT_NAME})

add_library(${CMAKE_PROJECT_NAME} lib_functions.cpp)

After generating build system from /test/CMakeLists.txt I call "make" and then "make clean", but my_lib is not cleaned, i. e. the file build/my_lib/libmy_lib.a is not deleted. In contrast when I direclty call "make clean" in the build/my_lib directory "make clean -C my_lib" my_lib is cleaned correctly, i. e. the file build/my_lib/libmy_lib.a IS deleted. This makes me assume that somehow the "make clean" is not correctly propagated down to the external project, but I cannot figure out why.

I know that I could use add_subdirectory by replacing the "# via external project" section in the /test/CMakeLists.txt file by

# via add_subdirectory
set(MY_LIB_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/my_lib)
add_subdirectory(my_lib ${MY_LIB_OUTPUT_DIR})

Using this way the "make clean" works correctly, i. e. build/my_lib/libmy_lib.a is deleted, but I need to work with external projects due to other reasons.

I also know that I could define ADDITIONAL_MAKE_CLEAN_FILES, e. g.

set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
    ${MY_LIB_OUTPUT_DIR}
    )

but this would

  • also delete the build system for my_lib
  • not call "make clean" on dependencies inside of /test/my_lib/CMakeLists.txt

so that is not a solution for me.

0 Answers
Related