[CMake] Detect build type

Brad King brad.king at kitware.com
Fri Apr 22 09:00:44 EDT 2005


Filipe Sousa wrote:
> I'm using it for two things: First, I need to control CMAKE_SKIP_RPATH, I 
> don't want rpath in release mode. Second, my application needs to know where 
> to find some resources in the source tree when I'm in the development mode 
> (that's when I set CMAKE_BUILD_TYPE to Debug), in the release mode the 
> application is able to find the resources in the installation directory. 
> Depending on the build type, cmake generates two different files called 
> PathResolver.cc that knows where to find the application resources.

I deal with some software packages that have both of these issues. 
Often I run things from the build tree even in release mode in the case 
of a heisenbug.  Usually I have separate release scripts that 
build/install/package a project.  In the build phase the scripts runs 
CMake with -DCMAKE_SKIP_RPATH:BOOL=ON to get rid of rpaths for the 
release build.

For finding resources I usually configure a header file with macros 
containing strings indicating the build tree location and the relative 
path from the installed executable location to the installed resources. 
  Then at runtime the executable detects from argv[0] whether it is 
running from the build tree.  If so it uses the resources out of the 
build tree.  If not it looks relative to its location to find the 
installed resources.  This works well because I can then run the same 
executable from the build tree and install trees without compiling with 
different configurations.

You could also use a separate switch that enables a "distribution 
release" mode intended to be enabled when building the project for 
binary distribution or at least installation on your machine.

> How does cmake knows when to use CMAKE_CXX_FLAGS_DEBUG or 
> CMAKE_CXX_FLAGS_RELEASE?

The makefile generator uses the build type to choose one.  The project 
file generators generate multiple configurations into the same build 
tree.  Each configuration uses the flags from the appropriate variable. 
  Then the build environment chooses a configuration at build time after 
CMake has finished.

-Brad


More information about the CMake mailing list