Debian Codegraph @

subprojects/meson-docs/docs/markdown/Run-targets.md

raw
1 ---
2 short-description: Targets to run external commands
3 ...
4
5 # Run targets
6
7 Sometimes you need to have a target that just runs an external
8 command. As an example you might have a build target that reformats
9 your source code, runs `cppcheck` or something similar. In Meson this
10 is accomplished with a so called *run target*.
11
12 The recommended way of doing this is writing the command(s) you want
13 to run to a script file. Here's an example script.
14
15 ```bash
16 #!/bin/sh
17
18 cd "${MESON_SOURCE_ROOT}"
19 inspector_command -o "${MESON_BUILD_ROOT}/inspection_result.txt"
20 ```
21
22 Note the two environment variables `MESON_SOURCE_ROOT` and
23 `MESON_BUILD_ROOT`. These are absolute paths to your project's source
24 and build directories and they are automatically set up by Meson. In
25 addition to these Meson also sets up the variable `MESON_SUBDIR`,
26 which points to the subdirectory where the run command was specified.
27 Most commands don't need to set up this.
28
29 Note how the script starts by cd'ing into the source dir. Meson does
30 not guarantee that the script is run in any specific directory.
31 Whether you need to do the same depends on what your custom target
32 wants to do.
33
34 To make this a run target we write it to a script file called
35 `scripts/inspect.sh` and specify it in the top level Meson file like
36 this.
37
38 ```meson
39 run_target('inspector',
40 command : 'scripts/inspect.sh')
41 ```
42
43 Run targets are not run by default. To run it run the following command.
44
45 ```console
46 $ meson compile inspector
47 ```
48
49 All additional entries in `run_target`'s `command` array are passed
50 unchanged to the inspector script, so you can do things like this:
51
52 ```meson
53 run_target('inspector',
54 command : ['scripts/inspect.sh', '--exclude', 'tests'])
55 ```