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