[CMake] Disallowing in-source builds

Philip Lowman philip at yhbt.com
Thu Oct 7 09:30:22 EDT 2010


On Thu, Oct 7, 2010 at 3:19 AM, Michael Wild <themiwi at gmail.com> wrote:

>
> On 6. Oct, 2010, at 20:10 , <aaron.meadows at thomsonreuters.com> <
> aaron.meadows at thomsonreuters.com> wrote:
>
> > Hi all.
> >
> >
> >
> > Is there a good way to disallow in-source builds?  Ideally, I'd like to
> > prevent it before any cruft is written into the source tree.  I
> > experimented with writing a function into my CMakelists file and calling
> > it.  The function checked if CMAKE_BINARY_DIR was equal to
> > CMAKE_SOURCE_DIR and messaged a FATAL_ERROR if that was the case.  This
> > works ok, but still generates a CMakeFiles directory and a
> > CMakeCache.txt file.
>
> I don't think there's a way to prevent that from happening. The bad thing
> about this is that if the user doesn't clean away the in-source
> CMakeCache.txt file, subsequent out-of-source builds will fail. Perhaps you
> can do something like this:
>
> # check for polluted source tree
> if(EXISTS ${CMAKE_SOURCE_DIR}/CMakeCache.txt OR
>    EXISTS ${CMAKE_SOURCE_DIR}/CMakeFiles)
>  message(FATAL_ERROR
>    "CMakeCache.txt or CMakeFiles exists in source directory!")
> endif()
> # make sure the user doesn't play dirty with symlinks
> get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)
> get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)
> # disallow in-source builds
> if(${srcdir} STREQUAL ${bindir})
>  message(FATAL_ERROR "In-source builds are forbidden!")
> endif()
>
> >
> > The second half of the question is of course, is there an easy way to
> > clean out a source tree if an in-source build was accidentally kicked
> > off?  (short of dividing the files by their timestamp and removing the
> > newer ones, etc..)
>
> No, simply because CMake cannot. Your build system might have something
> like the following:
>
> execute_process(COMMAND echo "BOOM" > ${CMAKE_BINARY_DIR}/boom.txt
> VERBATIM)
>
> CMake never knows that the file boom.txt is written, and therefor can't
> clean it away.


I think this is a bit of a red herring.  CMake could be perfectly capable of
cleaning up after itself (i.e. its own files).
If the user is doing things like making a bunch of files that CMake isn't
aware of (that aren't generated), these could be added to
ADDITIONAL_MAKE_CLEAN_FILES, or a new property could be added for custom
files that are to be deleted on a distclean.

-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20101007/dc7f8a26/attachment-0001.htm>


More information about the CMake mailing list