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