meson: Don't check for posix_memalign on windows
[platform/upstream/mesa.git] / meson.build
1 # Copyright © 2017-2019 Intel Corporation
2
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 # SOFTWARE.
20
21 project(
22   'mesa',
23   ['c', 'cpp'],
24   version : run_command(
25     [find_program('python', 'python2', 'python3'), 'bin/meson_get_version.py']
26   ).stdout(),
27   license : 'MIT',
28   meson_version : '>= 0.46',
29   default_options : ['buildtype=debugoptimized', 'b_ndebug=if-release', 'c_std=c99', 'cpp_std=c++14']
30 )
31
32 cc = meson.get_compiler('c')
33 cpp = meson.get_compiler('cpp')
34
35 null_dep = dependency('', required : false)
36
37 # Arguments for the preprocessor, put these in a separate array from the C and
38 # C++ (cpp in meson terminology) arguments since they need to be added to the
39 # default arguments for both C and C++.
40 pre_args = [
41   '-D__STDC_CONSTANT_MACROS',
42   '-D__STDC_FORMAT_MACROS',
43   '-D__STDC_LIMIT_MACROS',
44   '-DPACKAGE_VERSION="@0@"'.format(meson.project_version()),
45   '-DPACKAGE_BUGREPORT="https://gitlab.freedesktop.org/mesa/mesa/issues"',
46 ]
47
48 with_vulkan_icd_dir = get_option('vulkan-icd-dir')
49 with_tests = get_option('build-tests')
50 with_valgrind = get_option('valgrind')
51 with_libunwind = get_option('libunwind')
52 with_glx_read_only_text = get_option('glx-read-only-text')
53 with_glx_direct = get_option('glx-direct')
54 with_osmesa = get_option('osmesa')
55 with_swr_arches = get_option('swr-arches')
56 with_tools = get_option('tools')
57 if with_tools.contains('all')
58   with_tools = [
59     'drm-shim',
60     'etnaviv',
61     'freedreno',
62     'glsl',
63     'intel',
64     'intel-ui',
65     'lima',
66     'nir',
67     'nouveau',
68     'xvmc',
69   ]
70 endif
71
72 dri_drivers_path = get_option('dri-drivers-path')
73 if dri_drivers_path == ''
74   dri_drivers_path = join_paths(get_option('prefix'), get_option('libdir'), 'dri')
75 endif
76 dri_search_path = get_option('dri-search-path')
77 if dri_search_path == ''
78   dri_search_path = dri_drivers_path
79 endif
80
81 with_gles1 = get_option('gles1')
82 with_gles2 = get_option('gles2')
83 if host_machine.system() == 'windows'
84   if with_gles1 == 'auto'
85     with_gles1 = 'false'
86   endif
87   if with_gles2 == 'auto'
88     with_gles2 = 'false'
89   endif
90 endif
91 with_opengl = get_option('opengl')
92
93 # Default shared glapi off for windows, on elsewhere.
94 _sg = get_option('shared-glapi')
95 if _sg == 'auto'
96   with_shared_glapi = host_machine.system() != 'windows'
97 else
98   with_shared_glapi = _sg == 'true'
99 endif
100
101 # shared-glapi is required if at least two OpenGL APIs are being built
102 if not with_shared_glapi
103   if ((with_gles1 == 'true' and with_gles2 == 'true') or
104       (with_gles1 == 'true' and with_opengl) or
105       (with_gles2 == 'true' and with_opengl))
106     error('shared-glapi required for building two or more of OpenGL, OpenGL ES 1.x, OpenGL ES 2.x')
107   endif
108   with_gles1 = 'false'
109   with_gles2 = 'false'
110 endif
111
112 # We require OpenGL for OpenGL ES
113 if not with_opengl
114   if (with_gles1 == 'true' or with_gles2 == 'true') and not with_opengl
115     error('building OpenGL ES without OpenGL is not supported.')
116   endif
117   with_gles1 = 'false'
118   with_gles2 = 'false'
119 endif
120
121 with_gles1 = with_gles1 != 'false'
122 with_gles2 = with_gles2 != 'false'
123 with_any_opengl = with_opengl or with_gles1 or with_gles2
124 # Only build shared_glapi if at least one OpenGL API is enabled
125 with_shared_glapi = with_shared_glapi and with_any_opengl
126
127 system_has_kms_drm = ['openbsd', 'netbsd', 'freebsd', 'gnu/kfreebsd', 'dragonfly', 'linux'].contains(host_machine.system())
128
129 dri_drivers = get_option('dri-drivers')
130 if dri_drivers.contains('auto')
131   if system_has_kms_drm
132     # TODO: PPC, Sparc
133     if ['x86', 'x86_64'].contains(host_machine.cpu_family())
134       dri_drivers = ['i915', 'i965', 'r100', 'r200', 'nouveau']
135     elif ['arm', 'aarch64'].contains(host_machine.cpu_family())
136       dri_drivers = []
137     else
138       error('Unknown architecture @0@. Please pass -Ddri-drivers to set driver options. Patches gladly accepted to fix this.'.format(
139             host_machine.cpu_family()))
140     endif
141   elif ['darwin', 'windows', 'cygwin', 'haiku'].contains(host_machine.system())
142     # only swrast would make sense here, but gallium swrast is a much better default
143     dri_drivers = []
144   else
145     error('Unknown OS @0@. Please pass -Ddri-drivers to set driver options. Patches gladly accepted to fix this.'.format(
146           host_machine.system()))
147   endif
148 endif
149
150 with_dri_i915 = dri_drivers.contains('i915')
151 with_dri_i965 = dri_drivers.contains('i965')
152 with_dri_r100 = dri_drivers.contains('r100')
153 with_dri_r200 = dri_drivers.contains('r200')
154 with_dri_nouveau = dri_drivers.contains('nouveau')
155 with_dri_swrast = dri_drivers.contains('swrast')
156
157 with_dri = dri_drivers.length() != 0 and dri_drivers != ['']
158
159 gallium_drivers = get_option('gallium-drivers')
160 if gallium_drivers.contains('auto')
161   if system_has_kms_drm
162     # TODO: PPC, Sparc
163     if ['x86', 'x86_64'].contains(host_machine.cpu_family())
164       gallium_drivers = [
165         'r300', 'r600', 'radeonsi', 'nouveau', 'virgl', 'svga', 'swrast'
166       ]
167     elif ['arm', 'aarch64'].contains(host_machine.cpu_family())
168       gallium_drivers = [
169         'kmsro', 'v3d', 'vc4', 'freedreno', 'etnaviv', 'nouveau',
170         'tegra', 'virgl', 'lima', 'panfrost', 'swrast'
171       ]
172     else
173       error('Unknown architecture @0@. Please pass -Dgallium-drivers to set driver options. Patches gladly accepted to fix this.'.format(
174             host_machine.cpu_family()))
175     endif
176   elif ['darwin', 'windows', 'cygwin', 'haiku'].contains(host_machine.system())
177     gallium_drivers = ['swrast']
178   else
179     error('Unknown OS @0@. Please pass -Dgallium-drivers to set driver options. Patches gladly accepted to fix this.'.format(
180           host_machine.system()))
181   endif
182 endif
183 with_gallium_kmsro = gallium_drivers.contains('kmsro')
184 with_gallium_radeonsi = gallium_drivers.contains('radeonsi')
185 with_gallium_r300 = gallium_drivers.contains('r300')
186 with_gallium_r600 = gallium_drivers.contains('r600')
187 with_gallium_nouveau = gallium_drivers.contains('nouveau')
188 with_gallium_freedreno = gallium_drivers.contains('freedreno')
189 with_gallium_softpipe = gallium_drivers.contains('swrast')
190 with_gallium_vc4 = gallium_drivers.contains('vc4')
191 with_gallium_v3d = gallium_drivers.contains('v3d')
192 with_gallium_panfrost = gallium_drivers.contains('panfrost')
193 with_gallium_etnaviv = gallium_drivers.contains('etnaviv')
194 with_gallium_tegra = gallium_drivers.contains('tegra')
195 with_gallium_iris = gallium_drivers.contains('iris')
196 with_gallium_i915 = gallium_drivers.contains('i915')
197 with_gallium_svga = gallium_drivers.contains('svga')
198 with_gallium_virgl = gallium_drivers.contains('virgl')
199 with_gallium_swr = gallium_drivers.contains('swr')
200 with_gallium_lima = gallium_drivers.contains('lima')
201
202 if cc.get_id() == 'intel'
203   if meson.version().version_compare('< 0.49.0')
204     error('Meson does not have sufficient support of ICC before 0.49.0 to compile mesa')
205   elif with_gallium_swr and meson.version().version_compare('== 0.49.0')
206     warning('Meson as of 0.49.0 is sufficient for compiling mesa with ICC, but there are some caveats with SWR. 0.49.1 should resolve all of these')
207   endif
208 endif
209
210 with_gallium = gallium_drivers.length() != 0 and gallium_drivers != ['']
211
212 if with_gallium and system_has_kms_drm
213   _glx = get_option('glx')
214   _egl = get_option('egl')
215   if _glx == 'dri' or _egl == 'true' or (_glx == 'disabled' and _egl != 'false')
216     with_dri = true
217   endif
218 endif
219
220 _vulkan_drivers = get_option('vulkan-drivers')
221 if _vulkan_drivers.contains('auto')
222   if system_has_kms_drm
223     if host_machine.cpu_family().startswith('x86')
224       _vulkan_drivers = ['amd', 'intel']
225     elif ['arm', 'aarch64'].contains(host_machine.cpu_family())
226       _vulkan_drivers = []
227     else
228       error('Unknown architecture @0@. Please pass -Dvulkan-drivers to set driver options. Patches gladly accepted to fix this.'.format(
229             host_machine.cpu_family()))
230     endif
231   elif ['darwin', 'windows', 'cygwin', 'haiku'].contains(host_machine.system())
232     # No vulkan driver supports windows or macOS currently
233     _vulkan_drivers = []
234   else
235     error('Unknown OS @0@. Please pass -Dvulkan-drivers to set driver options. Patches gladly accepted to fix this.'.format(
236           host_machine.system()))
237   endif
238 endif
239
240 with_intel_vk = _vulkan_drivers.contains('intel')
241 with_amd_vk = _vulkan_drivers.contains('amd')
242 with_freedreno_vk = _vulkan_drivers.contains('freedreno')
243 with_any_vk = _vulkan_drivers.length() != 0 and _vulkan_drivers != ['']
244
245 if with_freedreno_vk and get_option('I-love-half-baked-turnips') != true
246   error('Cannot enable freedreno vulkan driver')
247 endif
248
249 if with_dri_swrast and (with_gallium_softpipe or with_gallium_swr)
250   error('Only one swrast provider can be built')
251 endif
252 if with_dri_i915 and with_gallium_i915
253   error('Only one i915 provider can be built')
254 endif
255 if with_gallium_kmsro and not (with_gallium_v3d or with_gallium_vc4 or with_gallium_etnaviv or with_gallium_freedreno or with_gallium_panfrost or with_gallium_lima)
256   error('kmsro driver requires one or more renderonly drivers (vc4, etnaviv, freedreno, panfrost, lima)')
257 endif
258 if with_gallium_tegra and not with_gallium_nouveau
259   error('tegra driver requires nouveau driver')
260 endif
261
262 if host_machine.system() == 'darwin'
263   with_dri_platform = 'apple'
264   pre_args += '-DBUILDING_MESA'
265 elif ['windows', 'cygwin'].contains(host_machine.system())
266   with_dri_platform = 'windows'
267 elif system_has_kms_drm
268   with_dri_platform = 'drm'
269 else
270   # FIXME: haiku doesn't use dri, and xlib doesn't use dri, probably should
271   # assert here that one of those cases has been met.
272   # FIXME: illumos ends up here as well
273   with_dri_platform = 'none'
274 endif
275
276 _platforms = get_option('platforms')
277 if _platforms.contains('auto')
278   if system_has_kms_drm
279     _platforms = ['x11', 'wayland', 'drm', 'surfaceless']
280   elif ['darwin', 'cygwin'].contains(host_machine.system())
281     _platforms = ['x11', 'surfaceless']
282   elif ['haiku'].contains(host_machine.system())
283     _platforms = ['haiku']
284   elif host_machine.system() == 'windows'
285     _platforms = ['windows']
286   else
287     error('Unknown OS @0@. Please pass -Dplatforms to set platforms. Patches gladly accepted to fix this.'.format(
288           host_machine.system()))
289   endif
290 endif
291
292 with_platform_android = _platforms.contains('android')
293 with_platform_x11 = _platforms.contains('x11')
294 with_platform_wayland = _platforms.contains('wayland')
295 with_platform_drm = _platforms.contains('drm')
296 with_platform_haiku = _platforms.contains('haiku')
297 with_platform_surfaceless = _platforms.contains('surfaceless')
298 with_platform_windows = _platforms.contains('windows')
299
300 with_platforms = false
301 if _platforms.length() != 0 and _platforms != ['']
302   # sanity check that list contains no empty strings
303   if _platforms.contains('')
304     error('Invalid argument list given to -Dplatforms, please fix.')
305   endif
306   with_platforms = true
307   egl_native_platform = _platforms[0]
308 endif
309
310 _xlib_lease = get_option('xlib-lease')
311 if _xlib_lease == 'auto'
312   with_xlib_lease = with_platform_x11 and with_platform_drm
313 else
314   with_xlib_lease = _xlib_lease == 'true'
315 endif
316
317 with_glx = get_option('glx')
318 if with_glx == 'auto'
319   if with_dri
320     with_glx = 'dri'
321   elif with_platform_haiku
322     with_glx = 'disabled'
323   elif host_machine.system() == 'windows'
324     with_glx = 'disabled'
325   elif with_gallium
326     # Even when building just gallium drivers the user probably wants dri
327     with_glx = 'dri'
328   elif with_platform_x11 and with_any_opengl and not with_any_vk
329     # The automatic behavior should not be to turn on xlib based glx when
330     # building only vulkan drivers
331     with_glx = 'xlib'
332   else
333     with_glx = 'disabled'
334   endif
335 endif
336 if with_glx == 'dri'
337    if with_gallium
338       with_dri = true
339    endif
340 endif
341
342 if not (with_dri or with_gallium or with_glx != 'disabled')
343   with_gles1 = false
344   with_gles2 = false
345   with_opengl = false
346   with_any_opengl = false
347   with_shared_glapi = false
348 endif
349
350 _gbm = get_option('gbm')
351 if _gbm == 'auto'
352   with_gbm = system_has_kms_drm and with_dri
353 else
354   with_gbm = _gbm == 'true'
355 endif
356 if with_gbm and not system_has_kms_drm
357   error('GBM only supports DRM/KMS platforms')
358 endif
359
360 _egl = get_option('egl')
361 if _egl == 'auto'
362   with_egl = (
363     not ['darwin', 'windows'].contains(host_machine.system()) and
364     with_dri and with_shared_glapi and with_platforms
365   )
366 elif _egl == 'true'
367   if not with_dri
368     error('EGL requires dri')
369   elif not with_shared_glapi
370     error('EGL requires shared-glapi')
371   elif not with_platforms
372     error('No platforms specified, consider -Dplatforms=drm,x11,surfaceless at least')
373   elif not ['disabled', 'dri'].contains(with_glx)
374     error('EGL requires dri, but a GLX is being built without dri')
375   elif ['darwin', 'windows'].contains(host_machine.system())
376     error('EGL is not available on Windows or MacOS')
377   endif
378   with_egl = true
379 else
380   with_egl = false
381 endif
382
383 if with_egl and not (with_platform_drm or with_platform_surfaceless or with_platform_android)
384   if with_gallium_radeonsi
385     error('RadeonSI requires the drm, surfaceless or android platform when using EGL')
386   endif
387   if with_gallium_virgl
388     error('Virgl requires the drm, surfaceless or android platform when using EGL')
389   endif
390 endif
391
392 # Android uses emutls for versions <= P/28. For USE_ELF_TLS we need ELF TLS.
393 if not with_platform_android or get_option('platform-sdk-version') >= 29
394   pre_args += '-DUSE_ELF_TLS'
395 endif
396
397 if with_glx != 'disabled'
398   if not (with_platform_x11 and with_any_opengl)
399     error('Cannot build GLX support without X11 platform support and at least one OpenGL API')
400   elif with_glx == 'gallium-xlib'
401     if not with_gallium
402       error('Gallium-xlib based GLX requires at least one gallium driver')
403     elif not with_gallium_softpipe
404       error('Gallium-xlib based GLX requires softpipe or llvmpipe.')
405     elif with_dri
406       error('gallium-xlib conflicts with any dri driver')
407     endif
408   elif with_glx == 'xlib'
409     if with_dri
410       error('xlib conflicts with any dri driver')
411     endif
412   elif with_glx == 'dri'
413     if not with_shared_glapi
414       error('dri based GLX requires shared-glapi')
415     endif
416   endif
417 endif
418
419 with_glvnd = get_option('glvnd')
420 if with_glvnd
421   if with_platform_windows
422     error('glvnd cannot be used on Windows')
423   elif with_glx == 'xlib' or with_glx == 'gallium-xlib'
424     error('Cannot build glvnd support for GLX that is not DRI based.')
425   elif with_glx == 'disabled' and not with_egl
426     error('glvnd requires DRI based GLX and/or EGL')
427   endif
428   if get_option('egl-lib-suffix') != ''
429     error('''EGL lib suffix can't be used with libglvnd''')
430   endif
431 endif
432
433 if with_vulkan_icd_dir == ''
434   with_vulkan_icd_dir = join_paths(get_option('datadir'), 'vulkan/icd.d')
435 endif
436
437 with_dri2 = (with_dri or with_any_vk) and (with_dri_platform == 'drm' or
438   host_machine.system() == 'gnu')
439 _dri3 = get_option('dri3')
440 if _dri3 == 'auto'
441   with_dri3 = system_has_kms_drm and with_dri2
442 else
443   with_dri3 = _dri3 == 'true'
444 endif
445
446 if with_any_vk and (with_platform_x11 and not with_dri3)
447   error('Vulkan drivers require dri3 for X11 support')
448 endif
449 if with_dri
450   if with_glx == 'disabled' and not with_egl and not with_gbm and with_osmesa != 'classic'
451     error('building dri drivers require at least one windowing system or classic osmesa')
452   endif
453 endif
454
455 prog_pkgconfig = find_program('pkg-config')
456
457 _vdpau = get_option('gallium-vdpau')
458 if not system_has_kms_drm
459   if _vdpau == 'true'
460     error('VDPAU state tracker can only be build on unix-like OSes.')
461   else
462     _vdpau = 'false'
463   endif
464 elif not with_platform_x11
465   if _vdpau == 'true'
466     error('VDPAU state tracker requires X11 support.')
467   else
468     _vdpau = 'false'
469   endif
470 elif not (with_gallium_r300 or with_gallium_r600 or with_gallium_radeonsi or
471           with_gallium_nouveau)
472   if _vdpau == 'true'
473     error('VDPAU state tracker requires at least one of the following gallium drivers: r300, r600, radeonsi, nouveau.')
474   else
475     _vdpau = 'false'
476   endif
477 endif
478 dep_vdpau = null_dep
479 with_gallium_vdpau = false
480 if _vdpau != 'false'
481   dep_vdpau = dependency('vdpau', version : '>= 1.1', required : _vdpau == 'true')
482   if dep_vdpau.found()
483     dep_vdpau = declare_dependency(
484       compile_args : run_command(prog_pkgconfig, ['vdpau', '--cflags']).stdout().split()
485     )
486     with_gallium_vdpau = true
487   endif
488 endif
489
490 if with_gallium_vdpau
491   pre_args += '-DHAVE_ST_VDPAU'
492 endif
493 vdpau_drivers_path = get_option('vdpau-libs-path')
494 if vdpau_drivers_path == ''
495   vdpau_drivers_path = join_paths(get_option('libdir'), 'vdpau')
496 endif
497
498 _xvmc = get_option('gallium-xvmc')
499 if not system_has_kms_drm
500   if _xvmc == 'true'
501     error('XVMC state tracker can only be build on unix-like OSes.')
502   else
503     _xvmc = 'false'
504   endif
505 elif not with_platform_x11
506   if _xvmc == 'true'
507     error('XVMC state tracker requires X11 support.')
508   else
509     _xvmc = 'false'
510   endif
511 elif not (with_gallium_r600 or with_gallium_nouveau)
512   if _xvmc == 'true'
513     error('XVMC state tracker requires at least one of the following gallium drivers: r600, nouveau.')
514   else
515     _xvmc = 'false'
516   endif
517 endif
518 dep_xvmc = null_dep
519 dep_xv = null_dep
520 with_gallium_xvmc = false
521 if _xvmc != 'false'
522   dep_xvmc = dependency('xvmc', version : '>= 1.0.6', required : _xvmc == 'true')
523   dep_xv = dependency('xv', required : _xvmc == 'true')
524   with_gallium_xvmc = dep_xvmc.found() and dep_xv.found()
525 endif
526
527 xvmc_drivers_path = get_option('xvmc-libs-path')
528 if xvmc_drivers_path == ''
529   xvmc_drivers_path = get_option('libdir')
530 endif
531
532 _omx = get_option('gallium-omx')
533 if not system_has_kms_drm
534   if ['auto', 'disabled'].contains(_omx)
535     _omx = 'disabled'
536   else
537     error('OMX state tracker can only be built on unix-like OSes.')
538   endif
539 elif not (with_gallium_r600 or with_gallium_radeonsi or with_gallium_nouveau)
540   if ['auto', 'disabled'].contains(_omx)
541     _omx = 'disabled'
542   else
543     error('OMX state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau.')
544   endif
545 endif
546 with_gallium_omx = _omx
547 dep_omx = null_dep
548 dep_omx_other = []
549 if ['auto', 'bellagio'].contains(_omx)
550   dep_omx = dependency(
551     'libomxil-bellagio', required : _omx == 'bellagio'
552   )
553   if dep_omx.found()
554     with_gallium_omx = 'bellagio'
555   endif
556 endif
557 if ['auto', 'tizonia'].contains(_omx)
558   if with_dri and with_egl
559     dep_omx = dependency(
560       'libtizonia', version : '>= 0.10.0',
561       required : _omx == 'tizonia',
562     )
563     dep_omx_other = [
564       dependency('libtizplatform', required : _omx == 'tizonia'),
565       dependency('tizilheaders', required : _omx == 'tizonia'),
566     ]
567     if dep_omx.found() and dep_omx_other[0].found() and dep_omx_other[1].found()
568       with_gallium_omx = 'tizonia'
569     endif
570   elif _omx == 'tizonia'
571     error('OMX-Tizonia state tracker requires dri and egl')
572   endif
573 endif
574 if _omx == 'auto'
575   with_gallium_omx = 'disabled'
576 else
577   with_gallium_omx = _omx
578 endif
579
580 pre_args += [
581   '-DENABLE_ST_OMX_BELLAGIO=' + (with_gallium_omx == 'bellagio' ? '1' : '0'),
582   '-DENABLE_ST_OMX_TIZONIA=' + (with_gallium_omx == 'tizonia' ? '1' : '0'),
583 ]
584
585
586 omx_drivers_path = get_option('omx-libs-path')
587
588 if with_gallium_omx != 'disabled'
589   # Figure out where to put the omx driver.
590   # FIXME: this could all be vastly simplified by adding a 'defined_variable'
591   # argument to meson's get_pkgconfig_variable method.
592   if omx_drivers_path == ''
593     _omx_libdir = dep_omx.get_pkgconfig_variable('libdir')
594     _omx_drivers_dir = dep_omx.get_pkgconfig_variable('pluginsdir')
595     if _omx_libdir == get_option('libdir')
596       omx_drivers_path = _omx_drivers_dir
597     else
598       _omx_base_dir = []
599       # This will fail on windows. Does OMX run on windows?
600       _omx_libdir = _omx_libdir.split('/')
601       _omx_drivers_dir = _omx_drivers_dir.split('/')
602       foreach o : _omx_drivers_dir
603         if not _omx_libdir.contains(o)
604           _omx_base_dir += o
605         endif
606       endforeach
607       omx_drivers_path = join_paths(get_option('libdir'), _omx_base_dir)
608     endif
609   endif
610 endif
611
612 _va = get_option('gallium-va')
613 if not system_has_kms_drm
614   if _va == 'true'
615     error('VA state tracker can only be built on unix-like OSes.')
616   else
617     _va = 'false'
618   endif
619 elif not (with_gallium_r600 or with_gallium_radeonsi or with_gallium_nouveau)
620   if _va == 'true'
621     error('VA state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau.')
622   else
623     _va = 'false'
624   endif
625 endif
626 with_gallium_va = false
627 dep_va = null_dep
628 if _va != 'false'
629   dep_va = dependency('libva', version : '>= 0.38.0', required : _va == 'true')
630   if dep_va.found()
631     dep_va_headers = declare_dependency(
632       compile_args : run_command(prog_pkgconfig, ['libva', '--cflags']).stdout().split()
633     )
634     with_gallium_va = true
635   endif
636 endif
637
638 va_drivers_path = get_option('va-libs-path')
639 if va_drivers_path == ''
640   va_drivers_path = join_paths(get_option('libdir'), 'dri')
641 endif
642
643 _xa = get_option('gallium-xa')
644 if not system_has_kms_drm
645   if _xa == 'true'
646     error('XA state tracker can only be built on unix-like OSes.')
647   else
648     _xa = 'false'
649   endif
650 elif not (with_gallium_nouveau or with_gallium_freedreno or with_gallium_i915
651           or with_gallium_svga)
652   if _xa == 'true'
653     error('XA state tracker requires at least one of the following gallium drivers: nouveau, freedreno, i915, svga.')
654   else
655     _xa = 'false'
656   endif
657 endif
658 with_gallium_xa = _xa != 'false'
659
660 d3d_drivers_path = get_option('d3d-drivers-path')
661 if d3d_drivers_path == ''
662   d3d_drivers_path = join_paths(get_option('prefix'), get_option('libdir'), 'd3d')
663 endif
664
665 with_gallium_st_nine =  get_option('gallium-nine')
666 if with_gallium_st_nine
667   if not with_gallium_softpipe
668     error('The nine state tracker requires gallium softpipe/llvmpipe.')
669   elif not (with_gallium_radeonsi or with_gallium_nouveau or with_gallium_r600
670             or with_gallium_r300 or with_gallium_svga or with_gallium_i915
671             or with_gallium_iris)
672     error('The nine state tracker requires at least one non-swrast gallium driver.')
673   endif
674   if not with_dri3
675     error('Using nine with wine requires dri3')
676   endif
677 endif
678
679 if get_option('power8') != 'false'
680   # on old versions of meson the cpu family would return as ppc64le on little
681   # endian power8, this was changed in 0.48 such that the family would always
682   # be ppc64 regardless of endianness, and then the machine.endian() value
683   # should be checked. Since we support versions < 0.48 we need to use
684   # startswith.
685   if host_machine.cpu_family().startswith('ppc64') and host_machine.endian() == 'little'
686     if cc.get_id() == 'gcc' and cc.version().version_compare('< 4.8')
687       error('Altivec is not supported with gcc version < 4.8.')
688     endif
689     if cc.compiles('''
690         #include <altivec.h>
691         int main() {
692           vector unsigned char r;
693           vector unsigned int v = vec_splat_u32 (1);
694           r = __builtin_vec_vgbbd ((vector unsigned char) v);
695           return 0;
696         }''',
697         args : '-mpower8-vector',
698         name : 'POWER8 intrinsics')
699       pre_args += ['-D_ARCH_PWR8', '-mpower8-vector']
700     elif get_option('power8') == 'true'
701       error('POWER8 intrinsic support required but not found.')
702     endif
703   endif
704 endif
705
706 _opencl = get_option('gallium-opencl')
707 clover_cpp_std = []
708 if _opencl != 'disabled'
709   if not with_gallium
710     error('OpenCL Clover implementation requires at least one gallium driver.')
711   endif
712
713   dep_clc = dependency('libclc')
714   with_gallium_opencl = true
715   with_opencl_icd = _opencl == 'icd'
716
717   with_opencl_spirv = get_option('opencl-spirv')
718   if with_opencl_spirv
719     dep_spirv_tools = dependency('SPIRV-Tools', required : true, version : '>= 2018.0')
720     # LLVMSPIRVLib is available at https://github.com/KhronosGroup/SPIRV-LLVM-Translator
721     dep_llvmspirvlib = dependency('LLVMSPIRVLib', required : true, version : '>= 0.2.1')
722   else
723     dep_spirv_tools = null_dep
724     dep_llvmspirvlib = null_dep
725   endif
726
727   if host_machine.cpu_family().startswith('ppc') and cpp.compiles('''
728       #if !defined(__VEC__) || !defined(__ALTIVEC__)
729       #error "AltiVec not enabled"
730       #endif''',
731       name : 'Altivec')
732     clover_cpp_std += ['cpp_std=gnu++11']
733   endif
734 else
735   dep_clc = null_dep
736   dep_spirv_tools = null_dep
737   dep_llvmspirvlib = null_dep
738   with_gallium_opencl = false
739   with_opencl_icd = false
740   with_opencl_spirv = false
741 endif
742
743 gl_pkgconfig_c_flags = []
744 if with_platform_x11
745   if with_any_vk or with_egl or (with_glx == 'dri' and with_dri_platform == 'drm')
746     pre_args += '-DHAVE_X11_PLATFORM'
747   endif
748   if with_glx == 'xlib' or with_glx == 'gallium-xlib'
749     pre_args += '-DUSE_XSHM'
750   else
751     pre_args += '-DGLX_INDIRECT_RENDERING'
752     if with_glx_direct
753       pre_args += '-DGLX_DIRECT_RENDERING'
754     endif
755     if with_dri_platform == 'drm'
756       pre_args += '-DGLX_USE_DRM'
757     elif with_dri_platform == 'apple'
758       pre_args += '-DGLX_USE_APPLEGL'
759     elif with_dri_platform == 'windows'
760       pre_args += '-DGLX_USE_WINDOWSGL'
761     endif
762   endif
763 else
764   pre_args += '-DEGL_NO_X11'
765   gl_pkgconfig_c_flags += '-DEGL_NO_X11'
766 endif
767 if with_platform_drm
768   if with_egl and not with_gbm
769     error('EGL drm platform requires gbm')
770   endif
771   pre_args += '-DHAVE_DRM_PLATFORM'
772 endif
773 if with_platform_surfaceless
774   pre_args += '-DHAVE_SURFACELESS_PLATFORM'
775 endif
776 if with_platform_android
777   dep_android = [
778     dependency('cutils'),
779     dependency('hardware'),
780     dependency('sync'),
781   ]
782   if get_option('platform-sdk-version') >= 26
783     dep_android += dependency('nativewindow')
784   endif
785   pre_args += '-DHAVE_ANDROID_PLATFORM'
786 endif
787 if with_platform_haiku
788   pre_args += '-DHAVE_HAIKU_PLATFORM'
789 endif
790
791 if meson.version().version_compare('>=0.50')
792   prog_python = import('python').find_installation('python3')
793 else
794   prog_python = import('python3').find_python()
795 endif
796 has_mako = run_command(
797   prog_python, '-c',
798   '''
799 from distutils.version import StrictVersion
800 import mako
801 assert StrictVersion(mako.__version__) > StrictVersion("0.8.0")
802   ''')
803 if has_mako.returncode() != 0
804   error('Python (3.x) mako module >= 0.8.0 required to build mesa.')
805 endif
806
807 if cc.get_id() == 'gcc' and cc.version().version_compare('< 4.4.6')
808   error('When using GCC, version 4.4.6 or later is required.')
809 endif
810
811 # Support systems without ETIME (e.g. FreeBSD)
812 if cc.get_define('ETIME', prefix : '#include <errno.h>') == ''
813   pre_args += '-DETIME=ETIMEDOUT'
814 endif
815
816 # Define DEBUG for debug builds only (debugoptimized is not included on this one)
817 if get_option('buildtype') == 'debug'
818   pre_args += '-DDEBUG'
819 endif
820
821 with_shader_cache = false
822 _shader_cache = get_option('shader-cache')
823 if _shader_cache != 'false'
824   if host_machine.system() == 'windows'
825     if _shader_cache == 'true'
826       error('Shader Cache does not currently work on Windows')
827     endif
828   else
829     pre_args += '-DENABLE_SHADER_CACHE'
830     with_shader_cache = true
831   endif
832 endif
833 if with_amd_vk and not with_shader_cache
834   error('Radv requires shader cache support')
835 endif
836
837 # Check for GCC style builtins
838 foreach b : ['bswap32', 'bswap64', 'clz', 'clzll', 'ctz', 'expect', 'ffs',
839              'ffsll', 'popcount', 'popcountll', 'unreachable']
840   if cc.has_function(b)
841     pre_args += '-DHAVE___BUILTIN_@0@'.format(b.to_upper())
842   endif
843 endforeach
844
845 # check for GCC __attribute__
846 foreach a : ['const', 'flatten', 'malloc', 'pure', 'unused',
847              'warn_unused_result', 'weak',]
848   if cc.compiles('int foo(void) __attribute__((@0@));'.format(a),
849                  name : '__attribute__((@0@))'.format(a))
850     pre_args += '-DHAVE_FUNC_ATTRIBUTE_@0@'.format(a.to_upper())
851   endif
852 endforeach
853 if cc.compiles('int foo(const char *p, ...) __attribute__((format(printf, 1, 2)));',
854                name : '__attribute__((format(...)))')
855   pre_args += '-DHAVE_FUNC_ATTRIBUTE_FORMAT'
856 endif
857 if cc.compiles('struct __attribute__((packed)) foo { int bar; };',
858                name : '__attribute__((packed))')
859   pre_args += '-DHAVE_FUNC_ATTRIBUTE_PACKED'
860 endif
861 if cc.compiles('int *foo(void) __attribute__((returns_nonnull));',
862                name : '__attribute__((returns_nonnull))')
863   pre_args += '-DHAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL'
864 endif
865 if cc.compiles('''int foo_def(void) __attribute__((visibility("default")));
866                   int foo_hid(void) __attribute__((visibility("hidden")));
867                   int foo_int(void) __attribute__((visibility("internal")));
868                   int foo_pro(void) __attribute__((visibility("protected")));''',
869                name : '__attribute__((visibility(...)))')
870   pre_args += '-DHAVE_FUNC_ATTRIBUTE_VISIBILITY'
871 endif
872 if cc.compiles('int foo(void) { return 0; } int bar(void) __attribute__((alias("foo")));',
873                name : '__attribute__((alias(...)))')
874   pre_args += '-DHAVE_FUNC_ATTRIBUTE_ALIAS'
875 endif
876 if cc.compiles('int foo(void) __attribute__((__noreturn__));',
877                name : '__attribute__((__noreturn__))')
878   pre_args += '-DHAVE_FUNC_ATTRIBUTE_NORETURN'
879 endif
880 if cc.compiles('__uint128_t foo(void) { return 0; }',
881                name : '__uint128_t')
882   pre_args += '-DHAVE_UINT128'
883 endif
884
885 # TODO: this is very incomplete
886 if ['linux', 'cygwin', 'gnu', 'gnu/kfreebsd'].contains(host_machine.system())
887   pre_args += '-D_GNU_SOURCE'
888 elif host_machine.system() == 'windows'
889   pre_args += [
890     '-D_WINDOWS', '-D_WIN32_WINNT=0x0601', '-D_WINVER=0x0601',
891     '-DPIPE_SUBSYSTEM_WINDOWS_USER',
892     '-D_USE_MATH_DEFINES',  # XXX: scons doesn't use this for mingw
893   ]
894   if cc.get_id() == 'msvc'
895     pre_args += [
896       '-DVC_EXTRALEAN',
897       '-D_CRT_SECURE_NO_WARNINGS',
898       '-D_CRT_SECURE_NO_DEPRECATE',
899       '-D_SCL_SECURE_NO_WARNINGS',
900       '-D_SCL_SECURE_NO_DEPRECATE',
901       '-D_ALLOW_KEYWORD_MACROS',
902       '-D_HAS_EXCEPTIONS=0', # Tell C++ STL to not use exceptions
903     ]
904   else
905     pre_args += ['-D__MSVCRT_VERSION__=0x0700']
906   endif
907 endif
908
909 # Check for generic C arguments
910 c_args = []
911 c_vis_args = []
912 c_msvc_compat_args = []
913 no_override_init_args = []
914 cpp_args = []
915 cpp_vis_args = []
916 cpp_msvc_compat_args = []
917 if cc.get_id() == 'msvc'
918   foreach a : ['/wd4018',  # signed/unsigned mismatch
919                '/wd4056',  # overflow in floating-point constant arithmetic
920                '/wd4244',  # conversion from 'type1' to 'type2', possible loss of data
921                '/wd4267',  # 'var' : conversion from 'size_t' to 'type', possible loss of data
922                '/wd4305',  # trancation from 'type1' to 'type2'
923                '/wd4351',  # new behavior: elements of array 'array' will be default initialized
924                '/wd4756',  # overflow in constant arithmetic
925                '/wd4800',  # forcing value to bool 'true' or 'false' (performance warning)
926                '/wd4996',  # disabled deprecated POSIX name warnings
927                '/wd4291']  # no matching operator delete found
928     if cc.has_argument(a)
929       c_args += a
930     endif
931     if cpp.has_argument(a)
932       cpp_args += a
933     endif
934   endforeach
935   if cc.has_argument('-Wmicrosoft-enum-value')  # Clang
936     c_args += '-Wno-microsoft-enum-value'
937     cpp_args += '-Wno-microsoft-enum-value'
938   endif
939 else
940   foreach a : ['-Werror=implicit-function-declaration',
941               '-Werror=missing-prototypes',
942               '-Werror=return-type',
943               '-Werror=incompatible-pointer-types',
944               '-Werror=format',
945               '-Wformat-security',
946               '-Wno-missing-field-initializers',
947               '-Wno-format-truncation',
948               '-fno-math-errno',
949               '-fno-trapping-math',
950               '-Qunused-arguments']
951     if cc.has_argument(a)
952       c_args += a
953     endif
954   endforeach
955
956   # Check for generic C++ arguments
957   foreach a : ['-Werror=return-type',
958                '-Werror=format',
959                '-Wformat-security',
960                '-fno-math-errno', '-fno-trapping-math',
961                '-Qunused-arguments']
962     if cpp.has_argument(a)
963       cpp_args += a
964     endif
965   endforeach
966
967   foreach a : ['-Wno-override-init', '-Wno-initializer-overrides']
968     if cc.has_argument(a)
969       no_override_init_args += a
970     endif
971   endforeach
972
973   if cc.has_argument('-fvisibility=hidden')
974     c_vis_args += '-fvisibility=hidden'
975   endif
976
977   foreach a : ['-Werror=return-type',
978                '-Werror=format',
979                '-Wformat-security',
980                '-Wno-non-virtual-dtor',
981                '-Wno-missing-field-initializers',
982                '-Wno-format-truncation',
983                '-fno-math-errno',
984                '-fno-trapping-math',
985                '-Qunused-arguments']
986     if cpp.has_argument(a)
987       cpp_args += a
988     endif
989   endforeach
990
991   # Check for C and C++ arguments for MSVC2013 compatibility. These are only
992   # used in parts of the mesa code base that need to compile with old versions
993   # of MSVC, mainly common code
994   foreach a : ['-Werror=pointer-arith', '-Werror=vla']
995     if cc.has_argument(a)
996       c_msvc_compat_args += a
997     endif
998     if cpp.has_argument(a)
999       cpp_msvc_compat_args += a
1000     endif
1001   endforeach
1002
1003   if cpp.has_argument('-fvisibility=hidden')
1004     cpp_vis_args += '-fvisibility=hidden'
1005   endif
1006
1007 endif
1008
1009 # set linker arguments
1010 if host_machine.system() == 'windows'
1011   if cc.get_id() == 'msvc'
1012     add_project_link_arguments(
1013       '/fixed:no',
1014       '/incremental:no',
1015       '/dynamicbase',
1016       '/nxcompat',
1017       language : ['c', 'cpp'],
1018     )
1019   else
1020     add_project_link_arguments(
1021       '-Wl,--nxcompat',
1022       '-Wl,--dynamicbase',
1023       '-static-libgcc',
1024       '-static-libstdc++',
1025       language : ['c', 'cpp'],
1026     )
1027   endif
1028 endif
1029
1030 if host_machine.cpu_family().startswith('x86')
1031   pre_args += '-DUSE_SSE41'
1032   with_sse41 = true
1033   sse41_args = ['-msse4.1']
1034
1035   # GCC on x86 (not x86_64) with -msse* assumes a 16 byte aligned stack, but
1036   # that's not guaranteed
1037   if host_machine.cpu_family() == 'x86'
1038     sse41_args += '-mstackrealign'
1039   endif
1040 else
1041   with_sse41 = false
1042   sse41_args = []
1043 endif
1044
1045 # Check for GCC style atomics
1046 dep_atomic = null_dep
1047
1048 if cc.compiles('''#include <stdint.h>
1049                   int main() {
1050                     struct {
1051                       uint64_t *v;
1052                     } x;
1053                     return (int)__atomic_load_n(x.v, __ATOMIC_ACQUIRE) &
1054                            (int)__atomic_add_fetch(x.v, (uint64_t)1, __ATOMIC_ACQ_REL);
1055
1056                   }''',
1057                name : 'GCC atomic builtins')
1058   pre_args += '-DUSE_GCC_ATOMIC_BUILTINS'
1059
1060   # Not all atomic calls can be turned into lock-free instructions, in which
1061   # GCC will make calls into the libatomic library. Check whether we need to
1062   # link with -latomic.
1063   #
1064   # This can happen for 64-bit atomic operations on 32-bit architectures such
1065   # as ARM.
1066   if not cc.links('''#include <stdint.h>
1067                      int main() {
1068                        struct {
1069                          uint64_t *v;
1070                        } x;
1071                        return (int)__atomic_load_n(x.v, __ATOMIC_ACQUIRE) &
1072                               (int)__atomic_add_fetch(x.v, (uint64_t)1, __ATOMIC_ACQ_REL);
1073                      }''',
1074                   name : 'GCC atomic builtins required -latomic')
1075     dep_atomic = cc.find_library('atomic')
1076   endif
1077 endif
1078 if not cc.links('''#include <stdint.h>
1079                    uint64_t v;
1080                    int main() {
1081                      return __sync_add_and_fetch(&v, (uint64_t)1);
1082                    }''',
1083                 dependencies : dep_atomic,
1084                 name : 'GCC 64bit atomics')
1085   pre_args += '-DMISSING_64BIT_ATOMICS'
1086 endif
1087
1088 dep_ws2_32 = cc.find_library('ws2_32', required : with_platform_windows)
1089
1090 # TODO: shared/static? Is this even worth doing?
1091
1092 with_asm_arch = ''
1093 if host_machine.cpu_family() == 'x86'
1094   if system_has_kms_drm or host_machine.system() == 'gnu'
1095     with_asm_arch = 'x86'
1096     pre_args += ['-DUSE_X86_ASM', '-DUSE_MMX_ASM', '-DUSE_3DNOW_ASM',
1097                  '-DUSE_SSE_ASM']
1098
1099     if with_glx_read_only_text
1100       pre_args += ['-DGLX_X86_READONLY_TEXT']
1101     endif
1102   endif
1103 elif host_machine.cpu_family() == 'x86_64'
1104   if system_has_kms_drm
1105     with_asm_arch = 'x86_64'
1106     pre_args += ['-DUSE_X86_64_ASM']
1107   endif
1108 elif host_machine.cpu_family() == 'arm'
1109   if system_has_kms_drm
1110     with_asm_arch = 'arm'
1111     pre_args += ['-DUSE_ARM_ASM']
1112   endif
1113 elif host_machine.cpu_family() == 'aarch64'
1114   if system_has_kms_drm
1115     with_asm_arch = 'aarch64'
1116     pre_args += ['-DUSE_AARCH64_ASM']
1117   endif
1118 elif host_machine.cpu_family() == 'sparc64'
1119   if system_has_kms_drm
1120     with_asm_arch = 'sparc'
1121     pre_args += ['-DUSE_SPARC_ASM']
1122   endif
1123 elif host_machine.cpu_family().startswith('ppc64') and host_machine.endian() == 'little'
1124   if system_has_kms_drm
1125     with_asm_arch = 'ppc64le'
1126     pre_args += ['-DUSE_PPC64LE_ASM']
1127   endif
1128 endif
1129
1130 # Check for standard headers and functions
1131 if (cc.has_header_symbol('sys/sysmacros.h', 'major') and
1132   cc.has_header_symbol('sys/sysmacros.h', 'minor') and
1133   cc.has_header_symbol('sys/sysmacros.h', 'makedev'))
1134   pre_args += '-DMAJOR_IN_SYSMACROS'
1135 endif
1136 if (cc.has_header_symbol('sys/mkdev.h', 'major') and
1137   cc.has_header_symbol('sys/mkdev.h', 'minor') and
1138   cc.has_header_symbol('sys/mkdev.h', 'makedev'))
1139   pre_args += '-DMAJOR_IN_MKDEV'
1140 endif
1141
1142 foreach h : ['xlocale.h', 'sys/sysctl.h', 'linux/futex.h', 'endian.h', 'dlfcn.h', 'execinfo.h', 'sys/shm.h', 'cet.h']
1143   if cc.compiles('#include <@0@>'.format(h), name : '@0@'.format(h))
1144     pre_args += '-DHAVE_@0@'.format(h.to_upper().underscorify())
1145   endif
1146 endforeach
1147
1148 foreach f : ['strtof', 'mkostemp', 'timespec_get', 'memfd_create', 'random_r']
1149   if cc.has_function(f)
1150     pre_args += '-DHAVE_@0@'.format(f.to_upper())
1151   endif
1152 endforeach
1153
1154 if cc.has_header_symbol('errno.h', 'program_invocation_name',
1155                         args : '-D_GNU_SOURCE')
1156    pre_args += '-DHAVE_PROGRAM_INVOCATION_NAME'
1157 elif with_tools.contains('intel')
1158   error('Intel tools require the program_invocation_name variable')
1159 endif
1160
1161 # MinGW provides a __builtin_posix_memalign function, but not a posix_memalign.
1162 # This means that this check will succeed, but then compilation will later
1163 # fail. MSVC doesn't have this function at all, so only check for it on
1164 # non-windows platforms.
1165 if host_machine.system() != 'windows'
1166   if cc.has_function('posix_memalign')
1167     pre_args += '-DHAVE_POSIX_MEMALIGN'
1168   endif
1169 endif
1170
1171 # strtod locale support
1172 if cc.links('''
1173     #define _GNU_SOURCE
1174     #include <stdlib.h>
1175     #include <locale.h>
1176     #ifdef HAVE_XLOCALE_H
1177     #include <xlocale.h>
1178     #endif
1179     int main() {
1180       locale_t loc = newlocale(LC_CTYPE_MASK, "C", NULL);
1181       const char *s = "1.0";
1182       char *end;
1183       double d = strtod_l(s, end, loc);
1184       float f = strtof_l(s, end, loc);
1185       freelocale(loc);
1186       return 0;
1187     }''',
1188     args : pre_args,
1189     name : 'strtod has locale support')
1190   pre_args += '-DHAVE_STRTOD_L'
1191 endif
1192
1193 # Check for some linker flags
1194 ld_args_bsymbolic = []
1195 if cc.links('int main() { return 0; }', args : '-Wl,-Bsymbolic', name : 'Bsymbolic')
1196   ld_args_bsymbolic += '-Wl,-Bsymbolic'
1197 endif
1198 ld_args_gc_sections = []
1199 if cc.links('static char unused() { return 5; } int main() { return 0; }',
1200             args : '-Wl,--gc-sections', name : 'gc-sections')
1201   ld_args_gc_sections += '-Wl,--gc-sections'
1202 endif
1203 with_ld_version_script = false
1204 if cc.links('int main() { return 0; }',
1205             args : '-Wl,--version-script=@0@'.format(
1206               join_paths(meson.source_root(), 'build-support/conftest.map')),
1207             name : 'version-script')
1208   with_ld_version_script = true
1209 endif
1210 with_ld_dynamic_list = false
1211 if cc.links('int main() { return 0; }',
1212             args : '-Wl,--dynamic-list=@0@'.format(
1213               join_paths(meson.source_root(), 'build-support/conftest.dyn')),
1214             name : 'dynamic-list')
1215   with_ld_dynamic_list = true
1216 endif
1217
1218 ld_args_build_id = cc.get_supported_link_arguments('-Wl,--build-id=sha1')
1219
1220 # check for dl support
1221 dep_dl = null_dep
1222 if host_machine.system() != 'windows'
1223   if not cc.has_function('dlopen')
1224     dep_dl = cc.find_library('dl')
1225   endif
1226   if cc.has_function('dladdr', dependencies : dep_dl)
1227     # This is really only required for megadrivers
1228     pre_args += '-DHAVE_DLADDR'
1229   endif
1230 endif
1231
1232 if cc.has_function('dl_iterate_phdr')
1233   pre_args += '-DHAVE_DL_ITERATE_PHDR'
1234 elif with_intel_vk
1235   error('Intel "Anvil" Vulkan driver requires the dl_iterate_phdr function')
1236 elif with_dri_i965 and with_shader_cache
1237   error('Intel i965 GL driver requires dl_iterate_phdr when built with shader caching.')
1238 endif
1239
1240 # Determine whether or not the rt library is needed for time functions
1241 if cc.has_function('clock_gettime')
1242   dep_clock = null_dep
1243 else
1244   dep_clock = cc.find_library('rt')
1245 endif
1246
1247 # TODO: some of these may be conditional
1248 dep_zlib = dependency('zlib', version : '>= 1.2.3', fallback : ['zlib', 'zlib_dep'])
1249 pre_args += '-DHAVE_ZLIB'
1250 dep_thread = dependency('threads')
1251 if dep_thread.found() and host_machine.system() != 'windows'
1252   pre_args += '-DHAVE_PTHREAD'
1253   if cc.has_function(
1254       'pthread_setaffinity_np',
1255       dependencies : dep_thread,
1256       prefix : '#include <pthread.h>',
1257       args : '-D_GNU_SOURCE')
1258     pre_args += '-DHAVE_PTHREAD_SETAFFINITY'
1259   endif
1260   if cc.has_function(
1261       'pthread_setaffinity_np',
1262       dependencies : dep_thread,
1263       prefix : '#include <pthread_np.h>')
1264     pre_args += '-DPTHREAD_SETAFFINITY_IN_NP_HEADER'
1265   endif
1266 endif
1267 dep_expat = dependency('expat', fallback : ['expat', 'expat_dep'])
1268 # this only exists on linux so either this is linux and it will be found, or
1269 # it's not linux and wont
1270 dep_m = cc.find_library('m', required : false)
1271
1272 # Check for libdrm. Various drivers have different libdrm version requirements,
1273 # but we always want to use the same version for all libdrm modules. That means
1274 # even if driver foo requires 2.4.0 and driver bar requires 2.4.3, if foo and
1275 # bar are both on use 2.4.3 for both of them
1276 dep_libdrm_amdgpu = null_dep
1277 dep_libdrm_radeon = null_dep
1278 dep_libdrm_nouveau = null_dep
1279 dep_libdrm_intel = null_dep
1280
1281 _drm_amdgpu_ver = '2.4.99'
1282 _drm_radeon_ver = '2.4.71'
1283 _drm_nouveau_ver = '2.4.66'
1284 _drm_intel_ver = '2.4.75'
1285 _drm_ver = '2.4.81'
1286
1287 _libdrm_checks = [
1288   ['intel', with_dri_i915 or with_gallium_i915],
1289   ['amdgpu', with_amd_vk or with_gallium_radeonsi],
1290   ['radeon', (with_gallium_radeonsi or with_dri_r100 or with_dri_r200 or
1291               with_gallium_r300 or with_gallium_r600)],
1292   ['nouveau', (with_gallium_nouveau or with_dri_nouveau)],
1293 ]
1294
1295 # VC4 only needs core libdrm support of this version, not a libdrm_vc4
1296 # library.
1297 if with_gallium_vc4
1298   _drm_ver = '2.4.89'
1299 endif
1300
1301 # etnaviv only needs core libdrm
1302 if with_gallium_etnaviv
1303   _drm_ver = '2.4.89'
1304 endif
1305
1306 # Loop over the enables versions and get the highest libdrm requirement for all
1307 # active drivers.
1308 _drm_blame = ''
1309 foreach d : _libdrm_checks
1310   ver = get_variable('_drm_@0@_ver'.format(d[0]))
1311   if d[1] and ver.version_compare('>' + _drm_ver)
1312     _drm_ver = ver
1313     _drm_blame = d[0]
1314   endif
1315 endforeach
1316 if _drm_blame != ''
1317   message('libdrm @0@ needed because @1@ has the highest requirement'.format(_drm_ver, _drm_blame))
1318 endif
1319
1320 # Then get each libdrm module
1321 foreach d : _libdrm_checks
1322   if d[1]
1323     set_variable(
1324       'dep_libdrm_' + d[0],
1325       dependency('libdrm_' + d[0], version : '>=' + _drm_ver)
1326     )
1327   endif
1328 endforeach
1329
1330 with_gallium_drisw_kms = false
1331 dep_libdrm = dependency(
1332   'libdrm', version : '>=' + _drm_ver,
1333   required : with_dri2 or with_dri3
1334 )
1335 if dep_libdrm.found()
1336   pre_args += '-DHAVE_LIBDRM'
1337   if with_dri_platform == 'drm' and with_dri
1338     with_gallium_drisw_kms = true
1339   endif
1340 endif
1341
1342 llvm_modules = ['bitwriter', 'engine', 'mcdisassembler', 'mcjit']
1343 llvm_optional_modules = ['coroutines']
1344 if with_amd_vk or with_gallium_radeonsi or with_gallium_r600
1345   llvm_modules += ['amdgpu', 'native', 'bitreader', 'ipo']
1346   if with_gallium_r600
1347     llvm_modules += 'asmparser'
1348   endif
1349 endif
1350 if with_gallium_opencl
1351   llvm_modules += [
1352     'all-targets', 'linker', 'coverage', 'instrumentation', 'ipo', 'irreader',
1353     'lto', 'option', 'objcarcopts', 'profiledata',
1354   ]
1355 endif
1356
1357 if with_amd_vk or with_gallium_radeonsi
1358   _llvm_version = '>= 8.0.0'
1359 elif with_gallium_swr
1360   _llvm_version = '>= 6.0.0'
1361 else
1362   _llvm_version = '>= 3.9.0'
1363 endif
1364
1365 _shared_llvm = get_option('shared-llvm')
1366
1367 _llvm = get_option('llvm')
1368 dep_llvm = null_dep
1369 with_llvm = false
1370 if _llvm != 'false'
1371   dep_llvm = dependency(
1372     'llvm',
1373     version : _llvm_version,
1374     modules : llvm_modules,
1375     optional_modules : llvm_optional_modules,
1376     required : (
1377       with_amd_vk or with_gallium_radeonsi or with_gallium_swr or
1378       with_gallium_opencl or _llvm == 'true'
1379     ),
1380     static : not _shared_llvm,
1381     method : 'config-tool',
1382   )
1383   with_llvm = dep_llvm.found()
1384 endif
1385 if with_llvm
1386   pre_args += '-DLLVM_AVAILABLE'
1387   pre_args += '-DMESA_LLVM_VERSION_STRING="@0@"'.format(dep_llvm.version())
1388
1389   # LLVM can be built without rtti, turning off rtti changes the ABI of C++
1390   # programs, so we need to build all C++ code in mesa without rtti as well to
1391   # ensure that linking works.
1392   if dep_llvm.get_configtool_variable('has-rtti') == 'NO'
1393     if with_gallium_nouveau
1394       error('The Nouveau driver requires rtti. You either need to turn off nouveau or use an LLVM built with LLVM_ENABLE_RTTI.')
1395     elif with_gallium_opencl
1396       error('The Clover OpenCL state tracker requires rtti, you need to turn off clover or use an LLVM built with LLVM_ENABLE_RTTI.')
1397     endif
1398     if cc.get_id() == 'msvc'
1399       cpp_args += '/GR-'
1400     else
1401       cpp_args += '-fno-rtti'
1402     endif
1403   endif
1404 elif with_amd_vk or with_gallium_radeonsi or with_gallium_swr
1405   error('The following drivers require LLVM: Radv, RadeonSI, SWR. One of these is enabled, but LLVM is disabled.')
1406 elif with_gallium_opencl
1407   error('The OpenCL "Clover" state tracker requires LLVM, but LLVM is disabled.')
1408 endif
1409
1410 if (with_amd_vk or with_gallium_radeonsi or with_gallium_opencl or
1411     (with_gallium_r600 and with_llvm))
1412   dep_elf = dependency('libelf', required : false)
1413   if not dep_elf.found()
1414     dep_elf = cc.find_library('elf')
1415   endif
1416 else
1417   dep_elf = null_dep
1418 endif
1419
1420 dep_glvnd = null_dep
1421 glvnd_has_headers_and_pc_files = false
1422 if with_glvnd
1423   dep_glvnd = dependency('libglvnd', version : '>= 0.2.0')
1424   # GLVND before 1.2 was missing its pkg-config and header files, forcing every
1425   # vendor to provide them and the distro maintainers to resolve the conflict.
1426   glvnd_has_headers_and_pc_files = dep_glvnd.version().version_compare('>= 1.2.0')
1427   pre_args += '-DUSE_LIBGLVND=1'
1428 endif
1429
1430 if with_valgrind != 'false'
1431   dep_valgrind = dependency('valgrind', required : with_valgrind == 'true')
1432   if dep_valgrind.found()
1433     pre_args += '-DHAVE_VALGRIND'
1434   endif
1435 else
1436   dep_valgrind = null_dep
1437 endif
1438
1439 # pthread stubs. Lets not and say we didn't
1440
1441 prog_bison = find_program('bison', required : with_any_opengl)
1442 prog_flex = find_program('flex', required : with_any_opengl)
1443
1444 dep_selinux = null_dep
1445 if get_option('selinux')
1446   dep_selinux = dependency('libselinux')
1447   pre_args += '-DMESA_SELINUX'
1448 endif
1449
1450 if with_libunwind != 'false'
1451   dep_unwind = dependency('libunwind', required : with_libunwind == 'true')
1452   if dep_unwind.found()
1453     pre_args += '-DHAVE_LIBUNWIND'
1454   endif
1455 else
1456   dep_unwind = null_dep
1457 endif
1458
1459 if with_osmesa != 'none'
1460   if with_osmesa == 'gallium' and not with_gallium_softpipe
1461     error('OSMesa gallium requires gallium softpipe or llvmpipe.')
1462   endif
1463   if host_machine.system() == 'windows'
1464     osmesa_lib_name = 'osmesa'
1465   else
1466     osmesa_lib_name = 'OSMesa'
1467   endif
1468   osmesa_bits = get_option('osmesa-bits')
1469   if osmesa_bits != '8'
1470     if with_dri or with_glx != 'disabled'
1471       error('OSMesa bits must be 8 if building glx or dir based drivers')
1472     endif
1473     osmesa_lib_name = osmesa_lib_name + osmesa_bits
1474     pre_args += [
1475       '-DCHAN_BITS=@0@'.format(osmesa_bits), '-DDEFAULT_SOFTWARE_DEPTH_BITS=31'
1476     ]
1477   endif
1478 endif
1479
1480 # TODO: symbol mangling
1481
1482 if with_platform_wayland
1483   dep_wl_scanner = dependency('wayland-scanner', native: true)
1484   prog_wl_scanner = find_program(dep_wl_scanner.get_pkgconfig_variable('wayland_scanner'))
1485   if dep_wl_scanner.version().version_compare('>= 1.15')
1486     wl_scanner_arg = 'private-code'
1487   else
1488     wl_scanner_arg = 'code'
1489   endif
1490   dep_wl_protocols = dependency('wayland-protocols', version : '>= 1.8')
1491   dep_wayland_client = dependency('wayland-client', version : '>=1.11')
1492   dep_wayland_server = dependency('wayland-server', version : '>=1.11')
1493   if with_egl
1494     dep_wayland_egl = dependency('wayland-egl-backend', version : '>= 3')
1495     dep_wayland_egl_headers = declare_dependency(
1496       compile_args : run_command(prog_pkgconfig, ['wayland-egl-backend', '--cflags']).stdout().split())
1497   endif
1498   wayland_dmabuf_xml = join_paths(
1499     dep_wl_protocols.get_pkgconfig_variable('pkgdatadir'), 'unstable',
1500     'linux-dmabuf', 'linux-dmabuf-unstable-v1.xml'
1501   )
1502   pre_args += ['-DHAVE_WAYLAND_PLATFORM', '-DWL_HIDE_DEPRECATED']
1503 endif
1504
1505 dep_x11 = null_dep
1506 dep_xext = null_dep
1507 dep_xdamage = null_dep
1508 dep_xfixes = null_dep
1509 dep_x11_xcb = null_dep
1510 dep_xcb = null_dep
1511 dep_xcb_glx = null_dep
1512 dep_xcb_dri2 = null_dep
1513 dep_xcb_dri3 = null_dep
1514 dep_dri2proto = null_dep
1515 dep_glproto = null_dep
1516 dep_xxf86vm = null_dep
1517 dep_xcb_dri3 = null_dep
1518 dep_xcb_present = null_dep
1519 dep_xcb_sync = null_dep
1520 dep_xcb_xfixes = null_dep
1521 dep_xshmfence = null_dep
1522 dep_xcb_xrandr = null_dep
1523 dep_xlib_xrandr = null_dep
1524 if with_platform_x11
1525   if with_glx == 'xlib' or with_glx == 'gallium-xlib'
1526     dep_x11 = dependency('x11')
1527     dep_xext = dependency('xext')
1528     dep_xcb = dependency('xcb')
1529   elif with_glx == 'dri'
1530     dep_x11 = dependency('x11')
1531     dep_xext = dependency('xext')
1532     dep_xdamage = dependency('xdamage', version : '>= 1.1')
1533     dep_xfixes = dependency('xfixes')
1534     dep_xcb_glx = dependency('xcb-glx', version : '>= 1.8.1')
1535   endif
1536   if (with_any_vk or with_glx == 'dri' or with_egl or
1537        (with_gallium_vdpau or with_gallium_xvmc or with_gallium_va or
1538         with_gallium_omx != 'disabled'))
1539     dep_xcb = dependency('xcb')
1540     dep_x11_xcb = dependency('x11-xcb')
1541     if with_dri_platform == 'drm' and not dep_libdrm.found()
1542       error('libdrm required for gallium video statetrackers when using x11')
1543     endif
1544   endif
1545   if with_any_vk or with_egl or (with_glx == 'dri' and with_dri_platform == 'drm')
1546     dep_xcb_dri2 = dependency('xcb-dri2', version : '>= 1.8')
1547
1548     if with_dri3
1549       pre_args += '-DHAVE_DRI3'
1550       dep_xcb_dri3 = dependency('xcb-dri3')
1551       dep_xcb_present = dependency('xcb-present')
1552       # until xcb-dri3 has been around long enough to make a hard-dependency:
1553       if (dep_xcb_dri3.version().version_compare('>= 1.13') and
1554           dep_xcb_present.version().version_compare('>= 1.13'))
1555         pre_args += '-DHAVE_DRI3_MODIFIERS'
1556       endif
1557       dep_xcb_sync = dependency('xcb-sync')
1558       dep_xshmfence = dependency('xshmfence', version : '>= 1.1')
1559     endif
1560   endif
1561   if with_glx == 'dri' or with_glx == 'gallium-xlib'
1562     dep_glproto = dependency('glproto', version : '>= 1.4.14')
1563   endif
1564   if with_glx == 'dri'
1565     if with_dri_platform == 'drm'
1566       dep_dri2proto = dependency('dri2proto', version : '>= 2.8')
1567       dep_xxf86vm = dependency('xxf86vm')
1568     endif
1569   endif
1570   if (with_egl or (
1571       with_gallium_vdpau or with_gallium_xvmc or with_gallium_xa or
1572       with_gallium_omx != 'disabled'))
1573     dep_xcb_xfixes = dependency('xcb-xfixes')
1574   endif
1575   if with_xlib_lease
1576     dep_xcb_xrandr = dependency('xcb-randr')
1577     dep_xlib_xrandr = dependency('xrandr', version : '>= 1.3')
1578   endif
1579 endif
1580
1581 if get_option('gallium-extra-hud')
1582   pre_args += '-DHAVE_GALLIUM_EXTRA_HUD=1'
1583 endif
1584
1585 _sensors = get_option('lmsensors')
1586 if _sensors != 'false'
1587   dep_lmsensors = cc.find_library('sensors', required : _sensors == 'true')
1588   if dep_lmsensors.found()
1589     pre_args += '-DHAVE_LIBSENSORS=1'
1590   endif
1591 else
1592   dep_lmsensors = null_dep
1593 endif
1594
1595 foreach a : pre_args
1596   add_project_arguments(a, language : ['c', 'cpp'])
1597 endforeach
1598 foreach a : c_args
1599   add_project_arguments(a, language : ['c'])
1600 endforeach
1601 foreach a : cpp_args
1602   add_project_arguments(a, language : ['cpp'])
1603 endforeach
1604
1605 gl_priv_reqs = []
1606
1607 if with_glx == 'xlib' or with_glx == 'gallium-xlib'
1608   gl_priv_reqs += ['x11', 'xext', 'xcb']
1609 elif with_glx == 'dri'
1610   gl_priv_reqs += [
1611     'x11', 'xext', 'xdamage >= 1.1', 'xfixes', 'x11-xcb', 'xcb',
1612     'xcb-glx >= 1.8.1']
1613   if with_dri_platform == 'drm'
1614     gl_priv_reqs += 'xcb-dri2 >= 1.8'
1615     gl_priv_reqs += 'xxf86vm'
1616   endif
1617 endif
1618 if dep_libdrm.found()
1619   gl_priv_reqs += 'libdrm >= 2.4.75'
1620 endif
1621
1622 gl_priv_libs = []
1623 if dep_thread.found()
1624   gl_priv_libs += ['-lpthread', '-pthread']
1625 endif
1626 if dep_m.found()
1627   gl_priv_libs += '-lm'
1628 endif
1629 if dep_dl.found()
1630   gl_priv_libs += '-ldl'
1631 endif
1632
1633 pkg = import('pkgconfig')
1634
1635 prog_nm = find_program('nm', required : false)
1636
1637 # This quirk needs to be applied to sources with functions defined in assembly
1638 # as GCC LTO drops them. See: https://bugs.freedesktop.org/show_bug.cgi?id=109391
1639 gcc_lto_quirk = (cc.get_id() == 'gcc') ? ['-fno-lto'] : []
1640
1641 subdir('include')
1642 subdir('bin')
1643 subdir('src')
1644
1645 # Meson 0.49 and earlier seems to have a bug that fails to evaluate the string-
1646 # formatting below unless the first argument is passed as a variable. This has
1647 # been fixed in Meson 0.50 and beyond, but we need to keep it like this for now
1648 # for backwards compatibility.
1649 _with_opengl_string = with_opengl ? 'yes' : 'no'
1650
1651 lines = ['',
1652   'prefix:          ' + get_option('prefix'),
1653   'libdir:          ' + get_option('libdir'),
1654   'includedir:      ' + get_option('includedir'),
1655   '',
1656   'OpenGL:          @0@ (ES1: @1@ ES2: @2@)'.format(_with_opengl_string,
1657                                                     with_gles1 ? 'yes' : 'no',
1658                                                     with_gles2 ? 'yes' : 'no'),
1659 ]
1660
1661 if with_osmesa != 'none'
1662   lines += ''
1663   suffix = ''
1664   if with_osmesa == 'gallium'
1665     suffix = '(Gallium)'
1666   endif
1667   lines += 'OSMesa:          lib' + osmesa_lib_name + suffix
1668 else
1669   lines += 'OSMesa:          no'
1670 endif
1671
1672 if with_dri
1673   lines += ''
1674   lines += 'DRI platform:    ' + with_dri_platform
1675   if dri_drivers.length() != 0 and dri_drivers != ['']
1676     lines += 'DRI drivers:     ' + ' '.join(dri_drivers)
1677   else
1678     lines += 'DRI drivers:     no'
1679   endif
1680   lines += 'DRI driver dir:  ' + dri_drivers_path
1681 endif
1682
1683 if with_glx != 'disabled'
1684   lines += ''
1685   if with_glx == 'dri'
1686     lines += 'GLX:             DRI-based'
1687   elif with_glx == 'xlib'
1688     lines += 'GLX:             Xlib-based'
1689   elif with_glx == 'gallium-xlib'
1690     lines += 'GLX:             Xlib-based (Gallium)'
1691   else
1692     lines += 'GLX:             ' + with_glx
1693   endif
1694 endif
1695
1696 lines += ''
1697 lines += 'EGL:             ' + (with_egl ? 'yes' : 'no')
1698 if with_egl
1699   egl_drivers = []
1700   if with_dri
1701     egl_drivers += 'builtin:egl_dri2'
1702   endif
1703   if with_dri3
1704     egl_drivers += 'builtin:egl_dri3'
1705   endif
1706   lines += 'EGL drivers:     ' + ' '.join(egl_drivers)
1707 endif
1708 lines += 'GBM:             ' + (with_gbm ? 'yes' : 'no')
1709 if with_platforms
1710   lines += 'EGL/Vulkan/VL platforms:   ' + ' '.join(_platforms)
1711 endif
1712
1713 lines += ''
1714 if with_any_vk
1715   lines += 'Vulkan drivers:  ' + ' '.join(_vulkan_drivers)
1716   lines += 'Vulkan ICD dir:  ' + with_vulkan_icd_dir
1717 else
1718   lines += 'Vulkan drivers:  no'
1719 endif
1720
1721 lines += ''
1722 if with_llvm
1723   lines += 'llvm:            yes'
1724   lines += 'llvm-version:    ' + dep_llvm.version()
1725 else
1726   lines += 'llvm:            no'
1727 endif
1728
1729 lines += ''
1730 if with_gallium
1731   lines += 'Gallium drivers: ' + ' '.join(gallium_drivers)
1732   gallium_st = ['mesa']
1733   if with_gallium_xa
1734     gallium_st += 'xa'
1735   endif
1736   if with_gallium_xvmc
1737     gallium_st += 'xvmc'
1738   endif
1739   if with_gallium_xvmc
1740     gallium_st += 'xvmc'
1741   endif
1742   if with_gallium_vdpau
1743     gallium_st += 'vdpau'
1744   endif
1745   if with_gallium_omx != 'disabled'
1746     gallium_st += 'omx' + with_gallium_omx
1747   endif
1748   if with_gallium_va
1749     gallium_st += 'va'
1750   endif
1751   if with_gallium_st_nine
1752     gallium_st += 'nine'
1753   endif
1754   if with_gallium_opencl
1755     gallium_st += 'clover'
1756   endif
1757   lines += 'Gallium st:      ' + ' '.join(gallium_st)
1758 else
1759   lines += 'Gallium:         no'
1760 endif
1761
1762 lines += 'HUD lmsensors:   ' + (dep_lmsensors.found() ? 'yes' : 'no')
1763
1764 lines += ''
1765 lines += 'Shared-glapi:    ' + (with_shared_glapi ? 'yes' : 'no')
1766
1767
1768 indent = '        '
1769 summary = indent + ('\n' + indent).join(lines)
1770 message('Configuration summary:\n@0@\n'.format(summary))