No subject
Thu Aug 21 14:30:17 EDT 2008
<br>
aren't effective. I don'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's called a cache variable on an initial configure.<br><br>Regarding the cache... First, to clear up any confusion you should understand that what you said above is not entirely true. 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). You can verify this by doing a "make VERBOSE=1" and examining the compile flags. The reason this is so is because local variables always trump cache variables in scope. "ccmake" and friends can only show you cache variables, not variables created when CMake executes your scripts. 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. 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 "documentation for this variable")<br><br>One of the side effects of the SET(...CACHE) command, however, is only to create a cache variable if one doesn't already exist with that name. (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. 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> SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel.")<br>
ELSE()<br> SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type
of build, options are: None(CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used)
Debug Release RelWithDebInfo MinSizeRel.")<br>ENDIF()<br><br>PROJECT(foo)<br>ADD_EXECUTABLE(foo foo.cc)<br>#<br><br><br>Hope this helps. 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