2

I'm trying to create a list of unit test targets in Meson, with each test case built from a single source file. The source files are defined with a files() command in a subdirectory:

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

What I'd like to do is something like this in the top-level build:

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

I know it's possible to do this if the file names are plain strings, but the above approach fails with a 'File object is not callable' error.

Is there a more 'Mesonic' way to derive the executable / test name from the source file name?

pevik
  • 4,523
  • 3
  • 33
  • 44
rellenberg
  • 105
  • 8

1 Answers1

3

It should work if you define your variable simply as array, e.g.:

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

the loop stays the same, except some typo fixed with:

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

instead of building array of file objects. This is because executable() accepts input files in many forms: as file objects (which you tried to do) and as strings either source files (that should be compiled) or object files (to be linked) - detected by file name extension.

For more flexibility and better control, one can use array of arrays (which is, of course, extendable and may contain anything that is needed to generate tests):

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
pevik
  • 4,523
  • 3
  • 33
  • 44
pmod
  • 10,450
  • 1
  • 37
  • 50
  • 1
    This would work, though the reason I wanted to use the "files" command (rather than plain strings) is to preserve paths. The files are defined in each subdirectory, while the executable / test rules are defined in a top-level file. The feature I'd really like is read-only access to the File object's string parts (e.g. path, name, extension, etc.). – rellenberg Apr 12 '19 at 17:49
  • @user2059564 ok, array can contain file objects as well... see my update – pmod Apr 12 '19 at 20:30
  • Your method makes sense and looks like a good compromise, especially since I could do string manipulations in the subdir file to generate the outer array. Thanks! – rellenberg Apr 15 '19 at 13:53