Packaging static libraries for Fedora

Fedora’s packaging guidelines for static libraries are to put them into a separate -static sub-package that is not required by the main -devel sub-package. However this causes problems for most CMake projects because the cmake target expects the static archive to be present event when not using it and you get errors like:

-- HDF5 find comps: C;shared
CMake Error at /usr/lib64/cmake/hdf5/hdf5-targets.cmake:305 (message):
  The imported target "hdf5-static" references the file

     "/usr/lib64/libhdf5.a"

  but this file does not exist.  Possible reasons include:

  * The file was deleted, renamed, or moved to another location.

  * An install or uninstall procedure did not complete successfully.

  * The installation package was faulty and contained

     "/usr/lib64/cmake/hdf5/hdf5-targets.cmake"

  but not all the files it references.

Call Stack (most recent call first):
  /usr/lib64/cmake/hdf5/hdf5-config.cmake:185 (include)
  CMakeLists.txt:286 (find_package)

Is there an easy way we can avoid this?

Anyone? This is a very common issue we face in Fedora packaging. It also comes up when one would want to split a large project up to avoid some dependencies if possible.

So the general methodology we have come up with goes as follows:

  • Export the static targets to a separate file:
export ( EXPORT LIBTargets_static ${CMAKE_CURRENT_BINARY_DIR}/LIBTargets_static.cmake )
install ( EXPORT LIBTargets_static DESTINATION ${PKGFILEDIR}/cmake/LIB )
  • Include that file with OPTIONAL in the cmake config file so there is no error when it is missing:
if ( @BUILD_SHARED_LIBS@ )
include ( ${CMAKE_CURRENT_LIST_DIR}/LIBTargets.cmake )
endif ( )
if ( @BUILD_STATIC_LIBS@ )
include ( ${CMAKE_CURRENT_LIST_DIR}/LIBTargets_static.cmake OPTIONAL )
endif ( )

Does that seem like a good approach in general? Other things to consider? Any way that cmake could make this easier to achieve?

For background: