3

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

Pietro
  • 12,086
  • 26
  • 100
  • 193

2 Answers2

5

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)
pmod
  • 10,450
  • 1
  • 37
  • 50
  • It works. A note: `join_paths` does not seem to work as an append. E.g.: `join_path('/path/to/', 'subdir/test.txt') --> /path/to/subdir/test.txt`, while `join_path('/path/to/', '/subdir/test.txt') --> /subdir/test.txt` – Pietro Oct 04 '18 at 15:29
  • 3
    @Pietro exactly, that's why ref manual says for join_paths: "If any one of the individual segments is an absolute path, all segments before it are dropped." – pmod Oct 04 '18 at 16:51
1

I came across this answer recently.

First, in recent meson releases source_root and build_root are deprecated. Use current_source_dir and current_build_dir functions instead.

Second, run_command is always run even for the clean operation, better to use custom_target to achieve the same results if possible.

Pietro
  • 12,086
  • 26
  • 100
  • 193
dturvene
  • 2,284
  • 1
  • 20
  • 18