1# Copyright 2013-2019 The Meson development team
2
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6
7#     http://www.apache.org/licenses/LICENSE-2.0
8
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# This file contains the detection logic for miscellaneous external dependencies.
16
17from pathlib import Path
18import functools
19import re
20import sysconfig
21import typing as T
22
23from .. import mesonlib
24from .. import mlog
25from ..environment import detect_cpu_family
26from .base import DependencyException, DependencyMethods
27from .base import BuiltinDependency, SystemDependency
28from .cmake import CMakeDependency
29from .configtool import ConfigToolDependency
30from .factory import DependencyFactory, factory_methods
31from .pkgconfig import PkgConfigDependency
32
33if T.TYPE_CHECKING:
34    from ..environment import Environment, MachineChoice
35    from .factory import DependencyGenerator
36
37
38@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE})
39def netcdf_factory(env: 'Environment',
40                   for_machine: 'MachineChoice',
41                   kwargs: T.Dict[str, T.Any],
42                   methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']:
43    language = kwargs.get('language', 'c')
44    if language not in ('c', 'cpp', 'fortran'):
45        raise DependencyException(f'Language {language} is not supported with NetCDF.')
46
47    candidates: T.List['DependencyGenerator'] = []
48
49    if DependencyMethods.PKGCONFIG in methods:
50        if language == 'fortran':
51            pkg = 'netcdf-fortran'
52        else:
53            pkg = 'netcdf'
54
55        candidates.append(functools.partial(PkgConfigDependency, pkg, env, kwargs, language=language))
56
57    if DependencyMethods.CMAKE in methods:
58        candidates.append(functools.partial(CMakeDependency, 'NetCDF', env, kwargs, language=language))
59
60    return candidates
61
62
63class OpenMPDependency(SystemDependency):
64    # Map date of specification release (which is the macro value) to a version.
65    VERSIONS = {
66        '201811': '5.0',
67        '201611': '5.0-revision1',  # This is supported by ICC 19.x
68        '201511': '4.5',
69        '201307': '4.0',
70        '201107': '3.1',
71        '200805': '3.0',
72        '200505': '2.5',
73        '200203': '2.0',
74        '199810': '1.0',
75    }
76
77    def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
78        language = kwargs.get('language')
79        super().__init__('openmp', environment, kwargs, language=language)
80        self.is_found = False
81        if self.clib_compiler.get_id() == 'pgi':
82            # through at least PGI 19.4, there is no macro defined for OpenMP, but OpenMP 3.1 is supported.
83            self.version = '3.1'
84            self.is_found = True
85            self.compile_args = self.link_args = self.clib_compiler.openmp_flags()
86            return
87        try:
88            openmp_date = self.clib_compiler.get_define(
89                '_OPENMP', '', self.env, self.clib_compiler.openmp_flags(), [self], disable_cache=True)[0]
90        except mesonlib.EnvironmentException as e:
91            mlog.debug('OpenMP support not available in the compiler')
92            mlog.debug(e)
93            openmp_date = None
94
95        if openmp_date:
96            try:
97                self.version = self.VERSIONS[openmp_date]
98            except KeyError:
99                mlog.debug(f'Could not find an OpenMP version matching {openmp_date}')
100                if openmp_date == '_OPENMP':
101                    mlog.debug('This can be caused by flags such as gcc\'s `-fdirectives-only`, which affect preprocessor behavior.')
102                return
103            # Flang has omp_lib.h
104            header_names = ('omp.h', 'omp_lib.h')
105            for name in header_names:
106                if self.clib_compiler.has_header(name, '', self.env, dependencies=[self], disable_cache=True)[0]:
107                    self.is_found = True
108                    self.compile_args = self.clib_compiler.openmp_flags()
109                    self.link_args = self.clib_compiler.openmp_link_flags()
110                    break
111            if not self.is_found:
112                mlog.log(mlog.yellow('WARNING:'), 'OpenMP found but omp.h missing.')
113
114
115class ThreadDependency(SystemDependency):
116    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
117        super().__init__(name, environment, kwargs)
118        self.is_found = True
119        # Happens if you are using a language with threads
120        # concept without C, such as plain Cuda.
121        if self.clib_compiler is None:
122            self.compile_args = []
123            self.link_args = []
124        else:
125            self.compile_args = self.clib_compiler.thread_flags(environment)
126            self.link_args = self.clib_compiler.thread_link_flags(environment)
127
128    @staticmethod
129    def get_methods() -> T.List[DependencyMethods]:
130        return [DependencyMethods.AUTO, DependencyMethods.CMAKE]
131
132
133class BlocksDependency(SystemDependency):
134    def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
135        super().__init__('blocks', environment, kwargs)
136        self.name = 'blocks'
137        self.is_found = False
138
139        if self.env.machines[self.for_machine].is_darwin():
140            self.compile_args = []
141            self.link_args = []
142        else:
143            self.compile_args = ['-fblocks']
144            self.link_args = ['-lBlocksRuntime']
145
146            if not self.clib_compiler.has_header('Block.h', '', environment, disable_cache=True) or \
147               not self.clib_compiler.find_library('BlocksRuntime', environment, []):
148                mlog.log(mlog.red('ERROR:'), 'BlocksRuntime not found.')
149                return
150
151        source = '''
152            int main(int argc, char **argv)
153            {
154                int (^callback)(void) = ^ int (void) { return 0; };
155                return callback();
156            }'''
157
158        with self.clib_compiler.compile(source, extra_args=self.compile_args + self.link_args) as p:
159            if p.returncode != 0:
160                mlog.log(mlog.red('ERROR:'), 'Compiler does not support blocks extension.')
161                return
162
163            self.is_found = True
164
165
166class Python3DependencySystem(SystemDependency):
167    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
168        super().__init__(name, environment, kwargs)
169
170        if not environment.machines.matches_build_machine(self.for_machine):
171            return
172        if not environment.machines[self.for_machine].is_windows():
173            return
174
175        self.name = 'python3'
176        self.static = kwargs.get('static', False)
177        # We can only be sure that it is Python 3 at this point
178        self.version = '3'
179        self._find_libpy3_windows(environment)
180
181    @staticmethod
182    def get_windows_python_arch() -> T.Optional[str]:
183        pyplat = sysconfig.get_platform()
184        if pyplat == 'mingw':
185            pycc = sysconfig.get_config_var('CC')
186            if pycc.startswith('x86_64'):
187                return '64'
188            elif pycc.startswith(('i686', 'i386')):
189                return '32'
190            else:
191                mlog.log(f'MinGW Python built with unknown CC {pycc!r}, please file a bug')
192                return None
193        elif pyplat == 'win32':
194            return '32'
195        elif pyplat in ('win64', 'win-amd64'):
196            return '64'
197        mlog.log(f'Unknown Windows Python platform {pyplat!r}')
198        return None
199
200    def get_windows_link_args(self) -> T.Optional[T.List[str]]:
201        pyplat = sysconfig.get_platform()
202        if pyplat.startswith('win'):
203            vernum = sysconfig.get_config_var('py_version_nodot')
204            if self.static:
205                libpath = Path('libs') / f'libpython{vernum}.a'
206            else:
207                comp = self.get_compiler()
208                if comp.id == "gcc":
209                    libpath = Path(f'python{vernum}.dll')
210                else:
211                    libpath = Path('libs') / f'python{vernum}.lib'
212            lib = Path(sysconfig.get_config_var('base')) / libpath
213        elif pyplat == 'mingw':
214            if self.static:
215                libname = sysconfig.get_config_var('LIBRARY')
216            else:
217                libname = sysconfig.get_config_var('LDLIBRARY')
218            lib = Path(sysconfig.get_config_var('LIBDIR')) / libname
219        if not lib.exists():
220            mlog.log('Could not find Python3 library {!r}'.format(str(lib)))
221            return None
222        return [str(lib)]
223
224    def _find_libpy3_windows(self, env: 'Environment') -> None:
225        '''
226        Find python3 libraries on Windows and also verify that the arch matches
227        what we are building for.
228        '''
229        pyarch = self.get_windows_python_arch()
230        if pyarch is None:
231            self.is_found = False
232            return
233        arch = detect_cpu_family(env.coredata.compilers.host)
234        if arch == 'x86':
235            arch = '32'
236        elif arch == 'x86_64':
237            arch = '64'
238        else:
239            # We can't cross-compile Python 3 dependencies on Windows yet
240            mlog.log(f'Unknown architecture {arch!r} for',
241                     mlog.bold(self.name))
242            self.is_found = False
243            return
244        # Pyarch ends in '32' or '64'
245        if arch != pyarch:
246            mlog.log('Need', mlog.bold(self.name), 'for {}-bit, but '
247                     'found {}-bit'.format(arch, pyarch))
248            self.is_found = False
249            return
250        # This can fail if the library is not found
251        largs = self.get_windows_link_args()
252        if largs is None:
253            self.is_found = False
254            return
255        self.link_args = largs
256        # Compile args
257        inc = sysconfig.get_path('include')
258        platinc = sysconfig.get_path('platinclude')
259        self.compile_args = ['-I' + inc]
260        if inc != platinc:
261            self.compile_args.append('-I' + platinc)
262        self.version = sysconfig.get_config_var('py_version')
263        self.is_found = True
264
265    @staticmethod
266    def get_methods() -> T.List[DependencyMethods]:
267        if mesonlib.is_windows():
268            return [DependencyMethods.PKGCONFIG, DependencyMethods.SYSCONFIG]
269        elif mesonlib.is_osx():
270            return [DependencyMethods.PKGCONFIG, DependencyMethods.EXTRAFRAMEWORK]
271        else:
272            return [DependencyMethods.PKGCONFIG]
273
274    def log_tried(self) -> str:
275        return 'sysconfig'
276
277class PcapDependencyConfigTool(ConfigToolDependency):
278
279    tools = ['pcap-config']
280    tool_name = 'pcap-config'
281
282    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
283        super().__init__(name, environment, kwargs)
284        if not self.is_found:
285            return
286        self.compile_args = self.get_config_value(['--cflags'], 'compile_args')
287        self.link_args = self.get_config_value(['--libs'], 'link_args')
288        self.version = self.get_pcap_lib_version()
289
290    @staticmethod
291    def get_methods() -> T.List[DependencyMethods]:
292        return [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL]
293
294    def get_pcap_lib_version(self) -> T.Optional[str]:
295        # Since we seem to need to run a program to discover the pcap version,
296        # we can't do that when cross-compiling
297        # FIXME: this should be handled if we have an exe_wrapper
298        if not self.env.machines.matches_build_machine(self.for_machine):
299            return None
300
301        v = self.clib_compiler.get_return_value('pcap_lib_version', 'string',
302                                                '#include <pcap.h>', self.env, [], [self])
303        v = re.sub(r'libpcap version ', '', str(v))
304        v = re.sub(r' -- Apple version.*$', '', v)
305        return v
306
307
308class CupsDependencyConfigTool(ConfigToolDependency):
309
310    tools = ['cups-config']
311    tool_name = 'cups-config'
312
313    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
314        super().__init__(name, environment, kwargs)
315        if not self.is_found:
316            return
317        self.compile_args = self.get_config_value(['--cflags'], 'compile_args')
318        self.link_args = self.get_config_value(['--ldflags', '--libs'], 'link_args')
319
320    @staticmethod
321    def get_methods() -> T.List[DependencyMethods]:
322        if mesonlib.is_osx():
323            return [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.EXTRAFRAMEWORK, DependencyMethods.CMAKE]
324        else:
325            return [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.CMAKE]
326
327
328class LibWmfDependencyConfigTool(ConfigToolDependency):
329
330    tools = ['libwmf-config']
331    tool_name = 'libwmf-config'
332
333    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
334        super().__init__(name, environment, kwargs)
335        if not self.is_found:
336            return
337        self.compile_args = self.get_config_value(['--cflags'], 'compile_args')
338        self.link_args = self.get_config_value(['--libs'], 'link_args')
339
340    @staticmethod
341    def get_methods() -> T.List[DependencyMethods]:
342        return [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL]
343
344
345class LibGCryptDependencyConfigTool(ConfigToolDependency):
346
347    tools = ['libgcrypt-config']
348    tool_name = 'libgcrypt-config'
349
350    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
351        super().__init__(name, environment, kwargs)
352        if not self.is_found:
353            return
354        self.compile_args = self.get_config_value(['--cflags'], 'compile_args')
355        self.link_args = self.get_config_value(['--libs'], 'link_args')
356        self.version = self.get_config_value(['--version'], 'version')[0]
357
358    @staticmethod
359    def get_methods() -> T.List[DependencyMethods]:
360        return [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL]
361
362
363class GpgmeDependencyConfigTool(ConfigToolDependency):
364
365    tools = ['gpgme-config']
366    tool_name = 'gpg-config'
367
368    def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
369        super().__init__(name, environment, kwargs)
370        if not self.is_found:
371            return
372        self.compile_args = self.get_config_value(['--cflags'], 'compile_args')
373        self.link_args = self.get_config_value(['--libs'], 'link_args')
374        self.version = self.get_config_value(['--version'], 'version')[0]
375
376    @staticmethod
377    def get_methods() -> T.List[DependencyMethods]:
378        return [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL]
379
380
381class ShadercDependency(SystemDependency):
382
383    def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any]):
384        super().__init__('shaderc', environment, kwargs)
385
386        static_lib = 'shaderc_combined'
387        shared_lib = 'shaderc_shared'
388
389        libs = [shared_lib, static_lib]
390        if self.static:
391            libs.reverse()
392
393        cc = self.get_compiler()
394
395        for lib in libs:
396            self.link_args = cc.find_library(lib, environment, [])
397            if self.link_args is not None:
398                self.is_found = True
399
400                if self.static and lib != static_lib:
401                    mlog.warning(f'Static library {static_lib!r} not found for dependency '
402                                 f'{self.name!r}, may not be statically linked')
403
404                break
405
406    def log_tried(self) -> str:
407        return 'system'
408
409    @staticmethod
410    def get_methods() -> T.List[DependencyMethods]:
411        return [DependencyMethods.SYSTEM, DependencyMethods.PKGCONFIG]
412
413
414class CursesConfigToolDependency(ConfigToolDependency):
415
416    """Use the curses config tools."""
417
418    tool = 'curses-config'
419    # ncurses5.4-config is for macOS Catalina
420    tools = ['ncursesw6-config', 'ncursesw5-config', 'ncurses6-config', 'ncurses5-config', 'ncurses5.4-config']
421
422    def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any], language: T.Optional[str] = None):
423        super().__init__(name, env, kwargs, language)
424        if not self.is_found:
425            return
426        self.compile_args = self.get_config_value(['--cflags'], 'compile_args')
427        self.link_args = self.get_config_value(['--libs'], 'link_args')
428
429
430class CursesSystemDependency(SystemDependency):
431
432    """Curses dependency the hard way.
433
434    This replaces hand rolled find_library() and has_header() calls. We
435    provide this for portability reasons, there are a large number of curses
436    implementations, and the differences between them can be very annoying.
437    """
438
439    def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any]):
440        super().__init__(name, env, kwargs)
441
442        candidates = [
443            ('pdcurses', ['pdcurses/curses.h']),
444            ('ncursesw',  ['ncursesw/ncurses.h', 'ncurses.h']),
445            ('ncurses',  ['ncurses/ncurses.h', 'ncurses/curses.h', 'ncurses.h']),
446            ('curses',  ['curses.h']),
447        ]
448
449        # Not sure how else to elegently break out of both loops
450        for lib, headers in candidates:
451            l = self.clib_compiler.find_library(lib, env, [])
452            if l:
453                for header in headers:
454                    h = self.clib_compiler.has_header(header, '', env)
455                    if h[0]:
456                        self.is_found = True
457                        self.link_args = l
458                        # Not sure how to find version for non-ncurses curses
459                        # implementations. The one in illumos/OpenIndiana
460                        # doesn't seem to have a version defined in the header.
461                        if lib.startswith('ncurses'):
462                            v, _ = self.clib_compiler.get_define('NCURSES_VERSION', f'#include <{header}>', env, [], [self])
463                            self.version = v.strip('"')
464                        if lib.startswith('pdcurses'):
465                            v_major, _ = self.clib_compiler.get_define('PDC_VER_MAJOR', f'#include <{header}>', env, [], [self])
466                            v_minor, _ = self.clib_compiler.get_define('PDC_VER_MINOR', f'#include <{header}>', env, [], [self])
467                            self.version = f'{v_major}.{v_minor}'
468
469                        # Check the version if possible, emit a wraning if we can't
470                        req = kwargs.get('version')
471                        if req:
472                            if self.version:
473                                self.is_found = mesonlib.version_compare(self.version, req)
474                            else:
475                                mlog.warning('Cannot determine version of curses to compare against.')
476
477                        if self.is_found:
478                            mlog.debug('Curses library:', l)
479                            mlog.debug('Curses header:', header)
480                            break
481            if self.is_found:
482                break
483
484    @staticmethod
485    def get_methods() -> T.List[DependencyMethods]:
486        return [DependencyMethods.SYSTEM]
487
488
489class IntlBuiltinDependency(BuiltinDependency):
490    def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any]):
491        super().__init__(name, env, kwargs)
492
493        if self.clib_compiler.has_function('ngettext', '', env)[0]:
494            self.is_found = True
495
496
497class IntlSystemDependency(SystemDependency):
498    def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any]):
499        super().__init__(name, env, kwargs)
500
501        h = self.clib_compiler.has_header('libintl.h', '', env)
502        self.link_args =  self.clib_compiler.find_library('intl', env, [])
503
504        if h and self.link_args:
505            self.is_found = True
506
507
508@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.SYSTEM})
509def curses_factory(env: 'Environment',
510                   for_machine: 'MachineChoice',
511                   kwargs: T.Dict[str, T.Any],
512                   methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']:
513    candidates: T.List['DependencyGenerator'] = []
514
515    if DependencyMethods.PKGCONFIG in methods:
516        pkgconfig_files = ['pdcurses', 'ncursesw', 'ncurses', 'curses']
517        for pkg in pkgconfig_files:
518            candidates.append(functools.partial(PkgConfigDependency, pkg, env, kwargs))
519
520    # There are path handling problems with these methods on msys, and they
521    # don't apply to windows otherwise (cygwin is handled separately from
522    # windows)
523    if not env.machines[for_machine].is_windows():
524        if DependencyMethods.CONFIG_TOOL in methods:
525            candidates.append(functools.partial(CursesConfigToolDependency, 'curses', env, kwargs))
526
527        if DependencyMethods.SYSTEM in methods:
528            candidates.append(functools.partial(CursesSystemDependency, 'curses', env, kwargs))
529
530    return candidates
531
532
533@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM})
534def shaderc_factory(env: 'Environment',
535                    for_machine: 'MachineChoice',
536                    kwargs: T.Dict[str, T.Any],
537                    methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']:
538    """Custom DependencyFactory for ShaderC.
539
540    ShaderC's odd you get three different libraries from the same build
541    thing are just easier to represent as a separate function than
542    twisting DependencyFactory even more.
543    """
544    candidates: T.List['DependencyGenerator'] = []
545
546    if DependencyMethods.PKGCONFIG in methods:
547        # ShaderC packages their shared and static libs together
548        # and provides different pkg-config files for each one. We
549        # smooth over this difference by handling the static
550        # keyword before handing off to the pkg-config handler.
551        shared_libs = ['shaderc']
552        static_libs = ['shaderc_combined', 'shaderc_static']
553
554        if kwargs.get('static', False):
555            c = [functools.partial(PkgConfigDependency, name, env, kwargs)
556                 for name in static_libs + shared_libs]
557        else:
558            c = [functools.partial(PkgConfigDependency, name, env, kwargs)
559                 for name in shared_libs + static_libs]
560        candidates.extend(c)
561
562    if DependencyMethods.SYSTEM in methods:
563        candidates.append(functools.partial(ShadercDependency, env, kwargs))
564
565    return candidates
566
567
568cups_factory = DependencyFactory(
569    'cups',
570    [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.EXTRAFRAMEWORK, DependencyMethods.CMAKE],
571    configtool_class=CupsDependencyConfigTool,
572    cmake_name='Cups',
573)
574
575gpgme_factory = DependencyFactory(
576    'gpgme',
577    [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
578    configtool_class=GpgmeDependencyConfigTool,
579)
580
581libgcrypt_factory = DependencyFactory(
582    'libgcrypt',
583    [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
584    configtool_class=LibGCryptDependencyConfigTool,
585)
586
587libwmf_factory = DependencyFactory(
588    'libwmf',
589    [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
590    configtool_class=LibWmfDependencyConfigTool,
591)
592
593pcap_factory = DependencyFactory(
594    'pcap',
595    [DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
596    configtool_class=PcapDependencyConfigTool,
597    pkgconfig_name='libpcap',
598)
599
600python3_factory = DependencyFactory(
601    'python3',
602    [DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM, DependencyMethods.EXTRAFRAMEWORK],
603    system_class=Python3DependencySystem,
604    # There is no version number in the macOS version number
605    framework_name='Python',
606    # There is a python in /System/Library/Frameworks, but thats python 2.x,
607    # Python 3 will always be in /Library
608    extra_kwargs={'paths': ['/Library/Frameworks']},
609)
610
611threads_factory = DependencyFactory(
612    'threads',
613    [DependencyMethods.SYSTEM, DependencyMethods.CMAKE],
614    cmake_name='Threads',
615    system_class=ThreadDependency,
616)
617
618intl_factory = DependencyFactory(
619    'intl',
620    [DependencyMethods.BUILTIN, DependencyMethods.SYSTEM],
621    builtin_class=IntlBuiltinDependency,
622    system_class=IntlSystemDependency,
623)
624