util/vbuf: fix multidraw unrolling
[platform/upstream/mesa.git] / docs / meson.rst
1 Compilation and Installation Using Meson
2 ========================================
3
4 1. Introduction
5 ---------------
6
7 For general information about Meson see the `Meson
8 website <https://mesonbuild.com/>`__.
9
10 **Mesa's Meson build system is generally considered stable and ready for
11 production.**
12
13 .. note::
14
15    Mesa requires Meson >= 0.53.0 to build.
16
17    If your distribution doesn't have something recent enough in its
18    repositories, you can `try the methods suggested here
19    <https://mesonbuild.com/Getting-meson.html>`__ to install the
20    current version of Meson.
21
22 The Meson build of Mesa is tested on Linux, macOS, Windows, Cygwin,
23 Haiku, FreeBSD, DragonflyBSD, NetBSD, and should work on OpenBSD.
24
25 Unix-like OSes
26 ^^^^^^^^^^^^^^
27
28 If Meson is not already installed on your system, you can typically
29 install it with your package installer. For example:
30
31 .. code-block:: console
32
33    sudo apt-get install meson   # Ubuntu
34
35 or
36
37 .. code-block:: console
38
39    sudo dnf install meson   # Fedora
40
41 Some older versions of Meson do not check that they are too old and will
42 error out in odd ways.
43
44 You'll also need `Ninja <https://ninja-build.org/>`__. If it's not
45 already installed, use apt-get or dnf to install the *ninja-build*
46 package.
47
48 Windows
49 ^^^^^^^
50
51 You will need to install Python 3 and Meson as a module using pip. This
52 is because we use Python for generating code, and rely on external
53 modules (Mako). You also need pkg-config (a hard dependency of Meson),
54 Flex, and Bison. The easiest way to install everything you need is with
55 `Chocolatey <https://chocolatey.org/>`__.
56
57 .. code-block:: console
58
59    choco install python3 winflexbison pkgconfiglite
60
61 You can even use Chocolatey to install MinGW and Ninja (Ninja can be
62 used with MSVC as well)
63
64 .. code-block:: console
65
66    choco install ninja mingw
67
68 Then install Meson using pip
69
70 .. code-block:: console
71
72    py -3 -m pip install meson mako
73
74 You may need to add the Python 3 scripts directory to your path for
75 Meson.
76
77 2. Basic Usage
78 --------------
79
80 The Meson program is used to configure the source directory and
81 generates either a Ninja build file or Visual Studio® build files. The
82 latter must be enabled via the ``--backend`` switch, as Ninja is the
83 default backend on all operating systems.
84
85 Meson only supports out-of-tree builds, and must be passed a directory
86 to put built and generated sources into. We'll call that directory
87 "build" here. It's recommended to create a `separate build
88 directory <https://mesonbuild.com/Using-multiple-build-directories.html>`__
89 for each configuration you might want to use.
90
91 Basic configuration is done with:
92
93 .. code-block:: console
94
95    meson build/
96
97 This will create the build directory. If any dependencies are missing,
98 you can install them, or try to remove the dependency with a Meson
99 configuration option (see below).
100
101 To review the options which Meson chose, run:
102
103 .. code-block:: console
104
105    meson configure build/
106
107 Meson does not currently support listing configuration options before
108 running "meson build/" but this feature is being discussed upstream. For
109 now, we have a ``bin/meson-options.py`` script that prints the options
110 for you. If that script doesn't work for some reason, you can always
111 look in the
112 `meson_options.txt <https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/meson_options.txt>`__
113 file at the root of the project.
114
115 With additional arguments ``meson configure`` can be used to change
116 options for a previously configured build directory. All options passed
117 to this command are in the form ``-D "option"="value"``. For example:
118
119 .. code-block:: console
120
121    meson configure build/ -Dprefix=/tmp/install -Dglx=true
122
123 Note that options taking lists (such as ``platforms``) are `a bit more
124 complicated <https://mesonbuild.com/Build-options.html#using-build-options>`__,
125 but the simplest form compatible with Mesa options is to use a comma to
126 separate values (``-D platforms=drm,wayland``) and brackets to represent
127 an empty list (``-D platforms=[]``).
128
129 Once you've run the initial ``meson`` command successfully you can use
130 your configured backend to build the project in your build directory:
131
132 .. code-block:: console
133
134    ninja -C build/
135
136 The next step is to install the Mesa libraries, drivers, etc. This also
137 finishes up some final steps of the build process (such as creating
138 symbolic links for drivers). To install:
139
140 .. code-block:: console
141
142    ninja -C build/ install
143
144 Windows specific instructions
145 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146
147 On Windows you have a couple of choices for compilers. If you installed
148 MinGW with Chocolatey and want to use Ninja you should be able to open
149 any shell and follow the instructions above. If you want to you MSVC,
150 clang-cl, or ICL (the Intel Compiler), read on.
151
152 Both ICL and MSVC come with shell environments, the easiest way to use
153 Meson with these it to open a shell. For clang-cl you will need to open
154 an MSVC shell, and then override the compilers, either using a `native
155 file <https://mesonbuild.com/Native-environments.html>`__, or with the
156 CC and CXX environment variables.
157
158 All of these compilers are tested and work with Ninja, but if you want
159 Visual Studio integration or you just like msbuild, passing
160 ``--backend=vs`` to Meson will generate a Visual Studio solution.
161
162 3. Advanced Usage
163 -----------------
164
165 Installation Location
166 ^^^^^^^^^^^^^^^^^^^^^
167
168 Meson default to installing :file:`libGL.so` in your system's main
169 :file:`lib/` directory and DRI drivers to a :file:`dri/` subdirectory.
170
171 Developers will often want to install Mesa to a testing directory rather
172 than the system library directory. This can be done with the --prefix
173 option. For example:
174
175 .. code-block:: console
176
177    meson --prefix="${PWD}/build/install" build/
178
179 will put the final libraries and drivers into the build/install/
180 directory. Then you can set LD_LIBRARY_PATH and LIBGL_DRIVERS_PATH to
181 that location to run/test the driver.
182
183 Meson also honors ``DESTDIR`` for installs.
184
185 Compiler Options
186 ^^^^^^^^^^^^^^^^
187
188 Meson supports the common CFLAGS, CXXFLAGS, etc. environment variables
189 but their use is discouraged because of the many caveats in using them.
190
191 Instead, it is recommended to use ``-D${lang}_args`` and
192 ``-D${lang}_link_args``. Among the benefits of these options is that
193 they are guaranteed to persist across rebuilds and reconfigurations.
194
195 This example sets -fmax-errors for compiling C sources and -DMAGIC=123
196 for C++ sources:
197
198 .. code-block:: console
199
200    meson builddir/ -Dc_args=-fmax-errors=10 -Dcpp_args=-DMAGIC=123
201
202 Compiler Specification
203 ^^^^^^^^^^^^^^^^^^^^^^
204
205 Meson supports the standard CC and CXX environment variables for
206 changing the default compiler. Note that Meson does not allow changing
207 the compilers in a configured build directory so you will need to create
208 a new build dir for a different compiler.
209
210 This is an example of specifying the Clang compilers and cleaning the
211 build directory before reconfiguring with an extra C option:
212
213 .. code-block:: console
214
215    CC=clang CXX=clang++ meson build-clang
216    ninja -C build-clang
217    ninja -C build-clang clean
218    meson configure build -Dc_args="-Wno-typedef-redefinition"
219    ninja -C build-clang
220
221 The default compilers depends on your operating system. Meson supports
222 most of the popular compilers, a complete list is available
223 `here <https://mesonbuild.com/Reference-tables.html#compiler-ids>`__.
224
225 LLVM
226 ^^^^
227
228 Meson includes upstream logic to wrap llvm-config using its standard
229 dependency interface.
230
231 Meson can use CMake to find LLVM. But due to the way LLVM implements its
232 CMake finder it will only find static libraries, it will never find
233 :file:`libllvm.so`. There is also a ``-Dcmake_module_path`` option,
234 which points to the root of an alternative installation (the prefix).
235 For example:
236
237 .. code-block:: console
238
239    meson builddir -Dcmake_module_path=/home/user/mycmake/prefix
240
241 As of Meson 0.49.0 Meson also has the concept of a `"native
242 file" <https://mesonbuild.com/Native-environments.html>`__, these files
243 provide information about the native build environment (as opposed to a
244 cross build environment). They are INI formatted and can override where
245 to find llvm-config:
246
247 .. code-block:: ini
248    :caption: custom-llvm.ini
249
250    [binaries]
251    llvm-config = '/usr/local/bin/llvm/llvm-config'
252
253 Then configure Meson:
254
255 .. code-block:: console
256
257    meson builddir/ --native-file custom-llvm.ini
258
259 For selecting llvm-config for cross compiling a `"cross
260 file" <https://mesonbuild.com/Cross-compilation.html#defining-the-environment>`__
261 should be used. It uses the same format as the native file above:
262
263 .. code-block:: ini
264    :caption: cross-llvm.ini
265
266    [binaries]
267    ...
268    llvm-config = '/usr/lib/llvm-config-32'
269    cmake = '/usr/bin/cmake-for-my-arch'
270
271 Obviously, only CMake or llvm-config is required.
272
273 Then configure Meson:
274
275 .. code-block:: console
276
277    meson builddir/ --cross-file cross-llvm.ini
278
279 See the :ref:`Cross Compilation <cross-compilation>` section for more
280 information.
281
282 On Windows (and in other cases), using llvm-config or CMake may be
283 either undesirable or impossible. Meson's solution for this is a
284 `wrap <https://mesonbuild.com/Wrap-dependency-system-manual.html>`__, in
285 this case a "binary wrap". Follow the steps below:
286
287 -  Install the binaries and headers into the
288    ``$mesa_src/subprojects/llvm``
289 -  Add a :file:`meson.build` file to that directory (more on that later)
290
291 The wrap file must define the following:
292
293 -  ``dep_llvm``: a ``declare_dependency()`` object with
294    include_directories, dependencies, and version set)
295
296 It may also define:
297
298 -  ``irbuilder_h``: a ``files()`` object pointing to llvm/IR/IRBuilder.h
299 -  ``has_rtti``: a ``bool`` that declares whether LLVM was built with
300    RTTI. Defaults to true
301
302 such a :file:`meson.build` file might look like:
303
304 ::
305
306    project('llvm', ['cpp'])
307
308    cpp = meson.get_compiler('cpp')
309
310    _deps = []
311    _search = join_paths(meson.current_source_dir(), 'lib')
312    foreach d : ['libLLVMCodeGen', 'libLLVMScalarOpts', 'libLLVMAnalysis',
313                 'libLLVMTransformUtils', 'libLLVMCore', 'libLLVMX86CodeGen',
314                 'libLLVMSelectionDAG', 'libLLVMipo', 'libLLVMAsmPrinter',
315                 'libLLVMInstCombine', 'libLLVMInstrumentation', 'libLLVMMC',
316                 'libLLVMGlobalISel', 'libLLVMObjectYAML', 'libLLVMDebugInfoPDB',
317                 'libLLVMVectorize', 'libLLVMPasses', 'libLLVMSupport',
318                 'libLLVMLTO', 'libLLVMObject', 'libLLVMDebugInfoCodeView',
319                 'libLLVMDebugInfoDWARF', 'libLLVMOrcJIT', 'libLLVMProfileData',
320                 'libLLVMObjCARCOpts', 'libLLVMBitReader', 'libLLVMCoroutines',
321                 'libLLVMBitWriter', 'libLLVMRuntimeDyld', 'libLLVMMIRParser',
322                 'libLLVMX86Desc', 'libLLVMAsmParser', 'libLLVMTableGen',
323                 'libLLVMFuzzMutate', 'libLLVMLinker', 'libLLVMMCParser',
324                 'libLLVMExecutionEngine', 'libLLVMCoverage', 'libLLVMInterpreter',
325                 'libLLVMTarget', 'libLLVMX86AsmParser', 'libLLVMSymbolize',
326                 'libLLVMDebugInfoMSF', 'libLLVMMCJIT', 'libLLVMXRay',
327                 'libLLVMX86AsmPrinter', 'libLLVMX86Disassembler',
328                 'libLLVMMCDisassembler', 'libLLVMOption', 'libLLVMIRReader',
329                 'libLLVMLibDriver', 'libLLVMDlltoolDriver', 'libLLVMDemangle',
330                 'libLLVMBinaryFormat', 'libLLVMLineEditor',
331                 'libLLVMWindowsManifest', 'libLLVMX86Info', 'libLLVMX86Utils']
332      _deps += cpp.find_library(d, dirs : _search)
333    endforeach
334
335    dep_llvm = declare_dependency(
336      include_directories : include_directories('include'),
337      dependencies : _deps,
338      version : '6.0.0',
339    )
340
341    has_rtti = false
342    irbuilder_h = files('include/llvm/IR/IRBuilder.h')
343
344 It is very important that version is defined and is accurate, if it is
345 not, workarounds for the wrong version of LLVM might be used resulting
346 in build failures.
347
348 ``PKG_CONFIG_PATH``
349 ^^^^^^^^^^^^^^^^^^^
350
351 The ``pkg-config`` utility is a hard requirement for configuring and
352 building Mesa on Unix-like systems. It is used to search for external
353 libraries on the system. This environment variable is used to control
354 the search path for ``pkg-config``. For instance, setting
355 ``PKG_CONFIG_PATH=/usr/X11R6/lib/pkgconfig`` will search for package
356 metadata in ``/usr/X11R6`` before the standard directories.
357
358 Options
359 ^^^^^^^
360
361 One of the oddities of Meson is that some options are different when
362 passed to the ``meson`` than to ``meson configure``. These options are
363 passed as --option=foo to ``meson``, but -Doption=foo to
364 ``meson configure``. Mesa defined options are always passed as
365 -Doption=foo.
366
367 For those coming from Autotools be aware of the following:
368
369 ``--buildtype/-Dbuildtype``
370    This option will set the compiler debug/optimization levels to aid
371    debugging the Mesa libraries.
372
373    Note that in Meson this defaults to ``debugoptimized``, and not
374    setting it to ``release`` will yield non-optimal performance and
375    binary size. Not using ``debug`` may interfere with debugging as some
376    code and validation will be optimized away.
377
378    For those wishing to pass their own optimization flags, use the
379    ``plain`` buildtype, which causes Meson to inject no additional
380    compiler arguments, only those in the C/CXXFLAGS and those that mesa
381    itself defines.
382
383 ``-Db_ndebug``
384    This option controls assertions in Meson projects. When set to
385    ``false`` (the default) assertions are enabled, when set to true they
386    are disabled. This is unrelated to the ``buildtype``; setting the
387    latter to ``release`` will not turn off assertions.
388
389 .. _cross-compilation:
390
391 4. Cross-compilation and 32-bit builds
392 --------------------------------------
393
394 `Meson supports
395 cross-compilation <https://mesonbuild.com/Cross-compilation.html>`__ by
396 specifying a number of binary paths and settings in a file and passing
397 this file to ``meson`` or ``meson configure`` with the ``--cross-file``
398 parameter.
399
400 This file can live at any location, but you can use the bare filename
401 (without the folder path) if you put it in
402 :file:`$XDG_DATA_HOME/meson/cross` or :file:`~/.local/share/meson/cross`
403
404 Below are a few example of cross files, but keep in mind that you will
405 likely have to alter them for your system.
406
407 Those running on Arch Linux can use the AUR-maintained packages for some
408 of those, as they'll have the right values for your system:
409
410 -  `meson-cross-x86-linux-gnu <https://aur.archlinux.org/packages/meson-cross-x86-linux-gnu>`__
411 -  `meson-cross-aarch64-linux-gnu <https://aur.archlinux.org/packages/meson-cross-aarch64-linux-gnu>`__
412
413 32-bit build on x86 linux:
414
415 .. code-block:: ini
416
417    [binaries]
418    c = '/usr/bin/gcc'
419    cpp = '/usr/bin/g++'
420    ar = '/usr/bin/gcc-ar'
421    strip = '/usr/bin/strip'
422    pkgconfig = '/usr/bin/pkg-config-32'
423    llvm-config = '/usr/bin/llvm-config32'
424
425    [properties]
426    c_args = ['-m32']
427    c_link_args = ['-m32']
428    cpp_args = ['-m32']
429    cpp_link_args = ['-m32']
430
431    [host_machine]
432    system = 'linux'
433    cpu_family = 'x86'
434    cpu = 'i686'
435    endian = 'little'
436
437 64-bit build on ARM linux:
438
439 .. code-block:: ini
440
441    [binaries]
442    c = '/usr/bin/aarch64-linux-gnu-gcc'
443    cpp = '/usr/bin/aarch64-linux-gnu-g++'
444    ar = '/usr/bin/aarch64-linux-gnu-gcc-ar'
445    strip = '/usr/bin/aarch64-linux-gnu-strip'
446    pkgconfig = '/usr/bin/aarch64-linux-gnu-pkg-config'
447    exe_wrapper = '/usr/bin/qemu-aarch64-static'
448
449    [host_machine]
450    system = 'linux'
451    cpu_family = 'aarch64'
452    cpu = 'aarch64'
453    endian = 'little'
454
455 64-bit build on x86 Windows:
456
457 .. code-block:: ini
458
459    [binaries]
460    c = '/usr/bin/x86_64-w64-mingw32-gcc'
461    cpp = '/usr/bin/x86_64-w64-mingw32-g++'
462    ar = '/usr/bin/x86_64-w64-mingw32-ar'
463    strip = '/usr/bin/x86_64-w64-mingw32-strip'
464    pkgconfig = '/usr/bin/x86_64-w64-mingw32-pkg-config'
465    exe_wrapper = 'wine'
466
467    [host_machine]
468    system = 'windows'
469    cpu_family = 'x86_64'
470    cpu = 'i686'
471    endian = 'little'