diff options
author | Andres Freund <andres@anarazel.de> | 2023-02-19 12:18:06 -0800 |
---|---|---|
committer | Eli Schwartz <eschwartz93@gmail.com> | 2023-02-27 20:09:32 -0500 |
commit | 9a41ce58d682faa59fcef5b067adc393a5412d30 (patch) | |
tree | b180bc1dfd4634b8c72fa0bf885c99e4f4ffce81 /mesonbuild/compilers | |
parent | ba72fc7f40dce218795758568c08c41a4657affe (diff) | |
download | meson-9a41ce58d682faa59fcef5b067adc393a5412d30.tar.gz |
Add Compiler.cached_run()
Caching Compiler.run() seems likely to cause problems, but some users, like
.sizeof(), we know enough about the program run to make it safe.
This commit just adds the Compiler.cached_run(), a subsequent commit makes use
of it.
Diffstat (limited to 'mesonbuild/compilers')
-rw-r--r-- | mesonbuild/compilers/compilers.py | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index c97b14a2c..3300a75fe 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -456,11 +456,13 @@ class CrossNoRunException(MesonException): class RunResult(HoldableObject): def __init__(self, compiled: bool, returncode: int = 999, - stdout: str = 'UNDEFINED', stderr: str = 'UNDEFINED'): + stdout: str = 'UNDEFINED', stderr: str = 'UNDEFINED', + cached: bool = False): self.compiled = compiled self.returncode = returncode self.stdout = stdout self.stderr = stderr + self.cached = cached class CompileResult(HoldableObject): @@ -689,6 +691,32 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): dependencies: T.Optional[T.List['Dependency']] = None) -> RunResult: raise EnvironmentException('Language %s does not support run checks.' % self.get_display_language()) + # Caching run() in general seems too risky (no way to know what the program + # depends on), but some callers know more about the programs they intend to + # run. + # For now we just accept code as a string, as that's what internal callers + # need anyway. If we wanted to accept files, the cache key would need to + # include mtime. + def cached_run(self, code: str, env: 'Environment', *, + extra_args: T.Union[T.List[str], T.Callable[[CompileCheckMode], T.List[str]], None] = None, + dependencies: T.Optional[T.List['Dependency']] = None) -> RunResult: + run_check_cache = env.coredata.run_check_cache + args = self.build_wrapper_args(env, extra_args, dependencies, CompileCheckMode('link')) + key = (code, tuple(args)) + if key in run_check_cache: + p = run_check_cache[key] + p.cached = True + mlog.debug('Using cached run result:') + mlog.debug('Code:\n', code) + mlog.debug('Args:\n', extra_args) + mlog.debug('Cached run returncode:\n', p.returncode) + mlog.debug('Cached run stdout:\n', p.stdout) + mlog.debug('Cached run stderr:\n', p.stderr) + else: + p = self.run(code, env, extra_args=extra_args, dependencies=dependencies) + run_check_cache[key] = p + return p + 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: |