[Libguestfs] [libnbd PATCH] generator: Add #define witnesses for all API

Eric Blake eblake at redhat.com
Mon Jun 3 22:05:12 UTC 2019


Make it easier for C libraries to consume arbitrary versions of
libnbd, by giving a probe for which functions the current version of
the library exports.
---

I'm fuzzy enough on OCaml that I'll get review for this, although I
like the resulting libnbd.h.

 generator/generator | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/generator/generator b/generator/generator
index db7c10f..7d0ea3f 100755
--- a/generator/generator
+++ b/generator/generator
@@ -2712,6 +2712,12 @@ let print_extern name args ret =
   print_call name args ret;
   pr ";\n"

+let print_extern_and_define name args ret =
+  let name_upper = String.uppercase_ascii name in
+  print_extern name args ret;
+  pr "#define LIBNBD_HAVE_NBD_%s 1\n" name_upper;
+  pr "\n"
+
 let generate_include_libnbd_h () =
   generate_header CStyle;

@@ -2729,14 +2735,23 @@ let generate_include_libnbd_h () =
   List.iter (fun (n, i) -> pr "#define LIBNBD_%-30s %d\n" n i) constants;
   pr "\n";
   pr "extern struct nbd_handle *nbd_create (void);\n";
+  pr "#define LIBNBD_HAVE_NBD_CREATE 1\n";
+  pr "\n";
   pr "extern void nbd_close (struct nbd_handle *h);\n";
+  pr "#define LIBNBD_HAVE_NBD_CLOSE 1\n";
+  pr "\n";
   pr "extern const char *nbd_get_error (void);\n";
+  pr "#define LIBNBD_HAVE_NBD_GET_ERROR 1\n";
+  pr "\n";
   pr "extern int nbd_get_errno (void);\n";
+  pr "#define LIBNBD_HAVE_NBD_GET_ERRNO 1\n";
+  pr "\n";
   pr "extern int nbd_add_close_callback (struct nbd_handle *h,\n";
   pr "                                   nbd_close_callback cb, void *data);\n";
+  pr "#define LIBNBD_HAVE_NBD_ADD_CLOSE_CALLBACK 1\n";
   pr "\n";
   List.iter (
-    fun (name, { args; ret }) -> print_extern name args ret
+    fun (name, { args; ret }) -> print_extern_and_define name args ret
   ) handle_calls;
   pr "\n";
   pr "#endif /* LIBNBD_H */\n"
@@ -2933,6 +2948,13 @@ in detail.  If you want an overview of using the API, or to see
 how to call the API from other programming languages, start
 with libnbd(3).

+For the sake of conditional compilation across a range of libnbd
+versions, where a client may take advantage of newer API when present
+but gracefully continue to compile even when it is not, all functions
+declared in B<E<lt>libnbd.hE<gt>> have a corresponding witness macro
+with prefix C<LIBNBD_HAVE_>. For example, C<nbd_create> has a
+counterpart macro C<LIBNBD_HAVE_NBD_CREATE> defined to C<1>.
+
 =head1 CREATE, GET AND CLOSE HANDLES

  struct nbd_handle *nbd;
-- 
2.20.1




More information about the Libguestfs mailing list