compilers: Fix output ext while compile-only on MSVC

When doing a compile-only self.compile() check, cl.exe expects an output
file of type *.obj, and passing it `output.exe` makes it write the
compiled object to `output.exe.obj`. Detect when we're doing
a compily-only check and change the suffix ourselves.

This is fine most of the time, but fails when you need to inspect the
compiled file manually.

Also store stdout/stderr on the returned object.
pull/1184/head
Nirbheek Chauhan 8 years ago
parent 09f65b7a3c
commit 5240688f5c
  1. 32
      mesonbuild/compilers.py

@ -441,7 +441,7 @@ class Compiler():
return extra_flags return extra_flags
@contextlib.contextmanager @contextlib.contextmanager
def compile(self, code, extra_args=None): def compile(self, code, extra_args=None, compile_only=False):
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
@ -457,20 +457,25 @@ class Compiler():
# Extension only matters if running results; '.exe' is # Extension only matters if running results; '.exe' is
# guaranteed to be executable on every platform. # guaranteed to be executable on every platform.
output = os.path.join(tmpdirname, 'output.exe') if compile_only:
suffix = 'obj'
else:
suffix = 'exe'
output = os.path.join(tmpdirname, 'output.' + suffix)
commands = self.get_exelist() commands = self.get_exelist()
commands.append(srcname) commands.append(srcname)
commands += extra_args commands += extra_args
commands += self.get_output_args(output) commands += self.get_output_args(output)
if compile_only:
commands += self.get_compile_only_args()
mlog.debug('Running compile:') mlog.debug('Running compile:')
mlog.debug('Working directory: ', tmpdirname) mlog.debug('Working directory: ', tmpdirname)
mlog.debug('Command line: ', ' '.join(commands), '\n') mlog.debug('Command line: ', ' '.join(commands), '\n')
mlog.debug('Code:\n', code) mlog.debug('Code:\n', code)
p, stdo, stde = Popen_safe(commands, cwd=tmpdirname) p, p.stdo, p.stde = Popen_safe(commands, cwd=tmpdirname)
mlog.debug('Compiler stdout:\n', stdo) mlog.debug('Compiler stdout:\n', p.stdo)
mlog.debug('Compiler stderr:\n', stde) mlog.debug('Compiler stderr:\n', p.stde)
p.input_name = srcname p.input_name = srcname
p.output_name = output p.output_name = output
yield p yield p
@ -748,14 +753,13 @@ int main () {{
# Add CFLAGS/CXXFLAGS/OBJCFLAGS/OBJCXXFLAGS from the env # Add CFLAGS/CXXFLAGS/OBJCFLAGS/OBJCXXFLAGS from the env
# We assume that the user has ensured these are compiler-specific # We assume that the user has ensured these are compiler-specific
args += env.coredata.external_args[self.language] args += env.coredata.external_args[self.language]
# We only want to compile; not link
args += self.get_compile_only_args()
# Append extra_args to the compiler check args such that it overrides # Append extra_args to the compiler check args such that it overrides
extra_args = self._override_args(self.get_compiler_check_args(), extra_args) extra_args = self._override_args(self.get_compiler_check_args(), extra_args)
extra_args = self.unix_link_flags_to_native(extra_args) extra_args = self.unix_link_flags_to_native(extra_args)
# Append both to the compiler args such that they override them # Append both to the compiler args such that they override them
args = self._override_args(args, extra_args) args = self._override_args(args, extra_args)
with self.compile(code, args) as p: # We only want to compile; not link
with self.compile(code, args, compile_only=True) as p:
return p.returncode == 0 return p.returncode == 0
def _links_wrapper(self, code, env, extra_args, dependencies): def _links_wrapper(self, code, env, extra_args, dependencies):
@ -1408,14 +1412,16 @@ class ValaCompiler(Compiler):
def get_output_args(self, target): def get_output_args(self, target):
return ['-o', target] return ['-o', target]
def get_compile_only_args(self):
return ['-C']
def get_werror_args(self): def get_werror_args(self):
return ['--fatal-warnings'] return ['--fatal-warnings']
def sanity_check(self, work_dir, environment): def sanity_check(self, work_dir, environment):
code = 'class MesonSanityCheck : Object { }' code = 'class MesonSanityCheck : Object { }'
args = self.get_cross_extra_flags(environment, compile=True, link=False) args = self.get_cross_extra_flags(environment, compile=True, link=False)
args += ['-C'] with self.compile(code, args, compile_only=True) as p:
with self.compile(code, args) as p:
if p.returncode != 0: if p.returncode != 0:
msg = 'Vala compiler {!r} can not compile programs' \ msg = 'Vala compiler {!r} can not compile programs' \
''.format(self.name_string()) ''.format(self.name_string())
@ -1435,8 +1441,8 @@ class ValaCompiler(Compiler):
code = 'class MesonFindLibrary : Object { }' code = 'class MesonFindLibrary : Object { }'
vapi_args = ['--pkg', libname] vapi_args = ['--pkg', libname]
args = self.get_cross_extra_flags(env, compile=True, link=False) args = self.get_cross_extra_flags(env, compile=True, link=False)
args += ['-C'] + vapi_args args += vapi_args
with self.compile(code, args) as p: with self.compile(code, args, compile_only=True) as p:
if p.returncode == 0: if p.returncode == 0:
return vapi_args return vapi_args
# Not found? Try to find the vapi file itself. # Not found? Try to find the vapi file itself.

Loading…
Cancel
Save