aboutsummaryrefslogtreecommitdiff
path: root/docs/markdown/Run-targets.md
blob: 04959ab3b4181481490fb2060c9b3f2b875f2fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
short-description: Targets to run external commands
...

# Run targets

Sometimes you need to have a target that just runs an external
command. As an example you might have a build target that reformats
your source code, runs `cppcheck` or something similar. In Meson this
is accomplished with a so called *run target*.

The recommended way of doing this is writing the command(s) you want
to run to a script file. Here's an example script.

```bash
#!/bin/sh

cd "${MESON_SOURCE_ROOT}"
inspector_command -o "${MESON_BUILD_ROOT}/inspection_result.txt"
```

Note the two environment variables `MESON_SOURCE_ROOT` and
`MESON_BUILD_ROOT`. These are absolute paths to your project's source
and build directories and they are automatically set up by Meson. In
addition to these Meson also sets up the variable `MESON_SUBDIR`,
which points to the subdirectory where the run command was specified.
Most commands don't need to set up this.

Note how the script starts by cd'ing into the source dir. Meson does
not guarantee that the script is run in any specific directory.
Whether you need to do the same depends on what your custom target
wants to do.

To make this a run target we write it to a script file called
`scripts/inspect.sh` and specify it in the top level Meson file like
this.

```meson
run_target('inspector',
  command : 'scripts/inspect.sh')
```

Run targets are not run by default. To run it run the following command.

```console
$ meson compile inspector
```

All additional entries in `run_target`'s `command` array are passed
unchanged to the inspector script, so you can do things like this:

```meson
run_target('inspector',
  command : ['scripts/inspect.sh', '--exclude', 'tests'])
```