meson: cleanup compiler detection

Detect all compilers at the beginning of meson.build, and store
the available languages in an array.

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Paolo Bonzini 2022-10-12 14:15:06 +02:00
parent 6a97f39392
commit e513402436

View file

@ -14,8 +14,8 @@ keyval = import('keyval')
ss = import('sourceset') ss = import('sourceset')
fs = import('fs') fs = import('fs')
targetos = host_machine.system()
sh = find_program('sh') sh = find_program('sh')
cc = meson.get_compiler('c')
config_host = keyval.load(meson.current_build_dir() / 'config-host.mak') config_host = keyval.load(meson.current_build_dir() / 'config-host.mak')
enable_modules = 'CONFIG_MODULES' in config_host enable_modules = 'CONFIG_MODULES' in config_host
enable_static = 'CONFIG_STATIC' in config_host enable_static = 'CONFIG_STATIC' in config_host
@ -23,6 +23,18 @@ enable_static = 'CONFIG_STATIC' in config_host
# Allow both shared and static libraries unless --enable-static # Allow both shared and static libraries unless --enable-static
static_kwargs = enable_static ? {'static': true} : {} static_kwargs = enable_static ? {'static': true} : {}
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 # Temporary directory used for files created while
# configure runs. Since it is in the build directory # configure runs. Since it is in the build directory
# we can safely blow away any previous version of it # we can safely blow away any previous version of it
@ -58,8 +70,6 @@ if cpu in ['riscv32', 'riscv64']
cpu = 'riscv' cpu = 'riscv'
endif endif
targetos = host_machine.system()
target_dirs = config_host['TARGET_DIRS'].split() target_dirs = config_host['TARGET_DIRS'].split()
have_linux_user = false have_linux_user = false
have_bsd_user = false have_bsd_user = false
@ -165,7 +175,7 @@ if 'dtrace' in get_option('trace_backends')
# semaphores are linked into the main binary and not the module's shared # semaphores are linked into the main binary and not the module's shared
# object. # object.
add_global_arguments('-DSTAP_SDT_V2', add_global_arguments('-DSTAP_SDT_V2',
native: false, language: ['c', 'cpp', 'objc']) native: false, language: all_languages)
endif endif
endif endif
@ -207,7 +217,7 @@ endif
if get_option('fuzzing') if get_option('fuzzing')
add_project_link_arguments(['-Wl,-T,', add_project_link_arguments(['-Wl,-T,',
(meson.current_source_dir() / 'tests/qtest/fuzz/fork_fuzz.ld')], (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 # Specify a filter to only instrument code that is directly related to
# virtual-devices. # virtual-devices.
@ -220,7 +230,7 @@ if get_option('fuzzing')
args: ['-fsanitize-coverage-allowlist=/dev/null', args: ['-fsanitize-coverage-allowlist=/dev/null',
'-fsanitize-coverage=trace-pc'] ) '-fsanitize-coverage=trace-pc'] )
add_global_arguments('-fsanitize-coverage-allowlist=instrumentation-filter', add_global_arguments('-fsanitize-coverage-allowlist=instrumentation-filter',
native: false, language: ['c', 'cpp', 'objc']) native: false, language: all_languages)
endif endif
if get_option('fuzzing_engine') == '' if get_option('fuzzing_engine') == ''
@ -229,9 +239,9 @@ if get_option('fuzzing')
# everything with fsanitize=fuzzer-no-link. Otherwise, the linker will be # everything with fsanitize=fuzzer-no-link. Otherwise, the linker will be
# unable to bind the fuzzer-related callbacks added by instrumentation. # unable to bind the fuzzer-related callbacks added by instrumentation.
add_global_arguments('-fsanitize=fuzzer-no-link', 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', 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 # For the actual fuzzer binaries, we need to link against the libfuzzer
# library. They need to be configurable, to support OSS-Fuzz # library. They need to be configurable, to support OSS-Fuzz
fuzz_exe_ldflags = ['-fsanitize=fuzzer'] fuzz_exe_ldflags = ['-fsanitize=fuzzer']
@ -242,15 +252,11 @@ if get_option('fuzzing')
endif endif
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. # Check that the C++ compiler exists and works with the C compiler.
link_language = 'c' link_language = 'c'
linker = cc linker = cc
qemu_cxxflags = [] qemu_cxxflags = []
if add_languages('cpp', required: false, native: false) if 'cpp' in all_languages
cxx = meson.get_compiler('cpp')
add_global_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'], add_global_arguments(['-D__STDC_LIMIT_MACROS', '-D__STDC_CONSTANT_MACROS', '-D__STDC_FORMAT_MACROS'],
native: false, language: 'cpp') native: false, language: 'cpp')
foreach k: qemu_cflags foreach k: qemu_cflags
@ -259,7 +265,6 @@ if add_languages('cpp', required: false, native: false)
qemu_cxxflags += [k] qemu_cxxflags += [k]
endif endif
endforeach endforeach
add_global_arguments(qemu_cxxflags, native: false, language: 'cpp')
if cxx.links(files('scripts/main.c'), args: qemu_cflags) if cxx.links(files('scripts/main.c'), args: qemu_cflags)
link_language = 'cpp' link_language = 'cpp'
@ -275,22 +280,21 @@ if targetos != 'sunos' and not config_host.has_key('CONFIG_TSAN')
qemu_ldflags += linker.get_supported_link_arguments('-Wl,--warn-common') qemu_ldflags += linker.get_supported_link_arguments('-Wl,--warn-common')
endif 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' if targetos == 'linux'
add_project_arguments('-isystem', meson.current_source_dir() / 'linux-headers', add_project_arguments('-isystem', meson.current_source_dir() / 'linux-headers',
'-isystem', 'linux-headers', '-isystem', 'linux-headers',
language: ['c', 'cpp']) language: all_languages)
endif endif
add_project_arguments('-iquote', '.', add_project_arguments('-iquote', '.',
'-iquote', meson.current_source_dir(), '-iquote', meson.current_source_dir(),
'-iquote', meson.current_source_dir() / 'include', '-iquote', meson.current_source_dir() / 'include',
language: ['c', 'cpp', 'objc']) language: all_languages)
if host_machine.system() == 'darwin'
add_languages('objc', required: false, native: false)
endif
sparse = find_program('cgcc', required: get_option('sparse')) sparse = find_program('cgcc', required: get_option('sparse'))
if sparse.found() if sparse.found()
@ -472,7 +476,7 @@ if get_option('tcg').allowed()
tcg_arch = 'ppc' tcg_arch = 'ppc'
endif endif
add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch, add_project_arguments('-iquote', meson.current_source_dir() / 'tcg' / tcg_arch,
language: ['c', 'cpp', 'objc']) language: all_languages)
accelerators += 'CONFIG_TCG' accelerators += 'CONFIG_TCG'
config_host += { 'CONFIG_TCG': 'y' } config_host += { 'CONFIG_TCG': 'y' }
@ -498,7 +502,7 @@ endif
# The path to glib.h is added to all compilation commands. This was # The path to glib.h is added to all compilation commands. This was
# grandfathered in from the QEMU Makefiles. # grandfathered in from the QEMU Makefiles.
add_project_arguments(config_host['GLIB_CFLAGS'].split(), 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(), glib = declare_dependency(compile_args: config_host['GLIB_CFLAGS'].split(),
link_args: config_host['GLIB_LIBS'].split(), link_args: config_host['GLIB_LIBS'].split(),
version: config_host['GLIB_VERSION'], version: config_host['GLIB_VERSION'],
@ -1723,8 +1727,8 @@ if get_option('cfi')
error('-fno-sanitize-trap=cfi-icall is not supported by the compiler') error('-fno-sanitize-trap=cfi-icall is not supported by the compiler')
endif endif
endif endif
add_global_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: ['c', 'cpp', 'objc']) add_global_link_arguments(cfi_flags, native: false, language: all_languages)
endif endif
have_host_block_device = (targetos != 'darwin' or have_host_block_device = (targetos != 'darwin' or
@ -3773,8 +3777,12 @@ if link_args.length() > 0
summary_info += {'LDFLAGS': ' '.join(link_args)} summary_info += {'LDFLAGS': ' '.join(link_args)}
endif endif
summary_info += {'QEMU_CFLAGS': ' '.join(qemu_cflags)} summary_info += {'QEMU_CFLAGS': ' '.join(qemu_cflags)}
summary_info += {'QEMU_CXXFLAGS': ' '.join(qemu_cxxflags)} if 'cpp' in all_languages
summary_info += {'QEMU_OBJCFLAGS': ' '.join(qemu_objcflags)} 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 += {'QEMU_LDFLAGS': ' '.join(qemu_ldflags)}
summary_info += {'profiler': get_option('profiler')} summary_info += {'profiler': get_option('profiler')}
summary_info += {'link-time optimization (LTO)': get_option('b_lto')} summary_info += {'link-time optimization (LTO)': get_option('b_lto')}