diff mbox series

[4/5] configure, meson: move C++ compiler detection to meson.build

Message ID 20220927095921.261760-5-pbonzini@redhat.com
State New
Headers show
Series Small meson updates for QEMU 7.2 | expand

Commit Message

Paolo Bonzini Sept. 27, 2022, 9:59 a.m. UTC
The test is slightly weaker than before, because it does not
call an extern "C" function from a C source file.  However,
in practice what we seek to detect is ABI compatibility of the
various sanitizer flags, and for that it is enough to compile
anything with CC and link it with CXX.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 configure      | 57 --------------------------------------------------
 meson.build    | 37 ++++++++++++++++++++++----------
 scripts/main.c |  1 +
 3 files changed, 27 insertions(+), 68 deletions(-)
 create mode 100644 scripts/main.c

Comments

Marc-André Lureau Sept. 27, 2022, 11 a.m. UTC | #1
On Tue, Sep 27, 2022 at 1:59 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> The test is slightly weaker than before, because it does not
> call an extern "C" function from a C source file.  However,
> in practice what we seek to detect is ABI compatibility of the
> various sanitizer flags, and for that it is enough to compile
> anything with CC and link it with CXX.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>


> ---
>  configure      | 57 --------------------------------------------------
>  meson.build    | 37 ++++++++++++++++++++++----------
>  scripts/main.c |  1 +
>  3 files changed, 27 insertions(+), 68 deletions(-)
>  create mode 100644 scripts/main.c
>
> diff --git a/configure b/configure
> index 680d96d09f..0a6f57c371 100755
> --- a/configure
> +++ b/configure
> @@ -75,7 +75,6 @@ fi
>  TMPB="qemu-conf"
>  TMPC="${TMPDIR1}/${TMPB}.c"
>  TMPO="${TMPDIR1}/${TMPB}.o"
> -TMPCXX="${TMPDIR1}/${TMPB}.cxx"
>  TMPM="${TMPDIR1}/${TMPB}.m"
>  TMPE="${TMPDIR1}/${TMPB}.exe"
>
> @@ -158,10 +157,6 @@ do_cc() {
>      do_compiler_werror "$cc" $CPU_CFLAGS "$@"
>  }
>
> -do_cxx() {
> -    do_compiler_werror "$cxx" $CPU_CFLAGS "$@"
> -}
> -
>  do_objc() {
>      do_compiler_werror "$objcc" $CPU_CFLAGS "$@"
>  }
> @@ -171,24 +166,6 @@ add_to() {
>      eval $1=\${$1:+\"\$$1 \"}\$2
>  }
>
> -update_cxxflags() {
> -    # Set QEMU_CXXFLAGS from QEMU_CFLAGS by filtering out those
> -    # options which some versions of GCC's C++ compiler complain about
> -    # because they only make sense for C programs.
> -    QEMU_CXXFLAGS="-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS"
> -    CONFIGURE_CXXFLAGS=$(echo "$CONFIGURE_CFLAGS" | sed s/-std=gnu11/-std=gnu++11/)
> -    for arg in $QEMU_CFLAGS; do
> -        case $arg in
> -            -Wstrict-prototypes|-Wmissing-prototypes|-Wnested-externs|\
> -            -Wold-style-declaration|-Wold-style-definition|-Wredundant-decls)
> -                ;;
> -            *)
> -                QEMU_CXXFLAGS=${QEMU_CXXFLAGS:+$QEMU_CXXFLAGS }$arg
> -                ;;
> -        esac
> -    done
> -}
> -
>  compile_object() {
>    local_cflags="$1"
>    do_cc $CFLAGS $EXTRA_CFLAGS $CONFIGURE_CFLAGS $QEMU_CFLAGS $local_cflags -c -o $TMPO $TMPC
> @@ -2371,38 +2348,6 @@ fi
>  #######################################
>  # generate config-host.mak
>
> -# Check that the C++ compiler exists and works with the C compiler.
> -# All the QEMU_CXXFLAGS are based on QEMU_CFLAGS. Keep this at the end to don't miss any other that could be added.
> -if has $cxx; then
> -    cat > $TMPC <<EOF
> -int c_function(void);
> -int main(void) { return c_function(); }
> -EOF
> -
> -    compile_object
> -
> -    cat > $TMPCXX <<EOF
> -extern "C" {
> -   int c_function(void);
> -}
> -int c_function(void) { return 42; }
> -EOF
> -
> -    update_cxxflags
> -
> -    if do_cxx $CXXFLAGS $EXTRA_CXXFLAGS $CONFIGURE_CXXFLAGS $QEMU_CXXFLAGS -o $TMPE $TMPCXX $TMPO $QEMU_LDFLAGS; then
> -        # C++ compiler $cxx works ok with C compiler $cc
> -        :
> -    else
> -        echo "C++ compiler $cxx does not work with C compiler $cc"
> -        echo "Disabling C++ specific optional code"
> -        cxx=
> -    fi
> -else
> -    echo "No C++ compiler available; disabling C++ specific optional code"
> -    cxx=
> -fi
> -
>  if ! (GIT="$git" "$source_path/scripts/git-submodule.sh" "$git_submodules_action" "$git_submodules"); then
>      exit 1
>  fi
> @@ -2486,7 +2431,6 @@ echo "MESON=$meson" >> $config_host_mak
>  echo "NINJA=$ninja" >> $config_host_mak
>  echo "CC=$cc" >> $config_host_mak
>  echo "QEMU_CFLAGS=$QEMU_CFLAGS" >> $config_host_mak
> -echo "QEMU_CXXFLAGS=$QEMU_CXXFLAGS" >> $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
> @@ -2608,7 +2552,6 @@ if test "$skip_meson" = no; then
>        echo "${a}-softmmu = '$c'" >> $cross
>    done
>
> -  test -z "$cxx" && echo "link_language = 'c'" >> $cross
>    echo "[built-in options]" >> $cross
>    echo "c_args = [$(meson_quote $CFLAGS $EXTRA_CFLAGS)]" >> $cross
>    echo "cpp_args = [$(meson_quote $CXXFLAGS $EXTRA_CXXFLAGS)]" >> $cross
> diff --git a/meson.build b/meson.build
> index e1700d1d55..f57a60ac4c 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -180,7 +180,6 @@ endif
>  ##################
>
>  qemu_cflags = config_host['QEMU_CFLAGS'].split()
> -qemu_cxxflags = config_host['QEMU_CXXFLAGS'].split()
>  qemu_objcflags = config_host['QEMU_OBJCFLAGS'].split()
>  qemu_ldflags = config_host['QEMU_LDFLAGS'].split()
>
> @@ -194,7 +193,6 @@ endif
>
>  if get_option('gprof')
>    qemu_cflags += ['-p']
> -  qemu_cxxflags += ['-p']
>    qemu_objcflags += ['-p']
>    qemu_ldflags += ['-p']
>  endif
> @@ -240,8 +238,33 @@ if get_option('fuzzing')
>  endif
>
>  add_global_arguments(qemu_cflags, native: false, language: ['c'])
> -add_global_arguments(qemu_cxxflags, native: false, language: ['cpp'])
>  add_global_arguments(qemu_objcflags, native: false, language: ['objc'])
> +
> +# Check that the C++ compiler exists and works with the C compiler.
> +link_language = 'c'
> +linker = cc
> +qemu_cxxflags = []
> +if add_languages('cpp', required: false, native: false)
> +  cxx = meson.get_compiler('cpp')
> +  add_global_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'],
> +                       native: false, language: 'cpp')
> +  foreach k: qemu_cflags
> +    if k not in ['-Wstrict-prototypes', '-Wmissing-prototypes', '-Wnested-externs',
> +                 '-Wold-style-declaration', '-Wold-style-definition', '-Wredundant-decls']
> +      qemu_cxxflags += [k]
> +    endif
> +  endforeach
> +  add_global_arguments(qemu_cxxflags, native: false, language: 'cpp')
> +
> +  if cxx.links(files('scripts/main.c'), args: qemu_cflags)
> +    link_language = 'cpp'
> +    linker = cxx
> +  else
> +    message('C++ compiler does not work with C compiler')
> +    message('Disabling C++-specific optional code')
> +  endif
> +endif
> +
>  add_global_link_arguments(qemu_ldflags, native: false, language: ['c', 'cpp', 'objc'])
>
>  if targetos == 'linux'
> @@ -255,14 +278,6 @@ add_project_arguments('-iquote', '.',
>                        '-iquote', meson.current_source_dir() / 'include',
>                        language: ['c', 'cpp', 'objc'])
>
> -link_language = meson.get_external_property('link_language', 'cpp')
> -if link_language == 'cpp'
> -  add_languages('cpp', required: true, native: false)
> -  cxx = meson.get_compiler('cpp')
> -  linker = cxx
> -else
> -  linker = cc
> -endif
>  if host_machine.system() == 'darwin'
>    add_languages('objc', required: false, native: false)
>  endif
> diff --git a/scripts/main.c b/scripts/main.c
> new file mode 100644
> index 0000000000..b552c8e4ed
> --- /dev/null
> +++ b/scripts/main.c
> @@ -0,0 +1 @@
> +int main(void) {}
> --
> 2.37.3
>
Richard Henderson Sept. 28, 2022, 4:25 p.m. UTC | #2
On 9/27/22 02:59, Paolo Bonzini wrote:
> The test is slightly weaker than before, because it does not
> call an extern "C" function from a C source file.  However,
> in practice what we seek to detect is ABI compatibility of the
> various sanitizer flags, and for that it is enough to compile
> anything with CC and link it with CXX.

