[Libguestfs] [PATCH libnbd 2/3] build: Allow C programs using libnbd to be compiled against build dir.

Richard W.M. Jones rjones at redhat.com
Thu Mar 12 22:46:56 UTC 2020


We use a similar trick to libvirt and libguestfs to allow external C
programs that use libnbd to be compiled against the built (but not
installed) libnbd with:

  ../libnbd/run ./configure
  make

What actually happens is we have a second pkg-config file
(lib/local/libnbd.pc) which points to the locally built libnbd.  The
./run script sets up PKG_CONFIG_PATH to point to this directory.
Assuming that ./configure is using pkg-config/pkgconf and not some
other half-baked solution it will pick up the libnbd.pc file from here
which will set CFLAGS and LIBS appropriately.

See also this libguestfs commit:
https://github.com/libguestfs/libguestfs/commit/e33b3c83a02c1d7e82018105166e451373e8c218
---
 .gitignore             |  1 +
 configure.ac           |  1 +
 lib/Makefile.am        |  1 +
 lib/local/libnbd.pc.in | 36 ++++++++++++++++++++++++++++++++++++
 run.in                 | 12 +++++++++++-
 5 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/.gitignore b/.gitignore
index ec54b53..777471b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -90,6 +90,7 @@ Makefile.in
 /lib/api.c
 /lib/libnbd.pc
 /lib/libnbd.syms
+/lib/local/libnbd.pc
 /lib/states-run.c
 /lib/states.c
 /lib/states.h
diff --git a/configure.ac b/configure.ac
index 5e9bac4..9fd284b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -427,6 +427,7 @@ AC_CONFIG_FILES([Makefile
                  interop/Makefile
                  lib/Makefile
                  lib/libnbd.pc
+                 lib/local/libnbd.pc
                  ocaml/Makefile
                  ocaml/META
                  ocaml/examples/Makefile
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 1c46c54..4f5360d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -28,6 +28,7 @@ generator_built = \
 EXTRA_DIST = \
 	$(generator_built) \
 	libnbd.syms \
+	local/libnbd.pc.in \
 	$(NULL)
 
 lib_LTLIBRARIES = libnbd.la
diff --git a/lib/local/libnbd.pc.in b/lib/local/libnbd.pc.in
new file mode 100644
index 0000000..6d0d568
--- /dev/null
+++ b/lib/local/libnbd.pc.in
@@ -0,0 +1,36 @@
+# nbd client library in userspace
+# @configure_input@
+# Copyright (C) 2013-2020 Red Hat Inc.
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+# Dummy pkg-config file which is used to allow out of tree packages to
+# be configured against the libnbd tree without libnbd needing to be
+# installed.
+#
+# Note if you are using the ./run script then you don't need to worry
+# about this because the script sets PKG_CONFIG_PATH correctly.
+
+prefix=@abs_top_builddir@
+exec_prefix=@abs_top_builddir@
+libdir=@abs_top_builddir@/lib/.libs
+includedir=@abs_top_srcdir@/include
+
+Name: @PACKAGE_NAME@
+Version: @PACKAGE_VERSION@
+Description: NBD client library in userspace
+Requires:
+Cflags: -I${includedir}
+Libs: -L${libdir} -lnbd
diff --git a/run.in b/run.in
index 599752d..a7a2850 100755
--- a/run.in
+++ b/run.in
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 # libnbd 'run' programs locally script
-# Copyright (C) 2011-2019 Red Hat Inc.
+# Copyright (C) 2011-2020 Red Hat Inc.
 #
 # @configure_input@
 #
@@ -35,6 +35,12 @@
 #   >>> import nbd    # locally-compiled nbd module
 #
 # This works for any C program and most non-C bindings.
+#
+# You can also compile other programs against this uninstalled libnbd
+# tree if those programs are using pkgconf/pkg-config:
+#
+#   ../libnbd/run ./configure
+#   make
 
 #----------------------------------------------------------------------
 
@@ -78,6 +84,10 @@ export MALLOC_CHECK_=1
 random_val="$(@AWK@ 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)"
 export MALLOC_PERTURB_=$random_val
 
+# Allow dependent packages to be compiled against local libnbd.
+prepend PKG_CONFIG_PATH "$b/lib/local"
+export PKG_CONFIG_PATH
+
 # Do we have libtool?  If we have it then we can use it to make
 # running valgrind simpler.  However don't depend on it.
 if libtool --help >/dev/null 2>&1; then
-- 
2.25.0




More information about the Libguestfs mailing list