loader: Add Xe KMD support
[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   'posix_fallocate': '',
1281 }
1282
1283 foreach f, prefix: functions_to_detect
1284   if cc.has_function(f, prefix: prefix)
1285     pre_args += '-DHAVE_@0@'.format(f.to_upper())
1286   endif
1287 endforeach
1288
1289 if cpp.links('''
1290     #define _GNU_SOURCE
1291     #include <stdlib.h>
1292
1293     static int dcomp(const void *l, const void *r, void *t) { return 0; }
1294
1295     int main(int ac, char **av) {
1296       int arr[] = { 1 };
1297       void *t = NULL;
1298       qsort_r((void*)&arr[0], 1, 1, dcomp, t);
1299       return (0);
1300     }''',
1301     args : pre_args,
1302     name : 'GNU qsort_r')
1303   pre_args += '-DHAVE_GNU_QSORT_R'
1304 elif cpp.links('''
1305     #include <stdlib.h>
1306
1307     static int dcomp(void *t, const void *l, const void *r) { return 0; }
1308
1309     int main(int ac, char **av) {
1310       int arr[] = { 1 };
1311       void *t = NULL;
1312       qsort_r((void*)&arr[0], 1, 1, t, dcomp);
1313       return (0);
1314     }''',
1315     args : pre_args,
1316     name : 'BSD qsort_r')
1317   pre_args += '-DHAVE_BSD_QSORT_R'
1318 endif
1319
1320 if cc.has_header_symbol('time.h', 'struct timespec')
1321    pre_args += '-DHAVE_STRUCT_TIMESPEC'
1322 endif
1323
1324 with_c11_threads = false
1325 if cc.has_function('thrd_create', prefix: '#include <threads.h>')
1326   if with_platform_android
1327     # Current only Android's c11 <threads.h> are verified
1328     pre_args += '-DHAVE_THRD_CREATE'
1329     with_c11_threads = true
1330   endif
1331 endif
1332
1333 if cc.has_header_symbol('errno.h', 'program_invocation_name',
1334                         args : '-D_GNU_SOURCE')
1335    pre_args += '-DHAVE_PROGRAM_INVOCATION_NAME'
1336 elif with_tools.contains('intel')
1337   error('Intel tools require the program_invocation_name variable')
1338 endif
1339
1340 if cc.has_header_symbol('math.h', 'issignaling',
1341                         args : '-D_GNU_SOURCE')
1342    pre_args += '-DHAVE_ISSIGNALING'
1343 endif
1344
1345 # MinGW provides a __builtin_posix_memalign function, but not a posix_memalign.
1346 # This means that this check will succeed, but then compilation will later
1347 # fail. MSVC doesn't have this function at all, so only check for it on
1348 # non-windows platforms.
1349 if host_machine.system() != 'windows'
1350   if cc.has_function('posix_memalign')
1351     pre_args += '-DHAVE_POSIX_MEMALIGN'
1352   endif
1353 endif
1354
1355 if cc.has_member('struct dirent', 'd_type', prefix: '''#include <sys/types.h>
1356    #include <dirent.h>''')
1357    pre_args += '-DHAVE_DIRENT_D_TYPE'
1358 endif
1359
1360 # strtod locale support
1361 if cc.links('''
1362     #define _GNU_SOURCE
1363     #include <stdlib.h>
1364     #include <locale.h>
1365     #ifdef HAVE_XLOCALE_H
1366     #include <xlocale.h>
1367     #endif
1368     int main() {
1369       locale_t loc = newlocale(LC_CTYPE_MASK, "C", NULL);
1370       const char *s = "1.0";
1371       char *end;
1372       double d = strtod_l(s, end, loc);
1373       float f = strtof_l(s, end, loc);
1374       freelocale(loc);
1375       return 0;
1376     }''',
1377     args : pre_args,
1378     name : 'strtod has locale support')
1379   pre_args += '-DHAVE_STRTOD_L'
1380 endif
1381
1382 # Check for some linker flags
1383 ld_args_bsymbolic = []
1384 if cc.links('int main() { return 0; }', args : '-Wl,-Bsymbolic', name : 'Bsymbolic')
1385   ld_args_bsymbolic += '-Wl,-Bsymbolic'
1386 endif
1387 with_ld_version_script = false
1388 if cc.links('int main() { return 0; }',
1389             args : '-Wl,--version-script=@0@'.format(
1390               join_paths(meson.current_source_dir(), 'build-support/conftest.map')),
1391             name : 'version-script')
1392   with_ld_version_script = true
1393 endif
1394 with_ld_dynamic_list = false
1395 if cc.links('int main() { return 0; }',
1396             args : '-Wl,--dynamic-list=@0@'.format(
1397               join_paths(meson.current_source_dir(), 'build-support/conftest.dyn')),
1398             name : 'dynamic-list')
1399   with_ld_dynamic_list = true
1400 endif
1401
1402 ld_args_build_id = cc.get_supported_link_arguments('-Wl,--build-id=sha1')
1403
1404 # check for dl support
1405 dep_dl = null_dep
1406 if host_machine.system() != 'windows'
1407   if not cc.has_function('dlopen')
1408     dep_dl = cc.find_library('dl', required : true)
1409   endif
1410   if cc.has_function('dladdr', dependencies : dep_dl)
1411     # This is really only required for util/disk_cache.h
1412     pre_args += '-DHAVE_DLADDR'
1413   endif
1414 endif
1415
1416 if cc.has_function('dl_iterate_phdr')
1417   pre_args += '-DHAVE_DL_ITERATE_PHDR'
1418 elif with_intel_vk or with_intel_hasvk
1419   error('Intel "Anvil" Vulkan driver requires the dl_iterate_phdr function')
1420 endif
1421
1422 # only used in Iris and ANV
1423 if with_any_intel and ['x86', 'x86_64'].contains(host_machine.cpu_family())
1424   pre_args += '-DSUPPORT_INTEL_INTEGRATED_GPUS'
1425 endif
1426
1427 if get_option('intel-xe-kmd').enabled()
1428   pre_args += '-DINTEL_XE_KMD_SUPPORTED'
1429 endif
1430
1431 if with_intel_hasvk and host_machine.cpu_family().startswith('x86') == false
1432   error('Intel "hasvk" Vulkan driver requires x86 or x86_64 CPU family')
1433 endif
1434
1435 if with_gallium_crocus and host_machine.cpu_family().startswith('x86') == false
1436   error('Intel "crocus" Gallium driver requires x86 or x86_64 CPU family')
1437 endif
1438
1439 if with_gallium_i915 and host_machine.cpu_family().startswith('x86') == false
1440   error('Intel "i915" Gallium driver requires x86 or x86_64 CPU family')
1441 endif
1442
1443 # Determine whether or not the rt library is needed for time functions
1444 if host_machine.system() == 'windows' or cc.has_function('clock_gettime')
1445   dep_clock = null_dep
1446 else
1447   dep_clock = cc.find_library('rt')
1448 endif
1449
1450 dep_zlib = dependency('zlib', version : '>= 1.2.3',
1451                       fallback : ['zlib', 'zlib_dep'],
1452                       required : get_option('zlib'))
1453 if dep_zlib.found()
1454   pre_args += '-DHAVE_ZLIB'
1455 endif
1456
1457 dep_zstd = dependency('libzstd', required : get_option('zstd'))
1458 if dep_zstd.found()
1459   pre_args += '-DHAVE_ZSTD'
1460 endif
1461
1462 with_compression = dep_zlib.found() or dep_zstd.found()
1463 if with_compression
1464   pre_args += '-DHAVE_COMPRESSION'
1465 elif with_shader_cache
1466   error('Shader Cache requires compression')
1467 endif
1468
1469 if host_machine.system() == 'windows'
1470   # For MSVC and MinGW we aren't using pthreads, and dependency('threads') will add linkage
1471   # to pthread for MinGW, so leave the dependency null_dep for Windows. For Windows linking to
1472   # kernel32 is enough for c11/threads.h and it's already linked by meson by default
1473   dep_thread = null_dep
1474 else
1475   dep_thread = dependency('threads')
1476 endif
1477 if dep_thread.found()
1478   pre_args += '-DHAVE_PTHREAD'
1479   if host_machine.system() != 'netbsd' and cc.has_function(
1480       'pthread_setaffinity_np',
1481       dependencies : dep_thread,
1482       prefix : '#include <pthread.h>',
1483       args : '-D_GNU_SOURCE')
1484     pre_args += '-DHAVE_PTHREAD_SETAFFINITY'
1485   endif
1486 endif
1487
1488 # We don't have expat on Android or Windows, which is a needed dep for xmlconfig
1489 opt_xmlconfig = get_option('xmlconfig') \
1490   .require(not (with_platform_android or with_platform_windows),
1491            error_message : 'xmlconfig not available on Android or Windows')
1492
1493 if host_machine.system() == 'darwin'
1494   dep_expat = meson.get_compiler('c').find_library('expat', required : opt_xmlconfig)
1495 else
1496   dep_expat = dependency('expat', fallback : ['expat', 'expat_dep'],
1497                          required : opt_xmlconfig)
1498 endif
1499 use_xmlconfig = dep_expat.found()
1500   
1501 # Predefined macros for windows
1502 if host_machine.system() == 'windows'
1503   pre_args += '-DWIN32_LEAN_AND_MEAN' # http://msdn2.microsoft.com/en-us/library/6dwk3a1z.aspx
1504 endif
1505 # this only exists on linux so either this is linux and it will be found, or
1506 # it's not linux and wont
1507 dep_m = cc.find_library('m', required : false)
1508
1509 if host_machine.system() == 'windows'
1510   dep_regex = meson.get_compiler('c').find_library('regex', required : false)
1511   if not dep_regex.found()
1512     dep_regex = declare_dependency(compile_args : ['-DNO_REGEX'])
1513   endif
1514 else
1515   dep_regex = null_dep
1516 endif
1517
1518 if with_platform_haiku
1519   dep_network = cc.find_library('network')
1520 endif
1521
1522 dep_futex = null_dep
1523 if host_machine.system() == 'windows'
1524   if (get_option('min-windows-version') < 8)
1525     pre_args += '-DWINDOWS_NO_FUTEX'
1526   else
1527     dep_futex = cc.find_library('synchronization', required : true)
1528   endif
1529 endif
1530
1531 # Check for libdrm. Various drivers have different libdrm version requirements,
1532 # but we always want to use the same version for all libdrm modules. That means
1533 # even if driver foo requires 2.4.0 and driver bar requires 2.4.3, if foo and
1534 # bar are both on use 2.4.3 for both of them
1535 dep_libdrm_amdgpu = null_dep
1536 dep_libdrm_radeon = null_dep
1537 dep_libdrm_nouveau = null_dep
1538 dep_libdrm_intel = null_dep
1539
1540 _drm_amdgpu_ver = '2.4.110'
1541 _drm_radeon_ver = '2.4.71'
1542 _drm_nouveau_ver = '2.4.102'
1543 _drm_intel_ver = '2.4.75'
1544 _drm_ver = '2.4.109'
1545
1546 _libdrm_checks = [
1547   ['intel', with_gallium_i915],
1548   ['amdgpu', (with_amd_vk and not with_platform_windows) or with_gallium_radeonsi],
1549   ['radeon', (with_gallium_radeonsi or with_gallium_r300 or with_gallium_r600)],
1550   ['nouveau', with_gallium_nouveau],
1551 ]
1552
1553 # Loop over the enables versions and get the highest libdrm requirement for all
1554 # active drivers.
1555 _drm_blame = ''
1556 foreach d : _libdrm_checks
1557   ver = get_variable('_drm_@0@_ver'.format(d[0]))
1558   if d[1] and ver.version_compare('>' + _drm_ver)
1559     _drm_ver = ver
1560     _drm_blame = d[0]
1561   endif
1562 endforeach
1563 if _drm_blame != ''
1564   message('libdrm @0@ needed because @1@ has the highest requirement'.format(_drm_ver, _drm_blame))
1565 endif
1566
1567 # Then get each libdrm module
1568 foreach d : _libdrm_checks
1569   if d[1]
1570     set_variable(
1571       'dep_libdrm_' + d[0],
1572       dependency('libdrm_' + d[0], version : '>=' + _drm_ver)
1573     )
1574   endif
1575 endforeach
1576
1577 with_gallium_drisw_kms = false
1578 if system_has_kms_drm
1579   dep_libdrm = dependency(
1580     'libdrm', version : '>=' + _drm_ver,
1581     # GNU/Hurd includes egl_dri2, without drm.
1582     required : (with_dri2 and host_machine.system() != 'gnu') or with_dri3
1583   )
1584 else
1585   # We should prevent libdrm from being available when the target doesn't have it to avoid transitive
1586   # dependencies (such as vk-runtime) linking to it
1587   dep_libdrm = null_dep
1588 endif
1589 if dep_libdrm.found()
1590   pre_args += '-DHAVE_LIBDRM'
1591   if with_dri_platform == 'drm' and with_dri
1592     with_gallium_drisw_kms = true
1593   endif
1594 endif
1595
1596 dep_libudev = dependency('libudev', required : false)
1597 if dep_libudev.found()
1598   pre_args += '-DHAVE_LIBUDEV'
1599 endif
1600
1601 llvm_modules = ['bitwriter', 'engine', 'mcdisassembler', 'mcjit', 'core', 'executionengine', 'scalaropts', 'transformutils', 'instcombine']
1602 llvm_optional_modules = ['coroutines']
1603 if with_amd_vk or with_gallium_radeonsi or with_gallium_r600
1604   llvm_modules += ['amdgpu', 'bitreader', 'ipo']
1605   if with_gallium_r600
1606     llvm_modules += 'asmparser'
1607   endif
1608 endif
1609 if with_gallium_opencl
1610   llvm_modules += [
1611     'linker', 'coverage', 'instrumentation', 'ipo', 'irreader',
1612     'lto', 'option', 'objcarcopts', 'profiledata'
1613   ]
1614   # all-targets is needed to support static linking LLVM build with multiple targets
1615   # windowsdriver is needded with LLVM>=15, but we don't know what LLVM verrsion we are using yet
1616   llvm_optional_modules += ['all-targets', 'frontendopenmp', 'windowsdriver']
1617 endif
1618 if with_clc
1619   llvm_modules += ['coverage', 'target', 'linker', 'irreader', 'option', 'libdriver', 'lto']
1620   # all-targets is needed to support static linking LLVM build with multiple targets
1621   # windowsdriver is needded with LLVM>=15, but we don't know what LLVM verrsion we are using yet
1622   llvm_optional_modules += ['all-targets', 'windowsdriver']
1623 endif
1624 draw_with_llvm = get_option('draw-use-llvm')
1625 if draw_with_llvm
1626   llvm_modules += 'native'
1627   # lto is needded with LLVM>=15, but we don't know what LLVM verrsion we are using yet
1628   llvm_optional_modules += ['lto']
1629 endif
1630
1631 if with_intel_clc
1632   _llvm_version = '>= 13.0.0'
1633 elif with_amd_vk or with_gallium_radeonsi or with_gallium_opencl
1634   _llvm_version = '>= 11.0.0'
1635 elif with_clc
1636   _llvm_version = '>= 10.0.0'
1637 else
1638   _llvm_version = '>= 5.0.0'
1639 endif
1640
1641 _shared_llvm = get_option('shared-llvm') \
1642   .disable_auto_if(host_machine.system() == 'windows') \
1643   .allowed()
1644
1645 _llvm = get_option('llvm')
1646 dep_llvm = null_dep
1647 with_llvm = false
1648 if _llvm.allowed()
1649   dep_llvm = dependency(
1650     'llvm',
1651     version : _llvm_version,
1652     modules : llvm_modules,
1653     optional_modules : llvm_optional_modules,
1654     required : (
1655       with_amd_vk or with_gallium_radeonsi or with_gallium_opencl or with_clc
1656       or _llvm.enabled()
1657     ),
1658     static : not _shared_llvm,
1659     fallback : ['llvm', 'dep_llvm'],
1660     include_type : 'system',
1661   )
1662   with_llvm = dep_llvm.found()
1663 endif
1664 if with_llvm
1665   pre_args += '-DLLVM_AVAILABLE'
1666   pre_args += '-DMESA_LLVM_VERSION_STRING="@0@"'.format(dep_llvm.version())
1667   pre_args += '-DLLVM_IS_SHARED=@0@'.format(_shared_llvm.to_int())
1668
1669   if draw_with_llvm
1670     pre_args += '-DDRAW_LLVM_AVAILABLE'
1671   elif with_swrast_vk
1672     error('Lavapipe requires LLVM draw support.')
1673   endif
1674
1675   if host_machine.system() != 'windows'
1676     # LLVM can be built without rtti, turning off rtti changes the ABI of C++
1677     # programs, so we need to build all C++ code in mesa without rtti as well to
1678     # ensure that linking works. Note that Win32 compilers does handle mismatching RTTI
1679     # without issues, so only apply this for other compilers.
1680     if dep_llvm.type_name() == 'internal'
1681       _llvm_rtti = subproject('llvm').get_variable('has_rtti', true)
1682     else
1683       # The CMake finder will return 'ON', the llvm-config will return 'YES'
1684       _llvm_rtti = ['ON', 'YES'].contains(dep_llvm.get_variable(cmake : 'LLVM_ENABLE_RTTI', configtool: 'has-rtti'))
1685     endif
1686     if _rtti != _llvm_rtti
1687       if _llvm_rtti
1688         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.')
1689       else
1690         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.')
1691       endif
1692     endif
1693   endif
1694
1695   if cc.get_argument_syntax() == 'msvc'
1696     # Suppress "/DELAYLOAD:ole32.dll/shell32.dll ignored" warnings that LLVM adds
1697     add_project_link_arguments(
1698       '/ignore:4199',
1699       language : ['c', 'cpp'],
1700     )
1701   endif
1702 elif with_amd_vk and with_aco_tests
1703   error('ACO tests require LLVM, but LLVM is disabled.')
1704 elif with_gallium_radeonsi or with_swrast_vk
1705   error('The following drivers require LLVM: RadeonSI, SWR, Lavapipe. One of these is enabled, but LLVM is disabled.')
1706 elif with_gallium_opencl
1707   error('The OpenCL "Clover" state tracker requires LLVM, but LLVM is disabled.')
1708 elif with_clc
1709   error('The CLC compiler requires LLVM, but LLVM is disabled.')
1710 else
1711   draw_with_llvm = false
1712 endif
1713
1714 with_opencl_spirv = (_opencl != 'disabled' and get_option('opencl-spirv')) or with_clc
1715 if with_opencl_spirv
1716   chosen_llvm_version_array = dep_llvm.version().split('.')
1717   chosen_llvm_version_major = chosen_llvm_version_array[0].to_int()
1718   chosen_llvm_version_minor = chosen_llvm_version_array[1].to_int()
1719
1720   # Require an SPIRV-LLVM-Translator version compatible with the chosen LLVM
1721   # one.
1722
1723   # This first version check is still needed as maybe LLVM 8.0 was picked but
1724   # we do not want to accept SPIRV-LLVM-Translator 8.0.0.1 as that version
1725   # does not have the required API and those are only available starting from
1726   # 8.0.1.3.
1727   _llvmspirvlib_min_version = '>= 8.0.1.3'
1728   if with_intel_clc
1729     _llvmspirvlib_min_version = '>= 13.0.0.0'
1730   endif
1731
1732   _llvmspirvlib_version = [
1733     _llvmspirvlib_min_version,
1734     '>= @0@.@1@'.format(chosen_llvm_version_major, chosen_llvm_version_minor),
1735     '< @0@.@1@'.format(chosen_llvm_version_major, chosen_llvm_version_minor + 1) ]
1736
1737   dep_spirv_tools = dependency('SPIRV-Tools', required : true, version : '>= 2018.0')
1738   # LLVMSPIRVLib is available at https://github.com/KhronosGroup/SPIRV-LLVM-Translator
1739   dep_llvmspirvlib = dependency('LLVMSPIRVLib', required : true, version : _llvmspirvlib_version)
1740 else
1741   dep_spirv_tools = null_dep
1742   dep_llvmspirvlib = null_dep
1743 endif
1744
1745 dep_clang = null_dep
1746 if with_clc
1747   llvm_libdir = dep_llvm.get_variable(cmake : 'LLVM_LIBRARY_DIR', configtool: 'libdir')
1748
1749   dep_clang = cpp.find_library('clang-cpp', dirs : llvm_libdir, required : false)
1750
1751   if not dep_clang.found() or not _shared_llvm
1752     clang_modules = [
1753       'clangBasic', 'clangAST', 'clangCodeGen', 'clangLex',
1754       'clangDriver', 'clangFrontend', 'clangFrontendTool',
1755       'clangHandleCXX', 'clangHandleLLVM', 'clangSerialization',
1756       'clangSema', 'clangParse', 'clangEdit', 'clangAnalysis'
1757     ]
1758     if dep_llvm.version().version_compare('>= 15.0')
1759       clang_modules += 'clangSupport'
1760     endif
1761
1762     dep_clang = []
1763     foreach m : clang_modules
1764       dep_clang += cpp.find_library(m, dirs : llvm_libdir, required : true)
1765     endforeach
1766   endif
1767 endif
1768
1769 # Be explicit about only using this lib on Windows, to avoid picking
1770 # up random libs with the generic name 'libversion'
1771 dep_version = null_dep
1772 if host_machine.system() == 'windows'
1773   dep_version = cpp.find_library('version')
1774 endif
1775
1776 dep_elf = dependency('libelf', required : false)
1777 if not with_platform_windows and not dep_elf.found()
1778   dep_elf = cc.find_library('elf', required : false)
1779 endif
1780 if dep_elf.found()
1781   pre_args += '-DUSE_LIBELF'
1782 endif
1783
1784 dep_glvnd = null_dep
1785 if with_glvnd
1786   dep_glvnd = dependency('libglvnd', version : '>= 1.3.2')
1787   pre_args += '-DUSE_LIBGLVND=1'
1788 endif
1789
1790 dep_valgrind = dependency('valgrind', required : get_option('valgrind'))
1791 if dep_valgrind.found()
1792   pre_args += '-DHAVE_VALGRIND'
1793 endif
1794
1795 # AddressSanitizer's leak reports need all the symbols to be present at exit to
1796 # decode well, which runs afoul of our dlopen()/dlclose()ing of the DRI drivers.
1797 # Set a flag so we can skip the dlclose for asan builds.
1798 if ['address', 'address,undefined'].contains(get_option('b_sanitize'))
1799   asan_c_args = ['-DBUILT_WITH_ASAN=1']
1800 else
1801   asan_c_args = ['-DBUILT_WITH_ASAN=0']
1802 endif
1803
1804 yacc_is_bison = true
1805 needs_flex_bison = with_any_opengl or with_freedreno_vk or with_intel_tools or with_gallium
1806
1807 if build_machine.system() == 'windows'
1808   # Prefer the winflexbison versions, they're much easier to install and have
1809   # better windows support.
1810
1811   prog_flex = find_program('win_flex', required : false)
1812   if prog_flex.found()
1813     # windows compatibility (uses <io.h> instead of <unistd.h> and _isatty,
1814     # _fileno functions)
1815     prog_flex = [prog_flex, '--wincompat']
1816   else
1817     prog_flex = [find_program('flex', 'lex', required : needs_flex_bison, disabler : true)]
1818   endif
1819   # Force flex to use const keyword in prototypes, as relies on __cplusplus or
1820   # __STDC__ macro to determine whether it's safe to use const keyword
1821   prog_flex += '-DYY_USE_CONST='
1822
1823   prog_flex_cpp = prog_flex
1824   # Convince win_flex to use <inttypes.h> for C++ files
1825   # Note that we are using a C99 version here rather than C11,
1826   # because using a C11 version can cause the MSVC CRT headers to define
1827   # static_assert to _Static_assert, which breaks other parts of the CRT
1828   prog_flex_cpp += '-D__STDC_VERSION__=199901'
1829
1830   prog_bison = find_program('win_bison', required : false)
1831   if not prog_bison.found()
1832     prog_bison = find_program('bison', 'yacc', required : needs_flex_bison, disabler : true)
1833   endif
1834 else
1835   prog_bison = find_program('bison', required : false)
1836
1837   if not prog_bison.found()
1838     prog_bison = find_program('byacc', required : needs_flex_bison, disabler : true)
1839     yacc_is_bison = false
1840   endif
1841
1842   # Disable deprecated keyword warnings, since we have to use them for
1843   # old-bison compat.  See discussion in
1844   # https://gitlab.freedesktop.org/mesa/mesa/merge_requests/2161
1845   if find_program('bison', required : false, version : '> 2.3').found()
1846     prog_bison = [prog_bison, '-Wno-deprecated']
1847   endif
1848
1849   prog_flex = find_program('flex', required : needs_flex_bison, disabler : true)
1850   prog_flex_cpp = prog_flex
1851 endif
1852
1853 dep_selinux = null_dep
1854 if get_option('selinux')
1855   if get_option('execmem') != true
1856     warning('execmem option is disabled, selinux will not be able to use execmem.')
1857   endif
1858   dep_selinux = dependency('libselinux')
1859   pre_args += '-DMESA_SELINUX'
1860 endif
1861
1862 if get_option('execmem')
1863   pre_args += '-DMESA_EXECMEM'
1864 endif
1865
1866 _libunwind = get_option('libunwind') \
1867   .require(not with_platform_android, error_message : 'Android requires the use of the backtrace library, not libunwind')
1868 if host_machine.system() == 'darwin'
1869   dep_unwind = meson.get_compiler('c').find_library('System', required : _libunwind)
1870 else
1871   dep_unwind = dependency('libunwind', required : _libunwind)
1872 endif
1873 if dep_unwind.found()
1874   pre_args += '-DHAVE_LIBUNWIND'
1875 endif
1876
1877 if with_osmesa
1878   if not with_gallium_softpipe
1879     error('OSMesa gallium requires gallium softpipe or llvmpipe.')
1880   endif
1881   if host_machine.system() == 'windows'
1882     osmesa_lib_name = 'osmesa'
1883   else
1884     osmesa_lib_name = 'OSMesa'
1885   endif
1886 endif
1887
1888 # TODO: symbol mangling
1889
1890 if with_platform_wayland
1891   dep_wl_scanner = dependency('wayland-scanner', native: true)
1892   prog_wl_scanner = find_program(dep_wl_scanner.get_variable(pkgconfig : 'wayland_scanner'))
1893   if dep_wl_scanner.version().version_compare('>= 1.15')
1894     wl_scanner_arg = 'private-code'
1895   else
1896     wl_scanner_arg = 'code'
1897   endif
1898   dep_wl_protocols = dependency('wayland-protocols', version : '>= 1.24')
1899   dep_wayland_client = dependency('wayland-client', version : '>=1.18')
1900   dep_wayland_server = dependency('wayland-server', version : '>=1.18')
1901   if with_egl
1902     dep_wayland_egl = dependency('wayland-egl-backend', version : '>= 3')
1903     dep_wayland_egl_headers = dep_wayland_egl.partial_dependency(compile_args : true)
1904   endif
1905   wayland_dmabuf_xml = join_paths(
1906     dep_wl_protocols.get_variable(pkgconfig : 'pkgdatadir'), 'unstable',
1907     'linux-dmabuf', 'linux-dmabuf-unstable-v1.xml'
1908   )
1909   pre_args += '-DWL_HIDE_DEPRECATED'
1910 endif
1911
1912 dep_x11 = null_dep
1913 dep_xext = null_dep
1914 dep_xfixes = null_dep
1915 dep_x11_xcb = null_dep
1916 dep_xcb = null_dep
1917 dep_xcb_glx = null_dep
1918 dep_xcb_dri2 = null_dep
1919 dep_xcb_dri3 = null_dep
1920 dep_dri2proto = null_dep
1921 dep_glproto = null_dep
1922 dep_xxf86vm = null_dep
1923 dep_xcb_dri3 = null_dep
1924 dep_xcb_present = null_dep
1925 dep_xcb_sync = null_dep
1926 dep_xcb_xfixes = null_dep
1927 dep_xshmfence = null_dep
1928 dep_xcb_xrandr = null_dep
1929 dep_xcb_shm = null_dep
1930 dep_xlib_xrandr = null_dep
1931 dep_openmp = null_dep
1932
1933 # Even if we find OpenMP, Gitlab CI fails to link with gcc/i386 and clang/anyarch.
1934 if host_machine.cpu_family() == 'x86_64' and cc.get_id() == 'gcc'
1935   dep_openmp = dependency('openmp', required : false)
1936   if dep_openmp.found()
1937     pre_args += ['-DHAVE_OPENMP']
1938   endif
1939 endif
1940
1941 with_dri3_modifiers = false
1942 if with_platform_x11
1943   if with_glx == 'xlib'
1944     dep_x11 = dependency('x11')
1945     dep_xext = dependency('xext')
1946     dep_xcb = dependency('xcb')
1947     dep_xcb_xrandr = dependency('xcb-randr')
1948   elif with_glx == 'dri'
1949     dep_x11 = dependency('x11')
1950     dep_xext = dependency('xext')
1951     dep_xfixes = dependency('xfixes', version : '>= 2.0')
1952     dep_xcb_glx = dependency('xcb-glx', version : '>= 1.8.1')
1953     dep_xcb_shm = dependency('xcb-shm')
1954   endif
1955   if (with_any_vk or with_glx == 'dri' or with_egl or
1956        (with_gallium_vdpau or with_gallium_va or
1957         with_gallium_omx != 'disabled'))
1958     dep_xcb = dependency('xcb')
1959     dep_x11_xcb = dependency('x11-xcb')
1960     if with_dri_platform == 'drm' and not dep_libdrm.found()
1961       error('libdrm required for gallium video statetrackers when using x11')
1962     endif
1963   endif
1964   if with_any_vk or with_egl or (with_glx == 'dri' and with_dri_platform == 'drm')
1965     dep_xcb_dri2 = dependency('xcb-dri2', version : '>= 1.8')
1966
1967     if with_dri3
1968       dep_xcb_dri3 = dependency('xcb-dri3')
1969       dep_xcb_present = dependency('xcb-present')
1970       # until xcb-dri3 has been around long enough to make a hard-dependency:
1971       if (dep_xcb_dri3.version().version_compare('>= 1.13') and
1972           dep_xcb_present.version().version_compare('>= 1.13'))
1973         with_dri3_modifiers = true
1974       endif
1975       dep_xcb_shm = dependency('xcb-shm')
1976       dep_xcb_sync = dependency('xcb-sync')
1977       dep_xshmfence = dependency('xshmfence', version : '>= 1.1')
1978     endif
1979   endif
1980   if with_glx == 'dri' or with_glx == 'xlib'
1981     dep_glproto = dependency('glproto', version : '>= 1.4.14')
1982   endif
1983   if with_glx == 'dri'
1984     if with_dri_platform == 'drm'
1985       dep_dri2proto = dependency('dri2proto', version : '>= 2.8')
1986       if with_glx_direct
1987         dep_xxf86vm = dependency('xxf86vm')
1988       endif
1989     endif
1990   endif
1991   if (with_egl or
1992       with_dri3 or (
1993       with_gallium_vdpau or with_gallium_xa or
1994       with_gallium_omx != 'disabled'))
1995     dep_xcb_xfixes = dependency('xcb-xfixes')
1996   endif
1997   if with_xlib_lease or with_any_vk
1998     dep_xcb_xrandr = dependency('xcb-randr')
1999   endif
2000   if with_xlib_lease
2001     dep_xlib_xrandr = dependency('xrandr', version : '>= 1.3')
2002   endif
2003 endif
2004
2005 if with_dri
2006   pre_args += '-DHAVE_DRI'
2007 endif
2008 if with_dri2
2009   pre_args += '-DHAVE_DRI2'
2010 endif
2011 if with_dri3
2012   pre_args += '-DHAVE_DRI3'
2013 endif
2014 if with_dri3_modifiers
2015   pre_args += '-DHAVE_DRI3_MODIFIERS'
2016 endif
2017 if with_gallium_drisw_kms
2018   pre_args += '-DHAVE_DRISW_KMS'
2019 endif
2020
2021 if get_option('gallium-extra-hud')
2022   pre_args += '-DHAVE_GALLIUM_EXTRA_HUD=1'
2023 endif
2024
2025 dep_lmsensors = cc.find_library('sensors', required : get_option('lmsensors'))
2026 if dep_lmsensors.found()
2027   pre_args += '-DHAVE_LIBSENSORS=1'
2028 endif
2029
2030 _shader_replacement = get_option('custom-shader-replacement')
2031 if _shader_replacement == ''
2032 else
2033   pre_args += '-DCUSTOM_SHADER_REPLACEMENT'
2034 endif
2035
2036 with_perfetto = get_option('perfetto')
2037 with_datasources = get_option('datasources')
2038 with_any_datasource = with_datasources.length() != 0
2039 if with_perfetto
2040   dep_perfetto = dependency('perfetto', fallback: ['perfetto', 'dep_perfetto'])
2041   pre_args += '-DHAVE_PERFETTO'
2042 endif
2043
2044 add_project_arguments(pre_args, language : ['c', 'cpp'])
2045 add_project_arguments(c_cpp_args, language : ['c', 'cpp'])
2046
2047 add_project_arguments(c_args,   language : ['c'])
2048 add_project_arguments(cpp_args, language : ['cpp'])
2049
2050 gl_priv_reqs = []
2051
2052 if with_glx == 'xlib'
2053   gl_priv_reqs += ['x11', 'xext', 'xcb']
2054 elif with_glx == 'dri'
2055   gl_priv_reqs += [
2056     'x11', 'xext', 'xfixes', 'x11-xcb', 'xcb',
2057     'xcb-glx >= 1.8.1']
2058   if with_dri_platform == 'drm'
2059     gl_priv_reqs += 'xcb-dri2 >= 1.8'
2060     if with_glx_direct
2061       gl_priv_reqs += 'xxf86vm'
2062     endif
2063   endif
2064 endif
2065 if dep_libdrm.found()
2066   gl_priv_reqs += 'libdrm >= 2.4.75'
2067 endif
2068
2069 gl_priv_libs = []
2070 if dep_thread.found()
2071   gl_priv_libs += ['-lpthread', '-pthread']
2072 endif
2073 if dep_m.found()
2074   gl_priv_libs += '-lm'
2075 endif
2076 if dep_dl.found()
2077   gl_priv_libs += '-ldl'
2078 endif
2079
2080 # FIXME: autotools lists this as incomplete
2081 gbm_priv_libs = []
2082 if dep_dl.found()
2083   gbm_priv_libs += '-ldl'
2084 endif
2085
2086 pkg = import('pkgconfig')
2087
2088 if host_machine.system() == 'windows'
2089   prog_dumpbin = find_program('dumpbin', required : false)
2090   with_symbols_check = prog_dumpbin.found() and with_tests
2091   if with_symbols_check
2092     symbols_check_args = ['--dumpbin', prog_dumpbin.full_path()]
2093   endif
2094 else
2095   prog_nm = find_program('nm')
2096   with_symbols_check = with_tests
2097   symbols_check_args = ['--nm', prog_nm.full_path()]
2098 endif
2099
2100 # This quirk needs to be applied to sources with functions defined in assembly
2101 # as GCC LTO drops them. See: https://bugs.freedesktop.org/show_bug.cgi?id=109391
2102 gcc_lto_quirk = (cc.get_id() == 'gcc') ? ['-fno-lto'] : []
2103
2104 devenv = environment()
2105
2106 dir_compiler_nir = join_paths(meson.current_source_dir(), 'src/compiler/nir/')
2107 dir_source_root = meson.project_source_root()
2108
2109
2110 subdir('include')
2111 subdir('bin')
2112 subdir('src')
2113
2114 meson.add_devenv(devenv)
2115
2116 summary(
2117   {
2118     'prefix': get_option('prefix'),
2119     'libdir': get_option('libdir'),
2120     'includedir': get_option('includedir'),
2121   },
2122   section: 'Directories'
2123 )
2124
2125 summary(
2126   {
2127     'c_cpp_args': c_cpp_args,
2128   },
2129   section: 'Common C and C++ arguments'
2130 )
2131
2132 summary(
2133   {
2134     'OpenGL': with_opengl,
2135     'ES1': with_gles1,
2136     'ES2': with_gles2,
2137     'Shared glapi': with_shared_glapi,
2138   },
2139   section: 'OpenGL', bool_yn: true
2140 )
2141
2142 summary(
2143   {
2144     'Platform': with_dri_platform,
2145     'Driver dir': dri_drivers_path,
2146   },
2147   section: 'DRI', bool_yn: true, list_sep: ' '
2148 )
2149
2150 summary(
2151   {
2152     'Enabled': with_glx != 'disabled',
2153     'Provider': with_glx == 'disabled' ? 'None' : with_glx
2154   },
2155   section: 'GLX', bool_yn: true, list_sep: ' '
2156 )
2157
2158 egl_summary = {'Enabled': with_egl}
2159 if with_egl
2160   egl_drivers = []
2161   if with_dri
2162     egl_drivers += 'builtin:egl_dri2'
2163   endif
2164   if with_dri3
2165     egl_drivers += 'builtin:egl_dri3'
2166   endif
2167   if with_platform_windows
2168     egl_drivers += 'builtin:wgl'
2169   endif
2170   egl_summary += {'Drivers': egl_drivers}
2171   egl_summary += {'Platforms': _platforms}
2172 endif
2173 summary(egl_summary, section: 'EGL', bool_yn: true, list_sep: ' ')
2174
2175 gbm_summary = {'Enabled': with_gbm}
2176 if with_gbm
2177   gbm_summary += {'Backends path': gbm_backends_path}
2178 endif
2179 summary(gbm_summary, section: 'GBM', bool_yn: true, list_sep: ' ')
2180
2181 vulkan_summary = {'Drivers': _vulkan_drivers.length() != 0 ? _vulkan_drivers : false }
2182 if with_any_vk
2183   vulkan_summary += {'Platforms': _platforms}
2184   vulkan_summary += {'ICD dir': with_vulkan_icd_dir}
2185   if with_any_vulkan_layers
2186     vulkan_summary += {'Layers': get_option('vulkan-layers')}
2187   endif
2188   vulkan_summary += {'Video codecs': _codecs.length() != 0 ? _codecs : false}
2189   vulkan_summary += {'Ray tracing': with_intel_vk_rt ? 'intel' : false}
2190 endif
2191 summary(vulkan_summary, section: 'Vulkan', bool_yn: true, list_sep: ' ')
2192
2193 llvm_summary = {'Enabled': with_llvm}
2194 if with_llvm
2195   llvm_summary += {'Version': dep_llvm.version()}
2196 endif
2197 summary(llvm_summary, section: 'LLVM', bool_yn: true, list_sep: ' ')
2198
2199 gallium_summary = {'Enabled': with_gallium}
2200 if with_gallium
2201   gallium_summary += {'Drivers': gallium_drivers}
2202   gallium_summary += {'Platforms': _platforms}
2203
2204   gallium_frontends = ['mesa']
2205   if with_gallium_xa
2206     gallium_frontends += 'xa'
2207   endif
2208   if with_gallium_vdpau
2209     gallium_frontends += 'vdpau'
2210   endif
2211   if with_gallium_omx != 'disabled'
2212     gallium_frontends += 'omx' + with_gallium_omx
2213   endif
2214   if with_gallium_va
2215     gallium_frontends += 'va'
2216   endif
2217   if with_gallium_st_nine
2218     gallium_frontends += 'nine'
2219   endif
2220   if with_gallium_opencl
2221     gallium_frontends += 'clover'
2222   endif
2223   if with_gallium_rusticl
2224     gallium_frontends += 'rusticl'
2225   endif
2226   gallium_summary += {'Frontends': gallium_frontends}
2227   gallium_summary += {'Off-screen rendering': with_osmesa ? 'lib' + osmesa_lib_name : false}
2228   gallium_summary += {'HUD lm-sensors': dep_lmsensors.found()}
2229 endif
2230 summary(gallium_summary, section: 'Gallium', bool_yn: true, list_sep: ' ')
2231
2232 perfetto_summary = {'Enabled': with_perfetto}
2233 if with_perfetto and with_any_datasource
2234   perfetto_summary += {'Data source': with_datasources}
2235 endif
2236 summary(perfetto_summary, section: 'Perfetto', bool_yn: true, list_sep: ' ')