[CMake] How to test for the usability of a target

Matthew Woehlke matthew.woehlke at kitware.com
Tue Oct 15 19:22:09 EDT 2013


On 2013-10-15 11:49, Johannes Zarl wrote:
> I'd like to make some code in my build system dependent on the existance of a
> target. Basically I'd like to write something like this:
>
> if(TARGET optional_lib)
>    add_library(bar ${bar_srcs})
>    target_link_library(bar optional_lib)
> endif()
>
> The problem I have is that for a couple of reasons the CMakelists file for bar
> needs to be read before optional_lib. Therefore the "TARGET optional_lib" test
> fails even though I can link bar successfully against optional_lib.

If you *REALLY* need to know about all your targets before you decide on 
linking, then you're going to have to implement multi-pass project 
parsing. This is possible - see e.g. VTK - but makes the configuration 
process much more difficult to follow, can lead to serious maintenance 
issues, etc. The less magic involved in your build system, the better.

Personally, I'd say this smells like poor design that needs to be 
rethought. Maybe what you really want is a plugin system?

-- 
Matthew



More information about the CMake mailing list