From 93e8bb2a402012858500b608b4146cd5c756e34d Mon Sep 17 00:00:00 2001 From: James Le Cuirot Date: Sat, 2 Sep 2023 17:12:58 +0100 Subject: [PATCH] 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. --- CMakeLists.txt | 15 ++++++++++----- configure.ac | 20 ++++++++++++++++++++ libxml-2.0-uninstalled.pc.in | 5 ++--- libxml-2.0.pc.in | 5 ++--- xml2-config.in | 2 +- 5 files changed, 35 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1e6d6164..321ea6e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") diff --git a/configure.ac b/configure.ac index 580050de..e56769be 100644 --- a/configure.ac +++ b/configure.ac @@ -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) diff --git a/libxml-2.0-uninstalled.pc.in b/libxml-2.0-uninstalled.pc.in index d6a2123f..de05306c 100644 --- a/libxml-2.0-uninstalled.pc.in +++ b/libxml-2.0-uninstalled.pc.in @@ -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@ diff --git a/libxml-2.0.pc.in b/libxml-2.0.pc.in index a5b718e2..d5867091 100644 --- a/libxml-2.0.pc.in +++ b/libxml-2.0.pc.in @@ -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@ diff --git a/xml2-config.in b/xml2-config.in index bda32211..1f82f80c 100644 --- a/xml2-config.in +++ b/xml2-config.in @@ -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