diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2017-08-14 05:10:19 +0530 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek@centricular.com> | 2017-09-20 13:36:42 +0530 |
commit | 59473e9ed7b1333ff42efab0d5268d8d74bcc3b0 (patch) | |
tree | 011932d7d5546c7f76afa9b975d7a61121b30114 /mesonbuild/compilers/c.py | |
parent | 276e9c15cc63d266c9f81301229128d02b229a6f (diff) | |
download | meson-59473e9ed7b1333ff42efab0d5268d8d74bcc3b0.zip meson-59473e9ed7b1333ff42efab0d5268d8d74bcc3b0.tar.gz meson-59473e9ed7b1333ff42efab0d5268d8d74bcc3b0.tar.bz2 |
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.
Diffstat (limited to 'mesonbuild/compilers/c.py')
-rw-r--r-- | mesonbuild/compilers/c.py | 28 |
1 files changed, 28 insertions, 0 deletions
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 <stdio.h> + 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(): """ |