[libvirt] [PATCH 2/3] Add some autoconf helper macros for checking for libraries

Daniel P. Berrange berrange at redhat.com
Wed Sep 19 18:09:23 UTC 2012


From: "Daniel P. Berrange" <berrange at redhat.com>

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 at redhat.com>
---
 m4/virt-lib.m4    | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 m4/virt-result.m4 |  9 ++++++
 2 files changed, 91 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..9cdaf7f
--- /dev/null
+++ b/m4/virt-lib.m4
@@ -0,0 +1,82 @@
+dnl
+dnl Probe for existance of libattr and set WITH_LIBATTR
+dnl config header var, WITH_LIBATTR make conditional and
+dnl with_libattr configure shell var.
+dnl
+dnl  LIBVIRT_CHECK_LIB([VARNAME],[LIBNAME],[FUNCNAME],[HDRNAME])
+dnl
+dnl eg
+dnl
+dnl  LIBVIRT_CHECK_LIB([SELINUX],[selinux],[getfilecon][selinux/selinux.h])
+dnl
+dnl  LIBVIRT_RESULT_LIB([SELINUX],[selinux])
+dnl
+AC_DEFUN([LIBVIRT_CHECK_LIB],[
+  AS_VAR_PUSHDEF([with_var],[with_lib$2])
+  AS_VAR_PUSHDEF([cflags_var],[$1_CFLAGS])
+  AS_VAR_PUSHDEF([libs_var],[$1_LIBS])
+
+  AC_ARG_WITH([lib$2],
+    AC_HELP_STRING([--with-lib$2],
+                   [with lib$2 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([$2], [$3], [
+      AC_CHECK_HEADER([$4], [
+        AS_VAR_SET([with_var],[yes])
+      ],[
+        if test "$with_var" != "check"; then
+          fail=1
+        fi
+        AS_VAR_SET([with_var],[no])
+      ])
+    ],[
+      if test "$with_var" != "check"; then
+        fail=1
+      fi
+      AS_VAR_SET([with_var],[no])
+    ])
+  fi
+
+  LIBS="$old_LIBS"
+  CFLAGS="$old_CFLAGS"
+
+  if test $fail = 1; then
+    AC_MSG_ERROR([You must install the lib$2 library & headers to compile libvirt])
+  fi
+
+  if test "$with_var" = "yes" ; then
+    if test -z "$libs_var" ; then
+      AS_VAR_SET([libs_var],["-l$2"])
+    else
+      AS_VAR_SET([libs_var],["$][libs_var][ -l$2"])
+    fi
+
+    AC_DEFINE_UNQUOTED([WITH_$1], 1, [whether lib$2 is available])
+  fi
+
+  AM_CONDITIONAL([WITH_$1], [test "$with_var" = "yes"])
+
+  AC_SUBST(cflags_var)
+  AC_SUBST(libs_var)
+])
+
+AC_DEFUN([LIBVIRT_RESULT_LIB],[
+  AS_VAR_PUSHDEF([with_var],[with_lib$2])
+  AS_VAR_PUSHDEF([cflags_var],[$1_CFLAGS])
+  AS_VAR_PUSHDEF([libs_var],[$1_LIBS])
+
+  LIBVIRT_RESULT([$2], [$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..85ae98f
--- /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: %4s" "$1" "$2"
+  else
+    printf -v STR "%8s: %4s (%s)" "$1" "$2" "$3"
+  fi
+
+  AC_MSG_NOTICE([$STR])
+])
-- 
1.7.11.4




More information about the libvir-list mailing list