diff options
-rw-r--r-- | mesonbuild/compilers.py | 28 | ||||
-rw-r--r-- | mesonbuild/interpreter.py | 15 | ||||
-rw-r--r-- | test cases/common/139 compute int/config.h.in | 2 | ||||
-rw-r--r-- | test cases/common/139 compute int/foobar.h | 6 | ||||
-rw-r--r-- | test cases/common/139 compute int/meson.build | 35 | ||||
-rw-r--r-- | test cases/common/139 compute int/prog.c.in | 16 |
6 files changed, 102 insertions, 0 deletions
diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index 8c2bb92..d1a564b 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -1010,6 +1010,34 @@ class CCompiler(Compiler): return i raise EnvironmentException('Cross-compile check overflowed') + def cross_compute_int(self, fragment, prefix, env, extra_args=None, dependencies=None): + if extra_args is None: + extra_args = [] + fargs = {'prefix': prefix, 'fragment': fragment} + t = '''#include <stdio.h> + {prefix} + int temparray[{size}-({fragment})];''' + return self._bisect_compiles(t, fargs, env, extra_args, dependencies) + + def compute_int(self, fragment, prefix, env, extra_args=None, dependencies=None): + if extra_args is None: + extra_args = [] + fargs = {'prefix': prefix, 'fragment': fragment} + if self.is_cross: + return self.cross_compute_int(fragment, prefix, env, extra_args, dependencies) + t = '''#include<stdio.h> + {prefix} + int main(int argc, char **argv) {{ + printf("%ld\\n", (long)({fragment})); + return 0; + }};''' + res = self.run(t.format(**fargs), env, extra_args, dependencies) + if not res.compiled: + return -1 + if res.returncode != 0: + raise EnvironmentException('Could not run compute_int test binary.') + return int(res.stdout) + def cross_sizeof(self, element, prefix, env, extra_args=None, dependencies=None): if extra_args is None: extra_args = [] diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 07b5c40..dca7934 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -631,6 +631,7 @@ class CompilerHolder(InterpreterObject): self.methods.update({'compiles': self.compiles_method, 'links': self.links_method, 'get_id': self.get_id_method, + 'compute_int': self.compute_int_method, 'sizeof': self.sizeof_method, 'has_header': self.has_header_method, 'has_header_symbol': self.has_header_symbol_method, @@ -822,6 +823,20 @@ class CompilerHolder(InterpreterObject): mlog.log('Checking for type "', mlog.bold(typename), '": ', hadtxt, sep='') return had + def compute_int_method(self, args, kwargs): + if len(args) != 1: + raise InterpreterException('Compute_int takes exactly one argument.') + check_stringlist(args) + fragment = args[0] + prefix = kwargs.get('prefix', '') + if not isinstance(prefix, str): + raise InterpreterException('Prefix argument of compute_int must be a string.') + extra_args = self.determine_args(kwargs) + deps = self.determine_dependencies(kwargs) + res = self.compiler.compute_int(fragment, prefix, self.environment, extra_args, deps) + mlog.log('Computing int of "%s": %d' % (fragment, res)) + return res + def sizeof_method(self, args, kwargs): if len(args) != 1: raise InterpreterException('Sizeof takes exactly one argument.') diff --git a/test cases/common/139 compute int/config.h.in b/test cases/common/139 compute int/config.h.in new file mode 100644 index 0000000..ad8d077 --- /dev/null +++ b/test cases/common/139 compute int/config.h.in @@ -0,0 +1,2 @@ +#define INTSIZE @INTSIZE@ +#define FOOBAR_IN_CONFIG_H @FOOBAR@ diff --git a/test cases/common/139 compute int/foobar.h b/test cases/common/139 compute int/foobar.h new file mode 100644 index 0000000..fd3cb5e --- /dev/null +++ b/test cases/common/139 compute int/foobar.h @@ -0,0 +1,6 @@ +#ifndef __FOOBAR_H__ +#define __FOOBAR_H__ + +#define FOOBAR_IN_FOOBAR_H 10 + +#endif /*__FOOBAR_H__*/ diff --git a/test cases/common/139 compute int/meson.build b/test cases/common/139 compute int/meson.build new file mode 100644 index 0000000..6f813c5 --- /dev/null +++ b/test cases/common/139 compute int/meson.build @@ -0,0 +1,35 @@ +project('compute int', 'c', 'cpp') + +inc = include_directories('.') + +# Test with C +cc = meson.get_compiler('c') + +intsize = cc.compute_int('sizeof(int)') +foobar = cc.compute_int('FOOBAR_IN_FOOBAR_H', prefix : '#include "foobar.h"', include_directories : inc) + +cd = configuration_data() +cd.set('INTSIZE', intsize) +cd.set('FOOBAR', foobar) +cd.set('CONFIG', 'config.h') +configure_file(input : 'config.h.in', output : 'config.h', configuration : cd) +s = configure_file(input : 'prog.c.in', output : 'prog.c', configuration : cd) + +e = executable('prog', s) +test('compute int test', e) + +# Test with C++ +cpp = meson.get_compiler('cpp') + +intsize = cpp.compute_int('sizeof(int)') +foobar = cpp.compute_int('FOOBAR_IN_FOOBAR_H', prefix : '#include "foobar.h"', include_directories : inc) + +cdpp = configuration_data() +cdpp.set('INTSIZE', intsize) +cdpp.set('FOOBAR', foobar) +cdpp.set('CONFIG', 'config.hpp') +configure_file(input : 'config.h.in', output : 'config.hpp', configuration : cdpp) +spp = configure_file(input : 'prog.c.in', output : 'prog.cc', configuration : cdpp) + +epp = executable('progpp', spp) +test('compute int test c++', epp) diff --git a/test cases/common/139 compute int/prog.c.in b/test cases/common/139 compute int/prog.c.in new file mode 100644 index 0000000..3ff1463 --- /dev/null +++ b/test cases/common/139 compute int/prog.c.in @@ -0,0 +1,16 @@ +#include "@CONFIG@" +#include <stdio.h> +#include <wchar.h> +#include "foobar.h" + +int main(int argc, char **argv) { + if(INTSIZE != sizeof(int)) { + fprintf(stderr, "Mismatch: computed int size %d, actual size %d.\n", INTSIZE, (int)sizeof(int)); + return 1; + } + if(FOOBAR_IN_CONFIG_H != FOOBAR_IN_FOOBAR_H) { + fprintf(stderr, "Mismatch: computed int %d, should be %d.\n", FOOBAR_IN_CONFIG_H, FOOBAR_IN_FOOBAR_H); + return 1; + } + return 0; +} |