Add unset_variable()

This should be useful for helping to control variable scope within
Meson. CMake has something similar for controlling scope.
pull/9125/head
Tristan Partin 3 years ago committed by Xavier Claessens
parent a2f110ff77
commit 1dc13e9951
  1. 3
      data/syntax-highlighting/vim/syntax/meson.vim
  2. 11
      docs/markdown/Reference-manual.md
  3. 16
      docs/markdown/snippets/unset_variable.md
  4. 1
      mesonbuild/ast/interpreter.py
  5. 11
      mesonbuild/interpreter/interpreter.py
  6. 2
      mesonbuild/interpreterbase/interpreterbase.py
  7. 15
      test cases/common/244 variable scope/meson.build

@ -3,7 +3,7 @@
" License: VIM License
" Maintainer: Nirbheek Chauhan <nirbheek.chauhan@gmail.com>
" Liam Beguin <liambeguin@gmail.com>
" Last Change: 2016 Dec 7
" Last Change: 2021 Aug 16
" Credits: Zvezdan Petkovic <zpetkovic@acm.org>
" Neil Schemenauer <nas@meson.ca>
" Dmitry Vasiliev
@ -118,6 +118,7 @@ syn keyword mesonBuiltin
\ summary
\ target_machine
\ test
\ unset_variable
\ vcs_tag
\ warning
\ range

@ -1852,6 +1852,17 @@ Defined tests can be run in a backend-agnostic way by calling
`meson test` inside the build dir, or by using backend-specific
commands, such as `ninja test` or `msbuild RUN_TESTS.vcxproj`.
### unset_variable()
*(since 0.60.0)*
```meson
void unset_variable(varname)
```
Unsets a variable. Referencing a variable which has been unset is an error until
it has been set again.
### vcs_tag()
``` meson

@ -0,0 +1,16 @@
## `unset_variable()`
`unset_variable()` can be used to unset a variable. Reading a variable after
calling `unset_variable()` will raise an exception unless the variable is set
again.
```meson
# tests/meson.build
tests = ['test1', 'test2']
# ...
unset_variable('tests')
# tests is no longer usable until it is set again
```

@ -127,6 +127,7 @@ class AstInterpreter(InterpreterBase):
'subdir': self.func_subdir,
'set_variable': self.func_do_nothing,
'get_variable': self.func_do_nothing,
'unset_variable': self.func_do_nothing,
'is_disabler': self.func_do_nothing,
'is_variable': self.func_do_nothing,
'disabler': self.func_do_nothing,

@ -360,6 +360,7 @@ class Interpreter(InterpreterBase, HoldableObject):
'static_library': self.func_static_lib,
'both_libraries': self.func_both_lib,
'test': self.func_test,
'unset_variable': self.func_unset_variable,
'vcs_tag': self.func_vcs_tag,
'range': self.func_range,
})
@ -2638,6 +2639,16 @@ This will become a hard error in the future.''', location=self.current_node)
def func_is_variable(self, node: mparser.BaseNode, args: T.Tuple[str], kwargs: 'TYPE_kwargs') -> bool:
return args[0] in self.variables
@FeatureNew('unset_variable', '0.60.0')
@typed_pos_args('unset_variable', str)
@noKwargs
def func_unset_variable(self, node: mparser.BaseNode, args: T.Tuple[str], kwargs: 'TYPE_kwargs') -> None:
varname = args[0]
try:
del self.variables[varname]
except KeyError:
raise InterpreterException(f'Tried to unset unknown variable "{varname}".')
@staticmethod
def machine_from_native_kwarg(kwargs: T.Dict[str, T.Any]) -> MachineChoice:
native = kwargs.get('native', False)

@ -542,7 +542,7 @@ The result of this is undefined and will become a hard error in a future Meson r
func_name = node.func_name
(h_posargs, h_kwargs) = self.reduce_arguments(node.args)
(posargs, kwargs) = self._unholder_args(h_posargs, h_kwargs)
if is_disabled(posargs, kwargs) and func_name not in {'get_variable', 'set_variable', 'is_disabler'}:
if is_disabled(posargs, kwargs) and func_name not in {'get_variable', 'set_variable', 'unset_variable', 'is_disabler'}:
return Disabler()
if func_name in self.funcs:
func = self.funcs[func_name]

@ -0,0 +1,15 @@
project('variable scope')
x = 1
assert(is_variable('x'))
assert(get_variable('x') == 1)
set_variable('x', 10)
assert(get_variable('x') == 10)
unset_variable('x')
assert(not is_variable('x'))
Loading…
Cancel
Save