[PULL 21/26] configure, meson: move more compiler checks to Meson

Paolo Bonzini posted 26 patches 4 years, 3 months ago
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Darren Kenny <darren.kenny@oracle.com>, "Daniel P. Berrangé" <berrange@redhat.com>, Cleber Rosa <crosa@redhat.com>, Alexander Bulekov <alxndr@bu.edu>, Stefan Hajnoczi <stefanha@redhat.com>, Bandan Das <bsd@redhat.com>, Qiuhao Li <Qiuhao.Li@outlook.com>, Jason Wang <jasowang@redhat.com>, Eduardo Habkost <ehabkost@redhat.com>, Thomas Huth <thuth@redhat.com>, Laurent Vivier <lvivier@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
[PULL 21/26] configure, meson: move more compiler checks to Meson
Posted by Paolo Bonzini 4 years, 3 months ago
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Message-Id: <20211007130829.632254-15-pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 configure        | 91 ------------------------------------------------
 meson.build      | 44 +++++++++++++++++++++++
 util/meson.build |  4 ++-
 3 files changed, 47 insertions(+), 92 deletions(-)

diff --git a/configure b/configure
index 9b59dce2f8..7c58c7c28d 100755
--- a/configure
+++ b/configure
@@ -3110,19 +3110,6 @@ elif test "$tpm" = "yes"; then
   fi
 fi
 
-##########################################
-# iovec probe
-cat > $TMPC <<EOF
-#include <sys/types.h>
-#include <sys/uio.h>
-#include <unistd.h>
-int main(void) { return sizeof(struct iovec); }
-EOF
-iovec=no
-if compile_prog "" "" ; then
-  iovec=yes
-fi
-
 ##########################################
 # fdt probe
 
@@ -3564,42 +3551,6 @@ EOF
   fi
 fi
 
