diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index eee44055a..92c8a1ff6 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -1556,6 +1556,11 @@ page](Configuration.md) It has three methods: value has not been set returns `default_value` if it is defined *(added 0.38.0)* and errors out if not +- `get_unquoted(varname, default_value)` returns the value of `varname` + but without surrounding double quotes (`"`). If the value has not been + set returns `default_value` if it is defined and errors out if not. + Available since 0.43.0 + - `has(varname)`, returns `true` if the specified variable is set - `merge_from(other)` takes as argument a different configuration data diff --git a/docs/markdown/snippets/get_unquoted.md b/docs/markdown/snippets/get_unquoted.md new file mode 100755 index 000000000..5f8969d22 --- /dev/null +++ b/docs/markdown/snippets/get_unquoted.md @@ -0,0 +1,4 @@ +# `get_unquoted()` mehod for the `configuration` data object + +New convenience method that allow reusing a variable value +defined quoted. Useful in C for config.h strings for example. diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index c7a0fb71c..6300f7f1b 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -177,6 +177,7 @@ class ConfigurationDataHolder(MutableInterpreterObject, ObjectHolder): 'set_quoted': self.set_quoted_method, 'has': self.has_method, 'get': self.get_method, + 'get_unquoted': self.get_unquoted_method, 'merge_from': self.merge_from_method, }) @@ -232,6 +233,20 @@ class ConfigurationDataHolder(MutableInterpreterObject, ObjectHolder): return args[1] raise InterpreterException('Entry %s not in configuration data.' % name) + def get_unquoted_method(self, args, kwargs): + if len(args) < 1 or len(args) > 2: + raise InterpreterException('Get method takes one or two arguments.') + name = args[0] + if name in self.held_object: + val = self.held_object.get(name)[0] + elif len(args) > 1: + val = args[1] + else: + raise InterpreterException('Entry %s not in configuration data.' % name) + if val[0] == '"' and val[-1] == '"': + return val[1:-1] + return val + def get(self, name): return self.held_object.values[name] # (val, desc) diff --git a/test cases/common/16 configure file/dumpprog.c b/test cases/common/16 configure file/dumpprog.c index 685240ced..39a215ee8 100644 --- a/test cases/common/16 configure file/dumpprog.c +++ b/test cases/common/16 configure file/dumpprog.c @@ -12,7 +12,14 @@ #error Token did not get defined #endif +#define stringify(s) str(s) +#define str(s) #s + int main(int argc, char **argv) { +#if !(SHOULD_BE_UNQUOTED_STRING == string) + printf("String token (unquoted) defined wrong.\n"); + return 1; +#endif if(strcmp(SHOULD_BE_STRING, "string") != 0) { printf("String token defined wrong.\n"); return 1; diff --git a/test cases/common/16 configure file/meson.build b/test cases/common/16 configure file/meson.build index 9dc5fb55e..1e5a8196e 100644 --- a/test cases/common/16 configure file/meson.build +++ b/test cases/common/16 configure file/meson.build @@ -70,6 +70,12 @@ dump.set('SHOULD_BE_UNDEFINED', false) dump.set('SHOULD_BE_ONE', 1) dump.set('SHOULD_BE_ZERO', 0, description : 'Absolutely zero') dump.set('SHOULD_BE_QUOTED_ONE', '"1"') + +dump.set_quoted('INTEGER_AS_STRING', '12') +if dump.get_unquoted('INTEGER_AS_STRING').to_int() == 12 + dump.set('SHOULD_BE_UNQUOTED_STRING', dump.get_unquoted('SHOULD_BE_STRING')) +endif + configure_file(output : 'config3.h', configuration : dump)