Skip to content

PkgConfig: Populate _LINK_LIBRARIES variables. Add -static target.

Alex Birch requested to merge Birchlabs/cmake:pkgconfig-static-libs into master

Based on original work by @chuck.atkins !1900 (closed)
Adds the requested testing to !6844 (closed)

The FindPkgConfig documentation refers to a <XXX>_STATIC_LINK_LIBRARIES variable. This is imperative for good interop with target_link_libraries(), as it promises fully-qualified paths to libraries.

In version 3.22.1, this variable is not populated. This MR populates it with such data as:

FLUIDSYNTH_STATIC_LINK_LIBRARIES:INTERNAL=/Users/birch/juicydeps/lib64/libfluidsynth.a;/opt/homebrew/Cellar/glib/2.70.2/lib/libgthread-2.0.a;/opt/homebrew/Cellar/glib/2.70.2/lib/libglib-2.0.a;/opt/homebrew/opt/gettext/lib/libintl.a;/opt/homebrew/Cellar/libsndfile/1.0.31/lib/libsndfile.a;m;/opt/homebrew/Cellar/glib/2.70.2/lib/libgthread-2.0.a;/opt/homebrew/opt/gettext/lib/libintl.a;/opt/homebrew/Cellar/glib/2.70.2/lib/libglib-2.0.a;/opt/homebrew/opt/gettext/lib/libintl.a;iconv;m;/opt/homebrew/Cellar/pcre/8.45/lib/libpcre.a;/opt/homebrew/Cellar/libsndfile/1.0.31/lib/libsndfile.a;/opt/homebrew/Cellar/flac/1.3.3/lib/libFLAC.a;m;/opt/homebrew/Cellar/libvorbis/1.3.7/lib/libvorbisenc.a;/opt/homebrew/Cellar/libvorbis/1.3.7/lib/libvorbis.a;m;/opt/homebrew/Cellar/libogg/1.3.5/lib/libogg.a;/opt/homebrew/Cellar/opus/1.3.1/lib/libopus.a

The use of IMPORTED_TARGET has always created a PkgConfig::<prefix> target.
This MR introduces a new PkgConfig::<prefix>-static target, whose properties are initialized from the STATIC_ variants of each variable.

The upshot is that it's now possible to statically link to a library (and its every static dependency identified via pkg-config) using this technique:

find_package(PkgConfig REQUIRED)
pkg_search_module(FLUIDSYNTH REQUIRED IMPORTED_TARGET fluidsynth>=2)
target_link_libraries(MyCoolAudioPlugin PRIVATE PkgConfig::FLUIDSYNTH-static)

You could likewise make use of the <XXX>_STATIC_LINK_LIBRARIES variable:

target_link_libraries(MyCoolAudioPlugin PRIVATE ${FLUIDSYNTH_STATIC_LINK_LIBRARIES})

Either of these techniques will append (for example) the following options to <target>/link.txt:

/Users/birch/juicydeps/lib64/libfluidsynth.a /opt/homebrew/Cellar/glib/2.70.2/lib/libgthread-2.0.a /opt/homebrew/Cellar/glib/2.70.2/lib/libglib-2.0.a /opt/homebrew/opt/gettext/lib/libintl.a /opt/homebrew/Cellar/libsndfile/1.0.31/lib/libsndfile.a -lm -liconv /opt/homebrew/Cellar/pcre/8.45/lib/libpcre.a /opt/homebrew/Cellar/flac/1.3.3/lib/libFLAC.a /opt/homebrew/Cellar/libvorbis/1.3.7/lib/libvorbisenc.a /opt/homebrew/Cellar/libvorbis/1.3.7/lib/libvorbis.a /opt/homebrew/Cellar/glib/2.70.2/lib/libgthread-2.0.a /opt/homebrew/Cellar/glib/2.70.2/lib/libglib-2.0.a /opt/homebrew/opt/gettext/lib/libintl.a /opt/homebrew/Cellar/libsndfile/1.0.31/lib/libsndfile.a -lm -liconv /opt/homebrew/Cellar/pcre/8.45/lib/libpcre.a /opt/homebrew/Cellar/flac/1.3.3/lib/libFLAC.a /opt/homebrew/Cellar/libvorbis/1.3.7/lib/libvorbisenc.a /opt/homebrew/Cellar/libvorbis/1.3.7/lib/libvorbis.a /opt/homebrew/Cellar/libogg/1.3.5/lib/libogg.a /opt/homebrew/Cellar/opus/1.3.1/lib/libopus.a

====

Added LINK_LIBRARIES test to demonstrate static linking of transitive dependencies.
Updated existing tests concerning target or variables, to test -static target and <XXX>_STATIC_* variables also.

Breaking changes to pkg_check_modules() and pkg_search_module():

  • <XXX>_LINK_LIBRARIES will now only be populated with shared libraries.
  • Variables CMAKE_FIND_LIBRARY_PREFIXES and CMAKE_FIND_LIBRARY_SUFFIXES can no longer be used to influence library lookup (i.e. the internal call to find_library()), because FindPkgConfig now internally relies on these variables to differentiate between shared and static library lookup.
    Prefer CMAKE_SHARED_LIBRARY_PREFIX + CMAKE_SHARED_LIBRARY_SUFFIX, or CMAKE_STATIC_LIBRARY_PREFIX + CMAKE_STATIC_LIBRARY_SUFFIX, depending on whether you wish to impact static or shared lookup.
  • <XXX>_STATIC_LIBRARIES now processes -framework options
  • <XXX>_STATIC_LDFLAGS_OTHER now processes -framework options
  • <XXX>_STATIC_CFLAGS_OTHER now processes -isystem options
  • <XXX>_STATIC_INCLUDE_DIRS now processes -isystem options

Fixes: #21714

Edited by Brad King

Merge request reports