Maybe just wait a moment on this one?
We have patches on list to remove the last use of C++.



r~
Paolo Bonzini Sept. 28, 2022, 7:21 p.m. UTC | #3
Heh, I wanted to get it in for exactly that reason, so that a future revert
would not introduce the test in configure. But I guess having the patch out
there on the archives may also be enough.

Paolo

Il mer 28 set 2022, 18:25 Richard Henderson <richard.henderson@linaro.org>
ha scritto:

> On 9/27/22 02:59, Paolo Bonzini wrote:
> > The test is slightly weaker than before, because it does not
> > call an extern "C" function from a C source file.  However,
> > in practice what we seek to detect is ABI compatibility of the
> > various sanitizer flags, and for that it is enough to compile
> > anything with CC and link it with CXX.
>
> Maybe just wait a moment on this one?
> We have patches on list to remove the last use of C++.
>
>
>
> r~
>
>
Richard Henderson Sept. 28, 2022, 9:08 p.m. UTC | #4
On 9/28/22 12:21, Paolo Bonzini wrote:
> Heh, I wanted to get it in for exactly that reason, so that a future revert would not 
> introduce the test in configure. But I guess having the patch out there on the archives 
> may also be enough.

Heh.  I suppose that's fair, being wary of reversion.


r~

> 
> Paolo
> 
> Il mer 28 set 2022, 18:25 Richard Henderson <richard.henderson@linaro.org 
> <mailto:richard.henderson@linaro.org>> ha scritto:
> 
>     On 9/27/22 02:59, Paolo Bonzini wrote:
>      > The test is slightly weaker than before, because it does not
>      > call an extern "C" function from a C source file.  However,
>      > in practice what we seek to detect is ABI compatibility of the
>      > various sanitizer flags, and for that it is enough to compile
>      > anything with CC and link it with CXX.
> 
>     Maybe just wait a moment on this one?
>     We have patches on list to remove the last use of C++.
> 
> 
> 
>     r~
>
Paolo Bonzini Sept. 30, 2022, 7:34 a.m. UTC | #5
On Wed, Sep 28, 2022 at 11:08 PM Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 9/28/22 12:21, Paolo Bonzini wrote:
> > Heh, I wanted to get it in for exactly that reason, so that a future revert would not
> > introduce the test in configure. But I guess having the patch out there on the archives
> > may also be enough.
>
> Heh.  I suppose that's fair, being wary of reversion.

