Commit 1c513fbb8872bfac5631964963b6a751169a1ce6

Werner Lemberg 2017-02-04T20:30:40

Make `freetype-config' a wrapper of `pkg-config' if possible. Based on ideas taken from http://pkgs.fedoraproject.org/cgit/rpms/freetype.git/tree/freetype-multilib.patch http://pkgs.fedoraproject.org/cgit/rpms/freetype.git/tree/freetype-2.5.3-freetype-config-prefix.patch * builds/unix/freetype-config.in: Rewritten. Use `pkg-config' to set output variables if program is available. * docs/CHANGES, docs/freetype-config.1: Updated.

diff --git a/ChangeLog b/ChangeLog
index 61edea1..c85a698 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2017-02-04  Werner Lemberg  <wl@gnu.org>
 
+	Make `freetype-config' a wrapper of `pkg-config' if possible.
+
+	Based on ideas taken from
+
+	  http://pkgs.fedoraproject.org/cgit/rpms/freetype.git/tree/freetype-multilib.patch
+	  http://pkgs.fedoraproject.org/cgit/rpms/freetype.git/tree/freetype-2.5.3-freetype-config-prefix.patch
+
+	* builds/unix/freetype-config.in: Rewritten.  Use `pkg-config' to
+	set output variables if program is available.
+
+	* docs/CHANGES, docs/freetype-config.1: Updated.
+
+2017-02-04  Werner Lemberg  <wl@gnu.org>
+
 	* builds/unix/unix-def.in (freetype-config): Fix permissions.
 
 2017-02-03  Werner Lemberg  <wl@gnu.org>
diff --git a/builds/unix/freetype-config.in b/builds/unix/freetype-config.in
index 6c3e118..d20431b 100644
--- a/builds/unix/freetype-config.in
+++ b/builds/unix/freetype-config.in
@@ -12,11 +12,55 @@
 LC_ALL=C
 export LC_ALL
 
-prefix="%prefix%"
-exec_prefix="%exec_prefix%"
-exec_prefix_set="no"
-includedir="%includedir%"
-libdir="%libdir%"
+
+# if `pkg-config' is available, use values from `freetype.pc'
+pkg-config --version >/dev/null 2>&1
+if test $? -eq 0 ; then
+  # note that option `--variable' is not affected by the
+  # PKG_CONFIG_SYSROOT_DIR environment variable
+  if test "x$SYSROOT" != "x" ; then
+    PKG_CONFIG_SYSROOT_DIR="$SYSROOT"
+    export PKG_CONFIG_SYSROOT_DIR
+  fi
+
+  prefix=`pkg-config --variable prefix freetype2`
+  exec_prefix=`pkg-config --variable exec_prefix freetype2`
+
+  includedir=`pkg-config --variable includedir freetype2`
+  libdir=`pkg-config --variable libdir freetype2`
+
+  version=`pkg-config --modversion freetype2`
+
+  cflags=`pkg-config --cflags freetype2`
+  dynamic_libs=`pkg-config --libs freetype2`
+  static_libs=`pkg-config --static --libs freetype2`
+else
+  prefix="%prefix%"
+  exec_prefix="%exec_prefix%"
+
+  includedir="%includedir%"
+  libdir="%libdir%"
+
+  version=%ft_version%
+
+  cflags="-I${SYSROOT}$includedir/freetype2"
+  dynamic_libs="-lfreetype"
+  static_libs="%LIBSSTATIC_CONFIG%"
+  if test "${SYSROOT}$libdir" != "/usr/lib"   &&
+     test "${SYSROOT}$libdir" != "/usr/lib64" ; then
+    libs_L="-L${SYSROOT}$libdir"
+  fi
+fi
+
+orig_prefix=$prefix
+orig_exec_prefix=$exec_prefix
+
+orig_includedir=$includedir
+orig_libdir=$libdir
+
+include_suffix=`echo $includedir | sed "s|$prefix||"`
+lib_suffix=`echo $libdir | sed "s|$exec_prefix||"`
+
 
 usage()
 {
@@ -39,14 +83,17 @@ Options:
                          library
   --static               make command line options display flags
                          for static linking
+  --help                 display this help and exit
 EOF
   exit $1
 }
 
+
 if test $# -eq 0 ; then
   usage 1 1>&2
 fi
 
+
 while test $# -gt 0 ; do
   case "$1" in
   -*=*)
@@ -74,8 +121,8 @@ while test $# -gt 0 ; do
     echo_exec_prefix=yes
     ;;
   --version)
-    echo %ft_version%
-    exit 0
+    echo_version=yes
+    break
     ;;
   --ftversion)
     echo_ft_version=yes
@@ -92,6 +139,9 @@ while test $# -gt 0 ; do
   --static)
     show_static=yes
     ;;
+  --help)
+    usage 0
+    ;;
   *)
     usage 1 1>&2
     ;;
@@ -99,12 +149,27 @@ while test $# -gt 0 ; do
   shift
 done
 
+
 if test "$local_prefix" = "yes" ; then
   if test "$exec_prefix_set" != "yes" ; then
     exec_prefix=$prefix
   fi
 fi
 
