1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# Copyright 2013-2014 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import re
import typing as T
from . import coredata
from . import mesonlib
from . import mparser
from . import mlog
from .interpreterbase import FeatureNew, typed_pos_args, typed_kwargs, ContainerTypeInfo, KwargInfo, FeatureDeprecated
from .interpreter.type_checking import NoneType, in_set_validator
if T.TYPE_CHECKING:
from .interpreterbase import TYPE_var, TYPE_kwargs
from .interpreterbase import SubProject
from typing_extensions import TypedDict, Literal
_DEPRECATED_ARGS = T.Union[bool, str, T.Dict[str, str], T.List[str]]
FuncOptionArgs = TypedDict('FuncOptionArgs', {
'type': str,
'description': str,
'yield': bool,
'choices': T.Optional[T.List[str]],
'value': object,
'min': T.Optional[int],
'max': T.Optional[int],
'deprecated': _DEPRECATED_ARGS,
})
class StringArgs(TypedDict):
value: str
class BooleanArgs(TypedDict):
value: bool
class ComboArgs(TypedDict):
value: str
choices: T.List[str]
class IntegerArgs(TypedDict):
value: int
min: T.Optional[int]
max: T.Optional[int]
class StringArrayArgs(TypedDict):
value: T.Optional[T.Union[str, T.List[str]]]
choices: T.List[str]
class FeatureArgs(TypedDict):
value: Literal['enabled', 'disabled', 'auto']
choices: T.List[str]
class OptionException(mesonlib.MesonException):
pass
optname_regex = re.compile('[^a-zA-Z0-9_-]')
class OptionInterpreter:
def __init__(self, subproject: 'SubProject') -> None:
self.options: 'coredata.MutableKeyedOptionDictType' = {}
self.subproject = subproject
self.option_types: T.Dict[str, T.Callable[..., coredata.UserOption]] = {
'string': self.string_parser,
'boolean': self.boolean_parser,
'combo': self.combo_parser,
'integer': self.integer_parser,
'array': self.string_array_parser,
'feature': self.feature_parser,
}
def process(self, option_file: str) -> None:
try:
with open(option_file, encoding='utf-8') as f:
ast = mparser.Parser(f.read(), option_file).parse()
except mesonlib.MesonException as me:
me.file = option_file
raise me
if not isinstance(ast, mparser.CodeBlockNode):
e = OptionException('Option file is malformed.')
e.lineno = ast.lineno()
e.file = option_file
raise e
for cur in ast.lines:
try:
self.current_node = cur
self.evaluate_statement(cur)
except mesonlib.MesonException as e:
e.lineno = cur.lineno
e.colno = cur.colno
e.file = option_file
raise e
except Exception as e:
raise mesonlib.MesonException(
str(e), lineno=cur.lineno, colno=cur.colno, file=option_file)
def reduce_single(self, arg: T.Union[str, mparser.BaseNode]) -> 'TYPE_var':
if isinstance(arg, str):
return arg
elif isinstance(arg, (mparser.StringNode, mparser.BooleanNode,
mparser.NumberNode)):
return arg.value
elif isinstance(arg, mparser.ArrayNode):
return [self.reduce_single(curarg) for curarg in arg.args.arguments]
elif isinstance(arg, mparser.DictNode):
d = {}
for k, v in arg.args.kwargs.items():
if not isinstance(k, mparser.StringNode):
raise OptionException('Dictionary keys must be a string literal')
d[k.value] = self.reduce_single(v)
return d
elif isinstance(arg, mparser.UMinusNode):
res = self.reduce_single(arg.value)
if not isinstance(res, (int, float)):
raise OptionException('Token after "-" is not a number')
FeatureNew.single_use('negative numbers in meson_options.txt', '0.54.1', self.subproject)
return -res
elif isinstance(arg, mparser.NotNode):
res = self.reduce_single(arg.value)
if not isinstance(res, bool):
raise OptionException('Token after "not" is not a a boolean')
FeatureNew.single_use('negation ("not") in meson_options.txt', '0.54.1', self.subproject)
return not res
elif isinstance(arg, mparser.ArithmeticNode):
l = self.reduce_single(arg.left)
r = self.reduce_single(arg.right)
if not (arg.operation == 'add' and isinstance(l, str) and isinstance(r, str)):
raise OptionException('Only string concatenation with the "+" operator is allowed')
FeatureNew.single_use('string concatenation in meson_options.txt', '0.55.0', self.subproject)
return l + r
else:
raise OptionException('Arguments may only be string, int, bool, or array of those.')
def reduce_arguments(self, args: mparser.ArgumentNode) -> T.Tuple['TYPE_var', 'TYPE_kwargs']:
if args.incorrect_order():
raise OptionException('All keyword arguments must be after positional arguments.')
reduced_pos = [self.reduce_single(arg) for arg in args.arguments]
reduced_kw = {}
for key in args.kwargs.keys():
if not isinstance(key, mparser.IdNode):
raise OptionException('Keyword argument name is not a string.')
a = args.kwargs[key]
reduced_kw[key.value] = self.reduce_single(a)
return reduced_pos, reduced_kw
def evaluate_statement(self, node: mparser.BaseNode) -> None:
if not isinstance(node, mparser.FunctionNode):
raise OptionException('Option file may only contain option definitions')
func_name = node.func_name
if func_name != 'option':
raise OptionException('Only calls to option() are allowed in option files.')
(posargs, kwargs) = self.reduce_arguments(node.args)
self.func_option(posargs, kwargs)
@typed_kwargs(
'option',
KwargInfo(
'type',
str,
required=True,
validator=in_set_validator({'string', 'boolean', 'integer', 'combo', 'array', 'feature'})
),
KwargInfo('description', str, default=''),
KwargInfo(
'deprecated',
(bool, str, ContainerTypeInfo(dict, str), ContainerTypeInfo(list, str)),
default=False,
since='0.60.0',
feature_validator=lambda x: [FeatureNew('string value to "deprecated" keyword argument', '0.63.0')] if isinstance(x, str) else []
),
KwargInfo('yield', bool, default=coredata.DEFAULT_YIELDING, since='0.45.0'),
allow_unknown=True,
)
@typed_pos_args('option', str)
def func_option(self, args: T.Tuple[str], kwargs: 'FuncOptionArgs') -> None:
opt_name = args[0]
if optname_regex.search(opt_name) is not None:
raise OptionException('Option names can only contain letters, numbers or dashes.')
key = mesonlib.OptionKey.from_string(opt_name).evolve(subproject=self.subproject)
if not key.is_project():
raise OptionException('Option name %s is reserved.' % opt_name)
opt_type = kwargs['type']
parser = self.option_types[opt_type]
description = kwargs['description'] or opt_name
# Drop the arguments we've already consumed
n_kwargs = {k: v for k, v in kwargs.items()
if k not in {'type', 'description', 'deprecated', 'yield'}}
opt = parser(description, (kwargs['yield'], kwargs['deprecated']), n_kwargs)
if key in self.options:
mlog.deprecation(f'Option {opt_name} already exists.')
self.options[key] = opt
@typed_kwargs(
'string option',
KwargInfo('value', str, default=''),
)
def string_parser(self, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: StringArgs) -> coredata.UserOption:
return coredata.UserStringOption(description, kwargs['value'], *args)
@typed_kwargs(
'boolean option',
KwargInfo(
'value',
(bool, str),
default=True,
validator=lambda x: None if isinstance(x, bool) or x in {'true', 'false'} else 'boolean options must have boolean values',
deprecated_values={'true': ('1.1.0', 'use a boolean, not a string'), 'false': ('1.1.0', 'use a boolean, not a string')},
),
)
def boolean_parser(self, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: BooleanArgs) -> coredata.UserOption:
return coredata.UserBooleanOption(description, kwargs['value'], *args)
@typed_kwargs(
'combo option',
KwargInfo('value', (str, NoneType)),
KwargInfo('choices', ContainerTypeInfo(list, str, allow_empty=False), required=True),
)
def combo_parser(self, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: ComboArgs) -> coredata.UserOption:
choices = kwargs['choices']
value = kwargs['value']
if value is None:
value = kwargs['choices'][0]
return coredata.UserComboOption(description, choices, value, *args)
@typed_kwargs(
'integer option',
KwargInfo(
'value',
(int, str),
default=True,
feature_validator=lambda x: [FeatureDeprecated('number values as strings', '1.1.0', 'use a raw number instead')] if isinstance(x, str) else [],
convertor=int,
),
KwargInfo('min', (int, NoneType)),
KwargInfo('max', (int, NoneType)),
)
def integer_parser(self, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: IntegerArgs) -> coredata.UserOption:
value = kwargs['value']
inttuple = (kwargs['min'], kwargs['max'], value)
return coredata.UserIntegerOption(description, inttuple, *args)
@typed_kwargs(
'string array option',
KwargInfo('value', (ContainerTypeInfo(list, str), str, NoneType)),
KwargInfo('choices', ContainerTypeInfo(list, str), default=[]),
)
def string_array_parser(self, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: StringArrayArgs) -> coredata.UserOption:
choices = kwargs['choices']
value = kwargs['value'] if kwargs['value'] is not None else choices
return coredata.UserArrayOption(description, value,
choices=choices,
yielding=args[0],
deprecated=args[1])
@typed_kwargs(
'feature option',
KwargInfo('value', str, default='auto', validator=in_set_validator({'auto', 'enabled', 'disabled'})),
)
def feature_parser(self, description: str, args: T.Tuple[bool, _DEPRECATED_ARGS], kwargs: FeatureArgs) -> coredata.UserOption:
return coredata.UserFeatureOption(description, kwargs['value'], *args)
|