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