On 6/9/2026 2:47 PM, Pierrick Bouvier wrote:
> Every target declare its tests, and add them in global tcg_tests
> dictionary, indexed by target name. See comment for full description.
>
> We make sure that if a user repeats a test several time it won't result
> in duplicated executables or tests. In next commits, we'll introduce
> what's needed to deal with that.
>
> A test is compiled from a single source file (and later, a single set of
> compile flags). We make sure dependencies are correctly handled by
> generating a depfile.
>
> Signed-off-by: Pierrick Bouvier <[email protected]>
> ---
> tests/meson.build | 2 +-
> tests/tcg/meson.build | 94 +++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 95 insertions(+), 1 deletion(-)
> create mode 100644 tests/tcg/meson.build
>
> diff --git a/tests/meson.build b/tests/meson.build
> index 9ba04bbedd3..5877d2f60b1 100644
> --- a/tests/meson.build
> +++ b/tests/meson.build
> @@ -74,7 +74,7 @@ subdir('decode')
>
> if 'CONFIG_TCG' in config_all_accel
> subdir('fp')
> - subdir('tcg/plugins')
> + subdir('tcg')
> endif
>
> subdir('audio')
> diff --git a/tests/tcg/meson.build b/tests/tcg/meson.build
> new file mode 100644
> index 00000000000..72d1a9f8986
> --- /dev/null
> +++ b/tests/tcg/meson.build
> @@ -0,0 +1,94 @@
> +env = find_program('env')
> +
> +tcg_tests = {}
> +# tcg_tests is a dictionary with following structure:
> +#
> +# {
> +# 'name_of_target': {
> +# 'cc': cross_compiler,
> +# 'folder': 'folder_for_test_src_files',
> +# 'qemu': emulators['qemu-...'],
> +# 'tests': [
> +# {
> +# 'src_file': {
> +# }
> +# },
> +# ...
> +# ]
> +# }
> +# }
> +#
> +# Every test executable, is built only once.
> +# Tests for a given src use the same executable by default, and their
> definition
> +# is guaranteed to be unique also.
> +# Default name is derived from src.
> +
> +# plugins come first, as we need to build the list
> +subdir('plugins')
> +
> +# Finally, we can create all test executables and test targets
> +foreach target, plan: tcg_tests
> + # Detect duplicated executables/tests, and report an error to force user to
> + # choose how to deal with it.
> + built_tests = {}
> + added_tests = {}
> +
> + # return a clear error if user mispell a target entry
> + foreach key, _ : plan
> + if key not in ['cc', 'folder', 'qemu', 'tests']
> + error('unknown tcg test plan entry \'' + key + '\' for target ' +
> target)
> + endif
> + endforeach
> +
> + cc = plan['cc']
> + folder = plan['folder']
> + qemu = plan['qemu']
> + tests = plan['tests']
> +
> + foreach t : tests
> + foreach src, setup: t
> + # meson '/' operator drops left operand if right is an absolute path
> + src = folder / src
> + file = files(src)
> + test = fs.name(file)
dead variable, removed.
> + exe_name = fs.stem(src)
> +
> + exe_name = target + '-' + exe_name
> + test_name = exe_name
> +
> + if test_name in added_tests
> + error('test ' + test_name + ' was already added: ' +
> + 'specify a different \'test_name\'')
> + endif
> +
> + # build executable if needed
> + if exe_name not in built_tests
> + exe = custom_target(exe_name,
> + input: file,
> + command: [cc, '@INPUT@',
> + '-o', '@OUTPUT@', '-static',
> + '-MMD', '-MF', '@DEPFILE@',
> + '-Wall', '-Werror',
> + '-O0', '-g', '-fno-strict-aliasing',
> + ],
> + depfile: exe_name + '.d',
> + output: exe_name + '.test',
> + build_by_default: false)
> + built_tests += {exe_name: exe}
> + endif
> +
> + depends = []
> +
> + exe = built_tests[exe_name]
> + cmd = [qemu, exe]
> +
> + # some tests expect QEMU env var to be set
> + cmd = ['QEMU=' + qemu.full_path(), cmd]
> +
> + test(test_name, env, args: cmd,
> + depends: depends,
> + suite: ['tcg', 'tcg-' + target])
> + added_tests += {test_name: true}
> + endforeach
> + endforeach
> +endforeach