[CMake] Conditional dependency

Hendrik Sattler post at hendrik-sattler.de
Tue Nov 6 06:44:05 EST 2007


Zitat von Eric Noulard <eric.noulard at gmail.com>:

> 2007/11/6, Salvatore Iovene <salvatore.iovene+cmake at googlemail.com>:
>> On 11/6/07, Nicholas Yue <yue.nicholas at gmail.com> wrote:
>> >
>> >   As there are hundreds of file, I want to avoid duplicating and add
>> > to maintainence.
>>
>> Try this:
>>
>> IF(NOT WIN32)
>>  SET(b_SOUCE b.cpp)
>> ENDIF(NOT WIN32)
>>
>> ADD_LIBRARY ( myLib STATIC a.cpp ${b_SOURCE} c.cpp <etc> )
>>
>> If you're not on WIN32, then the variable ${b_SOURCE} will be empty.
>
> In the same spirit I usually define a var which contains the list
> of file to be included in a library and augment var content
> conditionally.
>
> The general pattern is the following:
>
> # put unconditional sources in
> SET(MYLIB_SRC c.cpp g.cpp <any otherunconditional source>)
>
> # then ADD the conditional ones
> IF(WIN32)
>    SET(MYLIB_SRC ${MYLIB_SRC} win32-a.cpp)
> ENDIF(WIN32)
>
> IF(LIBXML2_FOUND)
>   SET(MYLIB_SRC ${MYLIB_SRC}  xml2-a.cpp)
> ENDIF()

Additionally, I suggest using the LIST macro instead, the above  
actually reinvents LIST(APPEND ....).

HS






More information about the CMake mailing list