+if test "$local_prefix" = "yes" ; then
+  includedir=${prefix}${include_suffix}
+  if test "$exec_prefix_set" = "yes" ; then
+    libdir=${exec_prefix}${lib_suffix}
+  else
+    libdir=${prefix}${lib_suffix}
+  fi
+fi
+
+
+if test "$echo_version" = "yes" ; then
+  echo $version
+fi
+
 if test "$echo_prefix" = "yes" ; then
   echo ${SYSROOT}$prefix
 fi
@@ -113,15 +178,6 @@ if test "$echo_exec_prefix" = "yes" ; then
   echo ${SYSROOT}$exec_prefix
 fi
 
-if test "$exec_prefix_set" = "yes" ; then
-  libdir=$exec_prefix/lib
-else
-  if test "$local_prefix" = "yes" ; then
-    includedir=$prefix/include
-    libdir=$prefix/lib
-  fi
-fi
-
 if test "$echo_ft_version" = "yes" ; then
   major=`grep define ${SYSROOT}$includedir/freetype2/freetype/freetype.h \
          | grep FREETYPE_MAJOR \
@@ -136,26 +192,20 @@ if test "$echo_ft_version" = "yes" ; then
 fi
 
 if test "$echo_cflags" = "yes" ; then
-  echo -I${SYSROOT}$includedir/freetype2
+  echo $cflags | sed "s|$orig_includedir/freetype2|$includedir/freetype2|"
 fi
 
 if test "$echo_libs" = "yes" ; then
-  libs="-lfreetype"
-  staticlibs="%LIBSSTATIC_CONFIG%"
   if test "$show_static" = "yes" ; then
-    libs="$staticlibs"
-  fi
-  if test "${SYSROOT}$libdir" != "/usr/lib"  &&
-     test "${SYSROOT}$libdir" != "/usr/lib64"; then
-    echo -L${SYSROOT}$libdir $libs
+    libs="$libs_L $static_libs"
   else
-    echo $libs
+    libs="$libs_L $dynamic_libs"
   fi
+  echo $libs | sed "s|$orig_libdir|$libdir|"
 fi
 
 if test "$echo_libtool" = "yes" ; then
-  convlib="libfreetype.la"
-  echo ${SYSROOT}$libdir/$convlib
+  echo ${SYSROOT}$libdir/libfreetype.la
 fi
 
 # EOF
diff --git a/docs/CHANGES b/docs/CHANGES
index 6ef2a9d..7112b56 100644
--- a/docs/CHANGES
+++ b/docs/CHANGES
@@ -47,6 +47,9 @@ CHANGES BETWEEN 2.7.1 and 2.7.2
       . Language, encoding, and name IDs have been updated to OpenType
         version 1.8.1.
 
+    - The `freetype-config' script is now a wrapper of `pkg-config' if
+      this program is available in the path.
+
 
 ======================================================================
 
diff --git a/docs/freetype-config.1 b/docs/freetype-config.1
index 76082ec..d98400d 100644
--- a/docs/freetype-config.1
+++ b/docs/freetype-config.1
@@ -23,6 +23,14 @@ FreeType library version installed on the system, such as the
 installation (directory path) prefix or the FreeType version number.
 .
 .PP
+If
+.BR pkg-config (1)
+is found in the path,
+.B freetype-config
+acts as a wrapper for
+.BR pkg-config .
+.
+.PP
 This program is part of the FreeType package.
 .
 .
@@ -50,7 +58,8 @@ Return the executable prefix value of the installed FreeType library
 .
 .TP
 .B \-\-ftversion
-Return the FreeType version number.
+Return the FreeType version number, directly derived from file
+`freetype.h'.
 .
 .TP
 .B \-\-version
@@ -72,32 +81,61 @@ Return compiler flags for compiling against the installed FreeType library.
 .B \-\-static
 Make command line options display flags for static linking.
 .
+.TP
+.B \-\-help
+Show help and exit.
+.
 .
 .SS Path override options
 .
 These affect any selected output option, except the libtool version
-returned by `--version'.
+returned by
+.BR \-\-version .
 .
 .TP
 .BI \-\-prefix= PREFIX
-Override `--prefix' value with
+Override
+.B \-\-prefix
+value with
 .IR PREFIX .
 .
+This also sets
+.BI \-\-exec-prefix= PREFIX
+if option
+.B \-\-exec-prefix
+is not explicitly given.
+.
 .TP
 .BI \-\-exec-prefix= EPREFIX
-Override `--exec-prefix' value with
+Override
+.B \-\-exec-prefix
+value with
 .IR EPREFIX .
 .
 .
 .SH BUGS
 In case the libraries FreeType links to are located in non-standard
-directories, the output from option
+directories, and
+.BR pkg-config (1)
+is not available, the output from option
 .B \-\-libs
 might be incomplete.
+.
 It is thus recommended to use the
 .BR pkg-config (1)
 interface instead, which is able to correctly resolve all dependencies.
 .
+.PP
+Setting
+.B \-\-exec-prefix
+(either explicitly or implicitly) might return incorrect results if
+combined with option
+.BR \-\-static .
+.
+The same problem can occur if you set the
+.B SYSROOT
+environment variable.
+.
 .
 .SH AUTHOR
 .