Compilers: Reduce code duplication between compiles and links

This also fix links() not calling args.to_native() unlike compiles()
pull/3353/head
Xavier Claessens 7 years ago
parent 521ce883ad
commit 3d91a08bbc
  1. 37
      mesonbuild/compilers/c.py

@ -321,25 +321,21 @@ class CCompiler(Compiler):
args += extra_args args += extra_args
return args return args
def compiles(self, code, env, extra_args=None, dependencies=None, mode='compile', want_output=False): 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._build_wrapper(code, env, extra_args, dependencies, mode) as p:
# We only want to compile; not link
with self.compile(code, args.to_native(), mode) as p:
return p.returncode == 0 return p.returncode == 0
def _links_wrapper(self, code, env, extra_args, dependencies, want_output=False): def _build_wrapper(self, code, env, extra_args, dependencies=None, mode='compile', want_output=False):
"Shares common code between self.links and self.run" args = self._get_compiler_check_args(env, extra_args, dependencies, mode)
args = self._get_compiler_check_args(env, extra_args, dependencies, mode='link') return self.compile(code, args.to_native(), mode, want_output=want_output)
return self.compile(code, args, want_output=want_output)
def links(self, code, env, extra_args=None, dependencies=None): def links(self, code, env, extra_args=None, dependencies=None):
with self._links_wrapper(code, env, extra_args, dependencies) as p: return self.compiles(code, env, extra_args, dependencies, mode='link')
return p.returncode == 0
def run(self, code, env, extra_args=None, dependencies=None): def run(self, code, env, extra_args=None, dependencies=None):
if self.is_cross and self.exe_wrapper is None: if self.is_cross and self.exe_wrapper is None:
raise CrossNoRunException('Can not run test applications in this cross environment.') raise CrossNoRunException('Can not run test applications in this cross environment.')
with self._links_wrapper(code, env, extra_args, dependencies, True) as p: with self._build_wrapper(code, env, extra_args, dependencies, mode='link', want_output=True) as p:
if p.returncode != 0: if p.returncode != 0:
mlog.debug('Could not compile test file %s: %d\n' % ( mlog.debug('Could not compile test file %s: %d\n' % (
p.input_name, p.input_name,
@ -1142,21 +1138,10 @@ class VisualStudioCCompiler(CCompiler):
def has_multi_arguments(self, args, env): def has_multi_arguments(self, args, env):
warning_text = '9002' warning_text = '9002'
code = 'int i;\n' code = 'int i;\n'
(fd, srcname) = tempfile.mkstemp(suffix='.' + self.default_suffix) with self._build_wrapper(code, env, extra_args=args, mode='compile') as p:
os.close(fd) if p.returncode != 0:
with open(srcname, 'w') as ofile: return False
ofile.write(code) return not(warning_text in p.stde or warning_text in p.stdo)
# Read c_args/cpp_args/etc from the cross-info file (if needed)
extra_args = self.get_cross_extra_flags(env, link=False)
extra_args += self.get_compile_only_args()
commands = self.exelist + args + extra_args + [srcname]
mlog.debug('Running VS compile:')
mlog.debug('Command line: ', ' '.join(commands))
mlog.debug('Code:\n', code)
p, stdo, stde = Popen_safe(commands, cwd=os.path.dirname(srcname))
if p.returncode != 0:
return False
return not(warning_text in stde or warning_text in stdo)
def get_compile_debugfile_args(self, rel_obj, pch=False): def get_compile_debugfile_args(self, rel_obj, pch=False):
pdbarr = rel_obj.split('.')[:-1] pdbarr = rel_obj.split('.')[:-1]

Loading…
Cancel
Save