diff options
-rw-r--r-- | mesonbuild/compilers/c.py | 9 | ||||
-rw-r--r-- | mesonbuild/compilers/compilers.py | 61 | ||||
-rw-r--r-- | mesonbuild/compilers/d.py | 2 | ||||
-rw-r--r-- | mesonbuild/compilers/vala.py | 4 |
4 files changed, 45 insertions, 31 deletions
diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index e1001e7..da51ce2 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -450,7 +450,9 @@ class CCompiler(Compiler): def _build_wrapper(self, code, env, extra_args, dependencies=None, mode='compile', want_output=False, disable_cache=False): args = self._get_compiler_check_args(env, extra_args, dependencies, mode) - return self.compile(code, args, mode, want_output=want_output, cdata=env.coredata if not disable_cache else None) + if disable_cache or want_output: + return self.compile(code, extra_args=args, mode=mode, want_output=want_output) + return self.cached_compile(code, env.coredata, extra_args=args, mode=mode) def links(self, code, env, *, extra_args=None, dependencies=None, disable_cache=False): return self.compiles(code, env, extra_args=extra_args, @@ -652,7 +654,10 @@ class CCompiler(Compiler): {delim}\n{define}''' args = self._get_compiler_check_args(env, extra_args, dependencies, mode='preprocess').to_native() - with self.compile(code.format(**fargs), args, 'preprocess', cdata=env.coredata if not disable_cache else None) as p: + func = lambda: self.cached_compile(code.format(**fargs), env.coredata, extra_args=args, mode='preprocess') + if disable_cache: + func = lambda: self.compile(code.format(**fargs), extra_args=args, mode='preprocess') + with func() as p: cached = p.cached if p.returncode != 0: raise EnvironmentException('Could not get define {!r}'.format(dname)) diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index e705a75..b03458a 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -1146,24 +1146,9 @@ class Compiler: return os.path.join(dirname, 'output.' + suffix) @contextlib.contextmanager - def compile(self, code, extra_args=None, mode='link', want_output=False, cdata: Optional[coredata.CoreData] = None): + def compile(self, code, extra_args=None, mode='link', want_output=False): if extra_args is None: - textra_args = None extra_args = [] - else: - textra_args = tuple(extra_args) - key = (tuple(self.exelist), self.version, code, textra_args, mode) - if not want_output: - if cdata is not None and key in cdata.compiler_check_cache: - p = cdata.compiler_check_cache[key] - p.cached = True - mlog.debug('Using cached compile:') - mlog.debug('Cached command line: ', ' '.join(p.commands), '\n') - mlog.debug('Code:\n', code) - mlog.debug('Cached compiler stdout:\n', p.stdo) - mlog.debug('Cached compiler stderr:\n', p.stde) - yield p - return try: with tempfile.TemporaryDirectory() as tmpdirname: if isinstance(code, str): @@ -1205,16 +1190,7 @@ class Compiler: p.input_name = srcname if want_output: p.output_name = output - elif cdata is not None: - # Remove all attributes except the following - # This way the object can be serialized - tokeep = ['args', 'commands', 'input_name', 'output_name', - 'pid', 'returncode', 'stdo', 'stde', 'text_mode'] - todel = [x for x in vars(p).keys() if x not in tokeep] - for i in todel: - delattr(p, i) - cdata.compiler_check_cache[key] = p - p.cached = False + p.cached = False # Make sure that the cached attribute always exists yield p except (PermissionError, OSError): # On Windows antivirus programs and the like hold on to files so @@ -1222,6 +1198,39 @@ class Compiler: # catch OSError because the directory is then no longer empty. pass + @contextlib.contextmanager + def cached_compile(self, code, cdata: coredata.CoreData, extra_args=None, mode: str = 'link'): + assert(isinstance(cdata, coredata.CoreData)) + + # Calculate the key + textra_args = tuple(extra_args) if extra_args is not None else None + key = (tuple(self.exelist), self.version, code, textra_args, mode) + + # Check if not cached + if key not in cdata.compiler_check_cache: + with self.compile(code, extra_args=extra_args, mode=mode, want_output=False) as p: + # Remove all attributes except the following + # This way the object can be serialized + tokeep = ['args', 'commands', 'input_name', 'output_name', + 'pid', 'returncode', 'stdo', 'stde', 'text_mode'] + todel = [x for x in vars(p).keys() if x not in tokeep] + for i in todel: + delattr(p, i) + p.cached = False + cdata.compiler_check_cache[key] = p + yield p + return + + # Return cached + p = cdata.compiler_check_cache[key] + p.cached = True + mlog.debug('Using cached compile:') + mlog.debug('Cached command line: ', ' '.join(p.commands), '\n') + mlog.debug('Code:\n', code) + mlog.debug('Cached compiler stdout:\n', p.stdo) + mlog.debug('Cached compiler stderr:\n', p.stde) + yield p + def get_colorout_args(self, colortype): return [] diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py index a5f72d8..46cc054 100644 --- a/mesonbuild/compilers/d.py +++ b/mesonbuild/compilers/d.py @@ -326,7 +326,7 @@ class DCompiler(Compiler): def compiles(self, code, env, *, extra_args=None, dependencies=None, mode='compile'): args = self._get_compiler_check_args(env, extra_args, dependencies, mode) - with self.compile(code, args, mode, cdata=env.coredata) as p: + with self.cached_compile(code, env.coredata, extra_args=args, mode=mode) as p: return p.returncode == 0, p.cached def has_multi_arguments(self, args, env): diff --git a/mesonbuild/compilers/vala.py b/mesonbuild/compilers/vala.py index 789e00b..c0b2a68 100644 --- a/mesonbuild/compilers/vala.py +++ b/mesonbuild/compilers/vala.py @@ -96,7 +96,7 @@ class ValaCompiler(Compiler): extra_flags += self.get_compile_only_args() else: extra_flags += environment.coredata.get_external_link_args(for_machine, self.language) - with self.compile(code, extra_flags, 'compile') as p: + with self.cached_compile(code, environment.coredata, extra_args=extra_flags, mode='compile') as p: if p.returncode != 0: msg = 'Vala compiler {!r} can not compile programs' \ ''.format(self.name_string()) @@ -121,7 +121,7 @@ class ValaCompiler(Compiler): args = env.coredata.get_external_args(for_machine, self.language) vapi_args = ['--pkg', libname] args += vapi_args - with self.compile(code, args, 'compile', cdata=env.coredata) as p: + with self.cached_compile(code, env.coredata, extra_args=args, mode='compile') as p: if p.returncode == 0: return vapi_args # Not found? Try to find the vapi file itself. |