Skip to content

PkgConfig: Populate _LINK_LIBRARIES variables. Add -static target.

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

MR wasn't visible on https://gitlab.kitware.com/cmake/cmake/-/merge_requests, so I've closed Birchlabs/cmake!1 (closed) and re-submitted.
MR re-opened as: !7070 (merged)

Resolves #21714

Based on original work by @chuck.atkins !1900 (closed)

The FindPkgConfig documentation refers to <XXX>_LINK_LIBRARIES variables (including a STATIC_ variant). These are imperative for good interop with target_link_libraries(), as they promise fully-qualified paths to libraries.

In version 3.22.1, these variables are not populated. This MR fixes that, and populates them with data such as:

FLUIDSYNTH_LINK_LIBRARIES:INTERNAL=/Users/birch/juicydeps/lib64/libfluidsynth.dylib
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

This MR passes the LINK_LIBRARIES variables to the PkgConfig::<prefix> target which is created, as a INTERFACE_LINK_LIBRARIES property.

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 _LINK_LIBRARIES variables:

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
Edited by Alex Birch

Merge request reports