[PATCH 01/29] Add some autoconf helper macros for checking for libraries

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Most checks for libraries take the same format

  * --with-libFOO=yes|no|check|/some/path  argument
  * check for a function NNN in libFOO.so
  * check for a header file DDD/HHH.h
  * Define a WITH_FOO config.h symbol
  * Define a WITH_FOO make conditional
  * Substitute FOO_CFLAGS and FOO_LIBS make variables
  * Print CFLAGS & LIBS summary at the end

Doing all this correctly is rather difficult, typically
done by copy+paste of a previous usage. Further small
improvements people make are not applied to all previous
usages.

Improve this by creating some helper macros to apply
good practice. First, to perform the actual checks:

  LIBVIRT_CHECK_LIB([SELINUX],[selinux],[getfilecon][selinux/selinux.h])

This checks for 'getfilecon' in libselinux.so, and the
existance of 'selinux/selinux.h' header file. If successful
it sets SELINUX_CFLAGS and SELINUX_LIBS. The WITH_SELINUX
config.h macro and WITH_SELINUX make conditional are also
defined.

Finally to print a summary of CFLAGS & LIBs found (if any):

  LIBVIRT_RESULT_LIB([SELINUX],[selinux])

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 m4/virt-lib.m4    | 217 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 m4/virt-result.m4 |   9 +++
 2 files changed, 226 insertions(+)
 create mode 100644 m4/virt-lib.m4
 create mode 100644 m4/virt-result.m4

