interpreter: Accept more types in default_options dict values

pull/11896/head
Xavier Claessens 1 year ago committed by Dylan Baker
parent be20e0809f
commit 6f2956e76c
  1. 2
      docs/yaml/functions/_build_target_base.yaml
  2. 2
      docs/yaml/functions/dependency.yaml
  3. 2
      docs/yaml/functions/project.yaml
  4. 2
      docs/yaml/functions/subproject.yaml
  5. 6
      mesonbuild/interpreter/kwargs.py
  6. 21
      mesonbuild/interpreter/type_checking.py
  7. 1
      test cases/common/264 default_options dict/lib.c
  8. 22
      test cases/common/264 default_options dict/meson.build
  9. 4
      test cases/common/264 default_options dict/meson_options.txt

@ -228,7 +228,7 @@ kwargs:
Set this to `[]`, or omit the keyword argument for the default behaviour.
override_options:
type: list[str] | dict[str]
type: list[str] | dict[str | bool | int | list[str]]
since: 0.40.0
description: |
takes an array of strings in the same format as `project`'s `default_options`

@ -78,7 +78,7 @@ varargs:
kwargs:
default_options:
type: list[str] | dict[str]
type: list[str] | dict[str | bool | int | list[str]]
since: 0.38.0
description: |
An array of default option values

@ -38,7 +38,7 @@ varargs:
kwargs:
default_options:
type: list[str] | dict[str]
type: list[str] | dict[str | bool | int | list[str]]
description: |
Accepts strings in the form `key=value`
which have the same format as options to `meson configure`.

@ -42,7 +42,7 @@ posargs:
kwargs:
default_options:
type: list[str] | dict[str]
type: list[str] | dict[str | bool | int | list[str]]
since: 0.37.0
description: |
An array of default option values

@ -203,7 +203,7 @@ class Project(TypedDict):
version: T.Optional[FileOrString]
meson_version: T.Optional[str]
default_options: T.Dict[OptionKey, str]
default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
license: T.List[str]
subproject_dir: str
@ -298,13 +298,13 @@ class ConfigureFile(TypedDict):
class Subproject(ExtractRequired):
default_options: T.Dict[OptionKey, str]
default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
version: T.List[str]
class DoSubproject(ExtractRequired):
default_options: T.Dict[OptionKey, str]
default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
version: T.List[str]
cmake_options: T.List[str]
options: T.Optional[CMakeSubprojectOptions]

@ -184,7 +184,7 @@ REQUIRED_KW: KwargInfo[T.Union[bool, UserFeatureOption]] = KwargInfo(
DISABLER_KW: KwargInfo[bool] = KwargInfo('disabler', bool, default=False)
def _env_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Dict[str, 'TYPE_var'], str, None],
allow_dict_list: bool = True) -> T.Optional[str]:
only_dict_str: bool = True) -> T.Optional[str]:
def _splitter(v: str) -> T.Optional[str]:
split = v.split('=', 1)
if len(split) == 1:
@ -205,18 +205,21 @@ def _env_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Di
elif isinstance(value, dict):
# We don't need to spilt here, just do the type checking
for k, dv in value.items():
if allow_dict_list:
if only_dict_str:
if any(i for i in listify(dv) if not isinstance(i, str)):
return f"Dictionary element {k} must be a string or list of strings not {dv!r}"
elif not isinstance(dv, str):
return f"Dictionary element {k} must be a string not {dv!r}"
elif isinstance(dv, list):
if any(not isinstance(i, str) for i in dv):
return f"Dictionary element {k} must be a string, bool, integer or list of strings, not {dv!r}"
elif not isinstance(dv, (str, bool, int)):
return f"Dictionary element {k} must be a string, bool, integer or list of strings, not {dv!r}"
# We know that otherwise we have an EnvironmentVariables object or None, and
# we're okay at this point
return None
def _options_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Dict[str, 'TYPE_var'], str, None]) -> T.Optional[str]:
# Reusing the env validator is a little overkill, but nicer than duplicating the code
return _env_validator(value, allow_dict_list=False)
return _env_validator(value, only_dict_str=False)
def split_equal_string(input: str) -> T.Tuple[str, str]:
"""Split a string in the form `x=y`
@ -281,11 +284,11 @@ COMMAND_KW: KwargInfo[T.List[T.Union[str, BuildTarget, CustomTarget, CustomTarge
default=[],
)
def _override_options_convertor(raw: T.Union[str, T.List[str], T.Dict[str, str]]) -> T.Dict[OptionKey, str]:
def _override_options_convertor(raw: T.Union[str, T.List[str], T.Dict[str, T.Union[str, int, bool, T.List[str]]]]) -> T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]:
if isinstance(raw, str):
raw = [raw]
if isinstance(raw, list):
output: T.Dict[OptionKey, str] = {}
output: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]] = {}
for each in raw:
k, v = split_equal_string(each)
output[OptionKey.from_string(k)] = v
@ -293,9 +296,9 @@ def _override_options_convertor(raw: T.Union[str, T.List[str], T.Dict[str, str]]
return {OptionKey.from_string(k): v for k, v in raw.items()}
OVERRIDE_OPTIONS_KW: KwargInfo[T.Union[str, T.Dict[str, str], T.List[str]]] = KwargInfo(
OVERRIDE_OPTIONS_KW: KwargInfo[T.Union[str, T.Dict[str, T.Union[str, int, bool, T.List[str]]], T.List[str]]] = KwargInfo(
'override_options',
(str, ContainerTypeInfo(list, str), ContainerTypeInfo(dict, str)),
(str, ContainerTypeInfo(list, str), ContainerTypeInfo(dict, (str, int, bool, list))),
default={},
validator=_options_validator,
convertor=_override_options_convertor,

@ -0,0 +1 @@
#warning Make sure this is not fatal

@ -0,0 +1,22 @@
project('test default options', 'c',
default_options: {
'bool': true,
'int': 42,
'str': 'foo',
'array': ['foo'],
'werror': true,
},
)
assert(get_option('bool') == true)
assert(get_option('int') == 42)
assert(get_option('str') == 'foo')
assert(get_option('array') == ['foo'])
assert(get_option('werror') == true)
cc = meson.get_compiler('c')
# MSVC does not support #warning
if cc.get_id() != 'msvc'
static_library('foo', 'lib.c', override_options: {'werror': false})
endif

@ -0,0 +1,4 @@
option('bool', type: 'boolean', value: false)
option('int', type: 'integer', value: 0)
option('str', type: 'string', value: 'bar')
option('array', type: 'array', value: ['bar'])
Loading…
Cancel
Save