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.
pull/2187/head
Nirbheek Chauhan 7 years ago
parent 276e9c15cc
commit 59473e9ed7
  1. 28
      mesonbuild/compilers/c.py
  2. 2
      mesonbuild/interpreter.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():
"""

@ -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):

Loading…
Cancel
Save