[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


Hi,

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
trees.
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

mlvl
mlvl-static

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


include("@mlvl_CMAKE_DIR@/mlvlLibraryDepends.cmake")

set(mlvl_LIBRARY mlvl)

set(mlvl_LIBRARY_STATIC "mlvl_LIBRARY_STATIC-NOTFOUND")
if(TARGET mlvl-static)
    set(mlvl_LIBRARY_STATIC mlvl-static)
endif()

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


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
it.

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
ProjectConfig.cmake
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
for
projects with components or/and using external third-party packages, though
I didn't find a definitive answer (apologies if I missed an obvious
posting...).

Thanks,

Ben.
-------------- 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