From f08d5dada49cfa584c3b7ed3a6253c4bbc7e31f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Date: Fri, 3 Jan 2020 17:54:12 +0000 Subject: [PATCH] util: introduce compile time API version checking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GLib header files annotate every API with a version number. It is possible to define some constants before including glib.h which will result in useful compile time warnings. Setting GLIB_VERSION_MIN_REQUIRED will result in a warning if libvirt uses an API that was deprecated in the declared version, or before. Such API usage should be rewritten to use the documented new replacement API. Setting GLIB_VERSION_MAX_ALLOWED will result in a warning if libvirt uses an API that was not introduced until a version of GLib that's newer than our minimum declared version. This avoids accidentally using functionality that is not available on some supported platforms. Reviewed-by: Pavel Hrdina Reviewed-by: Daniel Henrique Barboza Signed-off-by: Daniel P. Berrangé --- config-post.h | 10 ++++++++++ src/util/glibcompat.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/config-post.h b/config-post.h index 415cc8cc72..de007393da 100644 --- a/config-post.h +++ b/config-post.h @@ -49,3 +49,13 @@ #else # error You either need at least GCC 4.8 or Clang 3.4 or XCode Clang 5.1 to compile libvirt #endif + +/* Ask for warnings for anything that was marked deprecated in + * the defined version, or before. It is a candidate for rewrite. + */ +#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_48 + +/* Ask for warnings if code tries to use function that did not + * exist in the defined version. These risk breaking builds + */ +#define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_48 diff --git a/src/util/glibcompat.c b/src/util/glibcompat.c index c61772f953..9f0f7f015c 100644 --- a/src/util/glibcompat.c +++ b/src/util/glibcompat.c @@ -24,6 +24,45 @@ #include "glibcompat.h" +/* + * Note that because of the GLIB_VERSION_MAX_ALLOWED constant in + * config-post.h, allowing use of functions from newer GLib via + * this compat impl needs a little trickery to prevent warnings + * being emitted. + * + * Consider a function from newer glib-X.Y that we want to use + * + * int g_foo(const char *wibble) + * + * We must define a function with the same signature that does + * what we need, but with a "vir_" prefix e.g. + * + * void vir_g_foo(const char *wibble) + * { + * #if GLIB_CHECK_VERSION(X, Y, 0) + * g_foo(wibble) + * #else + * g_something_equivalent_in_older_glib(wibble); + * #endif + * } + * + * The #pragma at the top of this file turns off -Wdeprecated-declarations, + * ensuring this wrapper function impl doesn't trigger the compiler + * warning about using too new glib APIs. Finally in glibcompat.h we can + * add + * + * #define g_foo(a) vir_g_foo(a) + * + * Thus all the code elsewhere in libvirt, which *does* have the + * -Wdeprecated-declarations warning active, can call g_foo(...) as + * normal, without generating warnings. The cost is an extra function + * call when using new glib, but this compat code will go away over + * time as we update the supported platforms target. + */ + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + #undef g_canonicalize_filename #undef g_fsync #undef g_strdup_printf