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