There's also a bit of C++ in the guest agent:

have_qga_vss = get_option('qga_vss') \
  .require(targetos == 'windows',
           error_message: 'VSS support requires Windows') \
  .require(link_language == 'cpp',
           error_message: 'VSS support requires a C++ compiler') \
...

so I included the patch in the pull request.

Paolo
diff mbox series

Patch

diff --git a/configure b/configure
index 680d96d09f..0a6f57c371 100755
--- a/configure
+++ b/configure
@@ -75,7 +75,6 @@  fi
 TMPB="qemu-conf"
 TMPC="${TMPDIR1}/${TMPB}.c"
 TMPO="${TMPDIR1}/${TMPB}.o"
-TMPCXX="${TMPDIR1}/${TMPB}.cxx"
 TMPM="${TMPDIR1}/${TMPB}.m"
 TMPE="${TMPDIR1}/${TMPB}.exe"
 
@@ -158,10 +157,6 @@  do_cc() {
     do_compiler_werror "$cc" $CPU_CFLAGS "$@"
 }
 
-do_cxx() {
-    do_compiler_werror "$cxx" $CPU_CFLAGS "$@"
-}
-
 do_objc() {
     do_compiler_werror "$objcc" $CPU_CFLAGS "$@"
 }
@@ -171,24 +166,6 @@  add_to() {
     eval $1=\${$1:+\"\$$1 \"}\$2
 }
 
-update_cxxflags() {
-    # Set QEMU_CXXFLAGS from QEMU_CFLAGS by filtering out those
-    # options which some versions of GCC's C++ compiler complain about
-    # because they only make sense for C programs.
-    QEMU_CXXFLAGS="-D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS"
-    CONFIGURE_CXXFLAGS=$(echo "$CONFIGURE_CFLAGS" | sed s/-std=gnu11/-std=gnu++11/)
-    for arg in $QEMU_CFLAGS; do
-        case $arg in
-            -Wstrict-prototypes|-Wmissing-prototypes|-Wnested-externs|\
-            -Wold-style-declaration|-Wold-style-definition|-Wredundant-decls)
-                ;;
-            *)
-                QEMU_CXXFLAGS=${QEMU_CXXFLAGS:+$QEMU_CXXFLAGS }$arg
-                ;;
-        esac
-    done
-}
-
 compile_object() {
   local_cflags="$1"
   do_cc $CFLAGS $EXTRA_CFLAGS $CONFIGURE_CFLAGS $QEMU_CFLAGS $local_cflags -c -o $TMPO $TMPC
@@ -2371,38 +2348,6 @@  fi
 #######################################
 # generate config-host.mak
 
-# Check that the C++ compiler exists and works with the C compiler.
-# All the QEMU_CXXFLAGS are based on QEMU_CFLAGS. Keep this at the end to don't miss any other that could be added.
-if has $cxx; then
-    cat > $TMPC <<EOF
-int c_function(void);
-int main(void) { return c_function(); }
-EOF
-
-    compile_object
-
-    cat > $TMPCXX <<EOF
-extern "C" {
-   int c_function(void);
-}
-int c_function(void) { return 42; }
-EOF
-
-    update_cxxflags
-
-    if do_cxx $CXXFLAGS $EXTRA_CXXFLAGS $CONFIGURE_CXXFLAGS $QEMU_CXXFLAGS -o $TMPE $TMPCXX $TMPO $QEMU_LDFLAGS; then
-        # C++ compiler $cxx works ok with C compiler $cc
-        :
-    else
-        echo "C++ compiler $cxx does not work with C compiler $cc"
-        echo "Disabling C++ specific optional code"
-        cxx=
-    fi
-else
-    echo "No C++ compiler available; disabling C++ specific optional code"
-    cxx=
-fi
-
 if ! (GIT="$git" "$source_path/scripts/git-submodule.sh" "$git_submodules_action" "$git_submodules"); then
     exit 1
 fi
@@ -2486,7 +2431,6 @@  echo "MESON=$meson" >> $config_host_mak
 echo "NINJA=$ninja" >> $config_host_mak
 echo "CC=$cc" >> $config_host_mak
 echo "QEMU_CFLAGS=$QEMU_CFLAGS" >> $config_host_mak
-echo "QEMU_CXXFLAGS=$QEMU_CXXFLAGS" >> $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
@@ -2608,7 +2552,6 @@  if test "$skip_meson" = no; then
       echo "${a}-softmmu = '$c'" >> $cross
   done
 
-  test -z "$cxx" && echo "link_language = 'c'" >> $cross
   echo "[built-in options]" >> $cross
   echo "c_args = [$(meson_quote $CFLAGS $EXTRA_CFLAGS)]" >> $cross
   echo "cpp_args = [$(meson_quote $CXXFLAGS $EXTRA_CXXFLAGS)]" >> $cross
diff --git a/meson.build b/meson.build
index e1700d1d55..f57a60ac4c 100644
--- a/meson.build
+++ b/meson.build
@@ -180,7 +180,6 @@  endif
 ##################
 
 qemu_cflags = config_host['QEMU_CFLAGS'].split()
-qemu_cxxflags = config_host['QEMU_CXXFLAGS'].split()
 qemu_objcflags = config_host['QEMU_OBJCFLAGS'].split()
 qemu_ldflags = config_host['QEMU_LDFLAGS'].split()
 
@@ -194,7 +193,6 @@  endif
 
 if get_option('gprof')
   qemu_cflags += ['-p']
-  qemu_cxxflags += ['-p']
   qemu_objcflags += ['-p']
   qemu_ldflags += ['-p']
 endif
@@ -240,8 +238,33 @@  if get_option('fuzzing')
 endif
 
 add_global_arguments(qemu_cflags, native: false, language: ['c'])
-add_global_arguments(qemu_cxxflags, native: false, language: ['cpp'])
 add_global_arguments(qemu_objcflags, native: false, language: ['objc'])
+
+# Check that the C++ compiler exists and works with the C compiler.
+link_language = 'c'
+linker = cc
+qemu_cxxflags = []
+if add_languages('cpp', required: false, native: false)
+  cxx = meson.get_compiler('cpp')
+  add_global_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'],
+                       native: false, language: 'cpp')
+  foreach k: qemu_cflags
+    if k not in ['-Wstrict-prototypes', '-Wmissing-prototypes', '-Wnested-externs',
+                 '-Wold-style-declaration', '-Wold-style-definition', '-Wredundant-decls']
+      qemu_cxxflags += [k]
+    endif
+  endforeach
+  add_global_arguments(qemu_cxxflags, native: false, language: 'cpp')
+
+  if cxx.links(files('scripts/main.c'), args: qemu_cflags)
+    link_language = 'cpp'
+    linker = cxx
+  else
+    message('C++ compiler does not work with C compiler')
+    message('Disabling C++-specific optional code')
+  endif
+endif
+
 add_global_link_arguments(qemu_ldflags, native: false, language: ['c', 'cpp', 'objc'])
 
 if targetos == 'linux'
@@ -255,14 +278,6 @@  add_project_arguments('-iquote', '.',
                       '-iquote', meson.current_source_dir() / 'include',
                       language: ['c', 'cpp', 'objc'])
 
-link_language = meson.get_external_property('link_language', 'cpp')
-if link_language == 'cpp'
-  add_languages('cpp', required: true, native: false)
-  cxx = meson.get_compiler('cpp')
-  linker = cxx
-else
-  linker = cc
-endif
 if host_machine.system() == 'darwin'
   add_languages('objc', required: false, native: false)
 endif
diff --git a/scripts/main.c b/scripts/main.c
new file mode 100644
index 0000000000..b552c8e4ed
--- /dev/null
+++ b/scripts/main.c
@@ -0,0 +1 @@ 
+int main(void) {}