6

Is it possible for Meson to read the contents of a file into an array or a string? From here a string can be split into an array, and an array can be looped over with foreach, but I haven't been able to find a way to get the data from the file to start with.

Pentarctagon
  • 413
  • 2
  • 5
  • 12

3 Answers3

7

Update

Since Meson 0.57.0, you can use the read function of the Filesystem module:

fs = import('fs') 
...

my_list = fs.read('list.txt').strip().split('\n')

foreach item : my_list
  # Do something
endforeach
ManuelAtWork
  • 2,198
  • 29
  • 34
6

To complete @TingPing's answer, I usually do that:

  files = run_command(
    'cat', files('thefile.txt'),
  ).stdout().strip()

That method can also be used for something like:

  images = run_command('find',
    meson.current_source_dir(),
    '-type', 'f',
    '-name', '*.png',
    '-printf', '%f\n'
  ).stdout().strip().split('\n')

Don't forget that file referencing can be a bit imprecise with Meson, so you need to use one of those:

  • files('thefilename')
  • join_paths(meson.source_root(), meson.current_source_dir(), 'thefilename')

EDIT : For a more cross-compatible solution you can use python instead of cat :

files = run_command('python', '-c',
    '[print(line, end="") for line in open("@0@")]'.format(myfile)
).stdout().strip()
Salamandar
  • 589
  • 6
  • 16
3

Not directly no, you can use run_command() to get it from another tool/script though.

TingPing
  • 2,129
  • 1
  • 12
  • 15