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