Adding compilation options

From OTBWiki
Revision as of 10:30, 12 December 2008 by Julien (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

To add a compilation option within CMake you have to

  1. define the option itself for CMake with the OPTION command
  2. you can hide it by using the MARK_AS_ADVANCED
  3. you can use IF/ELSE statements using the value of the option
  4. the compilation option itself is set with the ADD_DEFINITIONS command.

For example:

      OPTION(OTB_GL_USE_ACCEL "Use video card acceleration." ON)
	MARK_AS_ADVANCED(OTB_GL_USE_ACCEL)
	IF(OTB_GL_USE_ACCEL)
          # Add compiler option 
          ADD_DEFINITIONS(-DOTB_GL_USE_ACCEL)
	ENDIF(OTB_GL_USE_ACCEL)

If you want to be able to use this option from outside projects (say OTB-Applications using an option defined in OTB), you have to export the option into the OTBConfig.cmake file. This is done by adding the option value into OTBConfig.cmake.in as follows:

SET(OTB_GL_USE_ACCEL "@OTB_GL_USE_ACCEL@")

This way you can write this into the external project CMakeLists.txt:

      IF(OTB_GL_USE_ACCEL)
	  MESSAGE("Video card acceleration enabled.")
          ADD_DEFINITIONS(-DOTB_GL_USE_ACCEL)
	ELSE(OTB_GL_USE_ACCEL)
	  MESSAGE("Video card acceleration disabled.")
	ENDIF(OTB_GL_USE_ACCEL)

Another way to do this is to report this definition in some generated headers like otbConfig.h. To do this, you have to edit the otbConfig.h.in and add the following line:

#cmakedine OTB_GL_USE_ACCEL

This way, every c++ code include otbConfig.h will now the OTB_GL_USE_ACCEL definition. This could replace the ADD_DEFINITIONS(-DOTB_GL_USE_ACCEL) and is lighter, because you do not have to modify the CMakeLists.txt of the external projects.