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