build: Generate better pkg-config files for static-only builds

pkg-config supports `Requires.private` and `Libs.private` fields for
static linking. However, if you're building a dynamic binary, then
pkg-config will use the non-private fields, even if just the static
libxml2 is available. This will result in libxml2 being underlinked,
causing the build to fail. The solution is to fold the private fields
into the non-private fields when the shared libxml2 is not being built.

This works for Autotools and CMake. Meson also knows how to handle this
when it automatically generates pkg-config files.
This commit is contained in:
James Le Cuirot 2023-09-02 17:12:58 +01:00
parent 4640ccac85
commit 93e8bb2a40
No known key found for this signature in database
GPG Key ID: 1226415D00DD3137
5 changed files with 35 additions and 12 deletions

View File

@ -348,11 +348,6 @@ add_library(LibXml2::LibXml2 ALIAS LibXml2)
target_compile_definitions(LibXml2 PRIVATE SYSCONFDIR="${CMAKE_INSTALL_FULL_SYSCONFDIR}")
if(NOT BUILD_SHARED_LIBS)
target_compile_definitions(LibXml2 PUBLIC LIBXML_STATIC)
set(XML_CFLAGS "-DLIBXML_STATIC")
endif()
if(CMAKE_C_COMPILER_ID MATCHES "Clang" OR CMAKE_C_COMPILER_ID STREQUAL "GNU")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic -Wall -Wextra -Wshadow \
-Wpointer-arith -Wcast-align -Wwrite-strings -Waggregate-return \
@ -654,6 +649,16 @@ set(XML_LIBS "-lxml2")
set(XML_PRIVATE_LIBS "${Z_LIBS} ${LZMA_LIBS} ${THREAD_LIBS} ${ICONV_LIBS} ${ICU_LIBS} ${LIBM} ${WINSOCK_LIBS}")
set(XML_PC_LIBS "${XML_PRIVATE_LIBS}")
if(BUILD_SHARED_LIBS)
set(XML_PC_PRIVATE ".private")
set(XML_PC_LIBS_PRIVATE "
Libs.private:")
else()
target_compile_definitions(LibXml2 PUBLIC LIBXML_STATIC)
set(XML_CFLAGS "-DLIBXML_STATIC")
set(XML_PRIVATE_LIBS_NO_SHARED "${XML_PRIVATE_LIBS}")
endif()
file(RELATIVE_PATH PACKAGE_RELATIVE_PATH "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig" "${CMAKE_INSTALL_PREFIX}")
string(REGEX REPLACE "/$" "" PACKAGE_RELATIVE_PATH "${PACKAGE_RELATIVE_PATH}")

View File

@ -1173,6 +1173,26 @@ XML_LIBTOOLLIBS="libxml2.la"
XML_PRIVATE_LIBS="${XML_PRIVATE_LIBS} $THREAD_LIBS $ICONV_LIBS $LIBM $NET_LIBS"
XML_PRIVATE_CFLAGS="${XML_PRIVATE_CFLAGS} $THREAD_CFLAGS $ICONV_CFLAGS"
dnl When static-only:
dnl * Duplicate xml-config static --libs into --dynamic.
dnl * Fold pkg-config private fields into main fields.
if test "x$enable_shared" = "xno"; then
XML_PRIVATE_LIBS_NO_SHARED="${XML_PRIVATE_LIBS}"
XML_PC_PRIVATE=
XML_PC_LIBS_PRIVATE=
else
XML_PRIVATE_LIBS_NO_SHARED=
XML_PC_PRIVATE=".private"
XML_PC_LIBS_PRIVATE="
Libs.private:"
fi
AC_SUBST(XML_PRIVATE_LIBS_NO_SHARED)
AC_SUBST(XML_PC_PRIVATE)
AC_SUBST(XML_PC_LIBS_PRIVATE)
AM_SUBST_NOTMAKE(XML_PRIVATE_LIBS_NO_SHARED)
AM_SUBST_NOTMAKE(XML_PC_PRIVATE)
AM_SUBST_NOTMAKE(XML_PC_LIBS_PRIVATE)
AC_SUBST(XML_PC_LIBS)
AC_SUBST(XML_PC_CFLAGS)
AC_SUBST(XML_PC_REQUIRES)

View File

@ -7,7 +7,6 @@ modules=@WITH_MODULES@
Name: libXML
Version: @VERSION@
Description: libXML library version2.
Requires: @XML_PC_REQUIRES@
Libs: -L${libdir} @XML_LIBS@
Libs.private: @XML_PC_LIBS@ @LIBS@
Requires@XML_PC_PRIVATE@: @XML_PC_REQUIRES@
Libs: -L${libdir} @XML_LIBS@ @XML_PC_LIBS_PRIVATE@ @XML_PC_LIBS@ @LIBS@
Cflags: @XML_INCLUDEDIR@ @XML_CFLAGS@ @XML_PC_CFLAGS@

View File

@ -7,7 +7,6 @@ modules=@WITH_MODULES@
Name: libXML
Version: @VERSION@
Description: libXML library version2.
Requires: @XML_PC_REQUIRES@
Libs: -L${libdir} @XML_LIBS@
Libs.private: @XML_PC_LIBS@ @LIBS@
Requires@XML_PC_PRIVATE@: @XML_PC_REQUIRES@
Libs: -L${libdir} @XML_LIBS@ @XML_PC_LIBS_PRIVATE@ @XML_PC_LIBS@ @LIBS@
Cflags: @XML_INCLUDEDIR@ @XML_CFLAGS@ @XML_PC_CFLAGS@

View File

@ -84,7 +84,7 @@ while test $# -gt 0; do
--libs)
if [ "$2" = "--dynamic" ]; then
shift
libs="@XML_LIBS@"
libs="@XML_LIBS@ @XML_PRIVATE_LIBS_NO_SHARED@"
else
libs="@XML_LIBS@ @XML_PRIVATE_LIBS@ @MODULE_PLATFORM_LIBS@ @LIBS@"
fi