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