diff mbox series

[15/30] meson: cleanup compiler detection

Message ID 20221209112409.184703-16-pbonzini@redhat.com
State New
Headers show
Series Meson changes for QEMU 8.0 | expand

Commit Message

Paolo Bonzini Dec. 9, 2022, 11:23 a.m. UTC
Detect all compilers at the beginning of meson.build, and store
the available languages in an array.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 meson.build | 62 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 27 deletions(-)

Comments

Marc-André Lureau Dec. 12, 2022, 9:04 a.m. UTC | #1
On Fri, Dec 9, 2022 at 3:29 PM Paolo Bonzini <pbonzini@redhat.com> wrote:
>
> Detect all compilers at the beginning of meson.build, and store
> the available languages in an array.
>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

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

> ---
>  meson.build | 62 ++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 35 insertions(+), 27 deletions(-)
>
> diff --git a/meson.build b/meson.build
> index 8a9ed5628317..c4fa82ae8ba4 100644
> --- a/meson.build
> +++ b/meson.build
> @@ -15,9 +15,21 @@ ss = import('sourceset')
>  fs = import('fs')
>
>  sh = find_program('sh')
> -cc = meson.get_compiler('c')
>  config_host = keyval.load(meson.current_build_dir() / 'config-host.mak')
>  enable_modules = 'CONFIG_MODULES' in config_host
> +targetos = host_machine.system()
> +
> +cc = meson.get_compiler('c')
> +all_languages = ['c']
> +if add_languages('cpp', required: false, native: false)
> +  all_languages += ['cpp']
> +  cxx = meson.get_compiler('cpp')
> +endif
> +if targetos == 'darwin' and \
> +   add_languages('objc', required: get_option('cocoa'), native: false)
> +  all_languages += ['objc']
> +  objc = meson.get_compiler('objc')
> +endif
>
>  # Temporary directory used for files created while
>  # configure runs. Since it is in the build directory
> @@ -54,8 +66,6 @@ if cpu in ['riscv32', 'riscv64']
>    cpu = 'riscv'
>  endif
>
> -targetos = host_machine.system()
> -
>  target_dirs = config_host['TARGET_DIRS'].split()
>  have_linux_user = false
>  have_bsd_user = false
> @@ -161,7 +171,7 @@ if 'dtrace' in get_option('trace_backends')
>      # semaphores are linked into the main binary and not the module's shared
>      # object.
>      add_global_arguments('-DSTAP_SDT_V2',
> -                         native: false, language: ['c', 'cpp', 'objc'])
> +                         native: false, language: all_languages)
>    endif
>  endif
>
> @@ -203,7 +213,7 @@ endif
>  if get_option('fuzzing')
>    add_project_link_arguments(['-Wl,-T,',
>                                (meson.current_source_dir() / 'tests/qtest/fuzz/fork_fuzz.ld')],
> -                             native: false, language: ['c', 'cpp', 'objc'])
> +                             native: false, language: all_languages)
>
>    # Specify a filter to only instrument code that is directly related to
>    # virtual-devices.
> @@ -216,7 +226,7 @@ if get_option('fuzzing')
>                   args: ['-fsanitize-coverage-allowlist=/dev/null',
>                          '-fsanitize-coverage=trace-pc'] )
>      add_global_arguments('-fsanitize-coverage-allowlist=instrumentation-filter',
> -                         native: false, language: ['c', 'cpp', 'objc'])
> +                         native: false, language: all_languages)
>    endif
>
>    if get_option('fuzzing_engine') == ''
> @@ -225,9 +235,9 @@ if get_option('fuzzing')
>      # everything with fsanitize=fuzzer-no-link. Otherwise, the linker will be
>      # unable to bind the fuzzer-related callbacks added by instrumentation.
>      add_global_arguments('-fsanitize=fuzzer-no-link',
> -                         native: false, language: ['c', 'cpp', 'objc'])
> +                         native: false, language: all_languages)
>      add_global_link_arguments('-fsanitize=fuzzer-no-link',
> -                              native: false, language: ['c', 'cpp', 'objc'])
> +                              native: false, language: all_languages)
>      # For the actual fuzzer binaries, we need to link against the libfuzzer
>      # library. They need to be configurable, to support OSS-Fuzz
>      fuzz_exe_ldflags = ['-fsanitize=fuzzer']
> @@ -238,15 +248,11 @@ if get_option('fuzzing')
>    endif
>  endif
>
> -add_global_arguments(qemu_cflags, native: false, language: ['c'])
> -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')
> +if 'cpp' in all_languages
>    add_global_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'],
>                         native: false, language: 'cpp')
>    foreach k: qemu_cflags
> @@ -255,7 +261,6 @@ if add_languages('cpp', required: false, native: false)
>        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'
> @@ -271,22 +276,21 @@ if targetos != 'sunos' and not config_host.has_key('CONFIG_TSAN')
>    qemu_ldflags += linker.get_supported_link_arguments('-Wl,--warn-common')
>  endif
>
> -add_global_link_arguments(qemu_ldflags, native: false, language: ['c', 'cpp', 'objc'])
> +add_global_link_arguments(qemu_ldflags, native: false, language: all_languages)
>
> +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')
>  if targetos == 'linux'
>    add_project_arguments('-isystem', meson.current_source_dir() / 'linux-headers',
>                          '-isystem', 'linux-headers',
> -                        language: ['c', 'cpp'])
> +                        language: all_languages)
>  endif
>
>  add_project_arguments('-iquote', '.',
>                        '-iquote', meson.current_source_dir(),
>                        '-iquote', meson.current_source_dir() / 'include',
> -                      language: ['c', 'cpp', 'objc'])
> -
> -if host_machine.system() == 'darwin'
> -  add_languages('objc', required: false, native: false)
> -endif
> +                      language: all_languages)
>
>  sparse = find_program('cgcc', required: get_option('sparse'))
>  if sparse.found()
> @@ -468,7 +472,7 @@ if get_option('tcg').allowed()
>      tcg_arch = 'ppc'
>    endif
>    add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch,
> -                        language: ['c', 'cpp', 'objc'])
> +                        language: all_languages)
>
>    accelerators += 'CONFIG_TCG'
>    config_host += { 'CONFIG_TCG': 'y' }
> @@ -494,7 +498,7 @@ endif
>  # 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: ['c', 'cpp', 'objc'])
> +                      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'],
> @@ -1674,8 +1678,8 @@ if get_option('cfi')
>        error('-fno-sanitize-trap=cfi-icall is not supported by the compiler')
>      endif
>    endif
> -  add_global_arguments(cfi_flags, native: false, language: ['c', 'cpp', 'objc'])
> -  add_global_link_arguments(cfi_flags, native: false, language: ['c', 'cpp', 'objc'])
> +  add_global_arguments(cfi_flags, native: false, language: all_languages)
> +  add_global_link_arguments(cfi_flags, native: false, language: all_languages)
>  endif
>
>  have_host_block_device = (targetos != 'darwin' or
> @@ -3713,8 +3717,12 @@ if link_args.length() > 0
>    summary_info += {'LDFLAGS':         ' '.join(link_args)}
>  endif
>  summary_info += {'QEMU_CFLAGS':       ' '.join(qemu_cflags)}
> -summary_info += {'QEMU_CXXFLAGS':     ' '.join(qemu_cxxflags)}
> -summary_info += {'QEMU_OBJCFLAGS':    ' '.join(qemu_objcflags)}
> +if 'cpp' in all_languages
> +  summary_info += {'QEMU_CXXFLAGS':     ' '.join(qemu_cxxflags)}
> +endif
> +if 'objc' in all_languages
> +  summary_info += {'QEMU_OBJCFLAGS':    ' '.join(qemu_objcflags)}
> +endif
>  summary_info += {'QEMU_LDFLAGS':      ' '.join(qemu_ldflags)}
>  summary_info += {'profiler':          get_option('profiler')}
>  summary_info += {'link-time optimization (LTO)': get_option('b_lto')}
> --
> 2.38.1
>
>
Daniel P. Berrangé Dec. 16, 2022, 3:03 p.m. UTC | #2
On Fri, Dec 09, 2022 at 12:23:54PM +0100, Paolo Bonzini wrote:
> Detect all compilers at the beginning of meson.build, and store
> the available languages in an array.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  meson.build | 62 ++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 35 insertions(+), 27 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


With regards,
Daniel
diff mbox series

Patch

diff --git a/meson.build b/meson.build
index 8a9ed5628317..c4fa82ae8ba4 100644
--- a/meson.build
+++ b/meson.build
@@ -15,9 +15,21 @@  ss = import('sourceset')
 fs = import('fs')
 
 sh = find_program('sh')
-cc = meson.get_compiler('c')
 config_host = keyval.load(meson.current_build_dir() / 'config-host.mak')
 enable_modules = 'CONFIG_MODULES' in config_host
+targetos = host_machine.system()
+
+cc = meson.get_compiler('c')
+all_languages = ['c']
+if add_languages('cpp', required: false, native: false)
+  all_languages += ['cpp']
+  cxx = meson.get_compiler('cpp')
+endif
+if targetos == 'darwin' and \
+   add_languages('objc', required: get_option('cocoa'), native: false)
+  all_languages += ['objc']
+  objc = meson.get_compiler('objc')
+endif
 
 # Temporary directory used for files created while
 # configure runs. Since it is in the build directory
@@ -54,8 +66,6 @@  if cpu in ['riscv32', 'riscv64']
   cpu = 'riscv'
 endif
 
-targetos = host_machine.system()
-
 target_dirs = config_host['TARGET_DIRS'].split()
 have_linux_user = false
 have_bsd_user = false
@@ -161,7 +171,7 @@  if 'dtrace' in get_option('trace_backends')
     # semaphores are linked into the main binary and not the module's shared
     # object.
     add_global_arguments('-DSTAP_SDT_V2',
-                         native: false, language: ['c', 'cpp', 'objc'])
+                         native: false, language: all_languages)
   endif
 endif
 
