aco: introduce helper to swap valu operands with modifiers
[platform/upstream/mesa.git] / meson.build
1 # Copyright © 2017-2020 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 : files('VERSION'),
25   license : 'MIT',
26   meson_version : '>= 0.60',
27   default_options : ['buildtype=debugoptimized', 'b_ndebug=if-release', 'c_std=c11', 'cpp_std=c++17', 'rust_std=2021']
28 )
29
30 cc = meson.get_compiler('c')
31 cpp = meson.get_compiler('cpp')
32
33 null_dep = dependency('', required : false)
34
35 if get_option('layout') != 'mirror'
36   error('`mirror` is the only build directory layout supported')
37 endif
38
39 # Arguments for the preprocessor, put these in a separate array from the C and
40 # C++ (cpp in meson terminology) arguments since they need to be added to the
41 # default arguments for both C and C++.
42 pre_args = [
43   '-D__STDC_CONSTANT_MACROS',
44   '-D__STDC_FORMAT_MACROS',
45   '-D__STDC_LIMIT_MACROS',
46   '-DPACKAGE_VERSION="@0@"'.format(meson.project_version()),
47   '-DPACKAGE_BUGREPORT="https://gitlab.freedesktop.org/mesa/mesa/-/issues"',
48 ]
49 # Arguments for c or cpp compiler, can be compiler options
50 c_cpp_args = []
51
52 c_args = []
53 cpp_args = []
54
55 with_moltenvk_dir = get_option('moltenvk-dir')
56 with_vulkan_icd_dir = get_option('vulkan-icd-dir')
57 with_tests = get_option('build-tests')
58 with_glcpp_tests = get_option('enable-glcpp-tests')
59 with_aco_tests = get_option('build-aco-tests')
60 with_glx_read_only_text = get_option('glx-read-only-text')
61 with_glx_direct = get_option('glx-direct')
62 with_osmesa = get_option('osmesa')
63 with_vulkan_overlay_layer = get_option('vulkan-layers').contains('overlay')
64 with_vulkan_device_select_layer = get_option('vulkan-layers').contains('device-select')
65 with_tools = get_option('tools')
66 if with_tools.contains('all')
67   with_tools = [
68     'drm-shim',
69     'dlclose-skip',
70     'etnaviv',
71     'freedreno',
72     'glsl',
73     'intel',
74     'intel-ui',
75     'lima',
76     'nir',
77     'nouveau',
78     'asahi',
79     'imagination',
80   ]
81 endif
82
83 with_any_vulkan_layers = get_option('vulkan-layers').length() != 0
84 with_intel_tools = with_tools.contains('intel') or with_tools.contains('intel-ui')
85 with_imgui = with_intel_tools or with_vulkan_overlay_layer
86
87 dri_drivers_path = get_option('dri-drivers-path')
88 if dri_drivers_path == ''
89   dri_drivers_path = join_paths(get_option('prefix'), get_option('libdir'), 'dri')
90 endif
91 dri_search_path = get_option('dri-search-path')
92 if dri_search_path == ''
93   dri_search_path = dri_drivers_path
94 endif
95
96 gbm_backends_path = get_option('gbm-backends-path')
97 if gbm_backends_path == ''
98   gbm_backends_path = join_paths(get_option('prefix'), get_option('libdir'), 'gbm')
99 endif
100
101 # Default shared glapi off for windows, on elsewhere.
102 with_shared_glapi = get_option('shared-glapi') \
103   .disable_auto_if(host_machine.system() == 'windows') \
104   .allowed()
105
106 with_opengl = get_option('opengl')
107
108 with_gles1 = get_option('gles1') \
109   .require(with_shared_glapi, error_message : 'OpengGL ES 1.x requires shared-glapi') \
110   .allowed()
111
112 with_gles2 = get_option('gles2') \
113   .require(with_shared_glapi, error_message : 'OpengGL ES 2.x requires shared-glapi') \
114   .allowed()
115
116 pre_args += '-DHAVE_OPENGL=@0@'.format(with_opengl ? '1' : '0')
117 pre_args += '-DHAVE_OPENGL_ES_1=@0@'.format(with_gles1 ? '1' : '0')
118 pre_args += '-DHAVE_OPENGL_ES_2=@0@'.format(with_gles2 ? '1' : '0')
119
120 with_any_opengl = with_opengl or with_gles1 or with_gles2
121 # Only build shared_glapi if at least one OpenGL API is enabled
122 with_shared_glapi = with_shared_glapi and with_any_opengl
123
124 system_has_kms_drm = ['openbsd', 'netbsd', 'freebsd', 'gnu/kfreebsd', 'dragonfly', 'linux', 'sunos', 'android'].contains(host_machine.system())
125
126 gallium_drivers = get_option('gallium-drivers')
127 if gallium_drivers.contains('auto')
128   if system_has_kms_drm
129     # TODO: PPC, Sparc
130     if ['x86', 'x86_64'].contains(host_machine.cpu_family())
131       gallium_drivers = [
132         'r300', 'r600', 'radeonsi', 'nouveau', 'virgl', 'svga', 'swrast',
133         'iris', 'crocus', 'i915'
134       ]
135     elif ['arm', 'aarch64'].contains(host_machine.cpu_family())
136       gallium_drivers = [
137         'v3d', 'vc4', 'freedreno', 'etnaviv', 'nouveau', 'svga',
138         'tegra', 'virgl', 'lima', 'panfrost', 'swrast', 'iris'
139       ]
140     elif ['mips', 'mips64', 'riscv32', 'riscv64'].contains(host_machine.cpu_family())
141       gallium_drivers = [
142         'r300', 'r600', 'radeonsi', 'nouveau', 'virgl', 'swrast'
143       ]
144     elif ['loongarch64'].contains(host_machine.cpu_family())
145       gallium_drivers = [
146         'r300', 'r600', 'radeonsi', 'nouveau', 'virgl', 'etnaviv', 'swrast'
147       ]
148     else
149       error('Unknown architecture @0@. Please pass -Dgallium-drivers to set driver options. Patches gladly accepted to fix this.'.format(
150             host_machine.cpu_family()))
151     endif
152   elif ['darwin', 'windows', 'cygwin', 'haiku'].contains(host_machine.system())
153     gallium_drivers = ['swrast']
154   else
155     error('Unknown OS @0@. Please pass -Dgallium-drivers to set driver options. Patches gladly accepted to fix this.'.format(
156           host_machine.system()))
157   endif
158 endif
159 with_gallium_radeonsi = gallium_drivers.contains('radeonsi')
160 with_gallium_r300 = gallium_drivers.contains('r300')
161 with_gallium_r600 = gallium_drivers.contains('r600')
162 with_gallium_nouveau = gallium_drivers.contains('nouveau')
163 with_gallium_freedreno = gallium_drivers.contains('freedreno')
164 with_gallium_softpipe = gallium_drivers.contains('swrast')
165 with_gallium_vc4 = gallium_drivers.contains('vc4')
166 with_gallium_v3d = gallium_drivers.contains('v3d')
167 with_gallium_panfrost = gallium_drivers.contains('panfrost')
168 with_gallium_etnaviv = gallium_drivers.contains('etnaviv')
169 with_gallium_tegra = gallium_drivers.contains('tegra')
170 with_gallium_crocus = gallium_drivers.contains('crocus')
171 with_gallium_iris = gallium_drivers.contains('iris')
172 with_gallium_i915 = gallium_drivers.contains('i915')
173 with_gallium_svga = gallium_drivers.contains('svga')
174 with_gallium_virgl = gallium_drivers.contains('virgl')
175 with_gallium_lima = gallium_drivers.contains('lima')
176 with_gallium_zink = gallium_drivers.contains('zink')
177 with_gallium_d3d12 = gallium_drivers.contains('d3d12')
178 with_gallium_asahi = gallium_drivers.contains('asahi')
179 foreach gallium_driver : gallium_drivers
180   pre_args += '-DHAVE_@0@'.format(gallium_driver.to_upper())
181 endforeach
182
183 freedreno_kmds = get_option('freedreno-kmds')
184 # If building only vulkan with only kgsl kmd support, there is no libdrm
185 # dependency:
186 if freedreno_kmds.length() == 1 and freedreno_kmds.contains('kgsl') and not with_gallium_freedreno
187   system_has_kms_drm = false
188 endif
189
190 with_gallium = gallium_drivers.length() != 0
191 with_gallium_kmsro = system_has_kms_drm and [
192   with_gallium_asahi,
193   with_gallium_etnaviv,
194   with_gallium_freedreno,
195   with_gallium_lima,
196   with_gallium_panfrost,
197   with_gallium_v3d,
198   with_gallium_vc4,
199 ].contains(true)
200
201 with_dri = false
202 if with_gallium and system_has_kms_drm
203   _glx = get_option('glx')
204   _egl = get_option('egl')
205   if _glx == 'dri' or _egl.enabled() or (_glx == 'disabled' and _egl.allowed())
206     with_dri = true
207   endif
208 endif
209
210 _vulkan_drivers = get_option('vulkan-drivers')
211 if _vulkan_drivers.contains('auto')
212   if system_has_kms_drm
213     if host_machine.cpu_family().startswith('x86')
214       _vulkan_drivers = ['amd', 'intel', 'intel_hasvk', 'swrast']
215     elif ['arm', 'aarch64'].contains(host_machine.cpu_family())
216       _vulkan_drivers = ['swrast', 'intel']
217     elif ['mips', 'mips64', 'riscv32', 'riscv64'].contains(host_machine.cpu_family())
218       _vulkan_drivers = ['amd', 'swrast']
219     elif ['loongarch64'].contains(host_machine.cpu_family())
220       _vulkan_drivers = ['amd', 'swrast']
221     else
222       error('Unknown architecture @0@. Please pass -Dvulkan-drivers to set driver options. Patches gladly accepted to fix this.'.format(
223             host_machine.cpu_family()))
224     endif
225   elif ['darwin', 'windows', 'cygwin', 'haiku'].contains(host_machine.system())
226     # No vulkan driver supports windows or macOS currently
227     _vulkan_drivers = []
228   else
229     error('Unknown OS @0@. Please pass -Dvulkan-drivers to set driver options. Patches gladly accepted to fix this.'.format(
230           host_machine.system()))
231   endif
232 endif
233
234 with_intel_vk = _vulkan_drivers.contains('intel')
235 with_intel_hasvk = _vulkan_drivers.contains('intel_hasvk')
236 with_amd_vk = _vulkan_drivers.contains('amd')
237 with_freedreno_vk = _vulkan_drivers.contains('freedreno')
238 with_panfrost_vk = _vulkan_drivers.contains('panfrost')
239 with_swrast_vk = _vulkan_drivers.contains('swrast')
240 with_virtio_vk = _vulkan_drivers.contains('virtio')
241 with_broadcom_vk = _vulkan_drivers.contains('broadcom')
242 with_imagination_vk = _vulkan_drivers.contains('imagination-experimental')
243 with_imagination_srv = get_option('imagination-srv')
244 with_microsoft_vk = _vulkan_drivers.contains('microsoft-experimental')
245 with_any_vk = _vulkan_drivers.length() != 0
246
247 with_any_broadcom = with_gallium_vc4 or with_gallium_v3d or with_broadcom_vk
248 with_any_intel = [
249   with_gallium_crocus,
250   with_gallium_i915,
251   with_gallium_iris,
252   with_intel_hasvk,
253   with_intel_tools,
254   with_intel_vk,
255 ].contains(true)
256
257 if with_swrast_vk and not with_gallium_softpipe
258   error('swrast vulkan requires gallium swrast')
259 endif
260 if with_gallium_tegra and not with_gallium_nouveau
261   error('tegra driver requires nouveau driver')
262 endif
263 if with_aco_tests and not with_amd_vk
264   error('ACO tests require Radv')
265 endif
266
267 with_microsoft_clc = get_option('microsoft-clc').enabled()
268 if ['x86_64'].contains(host_machine.cpu_family())
269   with_intel_clc = get_option('intel-clc').enabled()
270   with_intel_vk_rt = with_intel_vk and with_intel_clc
271 else
272   with_intel_clc = false
273   with_intel_vk_rt = false
274 endif
275 with_clc = with_microsoft_clc or with_intel_clc
276 with_libclc = with_clc
277 with_spirv_to_dxil = get_option('spirv-to-dxil')
278
279 if host_machine.system() == 'darwin'
280   with_dri_platform = 'apple'
281   pre_args += '-DBUILDING_MESA'
282 elif ['windows', 'cygwin'].contains(host_machine.system())
283   with_dri_platform = 'windows'
284 elif system_has_kms_drm
285   with_dri_platform = 'drm'
286 else
287   # FIXME: haiku doesn't use dri, and xlib doesn't use dri, probably should
288   # assert here that one of those cases has been met.
289   # FIXME: illumos ends up here as well
290   with_dri_platform = 'none'
291 endif
292
293 with_vulkan_beta = get_option('vulkan-beta')
294 if host_machine.system() == 'darwin'
295   #macOS seems to need beta extensions to build for now:
296   with_vulkan_beta = true
297 endif
298 if with_vulkan_beta
299   pre_args += '-DVK_ENABLE_BETA_EXTENSIONS'
300 endif
301
302 _codecs = get_option('video-codecs')
303 foreach c : ['vc1dec', 'h264dec', 'h264enc', 'h265dec', 'h265enc']
304    pre_args += '-DVIDEO_CODEC_@0@=@1@'.format(c.to_upper(), _codecs.contains(c).to_int())
305 endforeach
306
307 _platforms = get_option('platforms')
308 if _platforms.contains('auto')
309   if system_has_kms_drm
310     _platforms = ['x11', 'wayland']
311   elif ['darwin', 'cygwin'].contains(host_machine.system())
312     _platforms = ['x11']
313   elif ['haiku'].contains(host_machine.system())
314     _platforms = ['haiku']
315   elif host_machine.system() == 'windows'
316     _platforms = ['windows']
317   else
318     error('Unknown OS @0@. Please pass -Dplatforms to set platforms. Patches gladly accepted to fix this.'.format(
319           host_machine.system()))
320   endif
321 endif
322
323 with_platform_android = _platforms.contains('android')
324 with_platform_x11 = _platforms.contains('x11')
325 with_platform_wayland = _platforms.contains('wayland')
326 with_platform_haiku = _platforms.contains('haiku')
327 with_platform_windows = _platforms.contains('windows')
328
329 with_glx = get_option('glx')
330 if with_glx == 'auto'
331   if not with_opengl
332     with_glx = 'disabled'
333   elif with_platform_android
334     with_glx = 'disabled'
335   elif with_dri
336     with_glx = 'dri'
337   elif with_platform_haiku
338     with_glx = 'disabled'
339   elif host_machine.system() == 'windows'
340     with_glx = 'disabled'
341   elif with_gallium
342     # Even when building just gallium drivers the user probably wants dri
343     with_glx = 'dri'
344   elif with_platform_x11 and with_any_opengl and not with_any_vk
345     # The automatic behavior should not be to turn on xlib based glx when
346     # building only vulkan drivers
347     with_glx = 'xlib'
348   else
349     with_glx = 'disabled'
350   endif
351 endif
352 if with_glx == 'dri'
353    if with_gallium
354       with_dri = true
355    endif
356 endif
357
358 if not with_opengl and with_glx != 'disabled'
359   error('Building GLX without OpenGL is not supported.') \
360 endif
361
362 if not (with_dri or with_gallium or with_glx != 'disabled')
363   with_gles1 = false
364   with_gles2 = false
365   with_opengl = false
366   with_any_opengl = false
367   with_shared_glapi = false
368 endif
369
370 with_gbm = get_option('gbm') \
371   .require(system_has_kms_drm, error_message : 'GBM only supports DRM/KMS platforms') \
372   .disable_auto_if(not with_dri) \
373   .allowed()
374
375 with_xlib_lease = get_option('xlib-lease') \
376   .require(with_platform_x11 and system_has_kms_drm, error_message : 'xlib-lease requires X11 and KMS/DRM support') \
377   .allowed()
378
379 with_egl = get_option('egl') \
380   .require(host_machine.system() != 'darwin', error_message : 'EGL not supported on MacOS') \
381   .require(with_platform_windows or with_platform_haiku or with_dri or with_platform_android, error_message : 'EGL requires DRI, Haiku, Windows or Android') \
382   .require(with_shared_glapi, error_message : 'EGL requires shared-glapi') \
383   .require(with_glx != 'xlib', error_message :'EGL requires DRI, but GLX is being built with xlib support') \
384   .disable_auto_if(with_platform_haiku) \
385   .allowed()
386
387 if with_egl
388   _platforms += 'surfaceless'
389   if with_gbm and not with_platform_android
390     _platforms += 'drm'
391   endif
392
393   egl_native_platform = get_option('egl-native-platform')
394   if egl_native_platform.contains('auto')
395     egl_native_platform = _platforms[0]
396   endif
397 endif
398
399 if with_egl and not _platforms.contains(egl_native_platform)
400   error('-Degl-native-platform does not specify an enabled platform')
401 endif
402
403 if 'x11' in _platforms
404   _platforms += 'xcb'
405 endif
406
407 foreach platform : _platforms
408   pre_args += '-DHAVE_@0@_PLATFORM'.format(platform.to_upper())
409 endforeach
410
411 if with_platform_android and get_option('platform-sdk-version') >= 29
412   # By default the NDK compiler, at least, emits emutls references instead of
413   # ELF TLS, even when building targeting newer API levels.  Make it actually do
414   # ELF TLS instead.
415   c_cpp_args += '-fno-emulated-tls'
416 endif
417
418 # -mtls-dialect=gnu2 speeds up non-initial-exec TLS significantly but requires
419 # full toolchain (including libc) support.
420 have_mtls_dialect = false
421 foreach c_arg : get_option('c_args')
422   if c_arg.startswith('-mtls-dialect=')
423     have_mtls_dialect = true
424     break
425   endif
426 endforeach
427 if not have_mtls_dialect
428   # need .run to check libc support. meson aborts when calling .run when
429   # cross-compiling, but because this is just an optimization we can skip it
430   if meson.is_cross_build() and not meson.can_run_host_binaries()
431     warning('cannot auto-detect -mtls-dialect when cross-compiling, using compiler default')
432   else
433     # -fpic to force dynamic tls, otherwise TLS relaxation defeats check
434     gnu2_test = cc.run('int __thread x; int main() { return x; }',
435                        args: ['-mtls-dialect=gnu2', '-fpic'],
436                        name: '-mtls-dialect=gnu2')
437     if gnu2_test.returncode() == 0 and (
438           # check for lld 13 bug: https://gitlab.freedesktop.org/mesa/mesa/-/issues/5665
439           host_machine.cpu_family() != 'x86_64' or
440           # get_linker_id misses LDFLAGS=-fuse-ld=lld: https://github.com/mesonbuild/meson/issues/6377
441           #cc.get_linker_id() != 'ld.lld' or
442           cc.links('''int __thread x; int y; int main() { __asm__(
443                 "leaq x@TLSDESC(%rip), %rax\n"
444                 "movq y@GOTPCREL(%rip), %rdx\n"
445                 "call *x@TLSCALL(%rax)\n"); }''', name: 'split TLSDESC')
446           )
447       c_cpp_args += '-mtls-dialect=gnu2'
448     endif
449   endif
450 endif
451
452 if with_glx != 'disabled'
453   if not (with_platform_x11 and with_any_opengl)
454     error('Cannot build GLX support without X11 platform support and at least one OpenGL API')
455   elif with_glx == 'xlib'
456     if not with_gallium
457       error('xlib based GLX requires at least one gallium driver')
458     elif not with_gallium_softpipe
459       error('xlib based GLX requires softpipe or llvmpipe.')
460     elif with_dri
461       error('xlib conflicts with any dri driver')
462     endif
463   elif with_glx == 'dri'
464     if not with_shared_glapi
465       error('dri based GLX requires shared-glapi')
466     endif
467   endif
468 endif
469
470 with_glvnd = get_option('glvnd')
471 glvnd_vendor_name = get_option('glvnd-vendor-name')
472 if with_glvnd
473   if with_platform_windows
474     error('glvnd cannot be used on Windows')
475   elif with_glx == 'xlib'
476     error('Cannot build glvnd support for GLX that is not DRI based.')
477   elif with_glx == 'disabled' and not with_egl
478     error('glvnd requires DRI based GLX and/or EGL')
479   endif
480   if get_option('egl-lib-suffix') != ''
481     error('''EGL lib suffix can't be used with libglvnd''')
482   endif
483 endif
484
485 if with_vulkan_icd_dir == ''
486   with_vulkan_icd_dir = join_paths(get_option('datadir'), 'vulkan/icd.d')
487 endif
488
489 # GNU/Hurd includes egl_dri2, without drm.
490 with_dri2 = (with_dri or with_any_vk) and (with_dri_platform == 'drm' or
491   host_machine.system() == 'gnu')
492 with_dri3 = get_option('dri3').disable_auto_if(not (system_has_kms_drm and with_dri2)).allowed()
493
494 if with_any_vk and (with_platform_x11 and not with_dri3)
495   error('Vulkan drivers require dri3 for X11 support')
496 endif
497 if with_dri
498   if with_glx == 'disabled' and not with_egl and not with_gbm
499     error('building dri drivers require at least one windowing system')
500   endif
501 endif
502
503 if with_gallium_kmsro and (with_platform_x11 and not with_dri3)
504   error('kmsro requires dri3 for X11 support')
505 endif
506
507 dep_dxheaders = null_dep
508 if with_gallium_d3d12 or with_microsoft_clc or with_microsoft_vk
509   dep_dxheaders = dependency('directx-headers', required : false)
510   if not dep_dxheaders.found()
511     dep_dxheaders = dependency('DirectX-Headers',
512       version : '>= 1.610.0',
513       fallback : ['DirectX-Headers', 'dep_dxheaders'],
514       required : with_gallium_d3d12 or with_microsoft_vk
515     )
516   endif
517 endif
518
519 _with_gallium_d3d12_video = get_option('gallium-d3d12-video')
520 with_gallium_d3d12_video = false
521 if with_gallium_d3d12 and not _with_gallium_d3d12_video.disabled()
522   with_gallium_d3d12_video = true
523   pre_args += '-DHAVE_GALLIUM_D3D12_VIDEO'
524 endif
525
526 _vdpau_drivers = [
527   with_gallium_d3d12_video,
528   with_gallium_nouveau,
529   with_gallium_r600,
530   with_gallium_radeonsi,
531   with_gallium_virgl,
532 ]
533
534 vdpau = get_option('gallium-vdpau') \
535   .require(system_has_kms_drm, error_message : 'VDPAU state tracker can only be build on unix-like OSes.') \
536   .require(with_platform_x11, error_message : 'VDPAU state tracker requires X11 support.') \
537   .require(_vdpau_drivers.contains(true), error_message : 'VDPAU state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau, d3d12 (with option gallium-d3d12-video, virgl).') 
538
539 dep_vdpau = dependency('vdpau', version : '>= 1.1', required : vdpau)
540 if dep_vdpau.found()
541   dep_vdpau = dep_vdpau.partial_dependency(compile_args : true)
542   pre_args += '-DHAVE_ST_VDPAU'
543 endif
544 with_gallium_vdpau = dep_vdpau.found()
545
546 vdpau_drivers_path = get_option('vdpau-libs-path')
547 if vdpau_drivers_path == ''
548   vdpau_drivers_path = join_paths(get_option('libdir'), 'vdpau')
549 endif
550
551 if with_vulkan_overlay_layer or with_aco_tests or with_amd_vk or with_intel_vk
552   prog_glslang = find_program('glslangValidator', native : true)
553   if run_command(prog_glslang, [ '--quiet', '--version' ], check : false).returncode() == 0
554     glslang_quiet = ['--quiet']
555   else
556     glslang_quiet = []
557   endif
558 endif
559
560 dep_xv = null_dep
561 _omx = get_option('gallium-omx')
562 if not system_has_kms_drm
563   if ['auto', 'disabled'].contains(_omx)
564     _omx = 'disabled'
565   else
566     error('OMX state tracker can only be built on unix-like OSes.')
567   endif
568 elif not (with_gallium_r600 or with_gallium_radeonsi or with_gallium_nouveau)
569   if ['auto', 'disabled'].contains(_omx)
570     _omx = 'disabled'
571   else
572     error('OMX state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau.')
573   endif
574 endif
575 with_gallium_omx = _omx
576 dep_omx = null_dep
577 dep_omx_other = []
578 if ['auto', 'bellagio'].contains(_omx)
579   dep_omx = dependency(
580     'libomxil-bellagio', required : _omx == 'bellagio'
581   )
582   if dep_omx.found()
583     with_gallium_omx = 'bellagio'
584   endif
585 endif
586 if ['auto', 'tizonia'].contains(_omx)
587   if with_dri and with_egl
588     dep_omx = dependency(
589       'libtizonia', version : '>= 0.10.0',
590       required : _omx == 'tizonia',
591     )
592     dep_omx_other = [
593       dependency('libtizplatform', required : _omx == 'tizonia'),
594       dependency('tizilheaders', required : _omx == 'tizonia'),
595     ]
596     if dep_omx.found() and dep_omx_other[0].found() and dep_omx_other[1].found()
597       with_gallium_omx = 'tizonia'
598     endif
599   elif _omx == 'tizonia'
600     error('OMX-Tizonia state tracker requires dri and egl')
601   endif
602 endif
603 if _omx == 'auto'
604   with_gallium_omx = 'disabled'
605 else
606   with_gallium_omx = _omx
607 endif
608
609 pre_args += [
610   '-DENABLE_ST_OMX_BELLAGIO=' + (with_gallium_omx == 'bellagio' ? '1' : '0'),
611   '-DENABLE_ST_OMX_TIZONIA=' + (with_gallium_omx == 'tizonia' ? '1' : '0'),
612 ]
613
614
615 omx_drivers_path = get_option('omx-libs-path')
616
617 if with_gallium_omx != 'disabled'
618   # Figure out where to put the omx driver.
619   # FIXME: this could all be vastly simplified by adding a 'defined_variable'
620   # argument to meson's get_variable method.
621   if omx_drivers_path == ''
622     _omx_libdir = dep_omx.get_variable(pkgconfig : 'libdir')
623     _omx_drivers_dir = dep_omx.get_variable(pkgconfig : 'pluginsdir')
624     if _omx_libdir == get_option('libdir')
625       omx_drivers_path = _omx_drivers_dir
626     else
627       _omx_base_dir = []
628       # This will fail on windows. Does OMX run on windows?
629       _omx_libdir = _omx_libdir.split('/')
630       _omx_drivers_dir = _omx_drivers_dir.split('/')
631       foreach o : _omx_drivers_dir
632         if not _omx_libdir.contains(o)
633           _omx_base_dir += o
634         endif
635       endforeach
636       omx_drivers_path = join_paths(get_option('libdir'), _omx_base_dir)
637     endif
638   endif
639 endif
640
641 _va_drivers = [
642   with_gallium_d3d12_video,
643   with_gallium_nouveau,
644   with_gallium_r600,
645   with_gallium_radeonsi,
646   with_gallium_virgl,
647 ]
648
649 _va = get_option('gallium-va') \
650   .require(_va_drivers.contains(true),
651            error_message : 'VA state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau, d3d12 (with option gallium-d3d12-video), virgl.')
652 _dep_va_name = host_machine.system() == 'windows' ? 'libva-win32' : 'libva'
653 dep_va = dependency(_dep_va_name, version : '>= 1.8.0', required : _va)
654 if dep_va.found()
655   dep_va_headers = dep_va.partial_dependency(compile_args : true)
656   if cc.has_header_symbol('va/va.h', 'VASurfaceAttribDRMFormatModifiers',
657                           dependencies: dep_va_headers)
658     pre_args += '-DHAVE_VA_SURFACE_ATTRIB_DRM_FORMAT_MODIFIERS'
659   endif
660 endif
661 with_gallium_va = dep_va.found()
662
663 va_drivers_path = get_option('va-libs-path')
664 if va_drivers_path == ''
665   va_drivers_path = join_paths(get_option('libdir'), 'dri')
666 endif
667
668 with_gallium_xa = get_option('gallium-xa') \
669   .require(system_has_kms_drm, error_message : 'XA state tracker can only be built on unix-like OSes.') \
670   .require(with_gallium_nouveau or with_gallium_freedreno or with_gallium_i915 or with_gallium_svga,
671            error_message : 'XA state tracker requires at least one of the following gallium drivers: nouveau, freedreno, i915, svga.') \
672   .allowed()
673
674 d3d_drivers_path = get_option('d3d-drivers-path')
675 if d3d_drivers_path == ''
676   d3d_drivers_path = join_paths(get_option('prefix'), get_option('libdir'), 'd3d')
677 endif
678
679 with_gallium_st_nine =  get_option('gallium-nine')
680 if with_gallium_st_nine
681   if not with_gallium_softpipe
682     error('The nine state tracker requires gallium softpipe/llvmpipe.')
683   elif not [
684              with_gallium_crocus,
685              with_gallium_freedreno,
686              with_gallium_i915,
687              with_gallium_iris,
688              with_gallium_nouveau,
689              with_gallium_panfrost,
690              with_gallium_r300,
691              with_gallium_r600,
692              with_gallium_radeonsi,
693              with_gallium_svga,
694              with_gallium_zink,
695            ].contains(true)
696     error('The nine state tracker requires at least one non-swrast gallium driver.')
697   endif
698   if not with_dri3
699     error('Using nine with wine requires dri3')
700   endif
701 endif
702 with_gallium_st_d3d10umd =  get_option('gallium-d3d10umd')
703 if with_gallium_st_d3d10umd
704   if not with_gallium_softpipe
705     error('The d3d10umd state tracker requires gallium softpipe/llvmpipe.')
706   endif
707 endif
708 _power8 = get_option('power8')
709 if _power8.allowed()
710   if host_machine.cpu_family() == 'ppc64' and host_machine.endian() == 'little'
711     if cc.get_id() == 'gcc' and cc.version().version_compare('< 4.8')
712       error('Altivec is not supported with gcc version < 4.8.')
713     endif
714     if cc.compiles('''
715         #include <altivec.h>
716         int main() {
717           vector unsigned char r;
718           vector unsigned int v = vec_splat_u32 (1);
719           r = __builtin_vec_vgbbd ((vector unsigned char) v);
720           return 0;
721         }''',
722         args : '-mpower8-vector',
723         name : 'POWER8 intrinsics')
724       pre_args += ['-D_ARCH_PWR8']
725       c_cpp_args += '-mpower8-vector'
726     elif _power8.enabled()
727       error('POWER8 intrinsic support required but not found.')
728     endif
729   endif
730 endif
731
732 if get_option('vmware-mks-stats')
733   if not with_gallium_svga
734     error('vmware-mks-stats requires gallium VMware/svga driver.')
735   endif
736   pre_args += '-DVMX86_STATS=1'
737 endif
738
739 _opencl = get_option('gallium-opencl')
740 _rtti = get_option('cpp_rtti')
741 if _opencl != 'disabled'
742   if not with_gallium
743     error('OpenCL Clover implementation requires at least one gallium driver.')
744   endif
745   if not _rtti
746     error('The Clover OpenCL state tracker requires rtti')
747   endif
748
749   with_libclc = true
750   with_gallium_opencl = true
751   with_opencl_icd = _opencl == 'icd'
752 else
753   with_gallium_opencl = false
754   with_opencl_icd = false
755 endif
756
757 with_gallium_rusticl = get_option('gallium-rusticl')
758 if with_gallium_rusticl
759   if not with_gallium
760     error('rusticl requires at least one gallium driver.')
761   endif
762
763   if meson.version().version_compare('< 1.0.0')
764     error('rusticl requires meson 1.0.0 or newer')
765   endif
766
767   add_languages('rust', required: true)
768
769   with_clc = true
770   with_libclc = true
771 endif
772
773 dep_clc = null_dep
774 if with_libclc
775   dep_clc = dependency('libclc')
776 endif
777
778 gl_pkgconfig_c_flags = []
779 if with_platform_x11
780   if with_glx == 'xlib'
781     pre_args += '-DUSE_XSHM'
782   else
783     pre_args += '-DGLX_INDIRECT_RENDERING'
784     if with_glx_direct
785       pre_args += '-DGLX_DIRECT_RENDERING'
786     endif
787     if with_dri_platform == 'drm'
788       pre_args += '-DGLX_USE_DRM'
789     elif with_dri_platform == 'apple'
790       pre_args += '-DGLX_USE_APPLEGL'
791     elif with_dri_platform == 'windows'
792       pre_args += '-DGLX_USE_WINDOWSGL'
793     endif
794   endif
795 endif
796
797 with_android_stub = get_option('android-stub')
798 if with_android_stub and not with_platform_android
799   error('`-D android-stub=true` makes no sense without `-D platforms=android`')
800 endif
801
802 with_libbacktrace = get_option('android-libbacktrace') \
803   .require(with_platform_android, error_message : '`-D android-libbacktrace=enabled` makes no sense without `-D platforms=android`') \
804   .disable_auto_if(not with_platform_android) \
805   .allowed()
806
807 if with_libbacktrace
808   cpp_args += '-DWITH_LIBBACKTRACE'
809 endif
810
811 if with_platform_android
812   dep_android_mapper4 = null_dep
813   if not with_android_stub
814     dep_android = [
815       dependency('cutils'),
816       dependency('hardware'),
817       dependency('sync'),
818     ]
819     if with_libbacktrace
820       dep_android += dependency('backtrace')
821     endif
822     if get_option('platform-sdk-version') >= 26
823       dep_android += dependency('nativewindow')
824     endif
825     if get_option('platform-sdk-version') >= 30
826       dep_android_mapper4 = dependency('android.hardware.graphics.mapper', version : '>= 4.0', required : false)
827     endif
828   endif
829   pre_args += [
830     '-DANDROID',
831     '-DANDROID_API_LEVEL=' + get_option('platform-sdk-version').to_string()
832   ]
833 endif
834
835 # On Android, seccomp kills the process on kernels without
836 # CONFIG_KCMP/CONFIG_CHECKPOINT_RESTORE if it attemps to use KCMP.
837 # Since we can't detect that, err on the side of caution and disable
838 # KCMP by default on Android.
839 if get_option('allow-kcmp') \
840     .disable_auto_if(with_platform_android) \
841     .allowed()
842   pre_args += '-DALLOW_KCMP'
843 endif
844
845 prog_python = import('python').find_installation('python3')
846 has_mako = run_command(
847   prog_python, '-c',
848   '''
849 from distutils.version import StrictVersion
850 import mako
851 assert StrictVersion(mako.__version__) >= StrictVersion("0.8.0")
852   ''', check: false)
853 if has_mako.returncode() != 0
854   error('Python (3.x) mako module >= 0.8.0 required to build mesa.')
855 endif
856
857 if cc.get_id() == 'gcc' and cc.version().version_compare('< 4.4.6')
858   error('When using GCC, version 4.4.6 or later is required.')
859 endif
860
861 # Support systems without ETIME (e.g. FreeBSD)
862 if cc.get_define('ETIME', prefix : '#include <errno.h>') == ''
863   pre_args += '-DETIME=ETIMEDOUT'
864 endif
865
866 # Define DEBUG for debug builds only (debugoptimized is not included on this one)
867 if get_option('buildtype') == 'debug'
868   pre_args += '-DDEBUG'
869 endif
870
871 with_shader_cache = get_option('shader-cache') \
872   .require(host_machine.system() != 'windows', error_message : 'Shader Cache does not currently work on Windows') \
873   .allowed() 
874
875 if with_shader_cache
876   pre_args += '-DENABLE_SHADER_CACHE'
877   if not get_option('shader-cache-default')
878     pre_args += '-DSHADER_CACHE_DISABLE_BY_DEFAULT'
879   endif
880
881   shader_cache_max_size = get_option('shader-cache-max-size')
882   if shader_cache_max_size != ''
883     pre_args += '-DMESA_SHADER_CACHE_MAX_SIZE="@0@"'.format(shader_cache_max_size)
884   endif
885 endif
886
887 # Check for GCC style builtins
888 foreach b : ['bswap32', 'bswap64', 'clz', 'clzll', 'ctz', 'expect', 'ffs',
889              'ffsll', 'popcount', 'popcountll', 'unreachable', 'types_compatible_p']
890   if cc.has_function(b)
891     pre_args += '-DHAVE___BUILTIN_@0@'.format(b.to_upper())
892   endif
893 endforeach
894
895 # check for GCC __attribute__
896 _attributes = [
897   'const', 'flatten', 'malloc', 'pure', 'unused', 'warn_unused_result',
898   'weak', 'format', 'packed', 'returns_nonnull', 'alias', 'noreturn',
899 ]
900 foreach a : cc.get_supported_function_attributes(_attributes)
901   pre_args += '-DHAVE_FUNC_ATTRIBUTE_@0@'.format(a.to_upper())
902 endforeach
903 if cc.has_function_attribute('visibility:hidden')
904   pre_args += '-DHAVE_FUNC_ATTRIBUTE_VISIBILITY'
905 endif
906 if cc.compiles('__uint128_t foo(void) { return 0; }',
907                name : '__uint128_t')
908   pre_args += '-DHAVE_UINT128'
909 endif
910
911 if cc.has_function('reallocarray')
912    pre_args += '-DHAVE_REALLOCARRAY'
913 endif
914 if cc.has_function('fmemopen')
915    pre_args += '-DHAVE_FMEMOPEN'
916 endif
917
918 # TODO: this is very incomplete
919 if ['linux', 'cygwin', 'gnu', 'freebsd', 'gnu/kfreebsd', 'haiku', 'android'].contains(host_machine.system())
920   pre_args += '-D_GNU_SOURCE'
921 elif host_machine.system() == 'sunos'
922   pre_args += '-D__EXTENSIONS__'
923 elif host_machine.system() == 'windows'
924   pre_args += [
925     '-D_WINDOWS', '-D_WIN32_WINNT=0x0A00', '-DWINVER=0x0A00',
926     '-DPIPE_SUBSYSTEM_WINDOWS_USER',
927     '-D_USE_MATH_DEFINES',  # XXX: scons didn't use this for mingw
928   ]
929   if cc.get_argument_syntax() == 'msvc'
930     pre_args += [
931       '-DVC_EXTRALEAN',
932       '-D_CRT_SECURE_NO_WARNINGS',
933       '-D_CRT_SECURE_NO_DEPRECATE',
934       '-D_SCL_SECURE_NO_WARNINGS',
935       '-D_SCL_SECURE_NO_DEPRECATE',
936       '-D_ALLOW_KEYWORD_MACROS',
937       '-D_HAS_EXCEPTIONS=0', # Tell C++ STL to not use exceptions
938       '-DNOMINMAX',
939     ]
940   else
941     # When the target is not mingw/ucrt
942     # NOTE: clang's stddef.h are conflict with mingw/ucrt's stddef.h
943     # So do not include headers that defined in clang for detecting
944     # _UCRT
945     if cc.compiles('''
946       #include <string.h>
947       #if defined(__MINGW32__) && defined(_UCRT)
948       #error
949       #endif
950       int main(void) { return 0; }''')
951       pre_args += ['-D__MSVCRT_VERSION__=0x0700']
952     endif
953   endif
954 elif host_machine.system() == 'openbsd'
955   pre_args += '-D_ISOC11_SOURCE'
956 endif
957
958 # Check for generic C arguments
959 c_msvc_compat_args = []
960 no_override_init_args = []
961 cpp_msvc_compat_args = []
962 ld_args_gc_sections = []
963 if cc.get_argument_syntax() == 'msvc'
964   _trial = [
965     '/wd4018',  # signed/unsigned mismatch
966     '/wd4056',  # overflow in floating-point constant arithmetic
967     '/wd4244',  # conversion from 'type1' to 'type2', possible loss of data
968     '/wd4267',  # 'var' : conversion from 'size_t' to 'type', possible loss of data
969     '/wd4305',  # truncation from 'type1' to 'type2'
970     '/wd4351',  # new behavior: elements of array 'array' will be default initialized
971     '/wd4756',  # overflow in constant arithmetic
972     '/wd4800',  # forcing value to bool 'true' or 'false' (performance warning)
973     '/wd4996',  # disabled deprecated POSIX name warnings
974     '/wd4291',  # no matching operator delete found
975     '/wd4146',  # unary minus operator applied to unsigned type, result still unsigned
976     '/wd4200',  # nonstandard extension used: zero-sized array in struct/union
977     '/wd4624',  # destructor was implicitly defined as deleted [from LLVM]
978     '/wd4309',  # 'initializing': truncation of constant value
979     '/wd4838',  # conversion from 'int' to 'const char' requires a narrowing conversion
980     '/wd5105',  # macro expansion producing 'defined' has undefined behavior (winbase.h, need Windows SDK upgrade)
981     '/we4020',  # Error when passing the wrong number of parameters
982     '/we4024',  # Error when passing different type of parameter
983     '/we4189',  # 'identifier' : local variable is initialized but not referenced
984     '/Zc:__cplusplus', #Set __cplusplus macro to match the /std:c++<version> on the command line
985   ]
986   c_args += cc.get_supported_arguments(_trial)
987   cpp_args += cpp.get_supported_arguments(_trial)
988 else
989   _trial_c = [
990     '-Werror=implicit-function-declaration',
991     '-Werror=missing-prototypes',
992     '-Werror=return-type',
993     '-Werror=empty-body',
994     '-Werror=incompatible-pointer-types',
995     '-Werror=int-conversion',
996     '-Wimplicit-fallthrough',
997     '-Wmisleading-indentation',
998     '-Wno-missing-field-initializers',
999     '-Wno-format-truncation',
1000     '-Wno-nonnull-compare',
1001     '-fno-math-errno',
1002     '-fno-trapping-math',
1003     '-Qunused-arguments',
1004     '-fno-common',
1005     # Clang
1006     '-Wno-microsoft-enum-value',
1007     '-Wno-unused-function',
1008   ]
1009   _trial_cpp = [
1010     '-Werror=return-type',
1011     '-Werror=empty-body',
1012     '-Wmisleading-indentation',
1013     '-Wno-non-virtual-dtor',
1014     '-Wno-missing-field-initializers',
1015     '-Wno-format-truncation',
1016     '-fno-math-errno',
1017     '-fno-trapping-math',
1018     '-Qunused-arguments',
1019     # Some classes use custom new operator which zeroes memory, however
1020     # gcc does aggressive dead-store elimination which threats all writes
1021     # to the memory before the constructor as "dead stores".
1022     # For now we disable this optimization.
1023     '-flifetime-dse=1',
1024     # Clang
1025     '-Wno-microsoft-enum-value',
1026   ]
1027
1028   # MinGW chokes on format specifiers and I can't get it all working
1029   if not (cc.get_argument_syntax() == 'gcc' and host_machine.system() == 'windows')
1030     _trial_c += ['-Werror=format', '-Wformat-security']
1031     _trial_cpp += ['-Werror=format', '-Wformat-security']
1032   endif
1033
1034   # FreeBSD annotated <pthread.h> but Mesa isn't ready
1035   if not (cc.get_id() == 'clang' and host_machine.system() == 'freebsd')
1036     _trial_c += ['-Werror=thread-safety']
1037   endif
1038
1039   # If the compiler supports it, put function and data symbols in their
1040   # own sections and GC the sections after linking.  This lets drivers
1041   # drop shared code unused by that specific driver (particularly
1042   # relevant for Vulkan drivers).
1043   if cc.links('static char unused() { return 5; } int main() { return 0; }',
1044               args : '-Wl,--gc-sections', name : 'gc-sections')
1045     ld_args_gc_sections += '-Wl,--gc-sections'
1046     _trial_c += ['-ffunction-sections', '-fdata-sections']
1047     _trial_cpp += ['-ffunction-sections', '-fdata-sections']
1048   endif
1049
1050   # Variables that are only used for assertions are considered unused when assertions
1051   # are disabled. Don't treat this as an error, since we build with -Werror even if
1052   # assertions are disabled.
1053   if get_option('b_ndebug') == 'true' or (get_option('buildtype') == 'release' and get_option('b_ndebug') == 'if-release')
1054     _trial_c += ['-Wno-unused-variable', '-Wno-unused-but-set-variable', '/wd4189']
1055     _trial_cpp += ['-Wno-unused-variable', '-Wno-unused-but-set-variable', '/wd4189']
1056   endif
1057
1058   c_args += cc.get_supported_arguments(_trial_c)
1059   cpp_args += cpp.get_supported_arguments(_trial_cpp)
1060
1061   no_override_init_args += cc.get_supported_arguments(
1062     ['-Wno-override-init', '-Wno-initializer-overrides']
1063   )
1064
1065   # Check for C and C++ arguments for MSVC compatibility. These are only used
1066   # in parts of the mesa code base that need to compile with MSVC, mainly
1067   # common code
1068   _trial_msvc = ['-Werror=pointer-arith', '-Werror=vla', '-Werror=gnu-empty-initializer']
1069   c_msvc_compat_args += cc.get_supported_arguments(_trial_msvc)
1070   cpp_msvc_compat_args += cpp.get_supported_arguments(_trial_msvc)
1071 endif
1072
1073 # set linker arguments
1074 if host_machine.system() == 'windows'
1075   if cc.get_argument_syntax() == 'msvc'
1076     add_project_link_arguments(
1077       '/fixed:no',
1078       '/dynamicbase',
1079       '/nxcompat',
1080       language : ['c', 'cpp'],
1081     )
1082     if get_option('buildtype') != 'debug'
1083       add_project_link_arguments(
1084         '/incremental:no',
1085         language : ['c', 'cpp'],
1086       )
1087     endif
1088   else
1089     add_project_link_arguments(
1090       cc.get_supported_link_arguments(
1091         '-Wl,--nxcompat',
1092         '-Wl,--dynamicbase',
1093         '-static-libgcc',
1094         '-static-libstdc++',
1095       ),
1096       language : ['c'],
1097     )
1098     add_project_link_arguments(
1099       cpp.get_supported_link_arguments(
1100         '-Wl,--nxcompat',
1101         '-Wl,--dynamicbase',
1102         '-static-libgcc',
1103         '-static-libstdc++',
1104       ),
1105       language : ['cpp'],
1106     )
1107   endif
1108 endif
1109
1110 sse2_arg = []
1111 sse2_args = []
1112 sse41_args = []
1113 with_sse41 = false
1114 if host_machine.cpu_family().startswith('x86')
1115   pre_args += '-DUSE_SSE41'
1116   with_sse41 = true
1117
1118   if cc.get_id() != 'msvc'
1119     sse41_args = ['-msse4.1']
1120
1121     if host_machine.cpu_family() == 'x86'
1122       # x86_64 have sse2 by default, so sse2 args only for x86
1123       sse2_arg = ['-msse2', '-mfpmath=sse']
1124       sse2_args = [sse2_arg, '-mstackrealign']
1125       if get_option('sse2')
1126         # These settings make generated GCC code match MSVC and follow
1127         # GCC advice on https://gcc.gnu.org/wiki/FloatingPointMath#x86note
1128         #
1129         # NOTE: We need to ensure stack is realigned given that we
1130         # produce shared objects, and have no control over the stack
1131         # alignment policy of the application. Therefore we need
1132         # -mstackrealign or -mincoming-stack-boundary=2.
1133         #
1134         # XXX: We could have SSE without -mstackrealign if we always used
1135         # __attribute__((force_align_arg_pointer)), but that's not
1136         # always the case.
1137         c_cpp_args += sse2_args
1138         # sse2_args are adopted into c_cpp_args to avoid duplicated sse2 command line args
1139         sse2_arg = []
1140         sse2_args = []
1141       else
1142         # GCC on x86 (not x86_64) with -msse* assumes a 16 byte aligned stack, but
1143         # that's not guaranteed
1144         sse41_args += '-mstackrealign'
1145       endif
1146     endif
1147   endif
1148 endif
1149
1150 # Check for GCC style atomics
1151 dep_atomic = null_dep
1152
1153 if cc.compiles('''#include <stdint.h>
1154                   int main() {
1155                     struct {
1156                       uint64_t *v;
1157                     } x;
1158                     return (int)__atomic_load_n(x.v, __ATOMIC_ACQUIRE) &
1159                            (int)__atomic_add_fetch(x.v, (uint64_t)1, __ATOMIC_ACQ_REL);
1160
1161                   }''',
1162                name : 'GCC atomic builtins')
1163   pre_args += '-DUSE_GCC_ATOMIC_BUILTINS'
1164
1165   # Not all atomic calls can be turned into lock-free instructions, in which
1166   # GCC will make calls into the libatomic library. Check whether we need to
1167   # link with -latomic.
1168   #
1169   # This can happen for 64-bit atomic operations on 32-bit architectures such
1170   # as ARM.
1171   if not cc.links('''#include <stdint.h>
1172                      int main() {
1173                        struct {
1174                          uint64_t *v;
1175                        } x;
1176                        return (int)__atomic_load_n(x.v, __ATOMIC_ACQUIRE) &
1177                               (int)__atomic_add_fetch(x.v, (uint64_t)1, __ATOMIC_ACQ_REL);
1178                      }''',
1179                   name : 'GCC atomic builtins required -latomic')
1180     dep_atomic = cc.find_library('atomic')
1181   endif
1182 endif
1183 if not cc.links('''#include <stdint.h>
1184                    uint64_t v;
1185                    int main() {
1186                      return __sync_add_and_fetch(&v, (uint64_t)1);
1187                    }''',
1188                 dependencies : dep_atomic,
1189                 name : 'GCC 64bit atomics')
1190   pre_args += '-DMISSING_64BIT_ATOMICS'
1191 endif
1192
1193 dep_ws2_32 = cc.find_library('ws2_32', required : with_platform_windows)
1194
1195 # TODO: shared/static? Is this even worth doing?
1196
1197 with_asm_arch = ''
1198 if host_machine.cpu_family() == 'x86'
1199   if system_has_kms_drm or host_machine.system() == 'gnu'
1200     with_asm_arch = 'x86'
1201     pre_args += ['-DUSE_X86_ASM']
1202
1203     if with_glx_read_only_text
1204       pre_args += ['-DGLX_X86_READONLY_TEXT']
1205     endif
1206   endif
1207 elif host_machine.cpu_family() == 'x86_64'
1208   if system_has_kms_drm
1209     with_asm_arch = 'x86_64'
1210     pre_args += ['-DUSE_X86_64_ASM']
1211   endif
1212 elif host_machine.cpu_family() == 'arm'
1213   if system_has_kms_drm
1214     with_asm_arch = 'arm'
1215     pre_args += ['-DUSE_ARM_ASM']
1216   endif
1217 elif host_machine.cpu_family() == 'aarch64'
1218   if system_has_kms_drm
1219     with_asm_arch = 'aarch64'
1220     pre_args += ['-DUSE_AARCH64_ASM']
1221   endif
1222 elif host_machine.cpu_family() == 'sparc64'
1223   if system_has_kms_drm
1224     with_asm_arch = 'sparc'
1225     pre_args += ['-DUSE_SPARC_ASM']
1226   endif
1227 elif host_machine.cpu_family() == 'ppc64' and host_machine.endian() == 'little'
1228   if system_has_kms_drm
1229     with_asm_arch = 'ppc64le'
1230     pre_args += ['-DUSE_PPC64LE_ASM']
1231   endif
1232 elif host_machine.cpu_family() == 'mips64' and host_machine.endian() == 'little'
1233   if system_has_kms_drm
1234     with_asm_arch = 'mips64el'
1235     pre_args += ['-DUSE_MIPS64EL_ASM']
1236   endif
1237 elif host_machine.cpu_family() == 'loongarch64'
1238   if system_has_kms_drm
1239     with_asm_arch = 'loongarch64'
1240     pre_args += ['-DUSE_LOONGARCH64_ASM']
1241   endif
1242 endif
1243
1244 # Check for standard headers and functions
1245 if (cc.has_header_symbol('sys/sysmacros.h', 'major') and
1246   cc.has_header_symbol('sys/sysmacros.h', 'minor') and
1247   cc.has_header_symbol('sys/sysmacros.h', 'makedev'))
1248   pre_args += '-DMAJOR_IN_SYSMACROS'
1249 endif
1250 if (cc.has_header_symbol('sys/mkdev.h', 'major') and
1251   cc.has_header_symbol('sys/mkdev.h', 'minor') and
1252   cc.has_header_symbol('sys/mkdev.h', 'makedev'))
1253   pre_args += '-DMAJOR_IN_MKDEV'
1254 endif
1255
1256 if cc.check_header('sched.h')
1257   pre_args += '-DHAS_SCHED_H'
1258   if cc.has_function('sched_getaffinity')
1259     pre_args += '-DHAS_SCHED_GETAFFINITY'
1260   endif
1261 endif
1262
1263 if not ['linux'].contains(host_machine.system())
1264   # Deprecated on Linux and requires <sys/types.h> on FreeBSD and OpenBSD
1265   if cc.check_header('sys/sysctl.h', prefix : '#include <sys/types.h>')
1266     pre_args += '-DHAVE_SYS_SYSCTL_H'
1267   endif
1268 endif
1269
1270 foreach h : ['xlocale.h', 'linux/futex.h', 'endian.h', 'dlfcn.h', 'sys/shm.h',
1271              'cet.h', 'pthread_np.h', 'renderdoc_app.h', 'sys/inotify.h']
1272   if cc.check_header(h)
1273     pre_args += '-DHAVE_@0@'.format(h.to_upper().underscorify())
1274   endif
1275 endforeach
1276
1277 functions_to_detect = {
1278   'strtof': '',
1279   'mkostemp': '',
1280   'timespec_get': '#include <time.h>',
1281   'memfd_create': '',
1282   'random_r': '',
1283   'flock': '',
1284   'strtok_r': '',
1285   'getrandom': '',
1286   'qsort_s': '',
1287   'posix_fallocate': '',
1288 }
1289
1290 foreach f, prefix: functions_to_detect
1291   if cc.has_function(f, prefix: prefix)
1292     pre_args += '-DHAVE_@0@'.format(f.to_upper())
1293   endif
1294 endforeach
1295
1296 if cpp.links('''
1297     #define _GNU_SOURCE
1298     #include <stdlib.h>
1299
1300     static int dcomp(const void *l, const void *r, void *t) { return 0; }
1301
1302     int main(int ac, char **av) {
1303       int arr[] = { 1 };
1304       void *t = NULL;
1305       qsort_r((void*)&arr[0], 1, 1, dcomp, t);
1306       return (0);
1307     }''',
1308     args : pre_args,
1309     name : 'GNU qsort_r')
1310   pre_args += '-DHAVE_GNU_QSORT_R'
1311 elif cpp.links('''
1312     #include <stdlib.h>
1313
1314     static int dcomp(void *t, const void *l, const void *r) { return 0; }
1315
1316     int main(int ac, char **av) {
1317       int arr[] = { 1 };
1318       void *t = NULL;
1319       qsort_r((void*)&arr[0], 1, 1, t, dcomp);
1320       return (0);
1321     }''',
1322     args : pre_args,
1323     name : 'BSD qsort_r')
1324   pre_args += '-DHAVE_BSD_QSORT_R'
1325 endif
1326
1327 if cc.has_header_symbol('time.h', 'struct timespec')
1328    pre_args += '-DHAVE_STRUCT_TIMESPEC'
1329 endif
1330
1331 with_c11_threads = false
1332 if cc.has_function('thrd_create', prefix: '#include <threads.h>')
1333   if with_platform_android
1334     # Current only Android's c11 <threads.h> are verified
1335     pre_args += '-DHAVE_THRD_CREATE'
1336     with_c11_threads = true
1337   endif
1338 endif
1339
1340 if cc.has_header_symbol('errno.h', 'program_invocation_name',
1341                         args : '-D_GNU_SOURCE')
1342    pre_args += '-DHAVE_PROGRAM_INVOCATION_NAME'
1343 elif with_tools.contains('intel')
1344   error('Intel tools require the program_invocation_name variable')
1345 endif
1346
1347 if cc.has_header_symbol('math.h', 'issignaling',
1348                         args : '-D_GNU_SOURCE')
1349    pre_args += '-DHAVE_ISSIGNALING'
1350 endif
1351
1352 # MinGW provides a __builtin_posix_memalign function, but not a posix_memalign.
1353 # This means that this check will succeed, but then compilation will later
1354 # fail. MSVC doesn't have this function at all, so only check for it on
1355 # non-windows platforms.
1356 if host_machine.system() != 'windows'
1357   if cc.has_function('posix_memalign')
1358     pre_args += '-DHAVE_POSIX_MEMALIGN'
1359   endif
1360 endif
1361
1362 if cc.has_member('struct dirent', 'd_type', prefix: '''#include <sys/types.h>
1363    #include <dirent.h>''')
1364    pre_args += '-DHAVE_DIRENT_D_TYPE'
1365 endif
1366
1367 # strtod locale support
1368 if cc.links('''
1369     #define _GNU_SOURCE
1370     #include <stdlib.h>
1371     #include <locale.h>
1372     #ifdef HAVE_XLOCALE_H
1373     #include <xlocale.h>
1374     #endif
1375     int main() {
1376       locale_t loc = newlocale(LC_CTYPE_MASK, "C", NULL);
1377       const char *s = "1.0";
1378       char *end;
1379       double d = strtod_l(s, end, loc);
1380       float f = strtof_l(s, end, loc);
1381       freelocale(loc);
1382       return 0;
1383     }''',
1384     args : pre_args,
1385     name : 'strtod has locale support')
1386   pre_args += '-DHAVE_STRTOD_L'
1387 endif
1388
1389 # Check for some linker flags
1390 ld_args_bsymbolic = []
1391 if cc.links('int main() { return 0; }', args : '-Wl,-Bsymbolic', name : 'Bsymbolic')
1392   ld_args_bsymbolic += '-Wl,-Bsymbolic'
1393 endif
1394 with_ld_version_script = false
1395 if cc.links('int main() { return 0; }',
1396             args : '-Wl,--version-script=@0@'.format(
1397               join_paths(meson.current_source_dir(), 'build-support/conftest.map')),
1398             name : 'version-script')
1399   with_ld_version_script = true
1400 endif
1401 with_ld_dynamic_list = false
1402 if cc.links('int main() { return 0; }',
1403             args : '-Wl,--dynamic-list=@0@'.format(
1404               join_paths(meson.current_source_dir(), 'build-support/conftest.dyn')),
1405             name : 'dynamic-list')
1406   with_ld_dynamic_list = true
1407 endif
1408
1409 ld_args_build_id = cc.get_supported_link_arguments('-Wl,--build-id=sha1')
1410
1411 # check for dl support
1412 dep_dl = null_dep
1413 if host_machine.system() != 'windows'
1414   if not cc.has_function('dlopen')
1415     dep_dl = cc.find_library('dl', required : true)
1416   endif
1417   if cc.has_function('dladdr', dependencies : dep_dl)
1418     # This is really only required for util/disk_cache.h
1419     pre_args += '-DHAVE_DLADDR'
1420   endif
1421 endif
1422
1423 if cc.has_function('dl_iterate_phdr')
1424   pre_args += '-DHAVE_DL_ITERATE_PHDR'
1425 elif with_intel_vk or with_intel_hasvk
1426   error('Intel "Anvil" Vulkan driver requires the dl_iterate_phdr function')
1427 endif
1428
1429 # only used in Iris and ANV
1430 if with_any_intel and ['x86', 'x86_64'].contains(host_machine.cpu_family())
1431   pre_args += '-DSUPPORT_INTEL_INTEGRATED_GPUS'
1432 endif
1433
1434 if get_option('intel-xe-kmd').enabled()
1435   pre_args += '-DINTEL_XE_KMD_SUPPORTED'
1436 endif
1437
1438 if with_intel_hasvk and host_machine.cpu_family().startswith('x86') == false
1439   error('Intel "hasvk" Vulkan driver requires x86 or x86_64 CPU family')
1440 endif
1441
1442 if with_gallium_crocus and host_machine.cpu_family().startswith('x86') == false
1443   error('Intel "crocus" Gallium driver requires x86 or x86_64 CPU family')
1444 endif
1445
1446 if with_gallium_i915 and host_machine.cpu_family().startswith('x86') == false
1447   error('Intel "i915" Gallium driver requires x86 or x86_64 CPU family')
1448 endif
1449
1450 # Determine whether or not the rt library is needed for time functions
1451 if host_machine.system() == 'windows' or cc.has_function('clock_gettime')
1452   dep_clock = null_dep
1453 else
1454   dep_clock = cc.find_library('rt')
1455 endif
1456
1457 dep_zlib = dependency('zlib', version : '>= 1.2.3',
1458                       fallback : ['zlib', 'zlib_dep'],
1459                       required : get_option('zlib'))
1460 if dep_zlib.found()
1461   pre_args += '-DHAVE_ZLIB'
1462 endif
1463
1464 dep_zstd = dependency('libzstd', required : get_option('zstd'))
1465 if dep_zstd.found()
1466   pre_args += '-DHAVE_ZSTD'
1467 endif
1468
1469 with_compression = dep_zlib.found() or dep_zstd.found()
1470 if with_compression
1471   pre_args += '-DHAVE_COMPRESSION'
1472 elif with_shader_cache
1473   error('Shader Cache requires compression')
1474 endif
1475
1476 if host_machine.system() == 'windows'
1477   # For MSVC and MinGW we aren't using pthreads, and dependency('threads') will add linkage
1478   # to pthread for MinGW, so leave the dependency null_dep for Windows. For Windows linking to
1479   # kernel32 is enough for c11/threads.h and it's already linked by meson by default
1480   dep_thread = null_dep
1481 else
1482   dep_thread = dependency('threads')
1483 endif
1484 if dep_thread.found()
1485   pre_args += '-DHAVE_PTHREAD'
1486   if host_machine.system() != 'netbsd' and cc.has_function(
1487       'pthread_setaffinity_np',
1488       dependencies : dep_thread,
1489       prefix : '#include <pthread.h>',
1490       args : '-D_GNU_SOURCE')
1491     pre_args += '-DHAVE_PTHREAD_SETAFFINITY'
1492   endif
1493 endif
1494
1495 # We don't have expat on Android or Windows, which is a needed dep for xmlconfig
1496 opt_xmlconfig = get_option('xmlconfig') \
1497   .require(not (with_platform_android or with_platform_windows),
1498            error_message : 'xmlconfig not available on Android or Windows')
1499
1500 if host_machine.system() == 'darwin'
1501   dep_expat = meson.get_compiler('c').find_library('expat', required : opt_xmlconfig)
1502 else
1503   dep_expat = dependency('expat', fallback : ['expat', 'expat_dep'],
1504                          required : opt_xmlconfig)
1505 endif
1506 use_xmlconfig = dep_expat.found()
1507   
1508 # Predefined macros for windows
1509 if host_machine.system() == 'windows'
1510   pre_args += '-DWIN32_LEAN_AND_MEAN' # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx
1511 endif
1512 # this only exists on linux so either this is linux and it will be found, or
1513 # it's not linux and wont
1514 dep_m = cc.find_library('m', required : false)
1515
1516 if host_machine.system() == 'windows'
1517   dep_regex = meson.get_compiler('c').find_library('regex', required : false)
1518   if not dep_regex.found()
1519     dep_regex = declare_dependency(compile_args : ['-DNO_REGEX'])
1520   endif
1521 else
1522   dep_regex = null_dep
1523 endif
1524
1525 if with_platform_haiku
1526   dep_network = cc.find_library('network')
1527 endif
1528
1529 dep_futex = null_dep
1530 if host_machine.system() == 'windows'
1531   if (get_option('min-windows-version') < 8)
1532     pre_args += '-DWINDOWS_NO_FUTEX'
1533   else
1534     dep_futex = cc.find_library('synchronization', required : true)
1535   endif
1536 endif
1537
1538 # Check for libdrm. Various drivers have different libdrm version requirements,
1539 # but we always want to use the same version for all libdrm modules. That means
1540 # even if driver foo requires 2.4.0 and driver bar requires 2.4.3, if foo and
1541 # bar are both on use 2.4.3 for both of them
1542 dep_libdrm_amdgpu = null_dep
1543 dep_libdrm_radeon = null_dep
1544 dep_libdrm_nouveau = null_dep
1545 dep_libdrm_intel = null_dep
1546
1547 _drm_amdgpu_ver = '2.4.110'
1548 _drm_radeon_ver = '2.4.71'
1549 _drm_nouveau_ver = '2.4.102'
1550 _drm_intel_ver = '2.4.75'
1551 _drm_ver = '2.4.109'
1552
1553 _libdrm_checks = [
1554   ['intel', with_gallium_i915],
1555   ['amdgpu', (with_amd_vk and not with_platform_windows) or with_gallium_radeonsi],
1556   ['radeon', (with_gallium_radeonsi or with_gallium_r300 or with_gallium_r600)],
1557   ['nouveau', with_gallium_nouveau],
1558 ]
1559
1560 # Loop over the enables versions and get the highest libdrm requirement for all
1561 # active drivers.
1562 _drm_blame = ''
1563 foreach d : _libdrm_checks
1564   ver = get_variable('_drm_@0@_ver'.format(d[0]))
1565   if d[1] and ver.version_compare('>' + _drm_ver)
1566     _drm_ver = ver
1567     _drm_blame = d[0]
1568   endif
1569 endforeach
1570 if _drm_blame != ''
1571   message('libdrm @0@ needed because @1@ has the highest requirement'.format(_drm_ver, _drm_blame))
1572 endif
1573
1574 # Then get each libdrm module
1575 foreach d : _libdrm_checks
1576   if d[1]
1577     set_variable(
1578       'dep_libdrm_' + d[0],
1579       dependency('libdrm_' + d[0], version : '>=' + _drm_ver)
1580     )
1581   endif
1582 endforeach
1583
1584 with_gallium_drisw_kms = false
1585 if system_has_kms_drm
1586   dep_libdrm = dependency(
1587     'libdrm', version : '>=' + _drm_ver,
1588     # GNU/Hurd includes egl_dri2, without drm.
1589     required : (with_dri2 and host_machine.system() != 'gnu') or with_dri3
1590   )
1591 else
1592   # We should prevent libdrm from being available when the target doesn't have it to avoid transitive
1593   # dependencies (such as vk-runtime) linking to it
1594   dep_libdrm = null_dep
1595 endif
1596 if dep_libdrm.found()
1597   pre_args += '-DHAVE_LIBDRM'
1598   if with_dri_platform == 'drm' and with_dri
1599     with_gallium_drisw_kms = true
1600   endif
1601 endif
1602
1603 dep_libudev = dependency('libudev', required : false)
1604 if dep_libudev.found()
1605   pre_args += '-DHAVE_LIBUDEV'
1606 endif
1607
1608 llvm_modules = ['bitwriter', 'engine', 'mcdisassembler', 'mcjit', 'core', 'executionengine', 'scalaropts', 'transformutils', 'instcombine']
1609 llvm_optional_modules = ['coroutines']
1610 if with_amd_vk or with_gallium_radeonsi or with_gallium_r600
1611   llvm_modules += ['amdgpu', 'bitreader', 'ipo']
1612   if with_gallium_r600
1613     llvm_modules += 'asmparser'
1614   endif
1615 endif
1616 if with_gallium_opencl
1617   llvm_modules += [
1618     'linker', 'coverage', 'instrumentation', 'ipo', 'irreader',
1619     'lto', 'option', 'objcarcopts', 'profiledata'
1620   ]
1621   # all-targets is needed to support static linking LLVM build with multiple targets
1622   # windowsdriver is needded with LLVM>=15, but we don't know what LLVM verrsion we are using yet
1623   llvm_optional_modules += ['all-targets', 'frontendopenmp', 'windowsdriver']
1624 endif
1625 if with_clc
1626   llvm_modules += ['coverage', 'target', 'linker', 'irreader', 'option', 'libdriver', 'lto']
1627   # all-targets is needed to support static linking LLVM build with multiple targets.
1628   # windowsdriver is needded with LLVM>=15 and frontendhlsl is needed with LLVM>=16,
1629   # but we don't know what LLVM version we are using yet
1630   llvm_optional_modules += ['all-targets', 'windowsdriver', 'frontendhlsl']
1631 endif
1632 draw_with_llvm = get_option('draw-use-llvm')
1633 if draw_with_llvm
1634   llvm_modules += 'native'
1635   # lto is needded with LLVM>=15, but we don't know what LLVM verrsion we are using yet
1636   llvm_optional_modules += ['lto']
1637 endif
1638
1639 if with_intel_clc
1640   _llvm_version = '>= 13.0.0'
1641 elif with_amd_vk or with_gallium_radeonsi or with_gallium_opencl
1642   _llvm_version = '>= 11.0.0'
1643 elif with_clc
1644   _llvm_version = '>= 10.0.0'
1645 else
1646   _llvm_version = '>= 5.0.0'
1647 endif
1648
1649 _shared_llvm = get_option('shared-llvm') \
1650   .disable_auto_if(host_machine.system() == 'windows') \
1651   .allowed()
1652
1653 _llvm = get_option('llvm')
1654 dep_llvm = null_dep
1655 with_llvm = false
1656 if _llvm.allowed()
1657   dep_llvm = dependency(
1658     'llvm',
1659     version : _llvm_version,
1660     modules : llvm_modules,
1661     optional_modules : llvm_optional_modules,
1662     required : (
1663       with_amd_vk or with_gallium_radeonsi or with_gallium_opencl or with_clc
1664       or _llvm.enabled()
1665     ),
1666     static : not _shared_llvm,
1667     fallback : ['llvm', 'dep_llvm'],
1668     include_type : 'system',
1669   )
1670   with_llvm = dep_llvm.found()
1671 endif
1672 if with_llvm
1673   pre_args += '-DLLVM_AVAILABLE'
1674   pre_args += '-DMESA_LLVM_VERSION_STRING="@0@"'.format(dep_llvm.version())
1675   pre_args += '-DLLVM_IS_SHARED=@0@'.format(_shared_llvm.to_int())
1676
1677   if draw_with_llvm
1678     pre_args += '-DDRAW_LLVM_AVAILABLE'
1679   elif with_swrast_vk
1680     error('Lavapipe requires LLVM draw support.')
1681   endif
1682
1683   if host_machine.system() != 'windows'
1684     # LLVM can be built without rtti, turning off rtti changes the ABI of C++
1685     # programs, so we need to build all C++ code in mesa without rtti as well to
1686     # ensure that linking works. Note that Win32 compilers does handle mismatching RTTI
1687     # without issues, so only apply this for other compilers.
1688     if dep_llvm.type_name() == 'internal'
1689       _llvm_rtti = subproject('llvm').get_variable('has_rtti', true)
1690     else
1691       # The CMake finder will return 'ON', the llvm-config will return 'YES'
1692       _llvm_rtti = ['ON', 'YES'].contains(dep_llvm.get_variable(cmake : 'LLVM_ENABLE_RTTI', configtool: 'has-rtti'))
1693     endif
1694     if _rtti != _llvm_rtti
1695       if _llvm_rtti
1696         error('LLVM was built with RTTI, cannot build Mesa with RTTI disabled. Remove cpp_rtti disable switch or use LLVM built without LLVM_ENABLE_RTTI.')
1697       else
1698         error('LLVM was built without RTTI, so Mesa must also disable RTTI. Use an LLVM built with LLVM_ENABLE_RTTI or add cpp_rtti=false.')
1699       endif
1700     endif
1701   endif
1702
1703   if cc.get_argument_syntax() == 'msvc'
1704     # Suppress "/DELAYLOAD:ole32.dll/shell32.dll ignored" warnings that LLVM adds
1705     add_project_link_arguments(
1706       '/ignore:4199',
1707       language : ['c', 'cpp'],
1708     )
1709   endif
1710 elif with_amd_vk and with_aco_tests
1711   error('ACO tests require LLVM, but LLVM is disabled.')
1712 elif with_gallium_radeonsi or with_swrast_vk
1713   error('The following drivers require LLVM: RadeonSI, SWR, Lavapipe. One of these is enabled, but LLVM is disabled.')
1714 elif with_gallium_opencl
1715   error('The OpenCL "Clover" state tracker requires LLVM, but LLVM is disabled.')
1716 elif with_clc
1717   error('The CLC compiler requires LLVM, but LLVM is disabled.')
1718 else
1719   draw_with_llvm = false
1720 endif
1721
1722 with_opencl_spirv = (_opencl != 'disabled' and get_option('opencl-spirv')) or with_clc
1723 if with_opencl_spirv
1724   chosen_llvm_version_array = dep_llvm.version().split('.')
1725   chosen_llvm_version_major = chosen_llvm_version_array[0].to_int()
1726   chosen_llvm_version_minor = chosen_llvm_version_array[1].to_int()
1727
1728   # Require an SPIRV-LLVM-Translator version compatible with the chosen LLVM
1729   # one.
1730
1731   # This first version check is still needed as maybe LLVM 8.0 was picked but
1732   # we do not want to accept SPIRV-LLVM-Translator 8.0.0.1 as that version
1733   # does not have the required API and those are only available starting from
1734   # 8.0.1.3.
1735   _llvmspirvlib_min_version = '>= 8.0.1.3'
1736   if with_intel_clc
1737     _llvmspirvlib_min_version = '>= 13.0.0.0'
1738   endif
1739
1740   _llvmspirvlib_version = [
1741     _llvmspirvlib_min_version,
1742     '>= @0@.@1@'.format(chosen_llvm_version_major, chosen_llvm_version_minor),
1743     '< @0@.@1@'.format(chosen_llvm_version_major, chosen_llvm_version_minor + 1) ]
1744
1745   dep_spirv_tools = dependency('SPIRV-Tools', required : true, version : '>= 2018.0')
1746   # LLVMSPIRVLib is available at https://github.com/KhronosGroup/SPIRV-LLVM-Translator
1747   dep_llvmspirvlib = dependency('LLVMSPIRVLib', required : true, version : _llvmspirvlib_version)
1748 else
1749   dep_spirv_tools = null_dep
1750   dep_llvmspirvlib = null_dep
1751 endif
1752
1753 dep_clang = null_dep
1754 if with_clc
1755   llvm_libdir = dep_llvm.get_variable(cmake : 'LLVM_LIBRARY_DIR', configtool: 'libdir')
1756
1757   dep_clang = cpp.find_library('clang-cpp', dirs : llvm_libdir, required : false)
1758
1759   if not dep_clang.found() or not _shared_llvm
1760     clang_modules = [
1761       'clangBasic', 'clangAST', 'clangCodeGen', 'clangLex',
1762       'clangDriver', 'clangFrontend', 'clangFrontendTool',
1763       'clangHandleCXX', 'clangHandleLLVM', 'clangSerialization',
1764       'clangSema', 'clangParse', 'clangEdit', 'clangAnalysis'
1765     ]
1766     if dep_llvm.version().version_compare('>= 15.0')
1767       clang_modules += 'clangSupport'
1768     endif
1769     if dep_llvm.version().version_compare('>= 16.0')
1770       clang_modules += 'clangASTMatchers'
1771     endif
1772
1773     dep_clang = []
1774     foreach m : clang_modules
1775       dep_clang += cpp.find_library(m, dirs : llvm_libdir, required : true)
1776     endforeach
1777   endif
1778 endif
1779
1780 # Be explicit about only using this lib on Windows, to avoid picking
1781 # up random libs with the generic name 'libversion'
1782 dep_version = null_dep
1783 if host_machine.system() == 'windows'
1784   dep_version = cpp.find_library('version')
1785 endif
1786
1787 dep_elf = dependency('libelf', required : false)
1788 if not with_platform_windows and not dep_elf.found()
1789   dep_elf = cc.find_library('elf', required : false)
1790 endif
1791 if dep_elf.found()
1792   pre_args += '-DUSE_LIBELF'
1793 endif
1794
1795 dep_glvnd = null_dep
1796 if with_glvnd
1797   dep_glvnd = dependency('libglvnd', version : '>= 1.3.2')
1798   pre_args += '-DUSE_LIBGLVND=1'
1799 endif
1800
1801 dep_valgrind = dependency('valgrind', required : get_option('valgrind'))
1802 if dep_valgrind.found()
1803   pre_args += '-DHAVE_VALGRIND'
1804 endif
1805
1806 # AddressSanitizer's leak reports need all the symbols to be present at exit to
1807 # decode well, which runs afoul of our dlopen()/dlclose()ing of the DRI drivers.
1808 # Set a flag so we can skip the dlclose for asan builds.
1809 if ['address', 'address,undefined'].contains(get_option('b_sanitize'))
1810   asan_c_args = ['-DBUILT_WITH_ASAN=1']
1811 else
1812   asan_c_args = ['-DBUILT_WITH_ASAN=0']
1813 endif
1814
1815 yacc_is_bison = true
1816 needs_flex_bison = with_any_opengl or with_freedreno_vk or with_intel_tools or with_gallium
1817
1818 if build_machine.system() == 'windows'
1819   # Prefer the winflexbison versions, they're much easier to install and have
1820   # better windows support.
1821
1822   prog_flex = find_program('win_flex', required : false)
1823   if prog_flex.found()
1824     # windows compatibility (uses <io.h> instead of <unistd.h> and _isatty,
1825     # _fileno functions)
1826     prog_flex = [prog_flex, '--wincompat']
1827   else
1828     prog_flex = [find_program('flex', 'lex', required : needs_flex_bison, disabler : true)]
1829   endif
1830   # Force flex to use const keyword in prototypes, as relies on __cplusplus or
1831   # __STDC__ macro to determine whether it's safe to use const keyword
1832   prog_flex += '-DYY_USE_CONST='
1833
1834   prog_flex_cpp = prog_flex
1835   # Convince win_flex to use <inttypes.h> for C++ files
1836   # Note that we are using a C99 version here rather than C11,
1837   # because using a C11 version can cause the MSVC CRT headers to define
1838   # static_assert to _Static_assert, which breaks other parts of the CRT
1839   prog_flex_cpp += '-D__STDC_VERSION__=199901'
1840
1841   prog_bison = find_program('win_bison', required : false)
1842   if not prog_bison.found()
1843     prog_bison = find_program('bison', 'yacc', required : needs_flex_bison, disabler : true)
1844   endif
1845 else
1846   prog_bison = find_program('bison', required : false)
1847
1848   if not prog_bison.found()
1849     prog_bison = find_program('byacc', required : needs_flex_bison, disabler : true)
1850     yacc_is_bison = false
1851   endif
1852
1853   # Disable deprecated keyword warnings, since we have to use them for
1854   # old-bison compat.  See discussion in
1855   # https://gitlab.freedesktop.org/mesa/mesa/merge_requests/2161
1856   if find_program('bison', required : false, version : '> 2.3').found()
1857     prog_bison = [prog_bison, '-Wno-deprecated']
1858   endif
1859
1860   prog_flex = find_program('flex', required : needs_flex_bison, disabler : true)
1861   prog_flex_cpp = prog_flex
1862 endif
1863
1864 dep_selinux = null_dep
1865 if get_option('selinux')
1866   if get_option('execmem') != true
1867     warning('execmem option is disabled, selinux will not be able to use execmem.')
1868   endif
1869   dep_selinux = dependency('libselinux')
1870   pre_args += '-DMESA_SELINUX'
1871 endif
1872
1873 if get_option('execmem')
1874   pre_args += '-DMESA_EXECMEM'
1875 endif
1876
1877 _libunwind = get_option('libunwind') \
1878   .require(not with_platform_android, error_message : 'Android requires the use of the backtrace library, not libunwind')
1879 if host_machine.system() == 'darwin'
1880   dep_unwind = meson.get_compiler('c').find_library('System', required : _libunwind)
1881 else
1882   dep_unwind = dependency('libunwind', required : _libunwind)
1883 endif
1884 if dep_unwind.found()
1885   pre_args += '-DHAVE_LIBUNWIND'
1886 endif
1887
1888 if with_osmesa
1889   if not with_gallium_softpipe
1890     error('OSMesa gallium requires gallium softpipe or llvmpipe.')
1891   endif
1892   if host_machine.system() == 'windows'
1893     osmesa_lib_name = 'osmesa'
1894   else
1895     osmesa_lib_name = 'OSMesa'
1896   endif
1897 endif
1898
1899 # TODO: symbol mangling
1900
1901 if with_platform_wayland
1902   dep_wl_scanner = dependency('wayland-scanner', native: true)
1903   prog_wl_scanner = find_program(dep_wl_scanner.get_variable(pkgconfig : 'wayland_scanner'))
1904   if dep_wl_scanner.version().version_compare('>= 1.15')
1905     wl_scanner_arg = 'private-code'
1906   else
1907     wl_scanner_arg = 'code'
1908   endif
1909   dep_wl_protocols = dependency('wayland-protocols', version : '>= 1.24')
1910   dep_wayland_client = dependency('wayland-client', version : '>=1.18')
1911   dep_wayland_server = dependency('wayland-server', version : '>=1.18')
1912   if with_egl
1913     dep_wayland_egl = dependency('wayland-egl-backend', version : '>= 3')
1914     dep_wayland_egl_headers = dep_wayland_egl.partial_dependency(compile_args : true)
1915   endif
1916   pre_args += '-DWL_HIDE_DEPRECATED'
1917 endif
1918
1919 dep_x11 = null_dep
1920 dep_xext = null_dep
1921 dep_xfixes = null_dep
1922 dep_x11_xcb = null_dep
1923 dep_xcb = null_dep
1924 dep_xcb_glx = null_dep
1925 dep_xcb_dri2 = null_dep
1926 dep_xcb_dri3 = null_dep
1927 dep_dri2proto = null_dep
1928 dep_glproto = null_dep
1929 dep_xxf86vm = null_dep
1930 dep_xcb_dri3 = null_dep
1931 dep_xcb_present = null_dep
1932 dep_xcb_sync = null_dep
1933 dep_xcb_xfixes = null_dep
1934 dep_xshmfence = null_dep
1935 dep_xcb_xrandr = null_dep
1936 dep_xcb_shm = null_dep
1937 dep_xlib_xrandr = null_dep
1938 dep_openmp = null_dep
1939
1940 # Even if we find OpenMP, Gitlab CI fails to link with gcc/i386 and clang/anyarch.
1941 if host_machine.cpu_family() == 'x86_64' and cc.get_id() == 'gcc'
1942   dep_openmp = dependency('openmp', required : false)
1943   if dep_openmp.found()
1944     pre_args += ['-DHAVE_OPENMP']
1945   endif
1946 endif
1947
1948 with_dri3_modifiers = false
1949 if with_platform_x11
1950   if with_glx == 'xlib'
1951     dep_x11 = dependency('x11')
1952     dep_xext = dependency('xext')
1953     dep_xcb = dependency('xcb')
1954     dep_xcb_xrandr = dependency('xcb-randr')
1955   elif with_glx == 'dri'
1956     dep_x11 = dependency('x11')
1957     dep_xext = dependency('xext')
1958     dep_xfixes = dependency('xfixes', version : '>= 2.0')
1959     dep_xcb_glx = dependency('xcb-glx', version : '>= 1.8.1')
1960     dep_xcb_shm = dependency('xcb-shm')
1961   endif
1962   if (with_any_vk or with_glx == 'dri' or with_egl or
1963        (with_gallium_vdpau or with_gallium_va or
1964         with_gallium_omx != 'disabled'))
1965     dep_xcb = dependency('xcb')
1966     dep_x11_xcb = dependency('x11-xcb')
1967     if with_dri_platform == 'drm' and not dep_libdrm.found()
1968       error('libdrm required for gallium video statetrackers when using x11')
1969     endif
1970   endif
1971   if with_any_vk or with_egl or (with_glx == 'dri' and with_dri_platform == 'drm')
1972     dep_xcb_dri2 = dependency('xcb-dri2', version : '>= 1.8')
1973
1974     if with_dri3
1975       dep_xcb_dri3 = dependency('xcb-dri3')
1976       dep_xcb_present = dependency('xcb-present')
1977       # until xcb-dri3 has been around long enough to make a hard-dependency:
1978       if (dep_xcb_dri3.version().version_compare('>= 1.13') and
1979           dep_xcb_present.version().version_compare('>= 1.13'))
1980         with_dri3_modifiers = true
1981       endif
1982       dep_xcb_shm = dependency('xcb-shm')
1983       dep_xcb_sync = dependency('xcb-sync')
1984       dep_xshmfence = dependency('xshmfence', version : '>= 1.1')
1985     endif
1986   endif
1987   if with_glx == 'dri' or with_glx == 'xlib'
1988     dep_glproto = dependency('glproto', version : '>= 1.4.14')
1989   endif
1990   if with_glx == 'dri'
1991     if with_dri_platform == 'drm'
1992       dep_dri2proto = dependency('dri2proto', version : '>= 2.8')
1993       if with_glx_direct
1994         dep_xxf86vm = dependency('xxf86vm')
1995       endif
1996     endif
1997   endif
1998   if (with_egl or
1999       with_dri3 or (
2000       with_gallium_vdpau or with_gallium_xa or
2001       with_gallium_omx != 'disabled'))
2002     dep_xcb_xfixes = dependency('xcb-xfixes')
2003   endif
2004   if with_xlib_lease or with_any_vk
2005     dep_xcb_xrandr = dependency('xcb-randr')
2006   endif
2007   if with_xlib_lease
2008     dep_xlib_xrandr = dependency('xrandr', version : '>= 1.3')
2009   endif
2010 endif
2011
2012 if with_dri
2013   pre_args += '-DHAVE_DRI'
2014 endif
2015 if with_dri2
2016   pre_args += '-DHAVE_DRI2'
2017 endif
2018 if with_dri3
2019   pre_args += '-DHAVE_DRI3'
2020 endif
2021 if with_dri3_modifiers
2022   pre_args += '-DHAVE_DRI3_MODIFIERS'
2023 endif
2024 if with_gallium_drisw_kms
2025   pre_args += '-DHAVE_DRISW_KMS'
2026 endif
2027
2028 if get_option('gallium-extra-hud')
2029   pre_args += '-DHAVE_GALLIUM_EXTRA_HUD=1'
2030 endif
2031
2032 dep_lmsensors = cc.find_library('sensors', required : get_option('lmsensors'))
2033 if dep_lmsensors.found()
2034   pre_args += '-DHAVE_LIBSENSORS=1'
2035 endif
2036
2037 _shader_replacement = get_option('custom-shader-replacement')
2038 if _shader_replacement == ''
2039 else
2040   pre_args += '-DCUSTOM_SHADER_REPLACEMENT'
2041 endif
2042
2043 with_perfetto = get_option('perfetto')
2044 with_datasources = get_option('datasources')
2045 with_any_datasource = with_datasources.length() != 0
2046 if with_perfetto
2047   dep_perfetto = dependency('perfetto', fallback: ['perfetto', 'dep_perfetto'])
2048   pre_args += '-DHAVE_PERFETTO'
2049 endif
2050
2051 add_project_arguments(pre_args, language : ['c', 'cpp'])
2052 add_project_arguments(c_cpp_args, language : ['c', 'cpp'])
2053
2054 add_project_arguments(c_args,   language : ['c'])
2055 add_project_arguments(cpp_args, language : ['cpp'])
2056
2057 gl_priv_reqs = []
2058
2059 if with_glx == 'xlib'
2060   gl_priv_reqs += ['x11', 'xext', 'xcb']
2061 elif with_glx == 'dri'
2062   gl_priv_reqs += [
2063     'x11', 'xext', 'xfixes', 'x11-xcb', 'xcb',
2064     'xcb-glx >= 1.8.1']
2065   if with_dri_platform == 'drm'
2066     gl_priv_reqs += 'xcb-dri2 >= 1.8'
2067     if with_glx_direct
2068       gl_priv_reqs += 'xxf86vm'
2069     endif
2070   endif
2071 endif
2072 if dep_libdrm.found()
2073   gl_priv_reqs += 'libdrm >= 2.4.75'
2074 endif
2075
2076 gl_priv_libs = []
2077 if dep_thread.found()
2078   gl_priv_libs += ['-lpthread', '-pthread']
2079 endif
2080 if dep_m.found()
2081   gl_priv_libs += '-lm'
2082 endif
2083 if dep_dl.found()
2084   gl_priv_libs += '-ldl'
2085 endif
2086
2087 # FIXME: autotools lists this as incomplete
2088 gbm_priv_libs = []
2089 if dep_dl.found()
2090   gbm_priv_libs += '-ldl'
2091 endif
2092
2093 pkg = import('pkgconfig')
2094
2095 if host_machine.system() == 'windows'
2096   prog_dumpbin = find_program('dumpbin', required : false)
2097   with_symbols_check = prog_dumpbin.found() and with_tests
2098   if with_symbols_check
2099     symbols_check_args = ['--dumpbin', prog_dumpbin.full_path()]
2100   endif
2101 else
2102   prog_nm = find_program('nm')
2103   with_symbols_check = with_tests
2104   symbols_check_args = ['--nm', prog_nm.full_path()]
2105 endif
2106
2107 # This quirk needs to be applied to sources with functions defined in assembly
2108 # as GCC LTO drops them. See: https://bugs.freedesktop.org/show_bug.cgi?id=109391
2109 gcc_lto_quirk = (cc.get_id() == 'gcc') ? ['-fno-lto'] : []
2110
2111 devenv = environment()
2112
2113 dir_compiler_nir = join_paths(meson.current_source_dir(), 'src/compiler/nir/')
2114 dir_source_root = meson.project_source_root()
2115
2116
2117 subdir('include')
2118 subdir('bin')
2119 subdir('src')
2120
2121 meson.add_devenv(devenv)
2122
2123 summary(
2124   {
2125     'prefix': get_option('prefix'),
2126     'libdir': get_option('libdir'),
2127     'includedir': get_option('includedir'),
2128   },
2129   section: 'Directories'
2130 )
2131
2132 summary(
2133   {
2134     'c_cpp_args': c_cpp_args,
2135   },
2136   section: 'Common C and C++ arguments'
2137 )
2138
2139 summary(
2140   {
2141     'OpenGL': with_opengl,
2142     'ES1': with_gles1,
2143     'ES2': with_gles2,
2144     'Shared glapi': with_shared_glapi,
2145   },
2146   section: 'OpenGL', bool_yn: true
2147 )
2148
2149 summary(
2150   {
2151     'Platform': with_dri_platform,
2152     'Driver dir': dri_drivers_path,
2153   },
2154   section: 'DRI', bool_yn: true, list_sep: ' '
2155 )
2156
2157 summary(
2158   {
2159     'Enabled': with_glx != 'disabled',
2160     'Provider': with_glx == 'disabled' ? 'None' : with_glx
2161   },
2162   section: 'GLX', bool_yn: true, list_sep: ' '
2163 )
2164
2165 egl_summary = {'Enabled': with_egl}
2166 if with_egl
2167   egl_drivers = []
2168   if with_dri
2169     egl_drivers += 'builtin:egl_dri2'
2170   endif
2171   if with_dri3
2172     egl_drivers += 'builtin:egl_dri3'
2173   endif
2174   if with_platform_windows
2175     egl_drivers += 'builtin:wgl'
2176   endif
2177   egl_summary += {'Drivers': egl_drivers}
2178   egl_summary += {'Platforms': _platforms}
2179 endif
2180 summary(egl_summary, section: 'EGL', bool_yn: true, list_sep: ' ')
2181
2182 gbm_summary = {'Enabled': with_gbm}
2183 if with_gbm
2184   gbm_summary += {'Backends path': gbm_backends_path}
2185 endif
2186 summary(gbm_summary, section: 'GBM', bool_yn: true, list_sep: ' ')
2187
2188 vulkan_summary = {'Drivers': _vulkan_drivers.length() != 0 ? _vulkan_drivers : false }
2189 if with_any_vk
2190   vulkan_summary += {'Platforms': _platforms}
2191   vulkan_summary += {'ICD dir': with_vulkan_icd_dir}
2192   if with_any_vulkan_layers
2193     vulkan_summary += {'Layers': get_option('vulkan-layers')}
2194   endif
2195   vulkan_summary += {'Video codecs': _codecs.length() != 0 ? _codecs : false}
2196   vulkan_summary += {'Ray tracing': with_intel_vk_rt ? 'intel' : false}
2197 endif
2198 summary(vulkan_summary, section: 'Vulkan', bool_yn: true, list_sep: ' ')
2199
2200 llvm_summary = {'Enabled': with_llvm}
2201 if with_llvm
2202   llvm_summary += {'Version': dep_llvm.version()}
2203 endif
2204 summary(llvm_summary, section: 'LLVM', bool_yn: true, list_sep: ' ')
2205
2206 gallium_summary = {'Enabled': with_gallium}
2207 if with_gallium
2208   gallium_summary += {'Drivers': gallium_drivers}
2209   gallium_summary += {'Platforms': _platforms}
2210
2211   gallium_frontends = ['mesa']
2212   if with_gallium_xa
2213     gallium_frontends += 'xa'
2214   endif
2215   if with_gallium_vdpau
2216     gallium_frontends += 'vdpau'
2217   endif
2218   if with_gallium_omx != 'disabled'
2219     gallium_frontends += 'omx' + with_gallium_omx
2220   endif
2221   if with_gallium_va
2222     gallium_frontends += 'va'
2223   endif
2224   if with_gallium_st_nine
2225     gallium_frontends += 'nine'
2226   endif
2227   if with_gallium_opencl
2228     gallium_frontends += 'clover'
2229   endif
2230   if with_gallium_rusticl
2231     gallium_frontends += 'rusticl'
2232   endif
2233   gallium_summary += {'Frontends': gallium_frontends}
2234   gallium_summary += {'Off-screen rendering': with_osmesa ? 'lib' + osmesa_lib_name : false}
2235   gallium_summary += {'HUD lm-sensors': dep_lmsensors.found()}
2236 endif
2237 summary(gallium_summary, section: 'Gallium', bool_yn: true, list_sep: ' ')
2238
2239 perfetto_summary = {'Enabled': with_perfetto}
2240 if with_perfetto and with_any_datasource
2241   perfetto_summary += {'Data source': with_datasources}
2242 endif
2243 summary(perfetto_summary, section: 'Perfetto', bool_yn: true, list_sep: ' ')