From 7302a77e08e62fc3b00f75d8aa143c5565c3f680 Mon Sep 17 00:00:00 2001
From: Ross Burton <ross.burton@intel.com>
Date: Tue, 23 Jan 2018 00:54:13 +0000
Subject: [PATCH] gettext: beat library detection into shape

For reasons which I just can't fathom gnulib doesn't use the expected tools to
find libraries but badly reinvents the wheel.  This will trivially lead to host
contamination (explicit searches of /usr/lib) or incorrect RPATHs (bad
canonicalisation resulting in relative paths).

Simply delete all the crazy, and replace with a single call to pkg-config.

Upstream-Status: Inappropriate [upstream still refuse to consider pkg-config]
Signed-off-by: Ross Burton <ross.burton@intel.com>

Added also selinux discovery.

Signed-off-by: Gyorgy Sarvari <skandigraun@gmail.com>

---
 gettext-tools/gnulib-m4/libxml.m4       | 105 ++---------------------
 libtextstyle/gnulib-local/m4/libglib.m4 | 106 +++---------------------
 libtextstyle/gnulib-m4/libglib.m4       | 106 +++---------------------
 3 files changed, 31 insertions(+), 286 deletions(-)

diff --git a/gettext-tools/gnulib-m4/libxml.m4 b/gettext-tools/gnulib-m4/libxml.m4
index c7160d7..8e46cc7 100644
--- a/gettext-tools/gnulib-m4/libxml.m4
+++ b/gettext-tools/gnulib-m4/libxml.m4
@@ -14,6 +14,7 @@ dnl gl_LIBXML(FORCE-INCLUDED)
 dnl   forces the use of the included or an external libxml.
 AC_DEFUN([gl_LIBXML],
 [
+  AC_REQUIRE([PKG_PROG_PKG_CONFIG])
   AC_REQUIRE([AM_ICONV_LINK])
 
   ifelse([$1], , [
@@ -31,106 +32,10 @@ AC_DEFUN([gl_LIBXML],
   INCXML=
   ifelse([$1], [yes], , [
     if test "$gl_cv_libxml_use_included" != yes; then
-      dnl Figure out whether we can use a preinstalled libxml2, or have to use
-      dnl the included one.
-      AC_CACHE_VAL([gl_cv_libxml], [
-        gl_cv_libxml=no
-        gl_cv_LIBXML=
-        gl_cv_LTLIBXML=
-        gl_cv_INCXML=
-        gl_save_LIBS="$LIBS"
-        LIBS="$LIBS $LIBICONV"
-        dnl Search for libxml2 and define LIBXML2, LTLIBXML2 and INCXML2
-        dnl accordingly.
-        dnl Don't use xml2-config nor pkg-config, since it doesn't work when
-        dnl cross-compiling or when the C compiler in use is different from the
-        dnl one that built the library.
-        dnl Use a test program that tries to invoke xmlFree. On Cygwin 1.7.x,
-        dnl libxml2 is built in such a way that uses of xmlFree work fine with
-        dnl -Wl,--enable-auto-import but lead to a link error with
-        dnl -Wl,--disable-auto-import.
-        AC_LIB_LINKFLAGS_BODY([xml2])
-        LIBS="$gl_save_LIBS $LIBXML2 $LIBICONV"
-        AC_LINK_IFELSE(
-          [AC_LANG_PROGRAM(
-             [[#include <libxml/xmlversion.h>
-               #include <libxml/xmlmemory.h>
-               #include <libxml/xpath.h>
-             ]],
-             [[xmlCheckVersion (0);
-               xmlFree ((void *) 0);
-               xmlXPathSetContextNode ((void *)0, (void *)0);
-             ]])],
-          [gl_cv_libxml=yes
-           gl_cv_LIBXML="$LIBXML2 $LIBICONV"
-           gl_cv_LTLIBXML="$LTLIBXML2 $LTLIBICONV"
-          ])
-        if test "$gl_cv_libxml" != yes; then
-          gl_save_CPPFLAGS="$CPPFLAGS"
-          CPPFLAGS="$CPPFLAGS $INCXML2"
-          AC_LINK_IFELSE(
-            [AC_LANG_PROGRAM(
-               [[#include <libxml/xmlversion.h>
-                 #include <libxml/xmlmemory.h>
-                 #include <libxml/xpath.h>
-               ]],
-               [[xmlCheckVersion (0);
-                 xmlFree ((void *) 0);
-                 xmlXPathSetContextNode ((void *)0, (void *)0);
-               ]])],
-            [gl_cv_libxml=yes
-             gl_cv_LIBXML="$LIBXML2 $LIBICONV"
-             gl_cv_LTLIBXML="$LTLIBXML2 $LTLIBICONV"
-             gl_cv_INCXML="$INCXML2"
-            ])
-          if test "$gl_cv_libxml" != yes; then
-            dnl Often the include files are installed in /usr/include/libxml2.
-            dnl In libxml2-2.5, <libxml/xmlversion.h> is self-contained.
-            dnl In libxml2-2.6, it includes <libxml/xmlexports.h> which is
-            dnl self-contained.
-            libxml2_include_dir=
-            AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <libxml2/libxml/xmlexports.h>]])],
-              [gl_ABSOLUTE_HEADER([libxml2/libxml/xmlexports.h])
-               libxml2_include_dir=`echo "$gl_cv_absolute_libxml2_libxml_xmlexports_h" | sed -e 's,.libxml.xmlexports\.h$,,'`
-              ])
-            if test -z "$libxml2_include_dir"; then
-              AC_PREPROC_IFELSE([AC_LANG_SOURCE([[#include <libxml2/libxml/xmlversion.h>]])],
-                [gl_ABSOLUTE_HEADER([libxml2/libxml/xmlversion.h])
-                 libxml2_include_dir=`echo "$gl_cv_absolute_libxml2_libxml_xmlversion_h" | sed -e 's,.libxml.xmlversion\.h$,,'`
-                ])
-            fi
-            if test -n "$libxml2_include_dir" && test -d "$libxml2_include_dir"; then
-              CPPFLAGS="$gl_save_CPPFLAGS -I$libxml2_include_dir"
-              AC_LINK_IFELSE(
-                [AC_LANG_PROGRAM(
-                   [[#include <libxml/xmlversion.h>
-                     #include <libxml/xmlmemory.h>
-                     #include <libxml/xpath.h>
-                   ]],
-                   [[xmlCheckVersion (0);
-                     xmlFree ((void *) 0);
-                     xmlXPathSetContextNode ((void *)0, (void *)0);
-                   ]])],
-                [gl_cv_libxml=yes
-                 gl_cv_LIBXML="$LIBXML2 $LIBICONV"
-                 gl_cv_LTLIBXML="$LTLIBXML2 $LTLIBICONV"
-                 gl_cv_INCXML="-I$libxml2_include_dir"
-                ])
-            fi
-          fi
-          CPPFLAGS="$gl_save_CPPFLAGS"
-        fi
-        LIBS="$gl_save_LIBS"
-      ])
-      AC_MSG_CHECKING([for libxml])
-      AC_MSG_RESULT([$gl_cv_libxml])
-      if test $gl_cv_libxml = yes; then
-        LIBXML="$gl_cv_LIBXML"
-        LTLIBXML="$gl_cv_LTLIBXML"
-        INCXML="$gl_cv_INCXML"
-      else
-        gl_cv_libxml_use_included=yes
-      fi
+      PKG_CHECK_MODULES([XML], [libxml-2.0])
+      LIBXML=$XML_LIBS
+      LTLIBXML=$XML_LIBS
+      INCXML=$XML_CFLAGS
     fi
   ])
   AC_SUBST([LIBXML])
diff --git a/libtextstyle/gnulib-local/m4/libglib.m4 b/libtextstyle/gnulib-local/m4/libglib.m4
index baa2cf4..b287c5b 100644
--- a/libtextstyle/gnulib-local/m4/libglib.m4
+++ b/libtextstyle/gnulib-local/m4/libglib.m4
@@ -7,106 +7,26 @@ dnl with or without modifications, as long as this notice is preserved.
 
 dnl From Bruno Haible.
 
-dnl gl_LIBGLIB
-dnl   gives the user the option to decide whether to use the included or
-dnl   an external libglib.
-dnl gl_LIBGLIB(FORCE-INCLUDED)
-dnl   forces the use of the included or an external libglib.
 AC_DEFUN([gl_LIBGLIB],
 [
-  ifelse([$1], , [
-    AC_MSG_CHECKING([whether included glib is requested])
-    AC_ARG_WITH([included-glib],
-      [  --with-included-glib    use the glib2 included here],
-      [gl_cv_libglib_force_included=$withval],
-      [gl_cv_libglib_force_included=no])
-    AC_MSG_RESULT([$gl_cv_libglib_force_included])
-  ], [gl_cv_libglib_force_included=$1])
+  AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+  AC_MSG_CHECKING([whether included glib is requested])
+  AC_ARG_WITH([included-glib],
+    [  --with-included-glib    use the glib2 included here],
+    [gl_cv_libglib_force_included=$withval],
+    [gl_cv_libglib_force_included=no])
+  AC_MSG_RESULT([$gl_cv_libglib_force_included])
 
   gl_cv_libglib_use_included="$gl_cv_libglib_force_included"
   LIBGLIB=
   LTLIBGLIB=
   INCGLIB=
-  ifelse([$1], [yes], , [
-    if test "$gl_cv_libglib_use_included" != yes; then
-      dnl Figure out whether we can use a preinstalled libglib-2.0, or have to use
-      dnl the included one.
-      AC_CACHE_VAL([gl_cv_libglib], [
-        gl_cv_libglib=no
-        gl_cv_LIBGLIB=
-        gl_cv_LTLIBGLIB=
-        gl_cv_INCGLIB=
-        gl_save_LIBS="$LIBS"
-        dnl Search for libglib2 and define LIBGLIB_2_0, LTLIBGLIB_2_0 and
-        dnl INCGLIB_2_0 accordingly.
-        dnl Don't use glib-config nor pkg-config, since it doesn't work when
-        dnl cross-compiling or when the C compiler in use is different from the
-        dnl one that built the library.
-        AC_LIB_LINKFLAGS_BODY([glib-2.0])
-        LIBS="$gl_save_LIBS $LIBGLIB_2_0"
-        AC_LINK_IFELSE(
-          [AC_LANG_PROGRAM(
-             [[#include <glib.h>
-               #ifndef G_BEGIN_DECLS
-               error this glib.h includes a glibconfig.h from a glib version 1.x
-               #endif
-             ]],
-             [[g_string_new ("foo");]])],
-          [gl_cv_libglib=yes
-           gl_cv_LIBGLIB="$LIBGLIB_2_0"
-           gl_cv_LTLIBGLIB="$LTLIBGLIB_2_0"
-          ])
-        if test "$gl_cv_libglib" != yes; then
-          gl_save_CPPFLAGS="$CPPFLAGS"
-          CPPFLAGS="$CPPFLAGS $INCGLIB_2_0"
-          AC_LINK_IFELSE(
-            [AC_LANG_PROGRAM(
-               [[#include <glib.h>
-                 #ifndef G_BEGIN_DECLS
-                 error this glib.h includes a glibconfig.h from a glib version 1.x
-                 #endif
-               ]],
-               [[g_string_new ("foo");]])],
-            [gl_cv_libglib=yes
-             gl_cv_LIBGLIB="$LIBGLIB_2_0"
-             gl_cv_LTLIBGLIB="$LTLIBGLIB_2_0"
-             gl_cv_INCGLIB="$INCGLIB_2_0"
-            ])
-          if test "$gl_cv_libglib" != yes; then
-            dnl Often the include files are installed in /usr/include/glib-2.0
-            dnl and /usr/lib/glib-2.0/include.
-            if test -n "$LIBGLIB_2_0_PREFIX"; then
-              CPPFLAGS="$gl_save_CPPFLAGS -I$LIBGLIB_2_0_PREFIX/include/glib-2.0 -I$LIBGLIB_2_0_PREFIX/$acl_libdirstem/glib-2.0/include"
-              AC_LINK_IFELSE(
-                [AC_LANG_PROGRAM(
-                   [[#include <glib.h>
-                     #ifndef G_BEGIN_DECLS
-                     error this glib.h includes a glibconfig.h from a glib version 1.x
-                     #endif
-                   ]],
-                   [[g_string_new ("foo");]])],
-                [gl_cv_libglib=yes
-                 gl_cv_LIBGLIB="$LIBGLIB_2_0"
-                 gl_cv_LTLIBGLIB="$LTLIBGLIB_2_0"
-                 gl_cv_INCGLIB="-I$LIBGLIB_2_0_PREFIX/include/glib-2.0 -I$LIBGLIB_2_0_PREFIX/$acl_libdirstem/glib-2.0/include"
-                ])
-            fi
-          fi
-          CPPFLAGS="$gl_save_CPPFLAGS"
-        fi
-        LIBS="$gl_save_LIBS"
-      ])
-      AC_MSG_CHECKING([for glib])
-      AC_MSG_RESULT([$gl_cv_libglib])
-      if test $gl_cv_libglib = yes; then
-        LIBGLIB="$gl_cv_LIBGLIB"
-        LTLIBGLIB="$gl_cv_LTLIBGLIB"
-        INCGLIB="$gl_cv_INCGLIB"
-      else
-        gl_cv_libglib_use_included=yes
-      fi
-    fi
-  ])
+  if test "$gl_cv_libglib_use_included" != yes; then
+    PKG_CHECK_MODULES([GLIB], [glib-2.0])
+    LIBGLIB="$GLIB_LIBS"
+    LTLIBGLIB="$GLIB_LIBS"
+    INCGLIB="$GLIB_CFLAGS"
+  fi
   AC_SUBST([LIBGLIB])
   AC_SUBST([LTLIBGLIB])
   AC_SUBST([INCGLIB])
diff --git a/libtextstyle/gnulib-m4/libglib.m4 b/libtextstyle/gnulib-m4/libglib.m4
index baa2cf4..b287c5b 100644
--- a/libtextstyle/gnulib-m4/libglib.m4
+++ b/libtextstyle/gnulib-m4/libglib.m4
@@ -7,106 +7,26 @@ dnl with or without modifications, as long as this notice is preserved.
 
 dnl From Bruno Haible.
 
-dnl gl_LIBGLIB
-dnl   gives the user the option to decide whether to use the included or
-dnl   an external libglib.
-dnl gl_LIBGLIB(FORCE-INCLUDED)
-dnl   forces the use of the included or an external libglib.
 AC_DEFUN([gl_LIBGLIB],
 [
-  ifelse([$1], , [
-    AC_MSG_CHECKING([whether included glib is requested])
-    AC_ARG_WITH([included-glib],
-      [  --with-included-glib    use the glib2 included here],
-      [gl_cv_libglib_force_included=$withval],
-      [gl_cv_libglib_force_included=no])
-    AC_MSG_RESULT([$gl_cv_libglib_force_included])
-  ], [gl_cv_libglib_force_included=$1])
+  AC_REQUIRE([PKG_PROG_PKG_CONFIG])
+  AC_MSG_CHECKING([whether included glib is requested])
+  AC_ARG_WITH([included-glib],
+    [  --with-included-glib    use the glib2 included here],
+    [gl_cv_libglib_force_included=$withval],
+    [gl_cv_libglib_force_included=no])
+  AC_MSG_RESULT([$gl_cv_libglib_force_included])
 
   gl_cv_libglib_use_included="$gl_cv_libglib_force_included"
   LIBGLIB=
   LTLIBGLIB=
   INCGLIB=
-  ifelse([$1], [yes], , [
-    if test "$gl_cv_libglib_use_included" != yes; then
-      dnl Figure out whether we can use a preinstalled libglib-2.0, or have to use
-      dnl the included one.
-      AC_CACHE_VAL([gl_cv_libglib], [
-        gl_cv_libglib=no
-        gl_cv_LIBGLIB=
-        gl_cv_LTLIBGLIB=
-        gl_cv_INCGLIB=
-        gl_save_LIBS="$LIBS"
-        dnl Search for libglib2 and define LIBGLIB_2_0, LTLIBGLIB_2_0 and
-        dnl INCGLIB_2_0 accordingly.
-        dnl Don't use glib-config nor pkg-config, since it doesn't work when
-        dnl cross-compiling or when the C compiler in use is different from the
-        dnl one that built the library.
-        AC_LIB_LINKFLAGS_BODY([glib-2.0])
-        LIBS="$gl_save_LIBS $LIBGLIB_2_0"
-        AC_LINK_IFELSE(
-          [AC_LANG_PROGRAM(
-             [[#include <glib.h>
-               #ifndef G_BEGIN_DECLS
-               error this glib.h includes a glibconfig.h from a glib version 1.x
-               #endif
-             ]],
-             [[g_string_new ("foo");]])],
-          [gl_cv_libglib=yes
-           gl_cv_LIBGLIB="$LIBGLIB_2_0"
-           gl_cv_LTLIBGLIB="$LTLIBGLIB_2_0"
-          ])
-        if test "$gl_cv_libglib" != yes; then
-          gl_save_CPPFLAGS="$CPPFLAGS"
-          CPPFLAGS="$CPPFLAGS $INCGLIB_2_0"
-          AC_LINK_IFELSE(
-            [AC_LANG_PROGRAM(
-               [[#include <glib.h>
-                 #ifndef G_BEGIN_DECLS
-                 error this glib.h includes a glibconfig.h from a glib version 1.x
-                 #endif
-               ]],
-               [[g_string_new ("foo");]])],
-            [gl_cv_libglib=yes
-             gl_cv_LIBGLIB="$LIBGLIB_2_0"
-             gl_cv_LTLIBGLIB="$LTLIBGLIB_2_0"
-             gl_cv_INCGLIB="$INCGLIB_2_0"
-            ])
-          if test "$gl_cv_libglib" != yes; then
-            dnl Often the include files are installed in /usr/include/glib-2.0
-            dnl and /usr/lib/glib-2.0/include.
-            if test -n "$LIBGLIB_2_0_PREFIX"; then
-              CPPFLAGS="$gl_save_CPPFLAGS -I$LIBGLIB_2_0_PREFIX/include/glib-2.0 -I$LIBGLIB_2_0_PREFIX/$acl_libdirstem/glib-2.0/include"
-              AC_LINK_IFELSE(
-                [AC_LANG_PROGRAM(
-                   [[#include <glib.h>
-                     #ifndef G_BEGIN_DECLS
-                     error this glib.h includes a glibconfig.h from a glib version 1.x
-                     #endif
-                   ]],
-                   [[g_string_new ("foo");]])],
-                [gl_cv_libglib=yes
-                 gl_cv_LIBGLIB="$LIBGLIB_2_0"
-                 gl_cv_LTLIBGLIB="$LTLIBGLIB_2_0"
-                 gl_cv_INCGLIB="-I$LIBGLIB_2_0_PREFIX/include/glib-2.0 -I$LIBGLIB_2_0_PREFIX/$acl_libdirstem/glib-2.0/include"
-                ])
-            fi
-          fi
-          CPPFLAGS="$gl_save_CPPFLAGS"
-        fi
-        LIBS="$gl_save_LIBS"
-      ])
-      AC_MSG_CHECKING([for glib])
-      AC_MSG_RESULT([$gl_cv_libglib])
-      if test $gl_cv_libglib = yes; then
-        LIBGLIB="$gl_cv_LIBGLIB"
-        LTLIBGLIB="$gl_cv_LTLIBGLIB"
-        INCGLIB="$gl_cv_INCGLIB"
-      else
-        gl_cv_libglib_use_included=yes
-      fi
-    fi
-  ])
+  if test "$gl_cv_libglib_use_included" != yes; then
+    PKG_CHECK_MODULES([GLIB], [glib-2.0])
+    LIBGLIB="$GLIB_LIBS"
+    LTLIBGLIB="$GLIB_LIBS"
+    INCGLIB="$GLIB_CFLAGS"
+  fi
   AC_SUBST([LIBGLIB])
   AC_SUBST([LTLIBGLIB])
   AC_SUBST([INCGLIB])

diff --git a/gettext-tools/gnulib-m4/selinux-selinux-h.m4 b/gettext-tools/gnulib-m4/selinux-selinux-h.m4
index e2613fa..2a733a7 100644
--- a/gettext-tools/gnulib-m4/selinux-selinux-h.m4
+++ b/gettext-tools/gnulib-m4/selinux-selinux-h.m4
@@ -71,6 +71,7 @@ AC_DEFUN([gl_LIBSELINUX],
 [
   AC_REQUIRE([AC_CANONICAL_HOST])
   AC_REQUIRE([AC_CANONICAL_BUILD])
+  AC_REQUIRE([PKG_PROG_PKG_CONFIG])
 
   AC_ARG_WITH([selinux],
     AS_HELP_STRING([[--without-selinux]], [do not use SELinux, even on systems with SELinux]),
@@ -79,19 +80,8 @@ AC_DEFUN([gl_LIBSELINUX],
   LIB_SELINUX=
   if test "$with_selinux" != no; then
     gl_saved_LIBS=$LIBS
-    AC_SEARCH_LIBS([setfilecon], [selinux],
-                   [test "$ac_cv_search_setfilecon" = "none required" ||
-                    LIB_SELINUX=$ac_cv_search_setfilecon])
+    PKG_CHECK_MODULES([SELINUX], [libselinux], [LIB_SELINUX=$SELINUX_LIBS])
     LIBS=$gl_saved_LIBS
   fi
   AC_SUBST([LIB_SELINUX])
-
-  # Warn if SELinux is found but libselinux is absent;
-  if test "$ac_cv_search_setfilecon" = no; then
-    if test "$host" = "$build" && test -d /selinux; then
-      AC_MSG_WARN([This system supports SELinux but libselinux is missing.])
-      AC_MSG_WARN([AC_PACKAGE_NAME will be compiled without SELinux support.])
-    fi
-    with_selinux=no
-  fi
 ])
