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