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