qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [PATCH 16/30] build: move glib detection and workarounds to meson
Date: Fri,  9 Dec 2022 12:23:55 +0100	[thread overview]
Message-ID: <20221209112409.184703-17-pbonzini@redhat.com> (raw)
In-Reply-To: <20221209112409.184703-1-pbonzini@redhat.com>

QEMU adds the path to glib.h is added to all compilation commands.
This is simpler due to the pervasive use of static_library, and was
grandfathered in from the previous Make-based build system.  Until
Meson 0.63 the only way to do this was to detect glib in configure
and use add_project_arguments, but now it is possible to use
add_project_dependencies instead.

gmodule is detected in a separate variable and added via both
block_ss (for --enable-modules) and plugins/meson.build (for
--enable-plugins).

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 block/meson.build        |  1 +
 configure                | 83 +---------------------------------------
 contrib/plugins/Makefile |  4 +-
 meson.build              | 81 +++++++++++++++++++++++++++++++++------
 plugins/meson.build      |  2 +-
 5 files changed, 74 insertions(+), 97 deletions(-)

diff --git a/block/meson.build b/block/meson.build
index b7c68b83a36f..f2e0e86f3b0a 100644
--- a/block/meson.build
+++ b/block/meson.build
@@ -145,6 +145,7 @@ block_gen_c = custom_target('block-gen.c',
 block_ss.add(block_gen_c)
 
 block_ss.add(files('stream.c'))
+block_ss.add(gmodule)
 
 softmmu_ss.add(files('qapi-sysemu.c'))
 
diff --git a/configure b/configure
index 9c5393a25de7..fb28dd3963bd 100755
--- a/configure
+++ b/configure
@@ -1426,84 +1426,6 @@ if ! has "$pkg_config_exe"; then
   error_exit "pkg-config binary '$pkg_config_exe' not found"
 fi
 
-##########################################
-# glib support probe
-
-# When bumping glib_req_ver, please check also whether we should increase
-# the _WIN32_WINNT setting in osdep.h according to the value from glib
-glib_req_ver=2.56
-glib_modules=gthread-2.0
-if test "$modules" = yes; then
-    glib_modules="$glib_modules gmodule-export-2.0"
-elif test "$plugins" = "yes"; then
-    glib_modules="$glib_modules gmodule-no-export-2.0"
-fi
-
-for i in $glib_modules; do
-    if $pkg_config --atleast-version=$glib_req_ver $i; then
-        glib_cflags=$($pkg_config --cflags $i)
-        glib_libs=$($pkg_config --libs $i)
-    else
-        error_exit "glib-$glib_req_ver $i is required to compile QEMU"
-    fi
-done
-
-glib_bindir="$($pkg_config --variable=bindir glib-2.0)"
-if test -z "$glib_bindir" ; then
-	glib_bindir="$($pkg_config --variable=prefix glib-2.0)"/bin
-fi
-
-# This workaround is required due to a bug in pkg-config file for glib as it
-# doesn't define GLIB_STATIC_COMPILATION for pkg-config --static
-
-if test "$static" = yes && test "$mingw32" = yes; then
-    glib_cflags="-DGLIB_STATIC_COMPILATION $glib_cflags"
-fi
-
-# Sanity check that the current size_t matches the
-# size that glib thinks it should be. This catches
-# problems on multi-arch where people try to build
-# 32-bit QEMU while pointing at 64-bit glib headers
-cat > $TMPC <<EOF
-#include <glib.h>
-#include <unistd.h>
-
-#define QEMU_BUILD_BUG_ON(x) \
-  typedef char qemu_build_bug_on[(x)?-1:1] __attribute__((unused));
-
-int main(void) {
-   QEMU_BUILD_BUG_ON(sizeof(size_t) != GLIB_SIZEOF_SIZE_T);
-   return 0;
-}
-EOF
-
-if ! compile_prog "$glib_cflags" "$glib_libs" ; then
-    error_exit "sizeof(size_t) doesn't match GLIB_SIZEOF_SIZE_T."\
-               "You probably need to set PKG_CONFIG_LIBDIR"\
-	       "to point to the right pkg-config files for your"\
-	       "build target"
-fi
-
-# Silence clang warnings triggered by glib < 2.57.2
-cat > $TMPC << EOF
-#include <glib.h>
-typedef struct Foo {
-    int i;
-} Foo;
-static void foo_free(Foo *f)
-{
-    g_free(f);
-}
-G_DEFINE_AUTOPTR_CLEANUP_FUNC(Foo, foo_free)
-int main(void) { return 0; }
-EOF
-if ! compile_prog "$glib_cflags -Werror" "$glib_libs" ; then
-    if cc_has_warning_flag "-Wno-unused-function"; then
-        glib_cflags="$glib_cflags -Wno-unused-function"
-        CONFIGURE_CFLAGS="$CONFIGURE_CFLAGS -Wno-unused-function"
-    fi
-fi
-
 ##########################################
 # fdt probe
 
@@ -2361,13 +2283,10 @@ echo "PYTHON=$python" >> $config_host_mak
 echo "GENISOIMAGE=$genisoimage" >> $config_host_mak
 echo "MESON=$meson" >> $config_host_mak
 echo "NINJA=$ninja" >> $config_host_mak
+echo "PKG_CONFIG=${pkg_config_exe}" >> $config_host_mak
 echo "CC=$cc" >> $config_host_mak
 echo "QEMU_CFLAGS=$QEMU_CFLAGS" >> $config_host_mak
 echo "QEMU_OBJCFLAGS=$QEMU_OBJCFLAGS" >> $config_host_mak
-echo "GLIB_CFLAGS=$glib_cflags" >> $config_host_mak
-echo "GLIB_LIBS=$glib_libs" >> $config_host_mak
-echo "GLIB_BINDIR=$glib_bindir" >> $config_host_mak
-echo "GLIB_VERSION=$(pkg-config --modversion glib-2.0)" >> $config_host_mak
 echo "QEMU_LDFLAGS=$QEMU_LDFLAGS" >> $config_host_mak
 echo "EXESUF=$EXESUF" >> $config_host_mak
 
diff --git a/contrib/plugins/Makefile b/contrib/plugins/Makefile
index 23e0396687e8..8a316cd76f2f 100644
--- a/contrib/plugins/Makefile
+++ b/contrib/plugins/Makefile
@@ -3,7 +3,7 @@
 # This Makefile example is fairly independent from the main makefile
 # so users can take and adapt it for their build. We only really
 # include config-host.mak so we don't have to repeat probing for
-# cflags that the main configure has already done for us.
+# programs that the main configure has already done for us.
 #
 
 BUILD_DIR := $(CURDIR)/../..
@@ -26,7 +26,7 @@ SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES)))
 
 # The main QEMU uses Glib extensively so it's perfectly fine to use it
 # in plugins (which many example do).