-#########################################
-# See if 64-bit atomic operations are supported.
-# Note that without __atomic builtins, we can only
-# assume atomic loads/stores max at pointer size.
-
-cat > $TMPC << EOF
-#include <stdint.h>
-int main(void)
-{
-  uint64_t x = 0, y = 0;
-  y = __atomic_load_n(&x, __ATOMIC_RELAXED);
-  __atomic_store_n(&x, y, __ATOMIC_RELAXED);
-  __atomic_compare_exchange_n(&x, &y, x, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
-  __atomic_exchange_n(&x, y, __ATOMIC_RELAXED);
-  __atomic_fetch_add(&x, y, __ATOMIC_RELAXED);
-  return 0;
-}
-EOF
-if compile_prog "" "" ; then
-  atomic64=yes
-fi
-
-########################################
-# check if getauxval is available.
-
-getauxval=no
-cat > $TMPC << EOF
-#include <sys/auxv.h>
-int main(void) {
-  return getauxval(AT_HWCAP) == 0;
-}
-EOF
-if compile_prog "" "" ; then
-    getauxval=yes
-fi
-
 ########################################
 # check if ccache is interfering with
 # semantic analysis of macros
@@ -3673,33 +3624,6 @@ else
     membarrier=no
 fi
 
-##########################################
-# check for usable AF_VSOCK environment
-have_af_vsock=no
-cat > $TMPC << EOF
-#include <errno.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#if !defined(AF_VSOCK)
-# error missing AF_VSOCK flag
-#endif
-#include <linux/vm_sockets.h>
-int main(void) {
-    int sock, ret;
-    struct sockaddr_vm svm;
-    socklen_t len = sizeof(svm);
-    sock = socket(AF_VSOCK, SOCK_STREAM, 0);
-    ret = getpeername(sock, (struct sockaddr *)&svm, &len);
-    if ((ret == -1) && (errno == ENOTCONN)) {
-        return 0;
-    }
-    return -1;
-}
-EOF
-if compile_prog "" "" ; then
-    have_af_vsock=yes
-fi
-
 ##########################################
 # check for usable AF_ALG environment
 have_afalg=no
@@ -4175,9 +4099,6 @@ fi
 if test "$vhost_user_fs" = "yes" ; then
   echo "CONFIG_VHOST_USER_FS=y" >> $config_host_mak
 fi
-if test "$iovec" = "yes" ; then
-  echo "CONFIG_IOVEC=y" >> $config_host_mak
-fi
 if test "$membarrier" = "yes" ; then
   echo "CONFIG_MEMBARRIER=y" >> $config_host_mak
 fi
@@ -4247,14 +4168,6 @@ if test "$cmpxchg128" = "yes" ; then
   echo "CONFIG_CMPXCHG128=y" >> $config_host_mak
 fi
 
-if test "$atomic64" = "yes" ; then
-  echo "CONFIG_ATOMIC64=y" >> $config_host_mak
-fi
-
-if test "$getauxval" = "yes" ; then
-  echo "CONFIG_GETAUXVAL=y" >> $config_host_mak
-fi
-
 if test "$libssh" = "yes" ; then
   echo "CONFIG_LIBSSH=y" >> $config_host_mak
   echo "LIBSSH_CFLAGS=$libssh_cflags" >> $config_host_mak
@@ -4282,10 +4195,6 @@ if test "$replication" = "yes" ; then
   echo "CONFIG_REPLICATION=y" >> $config_host_mak
 fi
 
-if test "$have_af_vsock" = "yes" ; then
-  echo "CONFIG_AF_VSOCK=y" >> $config_host_mak
-fi
-
 if test "$debug_mutex" = "yes" ; then
   echo "CONFIG_DEBUG_MUTEX=y" >> $config_host_mak
 fi
diff --git a/meson.build b/meson.build
index 6bf43e6d30..6b7487b725 100644
--- a/meson.build
+++ b/meson.build
@@ -1550,6 +1550,8 @@ config_host_data.set('CONFIG_INOTIFY',
                      cc.has_header_symbol('sys/inotify.h', 'inotify_init'))
 config_host_data.set('CONFIG_INOTIFY1',
                      cc.has_header_symbol('sys/inotify.h', 'inotify_init1'))
+config_host_data.set('CONFIG_IOVEC',
+                     cc.has_header_symbol('sys/uio.h', 'struct iovec'))
 config_host_data.set('CONFIG_MACHINE_BSWAP_H',
                      cc.has_header_symbol('machine/bswap.h', 'bswap32',
                                           prefix: '''#include <sys/endian.h>
@@ -1697,6 +1699,48 @@ config_host_data.set('HAVE_BROKEN_SIZE_MAX', not cc.compiles('''
         return printf("%zu", SIZE_MAX);
     }''', args: ['-Werror']))
 
+# See if 64-bit atomic operations are supported.
+# Note that without __atomic builtins, we can only
+# assume atomic loads/stores max at pointer size.
+config_host_data.set('CONFIG_ATOMIC64', cc.links('''
+  #include <stdint.h>
+  int main(void)
+  {
+    uint64_t x = 0, y = 0;
+    y = __atomic_load_n(&x, __ATOMIC_RELAXED);
+    __atomic_store_n(&x, y, __ATOMIC_RELAXED);
+    __atomic_compare_exchange_n(&x, &y, x, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED);
+    __atomic_exchange_n(&x, y, __ATOMIC_RELAXED);
+    __atomic_fetch_add(&x, y, __ATOMIC_RELAXED);
+    return 0;
+  }'''))
+
+config_host_data.set('CONFIG_GETAUXVAL', cc.links(gnu_source_prefix + '''
+  #include <sys/auxv.h>
+  int main(void) {
+    return getauxval(AT_HWCAP) == 0;
+  }'''))
+
+config_host_data.set('CONFIG_AF_VSOCK', cc.compiles(gnu_source_prefix + '''
+  #include <errno.h>
+  #include <sys/types.h>
+  #include <sys/socket.h>
+  #if !defined(AF_VSOCK)
+  # error missing AF_VSOCK flag
+  #endif
+  #include <linux/vm_sockets.h>
+  int main(void) {
+    int sock, ret;
+    struct sockaddr_vm svm;
+    socklen_t len = sizeof(svm);
+    sock = socket(AF_VSOCK, SOCK_STREAM, 0);
+    ret = getpeername(sock, (struct sockaddr *)&svm, &len);
+    if ((ret == -1) && (errno == ENOTCONN)) {
+        return 0;
+    }
+    return -1;
+  }'''))
+
 ignored = ['CONFIG_QEMU_INTERP_PREFIX', # actually per-target
     'HAVE_GDB_BIN']
 arrays = ['CONFIG_BDRV_RW_WHITELIST', 'CONFIG_BDRV_RO_WHITELIST']
diff --git a/util/meson.build b/util/meson.build
index 779f413c86..05b593055a 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -1,5 +1,7 @@
 util_ss.add(files('osdep.c', 'cutils.c', 'unicode.c', 'qemu-timer-common.c'))
-util_ss.add(when: 'CONFIG_ATOMIC64', if_false: files('atomic64.c'))
+if not config_host_data.get('CONFIG_ATOMIC64')
+  util_ss.add(files('atomic64.c'))
+endif
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('aio-posix.c'))
 util_ss.add(when: 'CONFIG_POSIX', if_true: files('fdmon-poll.c'))
 if config_host_data.get('CONFIG_EPOLL_CREATE1')
-- 
2.31.1



Re: [PULL 21/26] configure, meson: move more compiler checks to Meson
Posted by Peter Maydell 4 years, 2 months ago
On Thu, 14 Oct 2021 at 17:49, Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> Message-Id: <20211007130829.632254-15-pbonzini@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  configure        | 91 ------------------------------------------------
>  meson.build      | 44 +++++++++++++++++++++++
>  util/meson.build |  4 ++-
>  3 files changed, 47 insertions(+), 92 deletions(-)


> diff --git a/meson.build b/meson.build
> index 6bf43e6d30..6b7487b725 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -1550,6 +1550,8 @@ config_host_data.set('CONFIG_INOTIFY',
>                       cc.has_header_symbol('sys/inotify.h', 'inotify_init'))
>  config_host_data.set('CONFIG_INOTIFY1',
>                       cc.has_header_symbol('sys/inotify.h', 'inotify_init1'))
> +config_host_data.set('CONFIG_IOVEC',
> +                     cc.has_header_symbol('sys/uio.h', 'struct iovec'))
>  config_host_data.set('CONFIG_MACHINE_BSWAP_H',
>                       cc.has_header_symbol('machine/bswap.h', 'bswap32',
>                                            prefix: '''#include <sys/endian.h>

Hi -- I've just noticed that this change breaks compilation for me,
because this test incorrectly fails to set CONFIG_IOVEC on a system
where the header defines 'struct iovec'. This seems to be because
"struct iovec" isn't a valid thing to test with has_header_symbol,
because it provokes a compiler error from clang.

The meson-log.txt shows:

Running compile:
Working directory:
/mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/arm-clang/meson-private/tmpfspzse_8
Command line:  clang-7 -m64 -mcx16
/mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/arm-clang/meson-private/tmpfspzse_8/testfile.c
-o /mnt/nvme
disk/linaro/qemu-from-laptop/qemu/build/arm-clang/meson-private/tmpfspzse_8/output.obj
-c -fsanitize=undefined -fno-sanitize=shift-base -Werror
 -D_FILE_OFFSET_BITS=64 -O0 -Werror=implicit-function-declaration
-Werror=unknown-warning-option -Werror=unused-command-line-argument
-Werror=i
gnored-optimization-argument -std=gnu11

Code:

        #include <sys/uio.h>
        int main(void) {
            /* If it's not defined as a macro, try to use as a symbol */
            #ifndef struct iovec
                struct iovec;
            #endif
            return 0;
        }
Compiler stdout:

Compiler stderr:
 /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/arm-clang/meson-private/tmpfspzse_8/testfile.c:5:28:
error: extra tokens at end of #ifndef di
rective [-Werror,-Wextra-tokens]
            #ifndef struct iovec
                           ^
                           //
1 error generated.

...skipping...
            #ifndef struct iovec
                struct iovec;
            #endif
            return 0;
        }
Compiler stdout:

Compiler stderr:
 /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/arm-clang/meson-private/tmpfspzse_8/testfile.c:5:28:
error: extra tokens at end of #ifndef r]
            #ifndef struct iovec
                           ^
                           //
1 error generated.

Header <sys/uio.h> has symbol "struct iovec" : NO


For comparison, with a gcc build the test works because gcc
happens to merely warn rather than fail for the syntax issue:

Running compile:
Working directory:
/mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/x86/meson-private/tmpidzebj6t
Command line:  ccache gcc -m64 -mcx16
/mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/x86/meson-private/tmpidzebj6t/testfile.c
-o /mnt/nvmedis
k/linaro/qemu-from-laptop/qemu/build/x86/meson-private/tmpidzebj6t/output.obj
-c -D_FILE_OFFSET_BITS=64 -O0 -std=gnu11

Code:

        #include <sys/uio.h>
        int main(void) {
            /* If it's not defined as a macro, try to use as a symbol */
 int main(void) { return 0; }

Compiler stdout:

Compiler stderr:
...skipping...
            #ifndef struct iovec
                struct iovec;
            #endif
            return 0;
        }
Compiler stdout:

Compiler stderr:
 /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/x86/meson-private/tmpidzebj6t/testfile.c:
In function 'main':
/mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/x86/meson-private/tmpidzebj6t/testfile.c:5:28:
warning: extra tokens at end of #ifndef direcve
    5 |             #ifndef struct iovec
      |                            ^~~~~

Header <sys/uio.h> has symbol "struct iovec" : YES



-- PMM

Re: [PULL 21/26] configure, meson: move more compiler checks to Meson
Posted by Peter Maydell 4 years, 2 months ago
On Mon, 15 Nov 2021 at 16:36, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On Thu, 14 Oct 2021 at 17:49, Paolo Bonzini <pbonzini@redhat.com> wrote:
> >
> > Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > Message-Id: <20211007130829.632254-15-pbonzini@redhat.com>
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> >  configure        | 91 ------------------------------------------------
> >  meson.build      | 44 +++++++++++++++++++++++
> >  util/meson.build |  4 ++-
> >  3 files changed, 47 insertions(+), 92 deletions(-)
>
>
> > diff --git a/meson.build b/meson.build
> > index 6bf43e6d30..6b7487b725 100644
> > --- a/meson.build
> > +++ b/meson.build
> > @@ -1550,6 +1550,8 @@ config_host_data.set('CONFIG_INOTIFY',
> >                       cc.has_header_symbol('sys/inotify.h', 'inotify_init'))
> >  config_host_data.set('CONFIG_INOTIFY1',
> >                       cc.has_header_symbol('sys/inotify.h', 'inotify_init1'))
> > +config_host_data.set('CONFIG_IOVEC',
> > +                     cc.has_header_symbol('sys/uio.h', 'struct iovec'))
> >  config_host_data.set('CONFIG_MACHINE_BSWAP_H',
> >                       cc.has_header_symbol('machine/bswap.h', 'bswap32',
> >                                            prefix: '''#include <sys/endian.h>
>
> Hi -- I've just noticed that this change breaks compilation for me,
> because this test incorrectly fails to set CONFIG_IOVEC on a system
> where the header defines 'struct iovec'. This seems to be because
> "struct iovec" isn't a valid thing to test with has_header_symbol,
> because it provokes a compiler error from clang.

https://github.com/mesonbuild/meson/issues/1975 says that for gcc
it's actually going to be wrong the other way (always setting CONFIG_IOVEC
whether the system header has the struct or not), because "struct wombat;"
is syntactically OK as a *declaration*, not a use.

Maybe we can work around this by testing for the presence of something else,
eg IOV_MAX or the readv or writev functions ?

-- PMM

Re: [PULL 21/26] configure, meson: move more compiler checks to Meson
Posted by Paolo Bonzini 4 years, 2 months ago
On 11/15/21 17:42, Peter Maydell wrote:
>> Hi -- I've just noticed that this change breaks compilation for me,
>> because this test incorrectly fails to set CONFIG_IOVEC on a system
>> where the header defines 'struct iovec'. This seems to be because
>> "struct iovec" isn't a valid thing to test with has_header_symbol,
>> because it provokes a compiler error from clang.
> https://github.com/mesonbuild/meson/issues/1975  says that for gcc
> it's actually going to be wrong the other way (always setting CONFIG_IOVEC
> whether the system header has the struct or not), because "struct wombat;"
> is syntactically OK as a*declaration*, not a use.
> 
> Maybe we can work around this by testing for the presence of something else,
> eg IOV_MAX or the readv or writev functions ?

It can just use cc.has_type.

Paolo


Re: [PULL 21/26] configure, meson: move more compiler checks to Meson
Posted by Peter Maydell 4 years, 2 months ago
On Mon, 15 Nov 2021 at 16:42, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On Mon, 15 Nov 2021 at 16:36, Peter Maydell <peter.maydell@linaro.org> wrote:
> >
> > On Thu, 14 Oct 2021 at 17:49, Paolo Bonzini <pbonzini@redhat.com> wrote:
> > > +config_host_data.set('CONFIG_IOVEC',
> > > +                     cc.has_header_symbol('sys/uio.h', 'struct iovec'))

> https://github.com/mesonbuild/meson/issues/1975 says that for gcc
> it's actually going to be wrong the other way (always setting CONFIG_IOVEC
> whether the system header has the struct or not), because "struct wombat;"
> is syntactically OK as a *declaration*, not a use.

In case anybody's interested in an easy-ish meson contribution,
meson upstream indicated in that issue that they'd be open to a patch
which made whitespace in the has_header_symbol argument be an error,
which would have caught this bug earlier.

-- PMM

Re: [PULL 21/26] configure, meson: move more compiler checks to Meson
Posted by Peter Maydell 4 years, 2 months ago
On Mon, 15 Nov 2021 at 16:36, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> On Thu, 14 Oct 2021 at 17:49, Paolo Bonzini <pbonzini@redhat.com> wrote:
> >
> > Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> > Message-Id: <20211007130829.632254-15-pbonzini@redhat.com>
> > Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> > ---
> >  configure        | 91 ------------------------------------------------
> >  meson.build      | 44 +++++++++++++++++++++++
> >  util/meson.build |  4 ++-
> >  3 files changed, 47 insertions(+), 92 deletions(-)
>
>
> > diff --git a/meson.build b/meson.build
> > index 6bf43e6d30..6b7487b725 100644
> > --- a/meson.build
> > +++ b/meson.build
> > @@ -1550,6 +1550,8 @@ config_host_data.set('CONFIG_INOTIFY',
> >                       cc.has_header_symbol('sys/inotify.h', 'inotify_init'))
> >  config_host_data.set('CONFIG_INOTIFY1',
> >                       cc.has_header_symbol('sys/inotify.h', 'inotify_init1'))
> > +config_host_data.set('CONFIG_IOVEC',
> > +                     cc.has_header_symbol('sys/uio.h', 'struct iovec'))
> >  config_host_data.set('CONFIG_MACHINE_BSWAP_H',
> >                       cc.has_header_symbol('machine/bswap.h', 'bswap32',
> >                                            prefix: '''#include <sys/endian.h>
>
> Hi -- I've just noticed that this change breaks compilation for me,
> because this test incorrectly fails to set CONFIG_IOVEC on a system
> where the header defines 'struct iovec'.

That wasn't quite right. On further investigation, the problem is that
the meson tests silently misbehave if you included "-Werror" in your
--extra-cflags. This results in meson building all the tests with -Werror,
but meson's generated code for tests can't handle that. (configure
gets this right because it has code in that specifically checks
"does this test case give the same result for -Werror and not".)

-- PMM