[CMake] Checking for empty list

Michael Hertling mhertling at online.de
Fri Nov 25 12:29:24 EST 2011


On 11/25/2011 03:15 PM, Michael Wild wrote:
> On 11/25/2011 02:52 PM, Daniel Dekkers wrote:
>> Hi,
>>
>>  
>>
>> I know it should be out there somewhere in the documentation, but can’t
>> find it.
>>
>> How do I check for an empty list?
>>
>> I can use LIST(LENGTH MYLIST LISTCOUNT) with an extra variable LISTCOUNT
>> but isn’t there a single command?
>>
>>  
>>
>> Thanks,
>>
>> Daniel
>>
> 
> 
> An empty list is an empty variable, and evaluates to FALSE. So you can use
> 
> if(NOT MYLIST)
>   message("Either MYLIST is empty or otherwise FALSE")
> endif()

...or otherwise FALSE: Exactly, and due to this, it's not bullet-proof:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(EMPTY NONE)
SET(MYLIST "FALSE")
LIST(LENGTH MYLIST n)
MESSAGE("Length of MYLIST: ${n}")
IF(MYLIST)
    MESSAGE("IF() says MYLIST is not empty.")
ELSE()
    MESSAGE("IF() says MYLIST is empty.")
ENDIF()

Because of non-empty strings - constituting non-empty lists - which
evaluate to FALSE in the IF() command, the only really safe way to
check whether a list is empty is using LIST(LENGTH ...), AFAICS.

Regards,

Michael


More information about the CMake mailing list