Debian Codegraph @

mesonbuild/interpreter/decorators.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2012-2021 The Meson development team
3
4 from __future__ import annotations
5
6 from functools import wraps
7 import typing as T
8
9 from ..interpreterbase import ObjectHolder
10 from ..interpreterbase.decorators import get_callee_args
11 from ..mesonlib import MesonBugException
12
13 if T.TYPE_CHECKING:
14 from ..interpreterbase import TV_func
15 from .kwargs import MachineMapArgs
16
17
18 def apply_machine_map(f: TV_func) -> TV_func:
19 """Enforce build-only subproject constraints on native/install kwargs.
20
21 Must be placed AFTER @typed_kwargs (so converters have run).
22 """
23 @wraps(f)
24 def wrapped(*wrapped_args: T.Any, **wrapped_kwargs: T.Any) -> T.Any:
25 # Import here to avoid circular imports at module load time
26 from . import Interpreter
27 from .mesonmain import MesonMain
28
29 # First argument could be Interpreter, InterpreterObject (with an interpreter
30 # attribute) or ModuleObject. In the case of a ModuleObject it is the 2nd
31 # argument (ModuleState) that contains the needed information.
32 s: Interpreter
33 if not hasattr(wrapped_args[0], 'current_node'):
34 s = wrapped_args[1]._interpreter
35 elif isinstance(wrapped_args[0], (MesonMain, ObjectHolder)):
36 s = wrapped_args[0].interpreter
37 elif isinstance(wrapped_args[0], Interpreter):
38 s = wrapped_args[0]
39 else:
40 raise MesonBugException(f'cannot use @apply_machine_map if the object is a {type(wrapped_args[0])}')
41
42 _, _, kwargs, _ = get_callee_args(wrapped_args)
43 if 'native' in kwargs:
44 s.apply_machine_map_to_kwargs(T.cast('MachineMapArgs', kwargs))
45
46 return f(*wrapped_args, **wrapped_kwargs)
47 return T.cast('TV_func', wrapped)