1# Copyright © 2017 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
21project(
22  'mesa',
23  ['c', 'cpp'],
24  version : run_command(
25    [find_program('python', 'python2', 'python3'), 'bin/meson_get_version.py']
26  ).stdout(),
27  license : 'MIT',
28  meson_version : '>= 0.42',
29  default_options : ['buildtype=debugoptimized', 'c_std=c99', 'cpp_std=c++11']
30)
31
32# Arguments for the preprocessor, put these in a separate array from the C and
33# C++ (cpp in meson terminology) arguments since they need to be added to the
34# default arguments for both C and C++.
35pre_args = [
36  '-D__STDC_CONSTANT_MACROS',
37  '-D__STDC_FORMAT_MACROS',
38  '-D__STDC_LIMIT_MACROS',
39  '-DVERSION="@0@"'.format(meson.project_version()),
40  '-DPACKAGE_VERSION=VERSION',
41  '-DPACKAGE_BUGREPORT="https://bugs.freedesktop.org/enter_bug.cgi?product=Mesa"',
42]
43
44with_vulkan_icd_dir = get_option('vulkan-icd-dir')
45with_tests = get_option('build-tests')
46with_valgrind = get_option('valgrind')
47with_libunwind = get_option('libunwind')
48with_asm = get_option('asm')
49with_osmesa = get_option('osmesa')
50with_swr_arches = get_option('swr-arches').split(',')
51if get_option('texture-float')
52  pre_args += '-DTEXTURE_FLOAT_ENABLED'
53  message('WARNING: Floating-point texture enabled. Please consult docs/patents.txt and your lawyer before building mesa.')
54endif
55
56dri_drivers_path = get_option('dri-drivers-path')
57if dri_drivers_path == ''
58  dri_drivers_path = join_paths(get_option('libdir'), 'dri')
59endif
60dri_search_path = get_option('dri-search-path')
61if dri_search_path == ''
62  dri_search_path = join_paths(get_option('prefix'), dri_drivers_path)
63endif
64
65with_gles1 = get_option('gles1')
66with_gles2 = get_option('gles2')
67with_opengl = get_option('opengl')
68with_any_opengl = with_opengl or with_gles1 or with_gles2
69# Only build shared_glapi if at least one OpenGL API is enabled
70with_shared_glapi = get_option('shared-glapi') and with_any_opengl
71
72
73# shared-glapi is required if at least two OpenGL APIs are being built
74if not with_shared_glapi
75  if ((with_gles1 and with_gles2) or (with_gles1 and with_opengl)
76      or (with_gles2 and with_opengl))
77    error('shared-glapi required for building two or more of OpenGL, OpenGL ES 1.x, OpenGL ES 2.x')
78  endif
79endif
80
81# We require OpenGL for OpenGL ES
82if (with_gles1 or with_gles2) and not with_opengl
83  error('building OpenGL ES without OpenGL is not supported.')
84endif
85
86with_dri = false
87with_dri_i915 = false
88with_dri_i965 = false
89with_dri_r100 = false
90with_dri_r200 = false
91with_dri_nouveau = false
92with_dri_swrast = false
93_drivers = get_option('dri-drivers')
94if _drivers == 'auto'
95  # TODO: PPC, Sparc
96  if not ['darwin', 'windows'].contains(host_machine.system())
97    if ['x86', 'x86_64'].contains(host_machine.cpu_family())
98      _drivers = 'i915,i965,r100,r200,nouveau'
99    else
100      error('Unknown architecture. Please pass -Ddri-drivers to set driver options. Patches gladly accepted to fix this.')
101    endif
102  else
103    error('Unknown OS. Please pass -Ddri-drivers to set driver options. Patches gladly accepted to fix this.')
104  endif
105endif
106if _drivers != ''
107  _split = _drivers.split(',')
108  with_dri_i915 = _split.contains('i915')
109  with_dri_i965 = _split.contains('i965')
110  with_dri_r100 = _split.contains('r100')
111  with_dri_r200 = _split.contains('r200')
112  with_dri_nouveau = _split.contains('nouveau')
113  with_dri_swrast = _split.contains('swrast')
114  with_dri = true
115endif
116
117with_gallium = false
118with_gallium_pl111 = false
119with_gallium_radeonsi = false
120with_gallium_r300 = false
121with_gallium_r600 = false
122with_gallium_nouveau = false
123with_gallium_freedreno = false
124with_gallium_softpipe = false
125with_gallium_vc4 = false
126with_gallium_vc5 = false
127with_gallium_etnaviv = false
128with_gallium_imx = false
129with_gallium_i915 = false
130with_gallium_svga = false
131with_gallium_virgl = false
132with_gallium_swr = false
133_drivers = get_option('gallium-drivers')
134if _drivers == 'auto'
135  if not ['darwin', 'windows'].contains(host_machine.system())
136    # TODO: PPC, Sparc
137    if ['x86', 'x86_64'].contains(host_machine.cpu_family())
138      _drivers = 'r300,r600,radeonsi,nouveau,virgl,svga,swrast'
139    elif ['arm', 'aarch64'].contains(host_machine.cpu_family())
140      _drivers = 'pl111,vc4,vc5,freedreno,etnaviv,imx,virgl,svga,swrast'
141    else
142      error('Unknown architecture. Please pass -Dgallium-drivers to set driver options. Patches gladly accepted to fix this.')
143    endif
144  else
145    error('Unknown OS. Please pass -Dgallium-drivers to set driver options. Patches gladly accepted to fix this.')
146  endif
147endif
148if _drivers != ''
149  _split = _drivers.split(',')
150  with_gallium_pl111 = _split.contains('pl111')
151  with_gallium_radeonsi = _split.contains('radeonsi')
152  with_gallium_r300 = _split.contains('r300')
153  with_gallium_r600 = _split.contains('r600')
154  with_gallium_nouveau = _split.contains('nouveau')
155  with_gallium_freedreno = _split.contains('freedreno')
156  with_gallium_softpipe = _split.contains('swrast')
157  with_gallium_vc4 = _split.contains('vc4')
158  with_gallium_vc5 = _split.contains('vc5')
159  with_gallium_etnaviv = _split.contains('etnaviv')
160  with_gallium_imx = _split.contains('imx')
161  with_gallium_i915 = _split.contains('i915')
162  with_gallium_svga = _split.contains('svga')
163  with_gallium_virgl = _split.contains('virgl')
164  with_gallium_swr = _split.contains('swr')
165  with_gallium = true
166endif
167
168with_intel_vk = false
169with_amd_vk = false
170with_any_vk = false
171_vulkan_drivers = get_option('vulkan-drivers')
172if _vulkan_drivers == 'auto'
173  if not ['darwin', 'windows'].contains(host_machine.system())
174    if host_machine.cpu_family().startswith('x86')
175      _vulkan_drivers = 'amd,intel'
176    else
177      error('Unknown architecture. Please pass -Dvulkan-drivers to set driver options. Patches gladly accepted to fix this.')
178    endif
179  else
180    # No vulkan driver supports windows or macOS currently
181    _vulkan_drivers = ''
182  endif
183endif
184if _vulkan_drivers != ''
185  _split = _vulkan_drivers.split(',')
186  with_intel_vk = _split.contains('intel')
187  with_amd_vk = _split.contains('amd')
188  with_any_vk = with_amd_vk or with_intel_vk
189endif
190
191if with_dri_swrast and (with_gallium_softpipe or with_gallium_swr)
192  error('Only one swrast provider can be built')
193endif
194if with_dri_i915 and with_gallium_i915
195  error('Only one i915 provider can be built')
196endif
197if with_gallium_imx and not with_gallium_etnaviv
198  error('IMX driver requires etnaviv driver')
199endif
200if with_gallium_pl111 and not with_gallium_vc4
201  error('pl111 driver requires vc4 driver')
202endif
203
204dep_libdrm_intel = []
205if with_dri_i915 or with_gallium_i915
206  dep_libdrm_intel = dependency('libdrm_intel', version : '>= 2.4.75')
207endif
208
209system_has_kms_drm = ['openbsd', 'netbsd', 'freebsd', 'dragonfly', 'linux'].contains(host_machine.system())
210
211if host_machine.system() == 'darwin'
212  with_dri_platform = 'apple'
213elif ['windows', 'cygwin'].contains(host_machine.system())
214  with_dri_platform = 'windows'
215elif system_has_kms_drm
216  with_dri_platform = 'drm'
217else
218  # FIXME: haiku doesn't use dri, and xlib doesn't use dri, probably should
219  # assert here that one of those cases has been met.
220  # FIXME: GNU (hurd) ends up here as well, but meson doesn't officially
221  # support Hurd at time of writing (2017/11)
222  # FIXME: illumos ends up here as well
223  with_dri_platform = 'none'
224endif
225
226with_platform_android = false
227with_platform_wayland = false
228with_platform_x11 = false
229with_platform_drm = false
230with_platform_surfaceless = false
231egl_native_platform = ''
232_platforms = get_option('platforms')
233if _platforms == 'auto'
234  if system_has_kms_drm
235    _platforms = 'x11,wayland,drm,surfaceless'
236  else
237    error('Unknown OS, no platforms enabled. Patches gladly accepted to fix this.')
238  endif
239endif
240if _platforms != ''
241  _split = _platforms.split(',')
242  with_platform_android = _split.contains('android')
243  with_platform_x11 = _split.contains('x11')
244  with_platform_wayland = _split.contains('wayland')
245  with_platform_drm = _split.contains('drm')
246  with_platform_surfaceless = _split.contains('surfaceless')
247  egl_native_platform = _split[0]
248endif
249
250with_glx = get_option('glx')
251if with_glx == 'auto'
252  if with_dri
253    with_glx = 'dri'
254  elif with_gallium
255    # Even when building just gallium drivers the user probably wants dri
256    with_glx = 'dri'
257  elif with_platform_x11 and with_any_opengl and not with_any_vk
258    # The automatic behavior should not be to turn on xlib based glx when
259    # building only vulkan drivers
260    with_glx = 'xlib'
261  else
262    with_glx = 'disabled'
263  endif
264endif
265if with_glx == 'dri'
266   if with_gallium
267      with_dri = true
268   endif
269endif
270
271if not (with_dri or with_gallium or with_glx == 'xlib' or with_glx == 'gallium-xlib')
272  with_gles1 = false
273  with_gles2 = false
274  with_opengl = false
275  with_any_opengl = false
276  with_shared_glapi = false
277endif
278
279with_gbm = get_option('gbm')
280if with_gbm == 'auto' and with_dri  # TODO: or gallium
281  with_gbm = system_has_kms_drm
282elif with_gbm == 'true'
283  if not system_has_kms_drm
284    error('GBM only supports DRM/KMS platforms')
285  endif
286  with_gbm = true
287else
288  with_gbm = false
289endif
290
291_egl = get_option('egl')
292if _egl == 'auto'
293  with_egl = with_dri and with_shared_glapi and egl_native_platform != ''
294elif _egl == 'true'
295  if not with_dri
296    error('EGL requires dri')
297  elif not with_shared_glapi
298    error('EGL requires shared-glapi')
299  elif egl_native_platform == ''
300    error('No platforms specified, consider -Dplatforms=drm,x11 at least')
301  endif
302  with_egl = true
303else
304  with_egl = false
305endif
306
307if with_egl and not (with_platform_drm or with_platform_surfaceless)
308  if with_gallium_radeonsi
309    error('RadeonSI requires drm or surfaceless platform when using EGL')
310  endif
311  if with_gallium_virgl
312    error('Virgl requires drm or surfaceless platform when using EGL')
313  endif
314endif
315
316pre_args += '-DGLX_USE_TLS'
317if with_glx != 'disabled'
318  if not (with_platform_x11 and with_any_opengl)
319    if with_glx == 'auto'
320      with_glx = 'disabled'
321    else
322      error('Cannot build GLX support without X11 platform support and at least one OpenGL API')
323    endif
324  elif with_glx == 'gallium-xlib'
325    if not with_gallium
326      error('Gallium-xlib based GLX requires at least one gallium driver')
327    elif not with_gallium_softpipe
328      error('Gallium-xlib based GLX requires softpipe or llvmpipe.')
329    elif with_dri
330      error('gallium-xlib conflicts with any dri driver')
331    endif
332  elif with_glx == 'xlib'
333    if with_dri
334      error('xlib conflicts with any dri driver')
335    endif
336  elif with_glx == 'dri' and not with_dri
337    error('dri based GLX requires at least one DRI driver')
338  endif
339endif
340
341with_glvnd = get_option('glvnd')
342if with_glvnd
343  if with_glx == 'xlib' or with_glx == 'gallium-xlib'
344    error('Cannot build glvnd support for GLX that is not DRI based.')
345  elif with_glx == 'disabled' and not with_egl
346    error('glvnd requires DRI based GLX and/or EGL')
347  endif
348endif
349
350# TODO: toggle for this
351with_glx_direct = true
352
353if with_vulkan_icd_dir == ''
354  with_vulkan_icd_dir = join_paths(get_option('datadir'), 'vulkan/icd.d')
355endif
356
357with_dri2 = (with_dri or with_any_vk) and with_dri_platform == 'drm'
358with_dri3 = get_option('dri3')
359if with_dri3 == 'auto'
360  if system_has_kms_drm and with_dri2
361    with_dri3 = true
362  else
363    with_dri3 = false
364 endif
365elif with_dri3 == 'true'
366  with_dri3 = true
367else
368  with_dri3 = false
369endif
370
371if with_any_vk and (with_platform_x11 and not with_dri3)
372  error('Vulkan drivers require dri3 for X11 support')
373endif
374if with_dri or with_gallium
375  if with_glx == 'disabled' and not with_egl
376    error('building dri or gallium drivers require at least one window system')
377  endif
378endif
379
380prog_pkgconfig = find_program('pkg-config')
381
382_vdpau = get_option('gallium-vdpau')
383if not system_has_kms_drm
384  if _vdpau == 'true'
385    error('VDPAU state tracker can only be build on unix-like OSes.')
386  else
387    _vdpau = 'false'
388  endif
389elif not with_platform_x11
390  if _vdpau == 'true'
391    error('VDPAU state tracker requires X11 support.')
392  else
393    _vdpau = 'false'
394  endif
395elif not (with_gallium_r300 or with_gallium_r600 or with_gallium_radeonsi or
396          with_gallium_nouveau)
397  if _vdpau == 'true'
398    error('VDPAU state tracker requires at least one of the following gallium drivers: r300, r600, radeonsi, nouveau.')
399  else
400    _vdpau = 'false'
401  endif
402elif _vdpau == 'auto'
403  _vdpau = 'true'
404endif
405with_gallium_vdpau = _vdpau == 'true'
406dep_vdpau = []
407if with_gallium_vdpau
408  dep_vdpau = dependency('vdpau', version : '>= 1.1')
409  dep_vdpau = declare_dependency(
410    compile_args : run_command(prog_pkgconfig, ['vdpau', '--cflags']).stdout().split()
411  )
412endif
413
414if with_gallium_vdpau
415  pre_args += '-DHAVE_ST_VDPAU'
416endif
417vdpau_drivers_path = get_option('vdpau-libs-path')
418if vdpau_drivers_path == ''
419  vdpau_drivers_path = join_paths(get_option('libdir'), 'vdpau')
420endif
421
422_xvmc = get_option('gallium-xvmc')
423if not system_has_kms_drm
424  if _xvmc == 'true'
425    error('XVMC state tracker can only be build on unix-like OSes.')
426  else
427    _xvmc = 'false'
428  endif
429elif not with_platform_x11
430  if _xvmc == 'true'
431    error('XVMC state tracker requires X11 support.')
432  else
433    _xvmc = 'false'
434  endif
435elif not (with_gallium_r600 or with_gallium_nouveau)
436  if _xvmc == 'true'
437    error('XVMC state tracker requires at least one of the following gallium drivers: r600, nouveau.')
438  else
439    _xvmc = 'false'
440  endif
441elif _xvmc == 'auto'
442  _xvmc = 'true'
443endif
444with_gallium_xvmc = _xvmc == 'true'
445dep_xvmc = []
446if with_gallium_xvmc
447  dep_xvmc = dependency('xvmc', version : '>= 1.0.6')
448endif
449
450xvmc_drivers_path = get_option('xvmc-libs-path')
451if xvmc_drivers_path == ''
452  xvmc_drivers_path = get_option('libdir')
453endif
454
455_omx = get_option('gallium-omx')
456if not system_has_kms_drm
457  if _omx == 'true'
458    error('OMX state tracker can only be built on unix-like OSes.')
459  else
460    _omx = 'false'
461  endif
462elif not (with_platform_x11 or with_platform_drm)
463  if _omx == 'true'
464    error('OMX state tracker requires X11 or drm platform support.')
465  else
466    _omx = 'false'
467  endif
468elif not (with_gallium_r600 or with_gallium_radeonsi or with_gallium_nouveau)
469  if _omx == 'true'
470    error('OMX state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau.')
471  else
472    _omx = 'false'
473  endif
474elif _omx == 'auto'
475  _omx = 'true'
476endif
477with_gallium_omx = _omx == 'true'
478dep_omx = []
479if with_gallium_omx
480  dep_omx = dependency('libomxil-bellagio')
481endif
482
483omx_drivers_path = get_option('omx-libs-path')
484if with_gallium_omx
485  # Figure out where to put the omx driver.
486  # FIXME: this could all be vastly simplified by adding a 'defined_variable'
487  # argument to meson's get_pkgconfig_variable method.
488  if omx_drivers_path == ''
489    _omx_libdir = dep_omx.get_pkgconfig_variable('libdir')
490    _omx_drivers_dir = dep_omx.get_pkgconfig_variable('pluginsdir')
491    if _omx_libdir == get_option('libdir')
492      omx_drivers_path = _omx_drivers_dir
493    else
494      _omx_base_dir = []
495      # This will fail on windows. Does OMX run on windows?
496      _omx_libdir = _omx_libdir.split('/')
497      _omx_drivers_dir = _omx_drivers_dir.split('/')
498      foreach o : _omx_drivers_dir
499        if not _omx_libdir.contains(o)
500          _omx_base_dir += o
501        endif
502      endforeach
503      omx_drivers_path = join_paths(get_option('libdir'), _omx_base_dir)
504    endif
505  endif
506endif
507
508_va = get_option('gallium-va')
509if not system_has_kms_drm
510  if _va == 'true'
511    error('VA state tracker can only be built on unix-like OSes.')
512  else
513    _va = 'false'
514  endif
515elif not (with_platform_x11 or with_platform_drm)
516  if _va == 'true'
517    error('VA state tracker requires X11 or drm or wayland platform support.')
518  else
519    _va = 'false'
520  endif
521elif not (with_gallium_r600 or with_gallium_radeonsi or with_gallium_nouveau)
522  if _va == 'true'
523    error('VA state tracker requires at least one of the following gallium drivers: r600, radeonsi, nouveau.')
524  else
525    _va = 'false'
526  endif
527elif _va == 'auto'
528  _va = 'true'
529endif
530with_gallium_va = _va == 'true'
531dep_va = []
532if with_gallium_va
533  dep_va = dependency('libva', version : '>= 0.38.0')
534  dep_va_headers = declare_dependency(
535    compile_args : run_command(prog_pkgconfig, ['libva', '--cflags']).stdout().split()
536  )
537endif
538
539va_drivers_path = get_option('va-libs-path')
540if va_drivers_path == ''
541  va_drivers_path = join_paths(get_option('libdir'), 'dri')
542endif
543
544_xa = get_option('gallium-xa')
545if not system_has_kms_drm
546  if _xa == 'true'
547    error('XA state tracker can only be built on unix-like OSes.')
548  else
549    _xa = 'false'
550  endif
551elif not (with_gallium_nouveau or with_gallium_freedreno or with_gallium_i915
552          or with_gallium_svga)
553  if _xa == 'true'
554    error('XA state tracker requires at least one of the following gallium drivers: nouveau, freedreno, i915, svga.')
555  else
556    _xa = 'false'
557  endif
558endif
559with_gallium_xa = _xa != 'false'
560
561d3d_drivers_path = get_option('d3d-drivers-path')
562if d3d_drivers_path == ''
563  d3d_drivers_path = join_paths(get_option('libdir'), 'd3d')
564endif
565
566with_gallium_st_nine =  get_option('gallium-nine')
567if with_gallium_st_nine
568  if not with_gallium_softpipe
569    error('The nine state tracker requires gallium softpipe/llvmpipe.')
570  elif not (with_gallium_radeonsi or with_gallium_nouveau or with_gallium_r600
571            or with_gallium_r300 or with_gallium_svga or with_gallium_i915)
572    error('The nine state tracker requires at least on non-swrast gallium driver.')
573  endif
574  if not with_dri3
575    error('Using nine with wine requires dri3')
576  endif
577endif
578
579_opencl = get_option('gallium-opencl')
580if _opencl != 'disabled'
581  if not with_gallium
582    error('OpenCL Clover implementation requires at least one gallium driver.')
583  endif
584
585  # TODO: alitvec?
586  dep_clc = dependency('libclc')
587  with_gallium_opencl = true
588  with_opencl_icd = _opencl == 'icd'
589else
590  dep_clc = []
591  with_gallium_opencl = false
592  with_gallium_icd = false
593endif
594
595gl_pkgconfig_c_flags = []
596if with_platform_x11
597  if with_any_vk or (with_glx == 'dri' and with_dri_platform == 'drm')
598    pre_args += '-DHAVE_X11_PLATFORM'
599  endif
600  if with_glx == 'xlib' or with_glx == 'gallium-xlib'
601    pre_args += '-DUSE_XSHM'
602  else
603    pre_args += '-DGLX_INDIRECT_RENDERING'
604    if with_glx_direct
605      pre_args += '-DGLX_DIRECT_RENDERING'
606    endif
607    if with_dri_platform == 'drm'
608      pre_args += '-DGLX_USE_DRM'
609    elif with_dri_platform == 'windows'
610      pre_args += '-DGLX_USE_WINDOWSGL'
611    endif
612  endif
613else
614  pre_args += '-DMESA_EGL_NO_X11_HEADERS'
615  gl_pkgconfig_c_flags += '-DMESA_EGL_NO_X11_HEADERS'
616endif
617if with_platform_drm
618  if with_egl and not with_gbm
619    error('EGL drm platform requires gbm')
620  endif
621  pre_args += '-DHAVE_DRM_PLATFORM'
622endif
623if with_platform_surfaceless
624  pre_args += '-DHAVE_SURFACELESS_PLATFORM'
625endif
626if with_platform_android
627  dep_android = [
628    dependency('cutils'),
629    dependency('hardware'),
630    dependency('sync'),
631  ]
632  pre_args += '-DHAVE_ANDROID_PLATFORM'
633endif
634
635prog_python2 = find_program('python2')
636has_mako = run_command(prog_python2, '-c', 'import mako')
637if has_mako.returncode() != 0
638  error('Python (2.x) mako module required to build mesa.')
639endif
640
641cc = meson.get_compiler('c')
642if cc.get_id() == 'gcc' and cc.version().version_compare('< 4.4.6')
643  error('When using GCC, version 4.4.6 or later is required.')
644endif
645
646# Define DEBUG for debug builds only (debugoptimized is not included on this one)
647if get_option('buildtype') == 'debug'
648  pre_args += '-DDEBUG'
649endif
650
651if get_option('shader-cache')
652  pre_args += '-DENABLE_SHADER_CACHE'
653elif with_amd_vk
654  error('Radv requires shader cache support')
655endif
656
657# Check for GCC style builtins
658foreach b : ['bswap32', 'bswap64', 'clz', 'clzll', 'ctz', 'expect', 'ffs',
659             'ffsll', 'popcount', 'popcountll', 'unreachable']
660  if cc.has_function(b)
661    pre_args += '-DHAVE___BUILTIN_@0@'.format(b.to_upper())
662  endif
663endforeach
664
665# check for GCC __attribute__
666foreach a : ['const', 'flatten', 'malloc', 'pure', 'unused',
667             'warn_unused_result', 'weak',]
668  if cc.compiles('int foo(void) __attribute__((@0@));'.format(a),
669                 name : '__attribute__((@0@))'.format(a))
670    pre_args += '-DHAVE_FUNC_ATTRIBUTE_@0@'.format(a.to_upper())
671  endif
672endforeach
673if cc.compiles('int foo(const char *p, ...) __attribute__((format(printf, 1, 2)));',
674               name : '__attribute__((format(...)))')
675  pre_args += '-DHAVE_FUNC_ATTRIBUTE_FORMAT'
676endif
677if cc.compiles('struct __attribute__((packed)) foo { int bar; };',
678               name : '__attribute__((packed))')
679  pre_args += '-DHAVE_FUNC_ATTRIBUTE_PACKED'
680endif
681if cc.compiles('int *foo(void) __attribute__((returns_nonnull));',
682               name : '__attribute__((returns_nonnull))')
683  pre_args += '-DHAVE_FUNC_ATTRIBUTE_RETURNS_NONNULL'
684endif
685if cc.compiles('''int foo_def(void) __attribute__((visibility("default")));
686                  int foo_hid(void) __attribute__((visibility("hidden")));
687                  int foo_int(void) __attribute__((visibility("internal")));
688                  int foo_pro(void) __attribute__((visibility("protected")));''',
689               name : '__attribute__((visibility(...)))')
690  pre_args += '-DHAVE_FUNC_ATTRIBUTE_VISIBILITY'
691endif
692if cc.compiles('int foo(void) { return 0; } int bar(void) __attribute__((alias("foo")));',
693               name : '__attribute__((alias(...)))')
694  pre_args += '-DHAVE_FUNC_ATTRIBUTE_ALIAS'
695endif
696if cc.compiles('int foo(void) __attribute__((__noreturn__));',
697               name : '__attribute__((__noreturn__))')
698  pre_args += '-DHAVE_FUNC_ATTRIBUTE_NORETURN'
699endif
700
701# TODO: this is very incomplete
702if ['linux', 'cygwin'].contains(host_machine.system())
703  pre_args += '-D_GNU_SOURCE'
704endif
705
706# Check for generic C arguments
707c_args = []
708foreach a : ['-Wall', '-Werror=implicit-function-declaration',
709             '-Werror=missing-prototypes', '-fno-math-errno',
710             '-fno-trapping-math', '-Qunused-arguments']
711  if cc.has_argument(a)
712    c_args += a
713  endif
714endforeach
715c_vis_args = []
716if cc.has_argument('-fvisibility=hidden')
717  c_vis_args += '-fvisibility=hidden'
718endif
719
720# Check for generic C++ arguments
721cpp = meson.get_compiler('cpp')
722cpp_args = []
723foreach a : ['-Wall', '-fno-math-errno', '-fno-trapping-math',
724             '-Qunused-arguments']
725  if cpp.has_argument(a)
726    cpp_args += a
727  endif
728endforeach
729
730# For some reason, the test for -Wno-foo always succeeds with gcc, even if the
731# option is not supported. Hence, check for -Wfoo instead.
732if cpp.has_argument('-Wnon-virtual-dtor')
733  cpp_args += '-Wno-non-virtual-dtor'
734endif
735
736no_override_init_args = []
737foreach a : ['override-init', 'initializer-overrides']
738  if cc.has_argument('-W' + a)
739    no_override_init_args += '-Wno-' + a
740  endif
741endforeach
742
743cpp_vis_args = []
744if cpp.has_argument('-fvisibility=hidden')
745  cpp_vis_args += '-fvisibility=hidden'
746endif
747
748# Check for C and C++ arguments for MSVC2013 compatibility. These are only used
749# in parts of the mesa code base that need to compile with old versions of
750# MSVC, mainly common code
751c_msvc_compat_args = []
752cpp_msvc_compat_args = []
753foreach a : ['-Werror=pointer-arith', '-Werror=vla']
754  if cc.has_argument(a)
755    c_msvc_compat_args += a
756  endif
757  if cpp.has_argument(a)
758    cpp_msvc_compat_args += a
759  endif
760endforeach
761
762if host_machine.cpu_family().startswith('x86')
763  pre_args += '-DUSE_SSE41'
764  with_sse41 = true
765  sse41_args = ['-msse4.1']
766
767  # GCC on x86 (not x86_64) with -msse* assumes a 16 byte aligned stack, but
768  # that's not guaranteed
769  if host_machine.cpu_family() == 'x86'
770    sse41_args += '-mstackrealign'
771  endif
772else
773  with_sse41 = false
774  sse41_args = []
775endif
776
777# Check for GCC style atomics
778if cc.compiles('int main() { int n; return __atomic_load_n(&n, __ATOMIC_ACQUIRE); }',
779               name : 'GCC atomic builtins')
780  pre_args += '-DUSE_GCC_ATOMIC_BUILTINS'
781endif
782if not cc.links('''#include <stdint.h>
783                   uint64_t v;
784                   int main() {
785                     return __sync_add_and_fetch(&v, (uint64_t)1);
786                   }''',
787                name : 'GCC 64bit atomics')
788  pre_args += '-DMISSING_64_BIT_ATOMICS'
789endif
790
791# TODO: endian
792# TODO: powr8
793# TODO: shared/static? Is this even worth doing?
794
795# Building x86 assembly code requires running x86 binaries. It is possible for
796# x86_64 OSes to run x86 binaries, so don't disable asm in those cases
797# TODO: it should be possible to use an exe_wrapper to run the binary during
798# the build.
799if meson.is_cross_build()
800  if not (build_machine.cpu_family() == 'x86_64' and host_machine.cpu_family() == 'x86'
801          and build_machine.system() == host_machine.system())
802    message('Cross compiling to x86 from non-x86, disabling asm')
803    with_asm = false
804  endif
805endif
806
807with_asm_arch = ''
808if with_asm
809  # TODO: SPARC and PPC
810  if host_machine.cpu_family() == 'x86'
811    if system_has_kms_drm
812      with_asm_arch = 'x86'
813      pre_args += ['-DUSE_X86_ASM', '-DUSE_MMX_ASM', '-DUSE_3DNOW_ASM',
814                   '-DUSE_SSE_ASM']
815    endif
816  elif host_machine.cpu_family() == 'x86_64'
817    if system_has_kms_drm
818      with_asm_arch = 'x86_64'
819      pre_args += ['-DUSE_X86_64_ASM']
820    endif
821  elif host_machine.cpu_family() == 'arm'
822    if system_has_kms_drm
823      with_asm_arch = 'arm'
824      pre_args += ['-DUSE_ARM_ASM']
825    endif
826  elif host_machine.cpu_family() == 'aarch64'
827    if system_has_kms_drm
828      with_asm_arch = 'aarch64'
829      pre_args += ['-DUSE_AARCH64_ASM']
830    endif
831  endif
832endif
833
834# Check for standard headers and functions
835if cc.has_header_symbol('sys/sysmacros.h', 'major')
836  pre_args += '-DMAJOR_IN_SYSMACROS'
837elif cc.has_header_symbol('sys/mkdev.h', 'major')
838  pre_args += '-DMAJOR_IN_MKDEV'
839endif
840
841foreach h : ['xlocale.h', 'sys/sysctl.h', 'linux/futex.h', 'endian.h']
842  if cc.compiles('#include <@0@>'.format(h), name : '@0@ works'.format(h))
843    pre_args += '-DHAVE_@0@'.format(h.to_upper().underscorify())
844  endif
845endforeach
846
847foreach f : ['strtof', 'mkostemp', 'posix_memalign', 'timespec_get', 'memfd_create']
848  if cc.has_function(f)
849    pre_args += '-DHAVE_@0@'.format(f.to_upper())
850  endif
851endforeach
852
853# strtod locale support
854if cc.links('''
855    #define _GNU_SOURCE
856    #include <stdlib.h>
857    #include <locale.h>
858    #ifdef HAVE_XLOCALE_H
859    #include <xlocale.h>
860    #endif
861    int main() {
862      locale_t loc = newlocale(LC_CTYPE_MASK, "C", NULL);
863      const char *s = "1.0";
864      char *end;
865      double d = strtod_l(s, end, loc);
866      float f = strtof_l(s, end, loc);
867      freelocale(loc);
868      return 0;
869    }''',
870    extra_args : pre_args,
871    name : 'strtod has locale support')
872  pre_args += '-DHAVE_STRTOD_L'
873endif
874
875# Check for some linker flags
876ld_args_bsymbolic = []
877if cc.links('int main() { return 0; }', args : '-Wl,-Bsymbolic', name : 'Bsymbolic')
878  ld_args_bsymbolic += '-Wl,-Bsymbolic'
879endif
880ld_args_gc_sections = []
881if cc.links('static char unused() { return 5; } int main() { return 0; }',
882            args : '-Wl,--gc-sections', name : 'gc-sections')
883  ld_args_gc_sections += '-Wl,--gc-sections'
884endif
885with_ld_version_script = false
886if cc.links('int main() { return 0; }',
887            args : '-Wl,--version-script=@0@'.format(
888              join_paths(meson.source_root(), 'build-support/conftest.map')),
889            name : 'version-script')
890  with_ld_version_script = true
891endif
892with_ld_dynamic_list = false
893if cc.links('int main() { return 0; }',
894            args : '-Wl,--dynamic-list=@0@'.format(
895              join_paths(meson.source_root(), 'build-support/conftest.dyn')),
896            name : 'dynamic-list')
897  with_ld_dynamic_list = true
898endif
899
900# check for dl support
901if cc.has_function('dlopen')
902  dep_dl = []
903else
904  dep_dl = cc.find_library('dl')
905endif
906if cc.has_function('dladdr', dependencies : dep_dl)
907  # This is really only required for megadrivers
908  pre_args += '-DHAVE_DLADDR'
909endif
910
911if cc.has_function('dl_iterate_phdr')
912  pre_args += '-DHAVE_DL_ITERATE_PHDR'
913elif with_intel_vk
914  error('Intel "Anvil" Vulkan driver requires the dl_iterate_phdr function')
915elif with_dri_i965 and get_option('shader-cache')
916  error('Intel i965 GL driver requires dl_iterate_phdr when built with shader caching.')
917endif
918
919# Determine whether or not the rt library is needed for time functions
920if cc.has_function('clock_gettime')
921  dep_clock = []
922else
923  dep_clock = cc.find_library('rt')
924endif
925
926with_gallium_drisw_kms = false
927dep_libdrm = dependency('libdrm', version : '>= 2.4.75',
928                        required : with_dri2 or with_dri3)
929if dep_libdrm.found()
930  pre_args += '-DHAVE_LIBDRM'
931  if with_dri_platform == 'drm' and with_dri
932    with_gallium_drisw_kms = true
933  endif
934endif
935
936# TODO: some of these may be conditional
937dep_zlib = dependency('zlib', version : '>= 1.2.3')
938pre_args += '-DHAVE_ZLIB'
939dep_thread = dependency('threads')
940if dep_thread.found() and host_machine.system() != 'windows'
941  pre_args += '-DHAVE_PTHREAD'
942endif
943if with_amd_vk or with_gallium_radeonsi or with_gallium_r600 or with_gallium_opencl
944  dep_elf = dependency('libelf', required : false)
945  if not dep_elf.found()
946    dep_elf = cc.find_library('elf')
947  endif
948else
949  dep_elf = []
950endif
951dep_expat = dependency('expat')
952# this only exists on linux so either this is linux and it will be found, or
953# its not linux and and wont
954dep_m = cc.find_library('m', required : false)
955
956dep_libdrm_amdgpu = []
957dep_libdrm_radeon = []
958dep_libdrm_nouveau = []
959dep_libdrm_etnaviv = []
960dep_libdrm_freedreno = []
961if with_amd_vk or with_gallium_radeonsi
962  dep_libdrm_amdgpu = dependency(
963    'libdrm_amdgpu', version : ['>= 2.4.89', '!= 2.4.90']
964  )
965endif
966if (with_gallium_radeonsi or with_dri_r100 or with_dri_r200 or
967    with_gallium_r300 or with_gallium_r600)
968  dep_libdrm_radeon = dependency('libdrm_radeon', version : '>= 2.4.71')
969endif
970if with_gallium_nouveau or with_dri_nouveau
971  dep_libdrm_nouveau = dependency('libdrm_nouveau', version : '>= 2.4.66')
972endif
973if with_gallium_etnaviv
974  dep_libdrm_etnaviv = dependency('libdrm_etnaviv', version : '>= 2.4.82')
975endif
976if with_gallium_freedreno
977  dep_libdrm_freedreno = dependency('libdrm_freedreno', version : '>= 2.4.89')
978endif
979
980llvm_modules = ['bitwriter', 'engine', 'mcdisassembler', 'mcjit']
981if with_amd_vk or with_gallium_radeonsi or with_gallium_r600
982  llvm_modules += ['amdgpu', 'bitreader', 'ipo']
983  if with_gallium_r600
984    llvm_modules += 'asmparser'
985  endif
986endif
987if with_gallium_opencl
988  llvm_modules += [
989    'all-targets', 'linker', 'coverage', 'instrumentation', 'ipo', 'irreader',
990    'lto', 'option', 'objcarcopts', 'profiledata',
991  ]
992  # TODO: optional modules
993endif
994
995if with_amd_vk
996  _llvm_version = '>= 4.0.0'
997elif with_gallium_opencl or with_gallium_swr or with_gallium_r600 or with_gallium_radeonsi
998  _llvm_version = '>= 3.9.0'
999else
1000  _llvm_version = '>= 3.3.0'
1001endif
1002
1003_llvm = get_option('llvm')
1004if _llvm == 'auto'
1005  dep_llvm = dependency(
1006    'llvm', version : _llvm_version, modules : llvm_modules,
1007    required : with_amd_vk or with_gallium_radeonsi or with_gallium_swr or with_gallium_opencl,
1008  )
1009  with_llvm = dep_llvm.found()
1010elif _llvm == 'true'
1011  dep_llvm = dependency('llvm', version : _llvm_version, modules : llvm_modules)
1012  with_llvm = true
1013else
1014  dep_llvm = []
1015  with_llvm = false
1016endif
1017if with_llvm
1018  _llvm_version = dep_llvm.version().split('.')
1019  # Development versions of LLVM have an 'svn' or 'git' suffix, we don't want
1020  # that for our version checks.
1021  # svn suffixes are stripped by meson as of 0.43, and git suffixes are
1022  # strippped as of 0.44, but we support older meson versions.
1023  _llvm_patch = _llvm_version[2]
1024  if _llvm_patch.endswith('svn')
1025    _llvm_patch = _llvm_patch.split('s')[0]
1026  elif _llvm_patch.contains('git')
1027    _llvm_patch = _llvm_patch.split('g')[0]
1028  endif
1029  pre_args += [
1030    '-DHAVE_LLVM=0x0@0@0@1@'.format(_llvm_version[0], _llvm_version[1]),
1031    '-DMESA_LLVM_VERSION_PATCH=@0@'.format(_llvm_patch),
1032  ]
1033elif with_amd_vk or with_gallium_radeonsi or with_gallium_swr
1034  error('The following drivers requires LLVM: Radv, RadeonSI, SWR. One of these is enabled, but LLVM is disabled.')
1035endif
1036
1037dep_glvnd = []
1038if with_glvnd
1039  dep_glvnd = dependency('libglvnd', version : '>= 0.2.0')
1040  pre_args += '-DUSE_LIBGLVND=1'
1041endif
1042
1043if with_valgrind != 'false'
1044  dep_valgrind = dependency('valgrind', required : with_valgrind == 'true')
1045  if dep_valgrind.found()
1046    pre_args += '-DHAVE_VALGRIND'
1047  endif
1048else
1049  dep_valgrind = []
1050endif
1051
1052# pthread stubs. Lets not and say we didn't
1053
1054prog_bison = find_program('bison', required : with_any_opengl)
1055prog_flex = find_program('flex', required : with_any_opengl)
1056
1057dep_selinux = []
1058if get_option('selinux')
1059  dep_selinux = dependency('libselinux')
1060  pre_args += '-DMESA_SELINUX'
1061endif
1062
1063# TODO: llvm-prefix and llvm-shared-libs
1064
1065if with_libunwind != 'false'
1066  dep_unwind = dependency('libunwind', required : with_libunwind == 'true')
1067  if dep_unwind.found()
1068    pre_args += '-DHAVE_LIBUNWIND'
1069  endif
1070else
1071  dep_unwind = []
1072endif
1073
1074# TODO: gallium-hud
1075
1076if with_osmesa != 'none'
1077  if with_osmesa == 'classic' and not with_dri_swrast
1078    error('OSMesa classic requires dri (classic) swrast.')
1079  endif
1080  if with_osmesa == 'gallium' and not with_gallium_softpipe
1081    error('OSMesa gallium requires gallium softpipe or llvmpipe.')
1082  endif
1083  osmesa_lib_name = 'OSMesa'
1084  osmesa_bits = get_option('osmesa-bits')
1085  if osmesa_bits != '8'
1086    if with_dri or with_glx != 'disabled'
1087      error('OSMesa bits must be 8 if building glx or dir based drivers')
1088    endif
1089    osmesa_lib_name = osmesa_lib_name + osmesa_bits
1090    pre_args += [
1091      '-DCHAN_BITS=@0@'.format(osmesa_bits), '-DDEFAULT_SOFTWARE_DEPTH_BITS=31'
1092    ]
1093  endif
1094endif
1095
1096# TODO: symbol mangling
1097
1098if with_platform_wayland
1099  prog_wl_scanner = find_program('wayland-scanner')
1100  dep_wl_protocols = dependency('wayland-protocols', version : '>= 1.8')
1101  dep_wayland_client = dependency('wayland-client', version : '>=1.11')
1102  dep_wayland_server = dependency('wayland-server', version : '>=1.11')
1103  wayland_dmabuf_xml = join_paths(
1104    dep_wl_protocols.get_pkgconfig_variable('pkgdatadir'), 'unstable',
1105    'linux-dmabuf', 'linux-dmabuf-unstable-v1.xml'
1106  )
1107  pre_args += ['-DHAVE_WAYLAND_PLATFORM', '-DWL_HIDE_DEPRECATED']
1108else
1109  prog_wl_scanner = []
1110  dep_wl_protocols = []
1111  dep_wayland_client = []
1112  dep_wayland_server = []
1113  wayland_dmabuf_xml = ''
1114endif
1115
1116dep_x11 = []
1117dep_xext = []
1118dep_xdamage = []
1119dep_xfixes = []
1120dep_x11_xcb = []
1121dep_xcb = []
1122dep_xcb_glx = []
1123dep_xcb_dri2 = []
1124dep_xcb_dri3 = []
1125dep_dri2proto = []
1126dep_glproto = []
1127dep_xxf86vm = []
1128dep_xcb_dri3 = []
1129dep_xcb_present = []
1130dep_xcb_sync = []
1131dep_xcb_xfixes = []
1132dep_xshmfence = []
1133if with_platform_x11
1134  if with_glx == 'xlib' or with_glx == 'gallium-xlib'
1135    dep_x11 = dependency('x11')
1136    dep_xext = dependency('xext')
1137    dep_xcb = dependency('xcb')
1138  elif with_glx == 'dri'
1139    dep_x11 = dependency('x11')
1140    dep_xext = dependency('xext')
1141    dep_xdamage = dependency('xdamage', version : '>= 1.1')
1142    dep_xfixes = dependency('xfixes')
1143    dep_xcb_glx = dependency('xcb-glx', version : '>= 1.8.1')
1144    dep_xxf86vm = dependency('xxf86vm', required : false)
1145  endif
1146  if (with_any_vk or with_glx == 'dri' or
1147       (with_gallium_vdpau or with_gallium_xvmc or with_gallium_omx or
1148        with_gallium_xa))
1149    dep_xcb = dependency('xcb')
1150    dep_x11_xcb = dependency('x11-xcb')
1151  endif
1152  if with_any_vk or (with_glx == 'dri' and with_dri_platform == 'drm')
1153    dep_xcb_dri2 = dependency('xcb-dri2', version : '>= 1.8')
1154
1155    if with_dri3
1156      pre_args += '-DHAVE_DRI3'
1157      dep_xcb_dri3 = dependency('xcb-dri3')
1158      dep_xcb_present = dependency('xcb-present')
1159      dep_xcb_sync = dependency('xcb-sync')
1160      dep_xshmfence = dependency('xshmfence', version : '>= 1.1')
1161    endif
1162  endif
1163  if with_glx == 'dri'
1164    if with_dri_platform == 'drm'
1165      dep_dri2proto = dependency('dri2proto', version : '>= 2.8')
1166    endif
1167    dep_glproto = dependency('glproto', version : '>= 1.4.14')
1168  endif
1169  if with_egl
1170    dep_xcb_xfixes = dependency('xcb-xfixes')
1171  endif
1172endif
1173
1174if get_option('gallium-extra-hud')
1175  pre_args += '-DHAVE_GALLIUM_EXTRA_HUD=1'
1176endif
1177
1178_sensors = get_option('lmsensors')
1179if _sensors != 'false'
1180  dep_lmsensors = cc.find_library('libsensors', required : _sensors == 'true')
1181  if dep_lmsensors.found()
1182    pre_args += '-DHAVE_LIBSENSORS=1'
1183  endif
1184else
1185  dep_lmsensors = []
1186endif
1187
1188# TODO: gallium tests
1189
1190# TODO: various libdirs
1191
1192# TODO: gallium driver dirs
1193
1194# FIXME: this is a workaround for #2326
1195prog_touch = find_program('touch')
1196dummy_cpp = custom_target(
1197  'dummy_cpp',
1198  output : 'dummy.cpp',
1199  command : [prog_touch, '@OUTPUT@'],
1200)
1201
1202foreach a : pre_args
1203  add_project_arguments(a, language : ['c', 'cpp'])
1204endforeach
1205foreach a : c_args
1206  add_project_arguments(a, language : ['c'])
1207endforeach
1208foreach a : cpp_args
1209  add_project_arguments(a, language : ['cpp'])
1210endforeach
1211
1212inc_include = include_directories('include')
1213
1214gl_priv_reqs = [
1215  'x11', 'xext', 'xdamage >= 1.1', 'xfixes', 'x11-xcb', 'xcb',
1216  'xcb-glx >= 1.8.1']
1217if dep_libdrm.found()
1218  gl_priv_reqs += 'libdrm >= 2.4.75'
1219endif
1220if dep_xxf86vm != [] and dep_xxf86vm.found()
1221  gl_priv_reqs += 'xxf86vm'
1222endif
1223if with_dri_platform == 'drm'
1224  gl_priv_reqs += 'xcb-dri2 >= 1.8'
1225endif
1226
1227gl_priv_libs = []
1228if dep_thread.found()
1229  gl_priv_libs += ['-lpthread', '-pthread']
1230endif
1231if dep_m.found()
1232  gl_priv_libs += '-lm'
1233endif
1234if dep_dl != [] and dep_dl.found()
1235  gl_priv_libs += '-ldl'
1236endif
1237
1238pkg = import('pkgconfig')
1239
1240subdir('include')
1241subdir('bin')
1242subdir('src')
1243