[CMake] ProjectConfig.cmake files with both dynamic and static libraries?

Ben Morgan bmorgan.warwick at gmail.com
Wed May 4 08:30:53 EDT 2011


I've been working on adding <Project>Config.cmake support to my CMake based
projects, and just had a quick question regarding 'best practice' in writing
these when the project builds both dynamic and (optionally) static
libraries, with clients of the project wishing to choose one or the other.

Thanks to the ProjectConfig tutorial on the Wiki :-), I've got the basic
files for a test project 'mlvl' working nicely from the build and install
By default, mlvl builds a dynamic library with a user option to additionally
build a static version, hence the mlvlLibraryDepends.cmake files
have the imported targets


though the mlvl-static target will only be present if the build static
option was enabled. The relevant section of my mlvlConfig.cmake.in file
then contains


set(mlvl_LIBRARY mlvl)

if(TARGET mlvl-static)
    set(mlvl_LIBRARY_STATIC mlvl-static)

        set(mlvl_LIBRARIES ${mlvl_LIBRARY_STATIC})
        message(WARNING "no static build of mlvl available, falling back to
use dynamic library")
        set(mlvl_LIBRARIES ${mlvl_LIBRARY})
    set(mlvl_LIBRARIES ${mlvl_LIBRARY})

where mlvl_USE_STATIC is a variable clients of mlvl can set before calling
find_package. This seems to work o.k., but I'm a
little unsure about:

a) if it looks reasonable, and within the scope of what a <NAME>Config.cmake
script is supposed to do (my impression from reading the lists
is that they're supposed to be very simple...).

b) If a FATAL_ERROR could/should be issued if the client has requested use
of the static library but the install of mlvl that's been found doesn't have

Replacing message(WARNING ...) with message(FATAL_ERROR ...) does work, and
I'm not sure if this should be done within the config script or
whether I should just leave it up to the client to decide what to do post
the find_package call - it would seem within the spirit of module mode
find_package to emit a FATAL_ERROR if both mlvl_USE_STATIC is set and
find_package was called with REQUIRED...?

I realize I could make my life much easier by always building both libraries
;-), but I'm interested in the limits of what should be done in a
file versus how much responsibility to pass onto the user.. This seems to
relate to previous discussions on this list about how to write config files
projects with components or/and using external third-party packages, though
I didn't find a definitive answer (apologies if I missed an obvious


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20110504/38061ce8/attachment.htm>

More information about the CMake mailing list