-CFLAGS = $(GLIB_CFLAGS)
+CFLAGS := $(shell $(PKG_CONFIG) --cflags glib-2.0)
 CFLAGS += -fPIC -Wall $(filter -W%, $(QEMU_CFLAGS))
 CFLAGS += $(if $(findstring no-psabi,$(QEMU_CFLAGS)),-Wpsabi)
 CFLAGS += $(if $(CONFIG_DEBUG_TCG), -ggdb -O0)
diff --git a/meson.build b/meson.build
index c4fa82ae8ba4..f63ab7f83bed 100644
--- a/meson.build
+++ b/meson.build
@@ -495,19 +495,77 @@ endif
 # Dependencies #
 ################
 
-# The path to glib.h is added to all compilation commands.  This was
-# grandfathered in from the QEMU Makefiles.
-add_project_arguments(config_host['GLIB_CFLAGS'].split(),
-                      native: false, language: all_languages)
-glib = declare_dependency(compile_args: config_host['GLIB_CFLAGS'].split(),
-                          link_args: config_host['GLIB_LIBS'].split(),
-                          version: config_host['GLIB_VERSION'],
+# When bumping glib minimum version, please check also whether to increase
+# the _WIN32_WINNT setting in osdep.h according to the value from glib
+glib_req_ver = '>=2.56.0'
+glib = dependency('glib-2.0', version: glib_req_ver, required: true,
+                  method: 'pkg-config')
+glib_cflags = []
+if config_host.has_key('CONFIG_MODULES')
+  gmodule = dependency('gmodule-export-2.0', version: glib_req_ver, required: true,
+                       method: 'pkg-config')
+elif config_host.has_key('CONFIG_PLUGIN')
+  gmodule = dependency('gmodule-no-export-2.0', version: glib_req_ver, required: true,
+                       method: 'pkg-config')
+else
+  gmodule = not_found
+endif
+
+# This workaround is required due to a bug in pkg-config file for glib as it
+# doesn't define GLIB_STATIC_COMPILATION for pkg-config --static
+if targetos == 'windows' and get_option('prefer_static')
+  glib_cflags += ['-DGLIB_STATIC_COMPILATION']
+endif
+
+# Sanity check that the current size_t matches the
+# size that glib thinks it should be. This catches
+# problems on multi-arch where people try to build
+# 32-bit QEMU while pointing at 64-bit glib headers
+
+if not cc.compiles('''
+  #include <glib.h>
+  #include <unistd.h>
+
+  #define QEMU_BUILD_BUG_ON(x) \
+  typedef char qemu_build_bug_on[(x)?-1:1] __attribute__((unused));
+
+  int main(void) {
+     QEMU_BUILD_BUG_ON(sizeof(size_t) != GLIB_SIZEOF_SIZE_T);
+     return 0;
+  }''', dependencies: glib, args: glib_cflags)
+  error('''sizeof(size_t) doesn't match GLIB_SIZEOF_SIZE_T.
+        You probably need to set PKG_CONFIG_LIBDIR" to point
+        to the right pkg-config files for your build target.''')
+endif
+
+# Silence clang warnings triggered by glib < 2.57.2
+if not cc.compiles('''
+  #include <glib.h>
+  typedef struct Foo {
+    int i;
+  } Foo;
+  static void foo_free(Foo *f)
+  {
+    g_free(f);
+  }
+  G_DEFINE_AUTOPTR_CLEANUP_FUNC(Foo, foo_free)
+  int main(void) { return 0; }''', dependencies: glib, args: ['-Werror'])
+  glib_cflags += cc.get_supported_arguments('-Wno-unused-function')
+endif
+glib = declare_dependency(dependencies: glib,
+                          compile_args: glib_cflags,
+                          version: glib.version(),
                           variables: {
-                            'bindir': config_host['GLIB_BINDIR'],
+                            'bindir': glib.get_variable('bindir')
                           })
-# override glib dep with the configure results (for subprojects)
+
+# override glib dep to include the above refinements
 meson.override_dependency('glib-2.0', glib)
 
+# The path to glib.h is added to all compilation commands.
+add_project_dependencies(glib.partial_dependency(compile_args: true, includes: true),
+                         native: false, language: all_languages)
+
 gio = not_found
 gdbus_codegen = not_found
 gdbus_codegen_error = '@0@ requires gdbus-codegen, please install libgio'
@@ -923,7 +981,7 @@ if have_system and get_option('curses').allowed()
         int main(void) {
           iconv_t conv = iconv_open("WCHAR_T", "UCS-2");
           return conv != (iconv_t) -1;
-        }''', args: config_host['GLIB_CFLAGS'].split() + config_host['GLIB_LIBS'].split() + link_args)
+        }''', args: link_args, dependencies: glib)
         iconv = declare_dependency(link_args: link_args, dependencies: glib)
         break
       endif
