No subject


Thu Aug 21 14:30:17 EDT 2008


<br>
aren&#39;t effective. &nbsp;I don&#39;t think I fully understood the explanations of<br>
why, though.</blockquote><div><br>Christian,<br><br>Ok, CMAKE_BUILD_TYPE is a bit weird principally because it typically gets created as what&#39;s called a cache variable on an initial configure.<br><br>Regarding the cache...&nbsp; First, to clear up any confusion you should understand that what you said above is not entirely true.&nbsp; If you specify CMAKE_BUILD_TYPE like so, for example:<br>
<br>PROJECT(foo)<br>SET(CMAKE_BUILD_TYPE RelWithDebInfo)<br>ADD_EXECUTABLE(foo foo.cc)<br><br>No matter what your users set CMAKE_BUILD_TYPE in the CMake cache editor it will *always* be RelWithDebInfo when cmake is run (even though it may appear to be set differently with the cmake gui).&nbsp; You can verify this by doing a &quot;make VERBOSE=1&quot; and examining the compile flags.&nbsp; The reason this is so is because local variables always trump cache variables in scope.&nbsp; &quot;ccmake&quot; and friends can only show you cache variables, not variables created when CMake executes your scripts.&nbsp; If you define a local variable like CMAKE_BUILD_TYPE with the same name as a cache variable, it will override the cache in scope.<br>
<br>So you could simply set CMAKE_BUILD_TYPE to whatever, but this is a bad idea as you probably realize because it leaves your users no choice in choosing their build type.&nbsp; What you probably want to do is create a cache variable which will allow you to define a default value and type for the variable that the user can then edit, e.g:<br>
<br>SET(CMAKE_BUILD_TYPE Debug CACHE STRING &quot;documentation for this variable&quot;)<br><br>One of the side effects of the SET(...CACHE) command, however, is only to create a cache variable if one doesn&#39;t already exist with that name.&nbsp; (This is to prevent users from overwriting existing cache variables, if you need to do that you should use the FORCE option on the SET() command).<br>
<br>The timing of setting CMAKE_BUILD_TYPE is, therefore, kinda tricky because as I mentioned earlier, CMake itself defines this on an initial configure.&nbsp; It turns out that this definition of CMAKE_BUILD_TYPE occurs in the PROJECT() command so to answer your original question, the best way to assign CMAKE_BUILD_TYPE a default option is to do something similar to the following:<br>
<br><br>#<br># If the user specifies -DCMAKE_BUILD_TYPE on the command line, take their definition<br># and dump it in the cache along with proper documentation, otherwise set CMAKE_BUILD_TYPE<br># to Debug prior to calling PROJECT()<br>
#<br>IF(DEFINED CMAKE_BUILD_TYPE)<br>&nbsp;&nbsp; SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING &quot;Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.&quot;)<br>
ELSE()<br>&nbsp;&nbsp; SET(CMAKE_BUILD_TYPE Debug CACHE STRING &quot;Choose the type
of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
Debug Release RelWithDebInfo MinSizeRel.&quot;)<br>ENDIF()<br><br>PROJECT(foo)<br>ADD_EXECUTABLE(foo foo.cc)<br>#<br><br><br>Hope this helps.&nbsp; Also, be aware it will not work on multiple solution generators like for Visual Studio.<br>
</div></div><br>-- <br>Philip Lowman<br>
</div>

------=_Part_11706_11595526.1220505011929--


More information about the CMake mailing list