Skip to content

Commit

Permalink
Add tristate user option required/not-required/disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
xclaesse committed Apr 9, 2018
1 parent d66c31b commit cbaca02
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/markdown/Build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option('combo_opt', type : 'combo', choices : ['one', 'two', 'three'], value : '
option('integer_opt', type : 'integer', min : 0, max : 5, value : 3) # Since 0.45.0
option('free_array_opt', type : 'array', value : ['one', 'two'])
option('array_opt', type : 'array', choices : ['one', 'two', 'three'], value : ['one', 'two'])
option('some_feature', type : 'dependency', value : 'required')
```

All types allow a `description` value to be set describing the option,
Expand Down Expand Up @@ -60,6 +61,23 @@ default.

This type is available since version 0.44.0

### Dependency

A `dependency` options has tree states: `required`, `not-required` or
`disabled`. It is special cased by [`get_option()`](Reference-manual.md#get_option)
and instead of returning their string representation it returns `true`, `false`
or [`Disabler`](Reference-manual.md#disabler) respectively.

It is indented to be passed directly to the `required` keyword of
[`dependency()`](Reference-manual.md#dependency) function, which will cause it
to return a `Disabler` if the option is set to `disabled`.

```meson
d = dependency('foo', required : get_option('foo_option'))
app = executable('myapp', 'main.c', dependencies : [d])
```

This type is available since version 0.46.0

## Using build options

Expand Down
4 changes: 4 additions & 0 deletions mesonbuild/coredata.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def validate_value(self, value, user_input=True):
', '.join(bad), ', '.join(self.choices)))
return newvalue

class UserDependencyOption(UserComboOption):
def __init__(self, name, description, value, yielding=None):
super().__init__(name, description, ['required', 'not-required', 'disabled'], value, yielding)


# This class contains all data that must persist over multiple
# invocations of Meson. It is roughly the same thing as
Expand Down
7 changes: 7 additions & 0 deletions mesonbuild/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1911,6 +1911,13 @@ def func_get_option(self, nodes, args, kwargs):
if opt.yielding and ':' in optname:
# If option not present in superproject, keep the original.
opt = self.environment.coredata.user_options.get(undecorated_optname, opt)
if isinstance(opt, coredata.UserDependencyOption):
if opt.value == 'required':
return True
elif opt.value == 'not-required':
return False
else:
return Disabler()
return opt.value
except KeyError:
pass
Expand Down
8 changes: 8 additions & 0 deletions mesonbuild/optinterpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,19 @@ def string_array_parser(name, description, kwargs):
choices=choices,
yielding=kwargs.get('yield', coredata.default_yielding))

@permitted_kwargs({'value', 'yield'})
def DependencyParser(name, description, kwargs):
return coredata.UserDependencyOption(name,
description,
kwargs.get('value', 'required'),
yielding=kwargs.get('yield', coredata.default_yielding))

option_types = {'string': StringParser,
'boolean': BooleanParser,
'combo': ComboParser,
'integer': IntegerParser,
'array': string_array_parser,
'dependency': DependencyParser,
}

class OptionInterpreter:
Expand Down

0 comments on commit cbaca02

Please sign in to comment.