docs: Fix typos in documentation
[platform/upstream/gstreamer.git] / subprojects / gst-docs / markdown / installing / building-from-source-using-meson.md
1 ---
2 short-description: Setting up a development environment the modern way
3 authors:
4   - name: Edward Hervey
5     email: edward@centricular.com
6     years: [2020]
7 ...
8
9
10 # Building from source using Meson
11
12 **If you just want to use GStreamer, please visit [the download page](https://gstreamer.freedesktop.org/download/).
13 We provide pre-built binaries for Windows, macOS, Android, and iOS**.
14
15 This is the recommended setup for developers who want to work on the GStreamer
16 code itself and/or modify it, or application developers who wish to quickly try a
17 feature which isn't yet in a released version of GStreamer.
18
19 Note: This only applies for doing GStreamer development on Linux, Windows and
20 macOS. If you:
21
22 1. Want to do GStreamer development for Android, iOS, or UWP, or
23 1. Have to build GStreamer packages for distribution or deployment, or
24 1. Need plugins with external dependencies without Meson ports
25
26 Please refer to [Building using Cerbero](installing/building-from-source-using-cerbero.md),
27 which can be used to build a specific GStreamer release or to build unreleased
28 GStreamer code.
29
30 ## What are Meson, gst-build and the GStreamer monorepo?
31
32 The [Meson build system][meson] is a portable build system which is fast and
33 meant to be more user friendly than alternatives. It generates build
34 instructions which can then be executed by [`ninja`][ninja]. The GStreamer
35 project uses it for all subprojects.
36
37 In September 2021 all of the main GStreamer modules were merged into a
38 single code repository, the GStreamer [mono repo][monorepo-faq] which lives
39 in the main [GStreamer git repository][gstreamer], and this is where all
40 GStreamer development happens nowadays for GStreamer version 1.19/1.20 and later.
41
42 Before the mono repository merge the different GStreamer modules lived in
43 separate git repositories and there was a separate meta-builder project
44 called [`gst-build`][gst-build] to download and build all the subprojects.
45 This is what you should use if you want to build or develop against older
46 stable branches such as GStreamer 1.16 or 1.18.
47
48 If you want to build or develop against upcoming development or stable branches
49 you should use the `main` branch of the GStreamer module containing the mono
50 repository.
51
52 In the following sections we will only talk about the GStreamer mono repo,
53 but `gst-build` works pretty much the same way, the only difference being
54 that it would download the various GStreamer submodules as well.
55
56 [monorepo-faq]: https://gstreamer.freedesktop.org/documentation/frequently-asked-questions/mono-repository.html
57
58 ## Setting up the build with Meson
59
60 In order to build the current GStreamer development version, which will become
61 the 1.20 stable branch in the near future, clone the GStreamer mono repository:
62 ``` shell
63 git clone https://gitlab.freedesktop.org/gstreamer/gstreamer.git
64 cd gstreamer
65 ```
66
67 Or if you have developer access to the repositories:
68 ``` shell
69 git clone git@gitlab.freedesktop.org:gstreamer/gstreamer.git
70 cd gstreamer
71 ```
72 If you want to build the stable 1.18 or 1.16 branches, clone `gst-build`:
73
74 ``` shell
75 git clone https://gitlab.freedesktop.org/gstreamer/gst-build.git
76 cd gst-build
77 ```
78
79 ### Repository layout
80
81 The repository contains a few notable scripts and directories:
82 1. `meson.build` is the top-level build definition which will recursively
83    configure all dependencies. It also defines some helper commands allowing you
84    to have a development environment or easily update git
85    repositories for the GStreamer modules.
86 2. `subprojects/` is the directory containing GStreamer modules and
87    a selection of dependencies.
88
89
90 ## Basic meson and ninja usage
91
92 Configuring a module (or several in one go when in gst-build) is done by
93 executing:
94
95 ``` shell
96 meson <build_directory>
97 ```
98
99 The `build_directory` is where all the build instructions and output will be
100 located (This is also called *"out of directory"* building). If the directory is
101 not created it will be done so at this point. Note that calling `meson` without
102 any *command* argument is implicitly calling the `meson setup` command (i.e. to
103 do the initial configuration of a project).
104
105 There is only one restriction regarding the location of the `build_directory`:
106 it can't be the same as the source directory (i.e. where you cloned your module).
107 It can be outside of that directory or below/within that directory though.
108
109 Once meson is done configuring, you can either:
110
111 1. enter the specified build directory and run ninja.
112
113    ``` shell
114    cd <build_directory>
115    ninja
116    ``` 
117 2. *or* instead of switching to the build directory every time you wish to
118    execute `ninja` commands, you can just specify the build directory as an
119    argument. The advantage of this option is that you can run it from anywhere
120    (instead of changing to the ninja directory)
121    
122    ``` shell
123    ninja -C </path/to/build_directory>
124    ```
125
126 This will build everything from that module (and subprojects if building
127 gst-build or the mono repository).
128
129 Note: You do not need to re-run `meson` when you modify source files, you just
130 need to re-run `ninja`. If you build/configuration files changed, `ninja` will
131 figure out on its own that `meson` needs to be re-run and will do that
132 automatically.
133
134
135 ## Entering the development environment
136
137 GStreamer is made of several tools, plugins and components. In order to make it
138 easier for development and testing, there is a target (provided by `gst-build`
139 or the mono repository, and in future directly by `meson` itself) which will
140 setup environment variables accordingly so that you can use all the
141 build results directly.
142
143 For anyone familiar with python and virtualenv, you will feel right at home.
144
145 The script is called **`gst-env.py`**, it is located in the root of the
146 GStreamer mono-repository.
147
148 Another way to enter the virtual environment is to execute **`ninja -C
149 <path/to/build_directory> devenv`**. This option has less options and is
150 therefore not compatible with some workflows.
151
152 *NOTE*: you cannot use `meson` or reconfigure with `ninja` within the virtual
153 environment, therefore build before entering the environment or build from
154 another terminal/terminal-tab.
155
156 ### How does it work?
157
158 Start a new shell session with a specific set of environment variables, that
159 tell GStreamer where to find plugins or libraries.
160
161 The most important options are:
162
163 + **Shell context related variables**
164   * *PATH* - System path used to search for executable files, `gst-env` will
165     append folders containing executables from the build directory.
166   * *GST_PLUGIN_PATH* - List of paths to search for plugins (`.so`/`.dll`
167     files), `gst-env` will add all plugins found within the
168     `GstPluginsPath.json` file and from a few other locations.
169   * *GST_PLUGIN_SYSTEM_PATH*  - When set this will make GStreamer check for
170     plugins in system wide paths, this is kept blank on purpose by `gst-env` to
171     avoid using plugins installed outside the environment.
172   * *GST_REGISTRY* - Use a custom file as plugin cache / registry. `gst-env`
173     utilizes the one found in the given build directory.
174 + **Meson (build environment) related variables**
175   * *GST_VERSION* - Sets the build version in meson.
176   * *GST_ENV* - Makes sure that neither meson or ninja are run from within the
177     `gst-env`. Can be used to identify if the environment is active.
178 + **Validation (test runners) related variables**
179   * *GST_VALIDATE_SCENARIOS_PATH* - List of paths to search for validation
180     scenario files (list of actions to be executed by the pipeline). By default
181     `gst-env` will use all scenarious found in the
182     `prefix/share/gstreamer-1.0/validate/scenarios` directory within the parent
183     directory of `gst-env.py`.
184   * *GST_VALIDATE_PLUGIN_PATH* - List of paths to search for plugin files to
185     add to the plugin registry. The default search path is in the given build
186     directory under `subprojects/gst-devtools/validate/plugins`.
187
188 The general idea is to set up the meson build directory, build the project and
189 the switch to the development environment with `gst-env`. This creates a
190 development environment in your shell, that provides a separate set of plugins
191 and tools.
192 To check if you are in the development environment run: `echo
193 $GST_ENV`, which will be set by `gst_env` to `gst-$GST_VERSION`.
194
195 You will notice the prompt changed accordingly. You can then run any GStreamer
196 tool you just built directly (like `gst-inspect-1.0`, `gst-launch-1.0`, ...).
197
198 ### Options `gst-env`
199
200 + **builddir**
201   - By default, the script will try to find the build directory within the
202     `build` or `builddir` directory within the same folder where
203     `gst-env.py` are located. This option allows to specify
204     a different location. This might be useful when you have multiple different
205     builds but you don't want to jump between folders.
206 + **srcdir**
207   - The parent folder of `gst-env.py` is used by default.
208     This option is used to get the current branch of the repository, to fetch
209     GstValidate plugins and for gdb.
210 + **sysroot**
211   - Useful if the project was cross-compiled on another machine and mounted via
212     a network file system/ssh file system/etc. Adjusts the paths (e.g. the
213     paths found in *GST_PLUGIN_PATH*) by removing the front part of the path
214     which matches *sysroot*.
215
216   <sub>For example if your rootfs is at /srv/rootfs, then the v4l2codecs plugin
217   might be built at
218   `/srv/rootfs/home/user/gstreamer/build/subprojects/gst-plugins-bad/sys/v4l2codecs`.
219   By executing `gst-env.py --sysroot /srv/rootfs` the path will be stored
220   within *GST_PLUGIN_PATH* as:
221   `/home/user/gstreamer/build/subprojects/gst-plugins-bad/sys/v4l2codecs`.</sub>
222
223 + **wine**
224   - Extend the GST_VERSION environment variable with a specific wine command
225 + **winepath**
226   - Add additional elements to the WINEPATH variable for wine environments.
227 + **only-environment**
228   - Instead of opening a new shell environment, print the environment variables
229     that would be used.
230
231 ### Use cases
232
233 #### Setting up a development environment while keeping the distribution package
234
235 This case is very simple all you have to do is either:
236
237 - `./gst-env.py` from the project root
238 - `ninja -C build devenv` (build is the generated meson build directory)
239 - `meson devenv` from the meson build directory (e.g. `build`) within the
240   project root
241
242 #### Using GStreamer as sub project to another project
243
244 This case is very similar to the previous, the only important deviation is that
245 the file system structure is important. **`gst-env`** will look for a
246 `GstPluginPaths.json` file either within the meson build directory (e.g.
247 `build`) or within `build/subprojects/gstreamer`.
248
249 #### Cross-compiling in combination with a network share
250
251 For cross compiling in general take a look at the [meson
252 documentation](https://mesonbuild.com/Cross-compilation.html) or at projects
253 like [gst-build-sdk](https://gitlab.collabora.com/collabora/gst-build-sdk).
254
255 The basic idea is to prepare a rootfs on the cross compile host, that is
256 similar to that of target machine, prepare a
257 [cross-file.txt](https://mesonbuild.com/Cross-compilation.html#defining-the-environment),
258 build the project and export it via a [NFS mount/NFS
259 rootfs](https://wiki.archlinux.org/title/NFS)/[SSHFS](https://wiki.archlinux.org/title/SSHFS)/[Syncthing](https://wiki.archlinux.org/title/Syncthing)
260 etc.
261
262 On the target machine you then have to remove the path to the rootfs on the
263 build machine from the GStreamer paths:
264
265 - `./gst-env.py --sysroot /path/to/rootfs-on-cross-compile-host`
266
267 ## Working with multiple branches or remotes
268
269 It is not uncommon to track multiple git remote repositories (such as the
270 official upstream repositories and your personal clone on gitlab).
271
272 In the gstreamer mono repository, just add your personal git remotes as you
273 would do with any other git repository, e.g.:
274
275 ``` shell
276 git remote add personal git@gitlab.freedesktop.org:awesomehacker/gstreamer.git
277 git fetch
278 ```
279
280 In gst-build (for 1.16/1.18 branches), you can add your personal
281 git remotes in the relevant subproject directory (and that would have to be
282 done for each subproject of interest, since the old 1.16/1.18 branches live in
283 separate git repositories), e.g.:
284
285 ``` shell
286 cd subprojects/gstreamer/
287 git remote add personal git@gitlab.freedesktop.org:awesomehacker/gstreamer.git
288 git fetch
289 ```
290
291
292 ## Configuration
293
294 You can list all the available options of a `meson` project by using the
295 configure command:
296
297 ``` shell
298 meson configure
299 ```
300
301 If you have an already configured build directory, you can provide that and you
302 will additionally get the configured values for that build:
303
304 ``` shell
305 meson configure <build-directory>
306 ```
307
308 That command will list for each option:
309 * The name of the option
310 * The default (or configured) value of the option
311 * The possible values
312 * The description of that option
313
314 > The values with `auto` mean that `meson` will figure out at configuration time
315 > the proper value (for example, if you have the available development packages
316 > to build a certain plugin).
317 >
318 > You will also see values with `<inherited from main project>`. This is mostly
319 > used for options which are generic options. For example the `doc` option is
320 > present at the top-level, and also on every submodules (ex:
321 > `gstreamer:doc`). Generally you only want to set the value of that option
322 > once, and all submodules will inherit from that.
323
324 You can then provide those options to `meson` when configuring the build with
325 `-D<option_name>=<option_value>`. For example, if one does not want to build the
326 rust plugins in `gst-build` (`rs` option), you would do:
327
328 ``` shell
329 meson -Drs=disabled <build-directory>
330 ```
331
332 You can also peek at the `meson_options.txt` files and `subproject/xyz/meson_options.txt`
333 files which is where the various project specific build options are listed.
334 These do not include all the standard Meson options however.
335
336 ## Running tests
337
338 Running the unit tests is done by calling `meson test` from the build directory,
339 or `meson test -C <path/to/build_directory>`. If there are any failures you can
340 have a look at the file specified at the end or you can run `meson test
341 --print-errorlogs` which will show you the logs of the failing test after
342 execution.
343
344 You can also execute just a subset of tests by specifying the name name. For
345 example `meson test gst_gstpad`. The complete list of tests is available with
346 `meson test --list`.
347
348 If the `gst-devtools` submodule is built, you can also use
349 `gst-validate-launcher`[gst-validate] for running tests.
350
351 ``` shell
352 gst-validate-launcher check.gst*
353 ```
354
355 ## Going further
356
357 More details are available in the [GStreamer mono repo README](https://gitlab.freedesktop.org/gstreamer/gstreamer/-/blob/main/README.md)
358 or (for the older 1.16/1.18 branches) in the [gst-build documentation](https://gitlab.freedesktop.org/gstreamer/gst-build/blob/master/README.md).
359
360   [meson]: https://mesonbuild.com/
361   [ninja]: https://ninja-build.org/
362   [gstreamer]: https://gitlab.freedesktop.org/gstreamer/gstreamer/
363   [gst-build]: https://gitlab.freedesktop.org/gstreamer/gst-build/
364   [gst-validate]: https://gstreamer.freedesktop.org/documentation/gst-devtools/