Can Meson read the contents of a file
Asked Answered
R

3

7

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.

Rale answered 8/11, 2017 at 8:27 Comment(0)
F
3

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

Firooc answered 11/11, 2017 at 0:54 Comment(0)
E
9

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
Erudite answered 14/6, 2021 at 16:41 Comment(0)
F
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()
Fadein answered 28/11, 2017 at 17:6 Comment(5)
Does that work cross-platform? (Since meson is written in python, I suppose you could use the python module to invoke a python script instead…)Reclamation
@Reclamation No, it does not. For example, the cat is not necessarily available in Windows.Erudite
To be more portable, you may use python instead. I'm editing the answer for that.Fadein
I believe @0@ should be used instead of {0} for substitution pattern in command stringLina
Yes, mistake on my part.Fadein
F
3

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

Firooc answered 11/11, 2017 at 0:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.