如何从Meson脚本运行shell命令?

vlju58qv  于 2023-01-31  发布在  Shell
关注(0)|答案(2)|浏览(232)

如何从Meson构建脚本运行shell命令(例如cp,即copy)?
我尝试了这个代码:

r = run_command('cp', 'test.txt', 'test2.txt')

if r.returncode() != 0
  warning('Command failed')
endif

但它什么也没做。
run_command运行成功(返回0),但未复制文件。
如果我用cp3替换cp,我会从Meson得到一条错误消息,进程终止,甚至不会到达下面的行。
如果我用test0.txt替换test.txt,我会从脚本中得到一条错误消息。
所以脚本的行为是正确的,但是命令不会在文件系统上留下任何痕迹。
run_command是从Meson运行shell命令的唯一方法吗?我做错了什么?
参考:https://mesonbuild.com/External-commands.html

flvtvl50

flvtvl501#

命令从未指定的目录运行,因此,请尝试指定完整的文件名,例如:

source = join_paths(meson.source_root(), 'test.txt')
dest = join_paths(meson.build_root(), 'test2.txt')
message('copying @0@ to @1@ ...'.format(source, dest))
r = run_command('cp', source, dest)
p4rjhz4m

p4rjhz4m2#

我最近偶然发现了这个答案。
首先,在最近的meson发行版中,source_rootbuild_root被弃用。请改用current_source_dircurrent_build_dir函数。
其次,run_command始终是clean操作的运行事件,如果可能,最好使用custom_target来实现相同的结果。

相关问题