介子可以读取文件的内容吗

Can Meson read the contents of a file

Meson是否可以将文件内容读取到数组或字符串中?从 here 可以将一个字符串拆分成一个数组,并且可以用 foreach 循环一个数组,但是我一直没能找到从文件中获取数据的方法.

不是直接没有,你可以使用 run_command() 从另一个 tool/script 获取它。

为了完成@TingPing 的回答,我通常这样做:

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

该方法也可用于以下情况:

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

不要忘记 Meson 的文件引用可能有点不精确,因此您需要使用其中之一:

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

编辑:对于更交叉兼容的解决方案,您可以使用 python 而不是 cat

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

更新

从Meson 0.57.0开始,可以使用Filesystem模块的read功能:

fs = import('fs') 
...

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

foreach item : my_list
  # Do something
endforeach