[PATCH] Don't use deprecated glib function to construct path on win32

LRN lrn1986 at gmail.com
Fri May 18 08:40:38 PDT 2012


g_win32_get_package_installation_subdirectory() has been deprecated
since GLib 2.18 and in recent (2.31) GLib versions disabled entirely by
default. This patch replaces usage of that function by
g_win32_get_package_installation_directory_of_module() and
g_build_filename(). Use the new functions and rework the code a bit so
it leaks less memory.

g_win32_get_package_installation_directory_of_module() is supported
since GLib 2.16. The minimal GLib version is bumped accordingly.

Freedesktop #45742
---
 configure.ac |   19 ++++++++++++-------
 main.c       |   43 ++++++++++++++++++++++++++++++++++++++++++-
 pkg.c        |    2 +-
 pkg.h        |   16 +++++-----------
 4 files changed, 60 insertions(+), 20 deletions(-)

diff --git a/configure.ac b/configure.ac
index 763c7d1..2c936d4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -123,6 +123,11 @@ esac
 AC_MSG_RESULT([$native_win32])
 AM_CONDITIONAL(NATIVE_WIN32, [test "x$native_win32" = xyes])
 
+dnl
+dnl Find glib or use internal copy. Required version is 2.16 for
+dnl g_win32_get_package_installation_directory_of_module().
+dnl
+m4_define([glib_module], ["glib-2.0 >= 2.16"])
 AC_ARG_WITH([internal-glib],
   [AS_HELP_STRING([--with-internal-glib], [use internal glib])],
   [with_internal_glib="$withval"],
@@ -136,14 +141,14 @@ if test "x$with_internal_glib" = xyes; then
 else
   if test "x$GLIB_CFLAGS" = "x" && test "x$GLIB_LIBS" = "x"; then
     AC_CHECK_PROGS([PKG_CONFIG], [pkg-config], [])
-    if test -n $PKG_CONFIG && $PKG_CONFIG --exists glib-2.0; then
-      GLIB_CFLAGS=`$PKG_CONFIG --cflags glib-2.0`
-      GLIB_LIBS=`$PKG_CONFIG --libs glib-2.0`
+    if test -n $PKG_CONFIG && $PKG_CONFIG --exists glib_module; then
+      GLIB_CFLAGS=`$PKG_CONFIG --cflags glib_module`
+      GLIB_LIBS=`$PKG_CONFIG --libs glib_module`
     else
-      AC_MSG_ERROR(m4_normalize([pkg-config and glib-2.0 not found, please set
-                                 GLIB_CFLAGS and GLIB_LIBS to the correct
-                                 values or pass --with-internal-glib to
-                                 configure]))
+      AC_MSG_ERROR(m4_normalize([pkg-config and ]glib_module[ not found,
+                                 please set GLIB_CFLAGS and GLIB_LIBS to
+                                 the correct values or pass
+                                 --with-internal-glib to configure]))
     fi
   fi
 fi
diff --git a/main.c b/main.c
index 280b94c..b56cd5e 100644
--- a/main.c
+++ b/main.c
@@ -39,6 +39,7 @@ static int want_debug_spew = 0;
 static int want_verbose_errors = 0;
 static int want_stdout_errors = 0;
 char *pcsysrootdir = NULL;
+char *pkg_config_pc_path = NULL;
 
 void
 debug_spew (const char *format, ...)
@@ -162,6 +163,34 @@ print_hashtable_key (gpointer key,
   printf("%s\n", (gchar*)key);
 }
 
+static void
+init_pc_path (void)
+{
+#ifdef G_OS_WIN32
+  char *instdir, *lpath, *shpath;
+
+  instdir = g_win32_get_package_installation_directory_of_module (NULL);
+  if (instdir == NULL)
+    {
+      /* This only happens when GetModuleFilename() fails. If it does, that
+       * failure should be investigated and fixed.
+       */
+      debug_spew ("g_win32_get_package_installation_directory_of_module failed\n");
+      return;
+    }
+
+  lpath = g_build_filename (pkg_config_pc_path, "lib", "pkgconfig", NULL);
+  shpath = g_build_filename (pkg_config_pc_path, "share", "pkgconfig", NULL);
+  pkg_config_pc_path = g_strconcat (lpath, G_SEARCHPATH_SEPARATOR_S, shpath,
+                                    NULL);
+  free (instdir);
+  free (lpath);
+  free (shpath);
+#else
+  pkg_config_pc_path = PKG_CONFIG_PC_PATH;
+#endif
+}
+
 int
 main (int argc, char **argv)
 {
@@ -284,6 +313,18 @@ main (int argc, char **argv)
       debug_spew ("PKG_CONFIG_DEBUG_SPEW variable enabling debug spew\n");
     }
 
+
+  /* Get the built-in search path */
+  init_pc_path ();
+  if (pkg_config_pc_path == NULL)
+    {
+      /* Even when we override the built-in search path, we still use it later
+       * to add pc_path to the virtual pkg-config package.
+       */
+      verbose_error ("Failed to get default search path\n");
+      exit (1);
+    }
+
   search_path = getenv ("PKG_CONFIG_PATH");
   if (search_path) 
     {
@@ -295,7 +336,7 @@ main (int argc, char **argv)
     }
   else
     {
-      add_search_dirs(PKG_CONFIG_PC_PATH, G_SEARCHPATH_SEPARATOR_S);
+      add_search_dirs(pkg_config_pc_path, G_SEARCHPATH_SEPARATOR_S);
     }
 
   pcsysrootdir = getenv ("PKG_CONFIG_SYSROOT_DIR");
diff --git a/pkg.c b/pkg.c
index 728c27c..551b867 100644
--- a/pkg.c
+++ b/pkg.c
@@ -240,7 +240,7 @@ add_virtual_pkgconfig_package (void)
 
   if (pkg->vars == NULL)
     pkg->vars = g_hash_table_new (g_str_hash, g_str_equal);
-  g_hash_table_insert (pkg->vars, "pc_path", PKG_CONFIG_PC_PATH);
+  g_hash_table_insert (pkg->vars, "pc_path", pkg_config_pc_path);
 
   debug_spew ("Adding virtual 'pkg-config' package to list of known packages\n");
   g_hash_table_insert (packages, pkg->key, pkg);
diff --git a/pkg.h b/pkg.h
index 5749518..ef18af0 100644
--- a/pkg.h
+++ b/pkg.h
@@ -22,17 +22,6 @@
 
 #include <glib.h>
 
-#ifdef G_OS_WIN32
-/* No hardcoded paths in the binary, thanks */
-/* It's OK to leak this */
-#undef PKG_CONFIG_PC_PATH
-#define PKG_CONFIG_PC_PATH \
-  g_strconcat (g_win32_get_package_installation_subdirectory (NULL, NULL, "lib/pkgconfig"), \
-	       ";", \
-	       g_win32_get_package_installation_subdirectory (NULL, NULL, "share/pkgconfig"), \
-	       NULL)
-#endif
-
 typedef enum
 {
   LESS_THAN,
@@ -136,6 +125,11 @@ extern gboolean disable_uninstalled;
 
 extern char *pcsysrootdir;
 
+/* pkg-config default search path. On Windows the current pkg-config install
+ * directory is used. Otherwise, the build-time defined PKG_CONFIG_PC_PATH.
+ */
+extern char *pkg_config_pc_path;
+
 #ifdef G_OS_WIN32
 /* If TRUE, do not automatically define "prefix"  while
  * parsing each .pc file */
-- 
1.7.7.6


--AhhlLboLdkugWU4S--


More information about the pkg-config mailing list