[cmake-developers] Exclusive Or operator

David Cole david.cole at kitware.com
Wed Sep 12 12:52:03 EDT 2012


Fair enough.

Thanks for the concrete example.


On Wed, Sep 12, 2012 at 12:47 PM, Robert Dailey
<rcdailey.lists at gmail.com> wrote:
> CMake tends to be incredibly verbose, so I like to avoid code like
> you've just shown me.
>
> Sometimes I have features that require two cache variables. Either
> both must be defined, or neither (in the latter case, the feature is
> disabled). Specifying one or the other is erroneous. A simple test
> would be like this with XOR:
>
> if( var1 AND var2 )
>     message( "Feature is ON" )
> elseif( var1 XOR var2 )
>     message( SEND_ERROR "Both variables must be specified" )
> endif()
>
> However, because I do not have XOR, I have to do crazy logic:
>
> if( var1 AND var2 )
>     message( "Feature is ON" )
> elseif( (NOT var1 AND var2) OR (var1 AND NOT var2) )
>     message( SEND_ERROR "Both variables must be specified" )
> endif()
>
> On Wed, Sep 12, 2012 at 11:30 AM, David Cole <david.cole at kitware.com> wrote:
>> Do you have a concrete example of why an IF XOR construct would be
>> useful to you? (i.e. -- do you encounter the need for this logical
>> construct frequently in your CMakeLists files?)
>>
>> It would certainly be easy to have a macro/function in the CMake
>> language that returns the logical equivalent of an XOR in a variable,
>> and then you could do a simple if test on that variable immediately
>> after calling that function...
>>
>> That requires no changes to CMake, but would not be quite as readable.
>>
>> And just like python, if you only care about the boolean state of a
>> variable, you could always do the "casting" something like this:
>>
>> if(var1)
>>   set(var1 1)
>> else()
>>   set(var1 0)
>> endif()
>> # same for var2, then
>> if(NOT ${var1} EQUAL ${var2})
>>   # voila, one is true, the other is not
>> else()
>>   # they're both the same
>> endif()
>>
>>
>> On Wed, Sep 12, 2012 at 12:00 PM, Robert Dailey
>> <rcdailey.lists at gmail.com> wrote:
>>> Honestly I'm not really worried about the "band wagon" or what other
>>> languages do. CMake is its own independent entity and domain language,
>>> if the feature is useful then it should have it :) Anyway I was just
>>> wondering if the feature would be accepted & useful. As I said I don't
>>> mind doing the work.
>>>
>>> Not every operator needs to short circuit. If you can't short circuit,
>>> then you can't. For example, in C++ you understand what short
>>> circuiting rules are and you work with that. But not every C++
>>> operator allows it.
>>>
>>> Also FWIW, in Python you can kind-of get XOR by converting both
>>> operands to boolean:
>>>
>>> bool(a) != bool(b)
>>>
>>> But we can't cast operands to bool on-the-fly in CMake (AFAIK) so this
>>> wouldn't be feasible.
>>>
>>> On Wed, Sep 12, 2012 at 6:49 AM, Brad King <brad.king at kitware.com> wrote:
>>>> On 09/11/2012 05:09 PM, Robert Dailey wrote:
>>>>> I don't see an exlusive or operator in CMake. Could I add one? Would
>>>>> such a feature be accepted?
>>>>>
>>>>> Syntax would be:
>>>>>
>>>>> if( var1 XOR var2 )
>>>>
>>>> Many languages like C, C++, python, lua, etc. do not offer
>>>> logical XOR operators, only bitwise XOR.  Unlike AND/OR/NOT
>>>> the XOR operation cannot be used in short-circuit evaluation
>>>> (CMake's if() command does not currently short-circuit, but
>>>> it could.).
>>>>
>>>> -Brad
>>> --
>>>
>>> Powered by www.kitware.com
>>>
>>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>>
>>> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/cgi-bin/mailman/listinfo/cmake-developers



More information about the cmake-developers mailing list