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