diff --git a/m4/virt-lib.m4 b/m4/virt-lib.m4
new file mode 100644
index 0000000..86a6aa0
--- /dev/null
+++ b/m4/virt-lib.m4
@@ -0,0 +1,217 @@
+dnl
+dnl Probe for existance of libXXXX and set WITH_XXX
+dnl config header var, WITH_XXXX make conditional and
+dnl with_XXX configure shell var.
+dnl
+dnl  LIBVIRT_CHECK_LIB([WITH_VAR],[NAME_VAR],[LIBNAME],[FUNCNAME],[HDRNAME])
+dnl
+dnl    WITH_VAR: Suffix for the WITH_XXX variable in config.h & conditional in make,
+dnl              and prefix for the XXX_CFLAGS and XXX_LIBS make variables
+dnl    NAME_VAR: Suffix for the --with-XXX configure arg and $with_XXX configure variable
+dnl     LIBNAME: Suffix for the library name libXXX.so (typically same as NAME_VAR)
+dnl    FUNCNAME: Name of function to check for in libXXX.so
+dnl     HDRNAME: Name of header file to check for
+dnl
+dnl eg
+dnl
+dnl  LIBVIRT_CHECK_LIB([SELINUX],[selinux],[selinux],[getfilecon],[selinux/selinux.h])
+dnl  LIBVIRT_CHECK_LIB([SANLOCK],[sanlock],[sanlock_client],[sanlock_init],[sanlock.h])
+dnl  LIBVIRT_CHECK_LIB([LIBATTR],[libattr],[attr],[getxattr],[attr/attr.h])
+dnl
+AC_DEFUN([LIBVIRT_CHECK_LIB],[
+  AS_VAR_PUSHDEF([config_var],[WITH_$1])
+  AS_VAR_PUSHDEF([make_var],[WITH_$1])
+  AS_VAR_PUSHDEF([cflags_var],[$1_CFLAGS])
+  AS_VAR_PUSHDEF([libs_var],[$1_LIBS])
+  AS_VAR_PUSHDEF([name_var],[$2])
+  AS_VAR_PUSHDEF([arg_var],[with-$2])
+  AS_VAR_PUSHDEF([with_var],[with_$2])
+  AS_VAR_PUSHDEF([libname_var],[$3])
+  AS_VAR_PUSHDEF([funcname_var],[$4])
+  dnl Does not work - '.' and '/' get changed into '_'
+  dnl AS_VAR_PUSHDEF([hdrname_var],[$5])
+
+  AC_ARG_WITH([name_var],
+    AC_HELP_STRING([--arg_var],
+                   [with lib$3 support @<:@default=check@:>@]),[],[with_var][=check])
+
+  old_LIBS="$LIBS"
+  old_CFLAGS="$CFLAGS"
+  AS_VAR_SET([cflags_var],[])
+  AS_VAR_SET([libs_var],[])
+
+  fail=0
+  if test "$with_var" != "no" ; then
+    if test "$with_var" != "yes" && test "$with_var" != "check" ; then
+      AS_VAR_SET([cflags_var],[-I$with_var/include])
+      AS_VAR_SET([libs_var],[-L$with_var/lib])
+    fi
+    CFLAGS="$CFLAGS $cflags_var"
+    LIBS="$LIBS $libs_var"
+    AC_CHECK_LIB([$3], funcname_var, [],[
+      if test "$with_var" != "check"; then
+        fail=1
+      fi
+      AS_VAR_SET([with_var],[no])
+    ])
+    if test "$fail" = "0" && test "$with_var" != "no" ; then
+      AC_CHECK_HEADER([$5], [
+        AS_VAR_SET([with_var],[yes])
+      ],[
+        if test "$with_var" != "check"; then
+          fail=1
+        fi
+        AS_VAR_SET([with_var],[no])
+      ])
+    fi
+  fi
+
+  LIBS="$old_LIBS"
+  CFLAGS="$old_CFLAGS"
+
+  if test $fail = 1; then
+    AC_MSG_ERROR([You must install the lib$3 library & headers to compile libvirt])
+  fi
+
+  if test "$with_var" = "yes" ; then
+    if test -z "$libs_var" ; then
+      AS_VAR_SET([libs_var],["-l$3"])
+    else
+      AS_VAR_SET([libs_var],["$]libs_var[ -l$3"])
+    fi
+
+    AC_DEFINE_UNQUOTED(config_var, 1, [whether $3 is available])
+  fi
+
+  AM_CONDITIONAL(make_var, [test "$with_var" = "yes"])
+
+  AC_SUBST(cflags_var)
+  AC_SUBST(libs_var)
+])
+
+dnl
+dnl Probe for existance of libXXXX, or alternatively libYYYY and set WITH_XXX
+dnl config header var, WITH_XXXX make conditional and with_XXX configure shell
+dnl var.
+dnl
+dnl  LIBVIRT_CHECK_LIB_FALLBACK([WITH_VAR],[WITH_VAR_2],[NAME_VAR],
+dnl                             [LIBNAME],[LIBNAME2],[FUNCNAME],[FUNCNAME2],[HDRNAME])
+dnl
+dnl    WITH_VAR: Suffix for the WITH_XXX variable in config.h & conditional in make,
+dnl              and prefix for the XXX_CFLAGS and XXX_LIBS make variables
+dnl   WITH_VAR2: Suffix for the WITH_XXX variable in config.h & conditional in make
+dnl              if the fallback library was required
+dnl    NAME_VAR: Suffix for the --with-XXX configure arg and $with_XXX configure variable
+dnl     LIBNAME: Suffix for the library name libXXX.so (typically same as NAME_VAR)
+dnl    LIBNAME2: Suffix for the library name libYYY.so fallback choice
+dnl    FUNCNAME: Name of function to check for in libXXX.so
+dnl   FUNCNAME2: Name of function to check for in libYYY.so
+dnl     HDRNAME: Name of header file to check for
+dnl
+dnl eg
+dnl
+dnl  LIBVIRT_CHECK_LIB([SASL],[SASL2],[sasl],
+dnl                    [sasl],[sasl2],[sasl_client_init],[sasl/sasl.h])
+dnl
+AC_DEFUN([LIBVIRT_CHECK_LIB_FALLBACK],[
+  AS_VAR_PUSHDEF([config_var],[WITH_$1])
+  AS_VAR_PUSHDEF([config2_var],[WITH_$2])
+  AS_VAR_PUSHDEF([make_var],[WITH_$1])
+  AS_VAR_PUSHDEF([make2_var],[WITH_$2])
+  AS_VAR_PUSHDEF([cflags_var],[$1_CFLAGS])
+  AS_VAR_PUSHDEF([libs_var],[$1_LIBS])
+  AS_VAR_PUSHDEF([name_var],[$3])
+  AS_VAR_PUSHDEF([arg_var],[with-$3])
+  AS_VAR_PUSHDEF([with_var],[with_$3])
+  AS_VAR_PUSHDEF([libname_var],[$4])
+  AS_VAR_PUSHDEF([libname2_var],[$5])
+  AS_VAR_PUSHDEF([funcname_var],[$6])
+  AS_VAR_PUSHDEF([funcname2_var],[$7])
+  dnl AS_VAR_PUSHDEF([hdrname_var],[$8])
+
+  AC_ARG_WITH([name_var],
+    AC_HELP_STRING([--arg_var],
+                   [with lib$3 support @<:@default=check@:>@]),[],[with_var][=check])
+
+  old_LIBS="$LIBS"
+  old_CFLAGS="$CFLAGS"
+  AS_VAR_SET([cflags_var],[])
+  AS_VAR_SET([libs_var],[])
+
+  fail=0
+  v2=0
+  if test "$with_var" != "no" ; then
+    if test "$with_var" != "yes" && test "$with_var" != "check" ; then
+      AS_VAR_SET([cflags_var],[-I$with_var/include])
+      AS_VAR_SET([libs_var],[-L$with_var/lib])
+    fi
+    CFLAGS="$CFLAGS $cflags_var"
+    LIBS="$LIBS $libs_var"
+    AC_CHECK_LIB([$4], funcname_var, [],[
+      AC_CHECK_LIB([$5], funcname2_var, [
+        v2=1
+      ],[
+        if test "$with_var" != "check"; then
+          fail=1
+        fi
+        AS_VAR_SET([with_var],[no])
+      ])
+    ])
+    if test "$fail" = "0" && test "$with_var" != "no" ; then
+      AC_CHECK_HEADER([$8], [
+        AS_VAR_SET([with_var],[yes])
+      ],[
+        if test "$with_var" != "check"; then
+          fail=1
+        fi
+        AS_VAR_SET([with_var],[no])
+      ])
+    fi
+  fi
+
+  LIBS="$old_LIBS"
+  CFLAGS="$old_CFLAGS"
+
+  if test $fail = 1; then
+    AC_MSG_ERROR([You must install the lib$4 library & headers to compile libvirt])
+  fi
+
+  if test "$with_var" = "yes" ; then
+    if test -z "$libs_var" ; then
+      AS_VAR_SET([libs_var],["-l$4"])
+    else
+      AS_VAR_SET([libs_var],["$]libs_var[ -l$4"])
+    fi
+
+    AC_DEFINE_UNQUOTED(config_var, 1, [whether lib$4 is available])
+    if test "$v2" = "1" ; then
+      AC_DEFINE_UNQUOTED(config2_var, 1, [whether lib$5 is available])
+    fi
+  fi
+
+  AM_CONDITIONAL(make_var, [test "$with_var" = "yes"])
+  AM_CONDITIONAL(make2_var, [test "$with_var" = "yes" && test "$v2" = "1"])
+
+  AC_SUBST(cflags_var)
+  AC_SUBST(libs_var)
+])
+
+dnl
+dnl To be called after a LIBVIRT_CHECK_LIB or LIBVIRT_CHECK_LIB_FALLBACK
+dnl invocation to print the result status
+dnl
+dnl  LIBVIRT_RESULT_LIB([WITH_VAR],[NAME_VAR])
+dnl
+dnl    WITH_VAR: Prefix for the XXX_CFLAGS and XXX_LIBS make variables
+dnl    NAME_VAR: Suffix for the --with-XXX configure arg and $with_XXX configure variable
+dnl
+dnl  LIBVIRT_RESULT_LIB([SELINUX],[selinux])
+dnl
+AC_DEFUN([LIBVIRT_RESULT_LIB],[
+  AS_VAR_PUSHDEF([cflags_var],[$1_CFLAGS])
+  AS_VAR_PUSHDEF([libs_var],[$1_LIBS])
+  AS_VAR_PUSHDEF([name_var],[$2])
+  AS_VAR_PUSHDEF([with_var],[with_$2])
+
+  LIBVIRT_RESULT(name_var, [$with_var], [CFLAGS=$cflags_var LIBS=$libs_var])
+])
diff --git a/m4/virt-result.m4 b/m4/virt-result.m4
new file mode 100644
index 0000000..c2e1517
--- /dev/null
+++ b/m4/virt-result.m4
@@ -0,0 +1,9 @@
+AC_DEFUN([LIBVIRT_RESULT], [
+  if test "$2" = "no" || test -z "$3" ; then
+    printf -v STR "%8s: %-3s" "$1" "$2"
+  else
+    printf -v STR "%8s: %-3s (%s)" "$1" "$2" "$3"
+  fi
+
+  AC_MSG_NOTICE([$STR])
+])
-- 
1.7.11.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]