我如何从 Meson 构建中的源文件派生可执行文件名称?

How do I derive an executable name from a source file in Meson build?

我正在尝试在 Meson 中创建单元测试目标列表,每个测试用例都是从单个源文件构建的。源文件在子目录中使用 files() 命令定义:

my_test_files = files(['test_foo.c','test_bar.c','test_baz.c'])

我想在顶级构建中做这样的事情:

foreach t_file : my_test_files
    t_name = t.split('.')[0]
    test(t_name, executable(t_name, t_file, ...))
endforeach

我知道如果文件名是纯字符串,可以这样做,但上述方法失败并出现 'File object is not callable' 错误。

有没有更多的'Mesonic'方法从源文件名派生可执行文件/测试名?

如果您将变量简单地定义为数组,它应该可以工作,例如:

my_test_files = ['test_foo.c','test_bar.c','test_baz.c']

循环保持不变,除了一些拼写错误已修复:

foreach t_file : my_test_files
    t_name = t_file.split('.')[0]
    test(t_name, executable(t_name, t_file, ...))
endforeach

而不是构建文件对象数组。这是因为 executable() accepts input files in many forms: 作为文件对象(你试图做的)和作为字符串的源文件(应该被编译)或目标文件(要链接) - 由文件扩展名检测。

为了获得更大的灵活性和更好的控制,可以使用数组的数组(当然,它是可扩展的,并且可以包含生成测试所需的任何内容):

foo_files = files('test_foo.c')
bar_files = files('test_bar.c')
baz_files = files('test_baz.c')

test_files = [
  ['foo', foo_files, foo_deps],
  ['bar', bar_files, []],
  ['baz', baz_files, baz_deps]]

foreach t : test_files
    test(t[0], executable(t[0], t[1], dependencies=t[2], ...))
endforeach