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.
Asked
Active
Viewed 1,659 times
6

Pentarctagon
- 413
- 2
- 5
- 12
3 Answers
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
-
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…) – Caesar May 05 '20 at 11:44
-
1@Caesar No, it does not. For example, the ```cat``` is not necessarily available in Windows. – ManuelAtWork Jun 14 '21 at 14:40
-
To be more portable, you may use python instead. I'm editing the answer for that. – Salamandar Jun 16 '21 at 06:46
-
I believe `@0@` should be used instead of `{0}` for substitution pattern in command string – Pavel.Zh Mar 09 '22 at 20:53
-
Yes, mistake on my part. – Salamandar Mar 10 '22 at 11:27
3
Not directly no, you can use run_command()
to get it from another tool/script though.

TingPing
- 2,129
- 1
- 12
- 15