From 6f2956e76c58c49b12298fbf144399c41c380520 Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Fri, 9 Jun 2023 17:58:49 -0400 Subject: interpreter: Accept more types in default_options dict values --- docs/yaml/functions/_build_target_base.yaml | 2 +- docs/yaml/functions/dependency.yaml | 2 +- docs/yaml/functions/project.yaml | 2 +- docs/yaml/functions/subproject.yaml | 2 +- mesonbuild/interpreter/kwargs.py | 6 +++--- mesonbuild/interpreter/type_checking.py | 21 ++++++++++++--------- test cases/common/264 default_options dict/lib.c | 1 + .../common/264 default_options dict/meson.build | 22 ++++++++++++++++++++++ .../264 default_options dict/meson_options.txt | 4 ++++ 9 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 test cases/common/264 default_options dict/lib.c create mode 100644 test cases/common/264 default_options dict/meson.build create mode 100644 test cases/common/264 default_options dict/meson_options.txt diff --git a/docs/yaml/functions/_build_target_base.yaml b/docs/yaml/functions/_build_target_base.yaml index f863288..79924d8 100644 --- a/docs/yaml/functions/_build_target_base.yaml +++ b/docs/yaml/functions/_build_target_base.yaml @@ -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` diff --git a/docs/yaml/functions/dependency.yaml b/docs/yaml/functions/dependency.yaml index 6a628bd..5ae1634 100644 --- a/docs/yaml/functions/dependency.yaml +++ b/docs/yaml/functions/dependency.yaml @@ -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 diff --git a/docs/yaml/functions/project.yaml b/docs/yaml/functions/project.yaml index 6eb7adc..5be8cac 100644 --- a/docs/yaml/functions/project.yaml +++ b/docs/yaml/functions/project.yaml @@ -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`. diff --git a/docs/yaml/functions/subproject.yaml b/docs/yaml/functions/subproject.yaml index 3017c20..bccac79 100644 --- a/docs/yaml/functions/subproject.yaml +++ b/docs/yaml/functions/subproject.yaml @@ -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 diff --git a/mesonbuild/interpreter/kwargs.py b/mesonbuild/interpreter/kwargs.py index 972b0f3..cf476ce 100644 --- a/mesonbuild/interpreter/kwargs.py +++ b/mesonbuild/interpreter/kwargs.py @@ -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] diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py index e950da0..8b57d06 100644 --- a/mesonbuild/interpreter/type_checking.py +++ b/mesonbuild/interpreter/type_checking.py @@ -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, diff --git a/test cases/common/264 default_options dict/lib.c b/test cases/common/264 default_options dict/lib.c new file mode 100644 index 0000000..3ef78ee --- /dev/null +++ b/test cases/common/264 default_options dict/lib.c @@ -0,0 +1 @@ +#warning Make sure this is not fatal diff --git a/test cases/common/264 default_options dict/meson.build b/test cases/common/264 default_options dict/meson.build new file mode 100644 index 0000000..06edd7b --- /dev/null +++ b/test cases/common/264 default_options dict/meson.build @@ -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 diff --git a/test cases/common/264 default_options dict/meson_options.txt b/test cases/common/264 default_options dict/meson_options.txt new file mode 100644 index 0000000..1b0b0e1 --- /dev/null +++ b/test cases/common/264 default_options dict/meson_options.txt @@ -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']) -- cgit v1.1