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