@@ -203,7 +213,7 @@  endif
 if get_option('fuzzing')
   add_project_link_arguments(['-Wl,-T,',
                               (meson.current_source_dir() / 'tests/qtest/fuzz/fork_fuzz.ld')],
-                             native: false, language: ['c', 'cpp', 'objc'])
+                             native: false, language: all_languages)
 
   # Specify a filter to only instrument code that is directly related to
   # virtual-devices.
@@ -216,7 +226,7 @@  if get_option('fuzzing')
                  args: ['-fsanitize-coverage-allowlist=/dev/null',
                         '-fsanitize-coverage=trace-pc'] )
     add_global_arguments('-fsanitize-coverage-allowlist=instrumentation-filter',
-                         native: false, language: ['c', 'cpp', 'objc'])
+                         native: false, language: all_languages)
   endif
 
   if get_option('fuzzing_engine') == ''
@@ -225,9 +235,9 @@  if get_option('fuzzing')
     # everything with fsanitize=fuzzer-no-link. Otherwise, the linker will be
     # unable to bind the fuzzer-related callbacks added by instrumentation.
     add_global_arguments('-fsanitize=fuzzer-no-link',
-                         native: false, language: ['c', 'cpp', 'objc'])
+                         native: false, language: all_languages)
     add_global_link_arguments('-fsanitize=fuzzer-no-link',
-                              native: false, language: ['c', 'cpp', 'objc'])
+                              native: false, language: all_languages)
     # For the actual fuzzer binaries, we need to link against the libfuzzer
     # library. They need to be configurable, to support OSS-Fuzz
     fuzz_exe_ldflags = ['-fsanitize=fuzzer']