@@ -2977,7 +3035,6 @@ subdir('ui')
 subdir('hw')
 subdir('gdbstub')
 
-
 if enable_modules
   libmodulecommon = static_library('module-common', files('module-common.c') + genh, pic: true, c_args: '-DBUILD_DSO')
   modulecommon = declare_dependency(link_whole: libmodulecommon, compile_args: '-DBUILD_DSO')
@@ -3590,7 +3647,7 @@ if host_machine.system() == 'windows'
     '@OUTPUT@',
     get_option('prefix'),
     meson.current_source_dir(),
-    config_host['GLIB_BINDIR'],
+    glib.get_variable('bindir'),
     host_machine.cpu(),
     '--',
     '-DDISPLAYVERSION=' + meson.project_version(),
diff --git a/plugins/meson.build b/plugins/meson.build
index 752377c66d32..04cb7d252aab 100644
--- a/plugins/meson.build
+++ b/plugins/meson.build
@@ -17,4 +17,4 @@ specific_ss.add(when: 'CONFIG_PLUGIN', if_true: [files(
   'loader.c',
   'core.c',
   'api.c',
-), declare_dependency(link_args: plugin_ldflags)])
+), gmodule, declare_dependency(link_args: plugin_ldflags)])
-- 
2.38.1



  parent reply	other threads:[~2022-12-09 11:43 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-09 11:23 [PATCH for-8.0 00/30] Meson changes for QEMU 8.0 Paolo Bonzini
2022-12-09 11:23 ` [PATCH 01/30] configure: remove useless write_c_skeleton Paolo Bonzini
2022-12-09 11:38   ` Peter Maydell
2022-12-16 14:33   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 02/30] configure: remove dead function Paolo Bonzini
2022-12-09 11:36   ` Peter Maydell
2022-12-16 14:33   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 03/30] configure: remove useless test Paolo Bonzini
2022-12-09 11:34   ` Peter Maydell
2022-12-09 16:35     ` Paolo Bonzini
2022-12-16 14:35   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 04/30] configure: preserve qemu-ga variables Paolo Bonzini
2022-12-12  7:52   ` Marc-André Lureau
2022-12-16 14:39   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 05/30] configure: remove backwards-compatibility and obsolete options Paolo Bonzini
2022-12-12  7:57   ` Marc-André Lureau
2022-12-16 14:40   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 06/30] meson: tweak hardening options for Windows Paolo Bonzini
2022-12-12  8:18   ` Marc-André Lureau
2022-12-12  8:52     ` Paolo Bonzini
2022-12-16 14:42   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 07/30] meson: support meson 0.64 -Doptimization=plain Paolo Bonzini
2022-12-12  8:21   ` Marc-André Lureau
2022-12-16 14:53   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 08/30] meson: require 0.63.0 Paolo Bonzini
2022-12-09 13:25   ` Thomas Huth
2022-12-09 11:23 ` [PATCH 09/30] meson: use prefer_static option Paolo Bonzini
2022-12-12  8:29   ` Marc-André Lureau
2022-12-16 14:58   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 10/30] meson: remove static_kwargs Paolo Bonzini
2022-12-12  8:32   ` Marc-André Lureau
2022-12-16 14:59   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 11/30] meson: cleanup dummy-cpus.c rules Paolo Bonzini
2022-12-12  8:39   ` Marc-André Lureau
2022-12-16 14:59   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 12/30] modinfo: lookup compile_commands.json by object Paolo Bonzini
2022-12-12  8:47   ` Marc-André Lureau
2022-12-09 11:23 ` [PATCH 13/30] configure: remove backwards-compatibility code Paolo Bonzini
2022-12-12  8:55   ` Marc-André Lureau
2022-12-09 11:23 ` [PATCH 14/30] configure: test all warnings Paolo Bonzini
2022-12-09 12:45   ` Philippe Mathieu-Daudé
2022-12-16 15:01   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 15/30] meson: cleanup compiler detection Paolo Bonzini
2022-12-12  9:04   ` Marc-André Lureau
2022-12-16 15:03   ` Daniel P. Berrangé
2022-12-09 11:23 ` Paolo Bonzini [this message]
2022-12-09 12:50   ` [PATCH 16/30] build: move glib detection and workarounds to meson Philippe Mathieu-Daudé
2022-12-09 11:23 ` [PATCH 17/30] configure: remove pkg-config functions Paolo Bonzini
2022-12-12  9:05   ` Marc-André Lureau
2022-12-16 15:07   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 18/30] configure, meson: move --enable-modules to Meson Paolo Bonzini
2022-12-12  9:07   ` Marc-André Lureau
2022-12-16 15:09   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 19/30] configure, meson: move --enable-debug-info " Paolo Bonzini
2022-12-09 12:55   ` Philippe Mathieu-Daudé
2022-12-09 13:54     ` Paolo Bonzini
2022-12-09 14:21       ` Philippe Mathieu-Daudé
2022-12-09 16:21         ` Paolo Bonzini
2022-12-16 15:09   ` Daniel P. Berrangé
2022-12-09 11:23 ` [PATCH 20/30] meson: prepare move of QEMU_CFLAGS to meson Paolo Bonzini
2022-12-12 12:02   ` Marc-André Lureau
2022-12-16 15:11   ` Daniel P. Berrangé
2022-12-09 11:24 ` [PATCH 21/30] build: move sanitizer tests " Paolo Bonzini
2022-12-12 12:16   ` Marc-André Lureau
2022-12-12 17:31     ` Paolo Bonzini
2022-12-12 20:37     ` Paolo Bonzini
2022-12-09 11:24 ` [PATCH 22/30] build: move SafeStack " Paolo Bonzini
2022-12-09 12:57   ` Philippe Mathieu-Daudé
2022-12-09 11:24 ` [PATCH 23/30] build: move coroutine backend selection " Paolo Bonzini
2022-12-12 12:18   ` Marc-André Lureau
2022-12-09 11:24 ` [PATCH 24/30] build: move stack protector flag " Paolo Bonzini
2022-12-12 12:22   ` Marc-André Lureau
2022-12-09 11:24 ` [PATCH 25/30] build: move warning " Paolo Bonzini
2022-12-12 12:58   ` Marc-André Lureau
2022-12-09 11:24 ` [PATCH 26/30] build: move remaining compiler flag tests " Paolo Bonzini
2022-12-12 13:54   ` Marc-André Lureau
2022-12-09 11:24 ` [PATCH 27/30] build: move compiler version check " Paolo Bonzini
2022-12-09 11:52   ` Peter Maydell
2022-12-09 14:09     ` Paolo Bonzini
2022-12-09 14:19       ` Peter Maydell
2022-12-09 16:21         ` Paolo Bonzini
2022-12-09 11:24 ` [PATCH 28/30] docs: update build system docs Paolo Bonzini
2022-12-12 14:00   ` Marc-André Lureau
2022-12-09 11:24 ` [PATCH 29/30] configure: do not rerun the tests with -Werror Paolo Bonzini
2022-12-12 14:00   ` Marc-André Lureau
2022-12-09 11:24 ` [PATCH 30/30] meson: always log qemu-iotests verbosely Paolo Bonzini
2022-12-12 14:02   ` Marc-André Lureau
2022-12-12 16:48     ` Paolo Bonzini
2022-12-12 16:55   ` Peter Maydell
2022-12-12 21:36     ` Paolo Bonzini
2022-12-16 15:35 ` [PATCH for-8.0 00/30] Meson changes for QEMU 8.0 Daniel P. Berrangé
2022-12-17 12:54   ` Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221209112409.184703-17-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).