From 59473e9ed7b1333ff42efab0d5268d8d74bcc3b0 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Mon, 14 Aug 2017 05:10:19 +0530 Subject: [PATCH] Add a new compiler method: get_return_value() This method accepts a single function that takes no arguments and returns a single value which can be a value that can be cast to a 64-bit signed integer, or a string, and returns that value. Mostly useful for running foolib_version() functions that return the currently-available version of libraries. --- mesonbuild/compilers/c.py | 28 ++++++++++++++++++++++++++++ mesonbuild/interpreter.py | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index ec1613497..80a0f3617 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -482,6 +482,34 @@ class CCompiler(Compiler): # minus the extra newline at the end return p.stdo.split(delim + '\n')[-1][:-1] + def get_return_value(self, fname, rtype, prefix, env, extra_args, dependencies): + if rtype == 'string': + fmt = '%s' + cast = '(char*)' + elif rtype == 'int': + fmt = '%lli' + cast = '(long long int)' + else: + raise AssertionError('BUG: Unknown return type {!r}'.format(rtype)) + fargs = {'prefix': prefix, 'f': fname, 'cast': cast, 'fmt': fmt} + code = '''{prefix} + #include + int main(int argc, char *argv[]) {{ + printf ("{fmt}", {cast} {f}()); + }}'''.format(**fargs) + res = self.run(code, env, extra_args, dependencies) + if not res.compiled: + m = 'Could not get return value of {}()' + raise EnvironmentException(m.format(fname)) + if rtype == 'string': + return res.stdout + elif rtype == 'int': + try: + return int(res.stdout.strip()) + except: + m = 'Return value of {}() is not an int' + raise EnvironmentException(m.format(fname)) + @staticmethod def _no_prototype_templ(): """ diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 2bcf198a2..47a1501ee 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -882,7 +882,7 @@ class CompilerHolder(InterpreterObject): extra_args = self.determine_args(kwargs) deps = self.determine_dependencies(kwargs) value = self.compiler.get_define(element, prefix, self.environment, extra_args, deps) - mlog.log('Checking for value of define "%s": %s' % (element, value)) + mlog.log('Fetching value of define "%s": %s' % (element, value)) return value def compiles_method(self, args, kwargs):