shellcommandfile-copyingmeson-build

How to run a shell command from a Meson script?


How can I run a shell command (e.g. cp, i.e. copy) from a Meson build script?

I tried with this code:

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

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

But it does nothing.
run_command runs successfully (0 is returned), but not file is copied.
If I substitute cp with cp3, I get an error message from Meson, the process terminates and it does not even get to the following line.
If I substitute test.txt with test0.txt, I get an error message from the script.

So the script behaves correctly, but the command leaves no trace of itself on the file system.

Is run_command the only way to run a shell command from Meson? What am I doing wrong?


Reference: https://mesonbuild.com/External-commands.html


Solution

  • The command is run from unspecified directory, so, try specifying full file names, e.g.:

    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)