[CMake] Problem with compiler options and clearing a variable

Timo Rumland cr at olympclan.de
Sun Nov 19 11:46:56 EST 2006


Hello,

I'm on a project that tries CMake as a new build system. For the
project it is important, that people can add their code/modules
without touching the CMakeLists.txt files.

I have a bunch of subdirs called "modules". In every subdir there is a
CMakeLists.txt file, configuring the additional compiler options
(defines) for that module. In the CMake GUI, I can then activate or
deactivate each module.

My main CMakeLists.txt file contains among others the following line:

  add_subdirectory( mymodules )

Now in every CMakeLists.txt file in the subdirs of "mymodules" (I get
those CMakeLists.txt files with the FILE command and traverse them
with FOREACH to include each one in the "mymodules/CMakeLists.txt"
file) a variable "compilerFlags" is used in this way (simplified):


  option( MODULE_TEST1 "Activate TEST1" ON )
  if ( MODULE_TEST1 )
    set( compilerFlags ${compilerFlags} -DENABLE_TEST1 CACHE INTERNAL "compile flags" )
  endif ( MODULE_TEST1 )


As you can see, if the option is ON, "-DENABLE_TEST1" is added to the
variable "compilerFlags". I used "CACHE INTERNAL" so this variable is
also available in my main CMakeLists.txt file.

The Problem is now, when I set the option to OFF in the CMake GUI and
click on "configure", the compiler flags are not beeing removed from
the "compilerFlags" variable. Furthermore, the more I click on
"configure", the more "-DENABLE_TEST1" are in the variable!

So I thought it would be a good idea, to just reset the variable
before the "add_subdirectory( mymodules )" in my mein CMakeLists.txt
file, so it looks like that:

  # clear the compilerFlags variable
  set( compilerFlags CACHE INTERNAL "compile flags" )
  
  # Add subdirectory in which the compiler options are collected
  add_subdirectory( mymodules )
  
  # now let's see the content of compilerFlags
  message( "Compiler Flags:" ${compilerFlags} )

Sadly the variable compilerFlags is always empty, whatever the
MODULE_TEST1 option is set to - why?

Can you explain, why this happens? Or do you know another way for me,
I can manage to have an activate/deactivate option for my modules?


Thanks a lot for your help!




More information about the CMake mailing list