[CMake] Proper options with cmake

Matthew Woehlke matthew.woehlke at kitware.com
Tue Jun 25 11:29:27 EDT 2013


On 2013-06-24 19:37, Gennadiy Rozental wrote:
> How do I implement a proper option with default value. Both boolean and string.
>
> If I define option like this:
>
> option( GOO "descr" OFF )
>
> here is what happends:
>
> 1. First time I run cmake without specifying GOO, GOO is OFF
> 2. Any time I run cmake with -DGOO=ON, GOO is ON
> 3. Any time I run cmake with -DGOO=OFF, GOO is OFF
> 4. Any time after the first I run cmake without specifying GOO, GOO is the
> same as it was in previous run, not necessarily OFF.
>
> My problem is semantic of (4). What I want is for GOO to have default value
> I specified if one was not provided, regardless of what was the value of GOO
> in previous run. This seems like such an obvious expectation, that I do not
> understand why it is not the case now.
>
> Can I get what I want?

Rolf's suggestion aside, why do you want to do this? Let's say you run 
CMake with -DGOO=ON, configure your project, and build your project. Now 
lets say you change a CMakeLists.txt and re-build your project. Now 
GOO=OFF, and as a result your reconfigure is going to behave 
differently, which means your rebuild is maybe now building something 
very different. You will have to take care to *always* re-run CMake 'by 
hand' before any build of your project, to prevent accidentally changing 
the value of GOO.

That said, I can think of things you might want to do with a variable 
that would not affect your build (e.g. dump extra logging during 
configure). If this is *not* what you're doing with GOO, however, I 
would encourage you to think very carefully about the ramifications of 
not remembering your build options.

-- 
Matthew



More information about the CMake mailing list