[CMake] What is the best way to clean up CMakeFiles and CMakeCache.txt if aborting build completely?

Ben Phillips phillipsb44 at gmail.com
Wed Jan 15 18:08:05 EST 2014


Hello list,

I came across this problem when I tried to write a CMakeLists.txt that
prevents building in the source tree. So I would try something like this:

string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" _is_in_src)
if(_is_in_src)
  file(REMOVE_RECURSE ${CMAKE_SOURCE_DIR}/CMakeFiles $
{CMAKE_SOURCE_DIR}/CMakeCache.txt)
  message(FATAL_ERROR "Aborting...in-source builds are forbidden")
endif(_is_in_src)

When running "cmake ." the error is given as expected. However, the source
directory is polluted with CMakeFiles and CMakeCache.txt. I expect they
can't be removed while the CMakeLists.txt is being interpreted (perhaps
they are but are just created before exit again).

Is there a better way to do this? Perhaps some function/macro that aborts
the process abnormally, cleaning up as if nothing ever happened exists?

Thank you for your time.
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20140116/c7e106d2/attachment.html>


More information about the CMake mailing list