| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2013-2021 The Meson development team |
| 3 |
# Copyright © 2021-2025 Intel Corporation |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import functools |
| 8 |
import typing as T |
| 9 |
|
| 10 |
from ..mesonlib import MachineChoice |
| 11 |
from .base import DependencyCandidate, DependencyException, DependencyMethods |
| 12 |
from .base import process_method_kw |
| 13 |
from .base import BuiltinDependency, SystemDependency |
| 14 |
from .cmake import CMakeDependency |
| 15 |
from .framework import ExtraFrameworkDependency |
| 16 |
from .pkgconfig import PkgConfigDependency |
| 17 |
|
| 18 |
if T.TYPE_CHECKING: |
| 19 |
from typing_extensions import TypeAlias |
| 20 |
|
| 21 |
from .base import DependencyObjectKWs, ExternalDependency, DepType |
| 22 |
from .configtool import ConfigToolDependency |
| 23 |
from ..environment import Environment |
| 24 |
|
| 25 |
# TODO: remove this? |
| 26 |
DependencyGenerator: TypeAlias = DependencyCandidate[ExternalDependency] |
| 27 |
FactoryFunc = T.Callable[ |
| 28 |
[ |
| 29 |
'Environment', |
| 30 |
DependencyObjectKWs, |
| 31 |
T.List[DependencyMethods] |
| 32 |
], |
| 33 |
T.List[DependencyGenerator] |
| 34 |
] |
| 35 |
|
| 36 |
WrappedFactoryFunc = T.Callable[ |
| 37 |
[ |
| 38 |
'Environment', |
| 39 |
DependencyObjectKWs, |
| 40 |
], |
| 41 |
T.List[DependencyGenerator] |
| 42 |
] |
| 43 |
|
| 44 |
class DependencyFactory: |
| 45 |
|
| 46 |
"""Factory to get dependencies from multiple sources. |
| 47 |
|
| 48 |
This class provides an initializer that takes a set of names and classes |
| 49 |
for various kinds of dependencies. When the initialized object is called |
| 50 |
it returns a list of callables return Dependency objects to try in order. |
| 51 |
|
| 52 |
:param name: The name of the dependency. This will be passed as the name |
| 53 |
parameter of the each dependency unless it is overridden on a per |
| 54 |
type basis. |
| 55 |
:param methods: An ordered list of DependencyMethods. This is the order |
| 56 |
dependencies will be returned in unless they are removed by the |
| 57 |
_process_method function |
| 58 |
:param extra_kwargs: Additional keyword arguments to add when creating the |
| 59 |
DependencyCandidate |
| 60 |
:param pkgconfig: A custom PackageConfig lookup to use |
| 61 |
:param cmake: A custom CMake lookup to use |
| 62 |
:param framework: A custom AppleFramework lookup to use |
| 63 |
:param configtool: A custom ConfigTool lookup to use. If |
| 64 |
DependencyMethods.CONFIG_TOOL is in the `:param:methods` argument, |
| 65 |
this must be set. |
| 66 |
:param builtin: A custom Builtin lookup to use. If |
| 67 |
DependencyMethods.BUILTIN is in the `:param:methods` argument, |
| 68 |
this must be set. |
| 69 |
:param system: A custom System lookup to use. If |
| 70 |
DependencyMethods.SYSTEM is in the `:param:methods` argument, |
| 71 |
this must be set. |
| 72 |
""" |
| 73 |
|
| 74 |
def __init__(self, name: str, methods: T.List[DependencyMethods], *, |
| 75 |
extra_kwargs: T.Optional[DependencyObjectKWs] = None, |
| 76 |
pkgconfig: T.Union[DependencyCandidate[PkgConfigDependency], T.Type[PkgConfigDependency], None] = PkgConfigDependency, |
| 77 |
cmake: T.Union[DependencyCandidate[CMakeDependency], T.Type[CMakeDependency], None] = CMakeDependency, |
| 78 |
framework: T.Union[DependencyCandidate[ExtraFrameworkDependency], T.Type[ExtraFrameworkDependency], None] = ExtraFrameworkDependency, |
| 79 |
configtool: T.Union[DependencyCandidate[ConfigToolDependency], T.Type[ConfigToolDependency], None] = None, |
| 80 |
builtin: T.Union[DependencyCandidate[BuiltinDependency], T.Type[BuiltinDependency], None] = None, |
| 81 |
system: T.Union[DependencyCandidate[SystemDependency], T.Type[SystemDependency], None] = None): |
| 82 |
|
| 83 |
if DependencyMethods.CONFIG_TOOL in methods and not configtool: |
| 84 |
raise DependencyException('A configtool dependency must have a custom class') |
| 85 |
if DependencyMethods.BUILTIN in methods and not builtin: |
| 86 |
raise DependencyException('A builtin dependency must have a custom class') |
| 87 |
if DependencyMethods.SYSTEM in methods and not system: |
| 88 |
raise DependencyException('A system dependency must have a custom class') |
| 89 |
|
| 90 |
def make(arg: T.Union[DependencyCandidate[DepType], T.Type[DepType], None]) -> T.Optional[DependencyCandidate[DepType]]: |
| 91 |
if arg is None or isinstance(arg, DependencyCandidate): |
| 92 |
return arg |
| 93 |
return DependencyCandidate.from_dependency(name, arg) |
| 94 |
|
| 95 |
self.extra_kwargs = extra_kwargs |
| 96 |
self.methods = methods |
| 97 |
self.classes: T.Mapping[DependencyMethods, T.Optional[DependencyCandidate[ExternalDependency]]] = { |
| 98 |
# Just attach the correct name right now, either the generic name |
| 99 |
# or the method specific name. |
| 100 |
DependencyMethods.EXTRAFRAMEWORK: make(framework), |
| 101 |
DependencyMethods.PKGCONFIG: make(pkgconfig), |
| 102 |
DependencyMethods.CMAKE: make(cmake), |
| 103 |
DependencyMethods.SYSTEM: make(system), |
| 104 |
DependencyMethods.BUILTIN: make(builtin), |
| 105 |
DependencyMethods.CONFIG_TOOL: make(configtool), |
| 106 |
} |
| 107 |
|
| 108 |
@staticmethod |
| 109 |
def _process_method(method: DependencyMethods, env: 'Environment', for_machine: MachineChoice) -> bool: |
| 110 |
"""Report whether a method is valid or not. |
| 111 |
|
| 112 |
If the method is valid, return true, otherwise return false. This is |
| 113 |
used in a list comprehension to filter methods that are not possible. |
| 114 |
|
| 115 |
By default this only remove EXTRAFRAMEWORK dependencies for non-mac platforms. |
| 116 |
""" |
| 117 |
# Extra frameworks are only valid for macOS and other apple products |
| 118 |
if (method is DependencyMethods.EXTRAFRAMEWORK and |
| 119 |
not env.machines[for_machine].is_darwin()): |
| 120 |
return False |
| 121 |
return True |
| 122 |
|
| 123 |
def __call__(self, env: 'Environment', kwargs: DependencyObjectKWs) -> T.List['DependencyGenerator']: |
| 124 |
"""Return a list of Dependencies with the arguments already attached.""" |
| 125 |
methods = process_method_kw(self.methods, kwargs) |
| 126 |
if self.extra_kwargs: |
| 127 |
nwargs = self.extra_kwargs.copy() |
| 128 |
nwargs.update(kwargs) |
| 129 |
else: |
| 130 |
nwargs = kwargs.copy() |
| 131 |
|
| 132 |
ret: T.List[DependencyGenerator] = [] |
| 133 |
for m in methods: |
| 134 |
if self._process_method(m, env, kwargs['native']): |
| 135 |
c = self.classes[m] |
| 136 |
if c is None: |
| 137 |
continue |
| 138 |
c.arguments = (env, nwargs) |
| 139 |
ret.append(c) |
| 140 |
return ret |
| 141 |
|
| 142 |
|
| 143 |
def factory_methods(methods: T.Set[DependencyMethods]) -> T.Callable[['FactoryFunc'], 'WrappedFactoryFunc']: |
| 144 |
"""Decorator for handling methods for dependency factory functions. |
| 145 |
|
| 146 |
This helps to make factory functions self documenting |
| 147 |
>>> @factory_methods([DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE]) |
| 148 |
>>> def factory(env: Environment, for_machine: MachineChoice, kwargs: DependencyObjectKWs, methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']: |
| 149 |
>>> pass |
| 150 |
""" |
| 151 |
|
| 152 |
def inner(func: 'FactoryFunc') -> 'WrappedFactoryFunc': |
| 153 |
|
| 154 |
@functools.wraps(func) |
| 155 |
def wrapped(env: 'Environment', kwargs: DependencyObjectKWs) -> T.List['DependencyGenerator']: |
| 156 |
return func(env, kwargs, process_method_kw(methods, kwargs)) |
| 157 |
|
| 158 |
return wrapped |
| 159 |
|
| 160 |
return inner |