Skip to content

Commit dbc0645

Browse files
committed
Allow specifying method in dependency() and subproject()
If the Cargo subproject is part of the main repo we had to write a dummy wrap file to specify the method: ``` [wrap-file] method=cargo ``` We can now instead directly call `subproject('rust', method: 'cargo')` with no wrap file. This also allows the method to be a user option: `method: get_option('cargo') ? 'cargo' : 'meson'`.
1 parent c16ecb1 commit dbc0645

File tree

13 files changed

+67
-10
lines changed

13 files changed

+67
-10
lines changed

docs/yaml/functions/dependency.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ kwargs:
123123
If the value is an empty array it has the same effect as
124124
`allow_fallback: false`.
125125
126+
fallback_method:
127+
type: str
128+
since: 1.10.0
129+
description: |
130+
Specifies which method should be used to configure the fallback if the
131+
dependency is not found in the system. Supported values are `meson`,
132+
`cmake` and `cargo`. It defaults to the `method` field in the wrap file
133+
if any, otherwise it defaults to `meson`.
134+
126135
language:
127136
type: str
128137
since: 0.42.0

docs/yaml/functions/subproject.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,11 @@ kwargs:
6565
default: true
6666
description: |
6767
Works just the same as in [[dependency]].
68+
69+
method:
70+
type: str
71+
since: 1.10.0
72+
description: |
73+
Specifies which method should be used to configure the subproject.
74+
Supported values are `meson`, `cmake` and `cargo`. It defaults to the
75+
`method` field in the wrap file if any, otherwise it defaults to `meson`.

mesonbuild/dependencies/detect.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ def get_dep_identifier(name: str, kwargs: T.Dict[str, T.Any]) -> 'TV_DepID':
4545
nkwargs.update(kwargs)
4646

4747
from ..interpreter import permitted_dependency_kwargs
48-
assert len(permitted_dependency_kwargs) == 19, \
48+
assert len(permitted_dependency_kwargs) == 20, \
4949
'Extra kwargs have been added to dependency(), please review if it makes sense to handle it here'
5050
for key, value in nkwargs.items():
5151
# 'version' is irrelevant for caching; the caller must check version matches
5252
# 'native' is handled above with `for_machine`
5353
# 'required' is irrelevant for caching; the caller handles it separately
54-
# 'fallback' and 'allow_fallback' is not part of the cache because,
55-
# once a dependency has been found through a fallback, it should
56-
# be used for the rest of the Meson run.
54+
# 'fallback', 'allow_fallback' and 'fallback_method' are not part of the
55+
# cache because, once a dependency has been found through a fallback,
56+
# it should be used for the rest of the Meson run.
5757
# 'default_options' is only used in fallback case
5858
# 'not_found_message' has no impact on the dependency lookup
5959
# 'include_type' is handled after the dependency lookup
60-
if key in {'version', 'native', 'required', 'fallback', 'allow_fallback', 'default_options',
61-
'not_found_message', 'include_type'}:
60+
if key in {'version', 'native', 'required', 'fallback', 'allow_fallback',
61+
'fallback_method', 'default_options', 'not_found_message',
62+
'include_type'}:
6263
continue
6364
# All keyword arguments are strings, ints, or lists (or lists of lists)
6465
if isinstance(value, list):

mesonbuild/interpreter/dependencyfallbacks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@
2020
from .interpreter import Interpreter
2121
from ..interpreterbase import TYPE_nkwargs, TYPE_nvar
2222
from .interpreterobjects import SubprojectHolder
23+
from ..wrap.wrap import Method
2324

2425

