<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<style>
<!--
/* Font Definitions */
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:purple;
        text-decoration:underline;}
p.MsoPlainText, li.MsoPlainText, div.MsoPlainText
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:Consolas;
        color:gray;
        font-weight:bold;}
p
        {mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman";}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 97.05pt 1.0in 97.05pt;}
div.Section1
        {page:Section1;}
-->
</style>
</head>
<body lang=EN-US link=blue vlink=purple>
<div class=Section1>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>Here's what I ended up with:<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>function(AssureOutOfSourceBuilds)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> # make sure the user
doesn't play dirty with symlinks<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
get_filename_component(srcdir "${CMAKE_SOURCE_DIR}" REALPATH)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
get_filename_component(bindir "${CMAKE_BINARY_DIR}" REALPATH)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> # disallow in-source builds<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> if(${srcdir} STREQUAL
${bindir})<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message("######################################################")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message("You are attempting to build in your Source Directory.")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message("You must run cmake from a build directory.")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message("######################################################")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> # attempt to
remove cache and cache files... this actually fails to work,<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> # but no hurt
trying incase it starts working..<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
file(REMOVE_RECURSE "${CMAKE_SOURCE_DIR}/CMakeCache.txt"
"${CMAKE_SOURCE_DIR}/CMakeFiles")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message(FATAL_ERROR "In-source builds are forbidden!")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> endif()<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> # check for polluted source
tree<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> if(EXISTS
${CMAKE_SOURCE_DIR}/CMakeCache.txt OR EXISTS ${CMAKE_SOURCE_DIR}/CMakeFiles)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message("################################################################")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> message(
"Found results from an in-source build in your source directory.")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message("################################################################")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> # attempt to
remove cache and cache files...<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
file(REMOVE_RECURSE "${CMAKE_SOURCE_DIR}/CMakeCache.txt"
"${CMAKE_SOURCE_DIR}/CMakeFiles")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>
message(FATAL_ERROR "Source Directory Cleaned, please rerun CMake.")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'> endif()<o:p></o:p></span></font></b></p>
<p class=MsoPlainText style='margin-left:.5in'><b><font size=2 color=gray
face=Consolas><span style='font-size:10.0pt'>endfunction()<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>It’s basically exactly what you mentioned.
I changed the order of the checks to allow it to check for an in-source build
before dealing with the consequences of such. I added more logging and
actual cleanup.<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>Thanks for all the help. If you have any suggestions
on improving this, please let me know. Incidentally, removing the
CMakeCache.txt and CMakeFiles directory does not work during the in-source
build, but does during the check to insure there isn’t anything left over
from an in source build. I went ahead and removed the files there and
then fatal_error’d out to allow the right CMakeCache.txt to be loaded in
the build tree when cmake is next run.<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>Aaron C. Meadows <o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>-----Original Message-----<br>
From: Michael Wild [mailto:themiwi@gmail.com] <br>
Sent: Thursday, October 07, 2010 2:20 AM<br>
To: Meadows, Aaron C.; Meadows, Aaron C.<br>
Cc: cmake@cmake.org<br>
Subject: Re: [CMake] Disallowing in-source builds</span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>On 6. Oct, 2010, at 20:10 ,
<aaron.meadows@thomsonreuters.com>
<aaron.meadows@thomsonreuters.com> wrote:<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> Hi all.<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> <o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> <o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> <o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> Is there a good way to disallow in-source
builds? Ideally, I'd like to<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> prevent it before any cruft is written into the
source tree. I<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> experimented with writing a function into my
CMakelists file and calling<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> it. The function checked if
CMAKE_BINARY_DIR was equal to<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> CMAKE_SOURCE_DIR and messaged a FATAL_ERROR if
that was the case. This<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> works ok, but still generates a CMakeFiles
directory and a<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> CMakeCache.txt file.<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>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:<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'># check for polluted source tree<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>if(EXISTS ${CMAKE_SOURCE_DIR}/CMakeCache.txt OR<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'> EXISTS
${CMAKE_SOURCE_DIR}/CMakeFiles)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'> message(FATAL_ERROR<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'> "CMakeCache.txt or CMakeFiles
exists in source directory!")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>endif()<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'># make sure the user doesn't play dirty with symlinks<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>get_filename_component(srcdir
"${CMAKE_SOURCE_DIR}" REALPATH)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>get_filename_component(bindir
"${CMAKE_BINARY_DIR}" REALPATH)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'># disallow in-source builds<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>if(${srcdir} STREQUAL ${bindir})<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'> message(FATAL_ERROR "In-source builds are
forbidden!")<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>endif()<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> <o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> The second half of the question is of course, is
there an easy way to<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> clean out a source tree if an in-source build was
accidentally kicked<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> off? (short of dividing the files by their
timestamp and removing the<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>> newer ones, etc..)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>No, simply because CMake cannot. Your build system
might have something like the following:<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>execute_process(COMMAND echo "BOOM" >
${CMAKE_BINARY_DIR}/boom.txt VERBATIM)<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>CMake never knows that the file boom.txt is written,
and therefor can't clean it away. The only reasonable way I know of is using
git (http://git-scm.com):<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>git clean -df<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>will remove all the files and directories that are not
part of the repository. With tar-ball builds it's easier. Just wipe the source
tree and unpack again.<o:p></o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'><o:p> </o:p></span></font></b></p>
<p class=MsoPlainText><b><font size=2 color=gray face=Consolas><span
style='font-size:10.0pt'>Michael<o:p></o:p></span></font></b></p>
</div>
<br clear=all> This email was sent to you by Thomson Reuters, the global news and information company.<br>
Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of Thomson Reuters.
</body>
</html>