aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathieu Duponchelle <mathieu@centricular.com>2019-05-26 15:13:21 +0200
committerJussi Pakkanen <jpakkane@gmail.com>2019-05-28 20:26:54 +0300
commit12a82e763d43f6280572b3f04357f77e50304855 (patch)
treea11ae68d4bd92ca1b84fae7cbc04b0cc238c1bc8
parent20eb948b974a96b3933d42db524fd584cb60c8b7 (diff)
downloadmeson-12a82e763d43f6280572b3f04357f77e50304855.zip
meson-12a82e763d43f6280572b3f04357f77e50304855.tar.gz
meson-12a82e763d43f6280572b3f04357f77e50304855.tar.bz2
interpreter: add fallback argument to subproject.get_variable()
-rw-r--r--docs/markdown/Reference-manual.md6
-rw-r--r--docs/markdown/snippets/add_fallback_argument_to_subproject_get_variable.md13
-rw-r--r--mesonbuild/interpreter.py19
-rw-r--r--test cases/common/46 subproject/meson.build5
4 files changed, 36 insertions, 7 deletions
diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md
index 9484c28..f59d627 100644
--- a/docs/markdown/Reference-manual.md
+++ b/docs/markdown/Reference-manual.md
@@ -2288,10 +2288,14 @@ opaque object representing it.
- `found()` *(added 0.48.0)* which returns whether the subproject was
successfully setup
-- `get_variable(name)` fetches the specified variable from inside the
+- `get_variable(name, fallback)` fetches the specified variable from inside the
subproject. This is useful to, for instance, get a [declared
dependency](#declare_dependency) from the [subproject](Subprojects.md).
+ If the variable does not exist, the variable `fallback` is returned.
+ If a fallback is not specified, then attempting to read a non-existing
+ variable will cause a fatal error.
+
### `run result` object
This object encapsulates the result of trying to compile and run a
diff --git a/docs/markdown/snippets/add_fallback_argument_to_subproject_get_variable.md b/docs/markdown/snippets/add_fallback_argument_to_subproject_get_variable.md
new file mode 100644
index 0000000..24dafa0
--- /dev/null
+++ b/docs/markdown/snippets/add_fallback_argument_to_subproject_get_variable.md
@@ -0,0 +1,13 @@
+---
+short-description: Add fallback argument to subproject.get_variable()
+...
+
+## subproject.get_variable() now accepts a `fallback` argument
+
+Similar to `get_variable`, a fallback argument can now be passed to
+`subproject.get_variable()`, it will be returned if the requested
+variable name did not exist.
+
+``` meson
+var = subproject.get_variable('does-not-exist', 'fallback-value')
+```
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 30be5ae..83fc2f1 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -917,18 +917,25 @@ class SubprojectHolder(InterpreterObject, ObjectHolder):
return self.held_object is not None
@permittedKwargs({})
+ @noArgsFlattening
def get_variable_method(self, args, kwargs):
- if len(args) != 1:
- raise InterpreterException('Get_variable takes one argument.')
+ if len(args) < 1 or len(args) > 2:
+ raise InterpreterException('Get_variable takes one or two arguments.')
if not self.found():
raise InterpreterException('Subproject "%s/%s" disabled can\'t get_variable on it.' % (
self.subproject_dir, self.name))
varname = args[0]
if not isinstance(varname, str):
- raise InterpreterException('Get_variable takes a string argument.')
- if varname not in self.held_object.variables:
- raise InvalidArguments('Requested variable "{0}" not found.'.format(varname))
- return self.held_object.variables[varname]
+ raise InterpreterException('Get_variable first argument must be a string.')
+ try:
+ return self.held_object.variables[varname]
+ except KeyError:
+ pass
+
+ if len(args) == 2:
+ return args[1]
+
+ raise InvalidArguments('Requested variable "{0}" not found.'.format(varname))
header_permitted_kwargs = set([
'required',
diff --git a/test cases/common/46 subproject/meson.build b/test cases/common/46 subproject/meson.build
index c6ec116..2b939d1 100644
--- a/test cases/common/46 subproject/meson.build
+++ b/test cases/common/46 subproject/meson.build
@@ -21,3 +21,8 @@ e = executable('user', 'user.c', include_directories : inc, link_with : lib, ins
test('subdirtest', e)
meson.install_dependency_manifest('share/sublib/sublib.depmf')
+
+unknown_var = sub.get_variable('does-not-exist', [])
+if unknown_var != []
+ error ('unexpetced fallback value for subproject.get_variable()')
+endif