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.
Can Meson read the contents of a file
Asked Answered
Not directly no, you can use run_command()
to get it from another tool/script though.
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
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()
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 string –
Lina Yes, mistake on my part. –
Fadein
Not directly no, you can use run_command()
to get it from another tool/script though.
© 2022 - 2024 — McMap. All rights reserved.