2526
class DependencyFallbacksHolder(MesonInterpreterObject):
2627
def __init__(self,
2728
interpreter: 'Interpreter',
2829
names: T.List[str],
2930
allow_fallback: T.Optional[bool] = None,
30-
default_options: T.Optional[T.Dict[str, str]] = None) -> None:
31+
default_options: T.Optional[T.Dict[str, str]] = None,
32+
fallback_method: Method = 'meson') -> None:
3133
super().__init__(subproject=interpreter.subproject)
3234
self.interpreter = interpreter
3335
self.subproject = interpreter.subproject
@@ -42,6 +44,7 @@ def __init__(self,
4244
self.names: T.List[str] = []
4345
self.forcefallback: bool = False
4446
self.nofallback: bool = False
47+
self.fallback_method = fallback_method
4548
for name in names:
4649
if not name:
4750
raise InterpreterException('dependency_fallbacks empty name \'\' is not allowed')
@@ -132,7 +135,7 @@ def _do_subproject(self, kwargs: TYPE_nkwargs, func_args: TYPE_nvar, func_kwargs
132135
subp_name = self.subproject_name
133136
varname = self.subproject_varname
134137
func_kwargs.setdefault('version', [])
135-
self.interpreter.do_subproject(subp_name, func_kwargs, forced_options=forced_options)
138+
self.interpreter.do_subproject(subp_name, func_kwargs, forced_options=forced_options, force_method=self.fallback_method)
136139
return self._get_subproject_dep(subp_name, varname, kwargs)
137140

138141
def _get_subproject(self, subp_name: str) -> T.Optional[SubprojectHolder]:

mesonbuild/interpreter/interpreter.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .. import compilers
1717
from .. import envconfig
1818
from ..wrap import wrap, WrapMode
19+
from ..wrap.wrap import METHODS
1920
from .. import mesonlib
2021
from ..mesonlib import (EnvironmentVariables, ExecutableSerialisation, MesonBugException, MesonException, HoldableObject,
2122
FileMode, MachineChoice, is_parent_path, listify,
@@ -91,6 +92,7 @@
9192
STATIC_LIB_KWS,
9293
VARIABLES_KW,
9394
TEST_KWS,
95+
FALLBACK_METHOD_KW,
9496
NoneType,
9597
in_set_validator,
9698
env_convertor_with_method
@@ -245,6 +247,7 @@ class InterpreterRuleRelaxation(Enum):
245247
'components',
246248
'default_options',
247249
'fallback',
250+
'fallback_method',
248251
'include_type',
249252
'language',
250253
'main',
@@ -867,6 +870,7 @@ def func_option(self, nodes, args, kwargs):
867870
REQUIRED_KW,
868871
DEFAULT_OPTIONS.evolve(since='0.38.0'),
869872
KwargInfo('version', ContainerTypeInfo(list, str), default=[], listify=True),
873+
FALLBACK_METHOD_KW.evolve(name='method'),
870874
)
871875
def func_subproject(self, nodes: mparser.BaseNode, args: T.Tuple[str], kwargs: kwtypes.Subproject) -> SubprojectHolder:
872876
kw: kwtypes.DoSubproject = {
@@ -876,7 +880,7 @@ def func_subproject(self, nodes: mparser.BaseNode, args: T.Tuple[str], kwargs: k
876880
'options': None,
877881
'cmake_options': [],
878882
}
879-
return self.do_subproject(args[0], kw)
883+
return self.do_subproject(args[0], kw, force_method=kwargs['method'])
880884

881885
def disabled_subproject(self, subp_name: str, disabled_feature: T.Optional[str] = None,
882886
exception: T.Optional[Exception] = None) -> SubprojectHolder:
@@ -1809,7 +1813,8 @@ def func_dependency(self, node: mparser.BaseNode, args: T.Tuple[T.List[str]], kw
18091813
raise InvalidArguments('"allow_fallback" argument must be boolean')
18101814
fallback = kwargs.get('fallback')
18111815
default_options = kwargs.get('default_options')
1812-
df = DependencyFallbacksHolder(self, names, allow_fallback, default_options)
1816+
fallback_method = kwargs.get('fallback_method')
1817+
df = DependencyFallbacksHolder(self, names, allow_fallback, default_options, fallback_method)
18131818
df.set_fallback(fallback)
18141819
not_found_message = kwargs.get('not_found_message', '')
18151820
if not isinstance(not_found_message, str):

mesonbuild/interpreter/kwargs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ..options import OptionKey
1818
from ..modules.cmake import CMakeSubprojectOptions
1919
from ..programs import ExternalProgram
20+
from ..wrap.wrap import Method as WrapMethod
2021
from .type_checking import PkgConfigDefineType, SourcesVarargsType
2122

2223
TestArgs = T.Union[str, File, build.Target, ExternalProgram]
@@ -315,6 +316,7 @@ class Subproject(ExtractRequired):
315316

316317
default_options: T.Dict[OptionKey, options.ElementaryOptionValues]
317318
version: T.List[str]
319+
method: T.Optional[WrapMethod]
318320

319321

320322
class DoSubproject(ExtractRequired):
@@ -491,3 +493,4 @@ class FuncDeclareDependency(TypedDict):
491493
class FuncDependency(TypedDict):
492494

493495
default_options: T.Dict[OptionKey, options.ElementaryOptionValues]
496+
fallback_method: T.Optional[WrapMethod]

mesonbuild/interpreter/type_checking.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ..mesonlib import (File, FileMode, MachineChoice, has_path_sep, listify, stringlistify,
1818
EnvironmentVariables)
1919
from ..programs import ExternalProgram
20+
from ..wrap.wrap import METHODS
2021

2122
# Helper definition for type checks that are `Optional[T]`
2223
NoneType: T.Type[None] = type(None)
@@ -892,7 +893,15 @@ def _pkgconfig_define_convertor(x: T.List[str]) -> PkgConfigDefineType:
892893
convertor=_pkgconfig_define_convertor,
893894
)
894895

896+
FALLBACK_METHOD_KW: KwargInfo = KwargInfo(
897+
'fallback_method',
898+
(str, NoneType),
899+
default=None,
900+
validator=in_set_validator(METHODS),
901+
since='1.10.0')
902+
895903

896904
DEPENDENCY_KWS: T.List[KwargInfo] = [
897905
DEFAULT_OPTIONS.evolve(since='0.38.0'),
906+
FALLBACK_METHOD_KW,
898907
]

mesonbuild/wrap/wrap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
WHITELIST_SUBDOMAIN = 'wrapdb.mesonbuild.com'
5757

5858
ALL_TYPES = ['file', 'git', 'hg', 'svn', 'redirect']
59+
METHODS = {'meson', 'cmake', 'cargo'}
5960

6061
if mesonlib.is_windows():
6162
from ..programs import ExternalProgram
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
project('cargo fallback method')
2+
3+
subproject('rust', method : 'cargo')
4+
dependency('foo-0-rs', fallback_method : 'cargo')
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[package]
2+
name = "empty"

0 commit comments

Comments
 (0)