Use caching in Compiler.sizeof() and Compiler.alignment()

pull/11478/head
Andres Freund 2 years ago committed by Eli Schwartz
parent 9a41ce58d6
commit 808d5934dd
  1. 4
      mesonbuild/compilers/compilers.py
  2. 14
      mesonbuild/compilers/d.py
  3. 30
      mesonbuild/compilers/mixins/clike.py
  4. 18
      mesonbuild/interpreter/compiler.py

@ -719,12 +719,12 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta):
def sizeof(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> int:
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]:
raise EnvironmentException('Language %s does not support sizeof checks.' % self.get_display_language())
def alignment(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> int:
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]:
raise EnvironmentException('Language %s does not support alignment checks.' % self.get_display_language())
def has_function(self, funcname: str, prefix: str, env: 'Environment', *,

@ -735,7 +735,7 @@ class DCompiler(Compiler):
def sizeof(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> int:
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]:
if extra_args is None:
extra_args = []
t = f'''
@ -745,17 +745,17 @@ class DCompiler(Compiler):
writeln(({typename}).sizeof);
}}
'''
res = self.run(t, env, extra_args=extra_args,
dependencies=dependencies)
res = self.cached_run(t, env, extra_args=extra_args,
dependencies=dependencies)
if not res.compiled:
return -1
return -1, False
if res.returncode != 0:
raise mesonlib.EnvironmentException('Could not run sizeof test binary.')
return int(res.stdout)
return int(res.stdout), res.cached
def alignment(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> int:
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]:
if extra_args is None:
extra_args = []
t = f'''
@ -774,7 +774,7 @@ class DCompiler(Compiler):
align = int(res.stdout)
if align == 0:
raise mesonlib.EnvironmentException(f'Could not determine alignment of {typename}. Sorry. You might want to file a bug.')
return align
return align, res.cached
def has_header(self, hname: str, prefix: str, env: 'Environment', *,
extra_args: T.Union[None, T.List[str], T.Callable[['CompileCheckMode'], T.List[str]]] = None,

@ -591,25 +591,26 @@ class CLikeCompiler(Compiler):
def sizeof(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> int:
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]:
if extra_args is None:
extra_args = []
if self.is_cross:
return self.cross_sizeof(typename, prefix, env, extra_args=extra_args,
dependencies=dependencies)
r = self.cross_sizeof(typename, prefix, env, extra_args=extra_args,
dependencies=dependencies)
return r, False
t = f'''#include<stdio.h>
{prefix}
int main(void) {{
printf("%ld\\n", (long)(sizeof({typename})));
return 0;
}}'''
res = self.run(t, env, extra_args=extra_args,
dependencies=dependencies)
res = self.cached_run(t, env, extra_args=extra_args,
dependencies=dependencies)
if not res.compiled:
return -1
return -1, False
if res.returncode != 0:
raise mesonlib.EnvironmentException('Could not run sizeof test binary.')
return int(res.stdout)
return int(res.stdout), res.cached
def cross_alignment(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None,
@ -635,12 +636,13 @@ class CLikeCompiler(Compiler):
def alignment(self, typename: str, prefix: str, env: 'Environment', *,
extra_args: T.Optional[T.List[str]] = None,
dependencies: T.Optional[T.List['Dependency']] = None) -> int:
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]:
if extra_args is None:
extra_args = []
if self.is_cross:
return self.cross_alignment(typename, prefix, env, extra_args=extra_args,
dependencies=dependencies)
r = self.cross_alignment(typename, prefix, env, extra_args=extra_args,
dependencies=dependencies)
return r, False
t = f'''#include <stdio.h>
#include <stddef.h>
{prefix}
@ -652,8 +654,8 @@ class CLikeCompiler(Compiler):
printf("%d", (int)offsetof(struct tmp, target));
return 0;
}}'''
res = self.run(t, env, extra_args=extra_args,
dependencies=dependencies)
res = self.cached_run(t, env, extra_args=extra_args,
dependencies=dependencies)
if not res.compiled:
raise mesonlib.EnvironmentException('Could not compile alignment test.')
if res.returncode != 0:
@ -661,7 +663,7 @@ class CLikeCompiler(Compiler):
align = int(res.stdout)
if align == 0:
raise mesonlib.EnvironmentException(f'Could not determine alignment of {typename}. Sorry. You might want to file a bug.')
return align
return align, res.cached
def get_define(self, dname: str, prefix: str, env: 'Environment',
extra_args: T.Union[T.List[str], T.Callable[[CompileCheckMode], T.List[str]]],
@ -1117,7 +1119,7 @@ class CLikeCompiler(Compiler):
'''
returns true if the output produced is 64-bit, false if 32-bit
'''
return self.sizeof('void *', '', env) == 8
return self.sizeof('void *', '', env)[0] == 8
def _find_library_real(self, libname: str, env: 'Environment', extra_dirs: T.List[str], code: str, libtype: LibType) -> T.Optional[T.List[str]]:
# First try if we can just add the library as -l.

@ -272,10 +272,12 @@ class CompilerHolder(ObjectHolder['Compiler']):
def alignment_method(self, args: T.Tuple[str], kwargs: 'AlignmentKw') -> int:
typename = args[0]
deps, msg = self._determine_dependencies(kwargs['dependencies'], compile_only=self.compiler.is_cross)
result = self.compiler.alignment(typename, kwargs['prefix'], self.environment,
extra_args=kwargs['args'],
dependencies=deps)
mlog.log('Checking for alignment of', mlog.bold(typename, True), msg, mlog.bold(str(result)))
result, cached = self.compiler.alignment(typename, kwargs['prefix'], self.environment,
extra_args=kwargs['args'],
dependencies=deps)
cached_msg = mlog.blue('(cached)') if cached else ''
mlog.log('Checking for alignment of',
mlog.bold(typename, True), msg, mlog.bold(str(result)), cached_msg)
return result
@typed_pos_args('compiler.run', (str, mesonlib.File))
@ -419,9 +421,11 @@ class CompilerHolder(ObjectHolder['Compiler']):
element = args[0]
extra_args = functools.partial(self._determine_args, kwargs['no_builtin_args'], kwargs['include_directories'], kwargs['args'])
deps, msg = self._determine_dependencies(kwargs['dependencies'], compile_only=self.compiler.is_cross)
esize = self.compiler.sizeof(element, kwargs['prefix'], self.environment,
extra_args=extra_args, dependencies=deps)
mlog.log('Checking for size of', mlog.bold(element, True), msg, mlog.bold(str(esize)))
esize, cached = self.compiler.sizeof(element, kwargs['prefix'], self.environment,
extra_args=extra_args, dependencies=deps)
cached_msg = mlog.blue('(cached)') if cached else ''
mlog.log('Checking for size of',
mlog.bold(element, True), msg, mlog.bold(str(esize)), cached_msg)
return esize
@FeatureNew('compiler.get_define', '0.40.0')

Loading…
Cancel
Save