From 59473e9ed7b1333ff42efab0d5268d8d74bcc3b0 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Mon, 14 Aug 2017 05:10:19 +0530 Subject: 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 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'mesonbuild/compilers/c.py') diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index ec16134..80a0f36 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(): """ -- cgit v1.1