@@ -238,15 +248,11 @@  if get_option('fuzzing')
   endif
 endif
 
-add_global_arguments(qemu_cflags, native: false, language: ['c'])
-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')
+if 'cpp' in all_languages
   add_global_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'],
                        native: false, language: 'cpp')
   foreach k: qemu_cflags
@@ -255,7 +261,6 @@  if add_languages('cpp', required: false, native: false)
       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'
@@ -271,22 +276,21 @@  if targetos != 'sunos' and not config_host.has_key('CONFIG_TSAN')
   qemu_ldflags += linker.get_supported_link_arguments('-Wl,--warn-common')
 endif
 
-add_global_link_arguments(qemu_ldflags, native: false, language: ['c', 'cpp', 'objc'])
+add_global_link_arguments(qemu_ldflags, native: false, language: all_languages)
 
+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')
 if targetos == 'linux'
   add_project_arguments('-isystem', meson.current_source_dir() / 'linux-headers',
                         '-isystem', 'linux-headers',
-                        language: ['c', 'cpp'])
+                        language: all_languages)
 endif
 
 add_project_arguments('-iquote', '.',
                       '-iquote', meson.current_source_dir(),
                       '-iquote', meson.current_source_dir() / 'include',
-                      language: ['c', 'cpp', 'objc'])
-
-if host_machine.system() == 'darwin'
-  add_languages('objc', required: false, native: false)
-endif
+                      language: all_languages)
 
 sparse = find_program('cgcc', required: get_option('sparse'))
 if sparse.found()
@@ -468,7 +472,7 @@  if get_option('tcg').allowed()
     tcg_arch = 'ppc'
   endif
   add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch,
-                        language: ['c', 'cpp', 'objc'])
+                        language: all_languages)
 
   accelerators += 'CONFIG_TCG'
   config_host += { 'CONFIG_TCG': 'y' }
@@ -494,7 +498,7 @@  endif
 # 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: ['c', 'cpp', 'objc'])
+                      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'],
@@ -1674,8 +1678,8 @@  if get_option('cfi')
       error('-fno-sanitize-trap=cfi-icall is not supported by the compiler')
     endif
   endif
-  add_global_arguments(cfi_flags, native: false, language: ['c', 'cpp', 'objc'])
-  add_global_link_arguments(cfi_flags, native: false, language: ['c', 'cpp', 'objc'])
+  add_global_arguments(cfi_flags, native: false, language: all_languages)
+  add_global_link_arguments(cfi_flags, native: false, language: all_languages)
 endif
 
 have_host_block_device = (targetos != 'darwin' or
@@ -3713,8 +3717,12 @@  if link_args.length() > 0
   summary_info += {'LDFLAGS':         ' '.join(link_args)}
 endif
 summary_info += {'QEMU_CFLAGS':       ' '.join(qemu_cflags)}
-summary_info += {'QEMU_CXXFLAGS':     ' '.join(qemu_cxxflags)}
-summary_info += {'QEMU_OBJCFLAGS':    ' '.join(qemu_objcflags)}
+if 'cpp' in all_languages
+  summary_info += {'QEMU_CXXFLAGS':     ' '.join(qemu_cxxflags)}
+endif
+if 'objc' in all_languages
+  summary_info += {'QEMU_OBJCFLAGS':    ' '.join(qemu_objcflags)}
+endif
 summary_info += {'QEMU_LDFLAGS':      ' '.join(qemu_ldflags)}
 summary_info += {'profiler':          get_option('profiler')}
 summary_info += {'link-time optimization (LTO)': get_option('b_lto')}