Merge branch 'QuLogic-compiler-file-checks'

pull/898/head
Jussi Pakkanen 8 years ago
commit 95a99682b0
  1. 150
      mesonbuild/compilers.py
  2. 18
      mesonbuild/interpreter.py
  3. 2
      test cases/common/33 try compile/invalid.c
  4. 8
      test cases/common/33 try compile/meson.build
  5. 2
      test cases/common/33 try compile/valid.c
  6. 3
      test cases/common/39 tryrun/error.c
  7. 74
      test cases/common/39 tryrun/meson.build
  8. 1
      test cases/common/39 tryrun/no_compile.c
  9. 6
      test cases/common/39 tryrun/ok.c

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import contextlib
import subprocess, os.path import subprocess, os.path
import tempfile import tempfile
from .import mesonlib from .import mesonlib
@ -643,23 +644,50 @@ int main () {{ {1}; }}'''
args = extra_args + self.get_no_optimization_args() args = extra_args + self.get_no_optimization_args()
return self.compiles(templ.format(hname, symbol, prefix), env, args, dependencies) return self.compiles(templ.format(hname, symbol, prefix), env, args, dependencies)
def compile(self, code, srcname, extra_args=None): @contextlib.contextmanager
def compile(self, code, extra_args=None):
if extra_args is None: if extra_args is None:
extra_args = [] extra_args = []
commands = self.get_exelist()
commands.append(srcname) try:
commands += extra_args with tempfile.TemporaryDirectory() as tmpdirname:
mlog.debug('Running compile:') if isinstance(code, str):
mlog.debug('Command line: ', ' '.join(commands), '\n') srcname = os.path.join(tmpdirname,
mlog.debug('Code:\n', code) 'testfile.' + self.default_suffix)
p = subprocess.Popen(commands, cwd=os.path.split(srcname)[0], stdout=subprocess.PIPE, stderr=subprocess.PIPE) with open(srcname, 'w') as ofile:
(stde, stdo) = p.communicate() ofile.write(code)
stde = stde.decode() elif isinstance(code, mesonlib.File):
stdo = stdo.decode() srcname = code.fname
mlog.debug('Compiler stdout:\n', stdo)
mlog.debug('Compiler stderr:\n', stde) # Extension only matters if running results; '.exe' is
os.remove(srcname) # guaranteed to be executable on every platform.
return p output = os.path.join(tmpdirname, 'output.exe')
commands = self.get_exelist()
commands.append(srcname)
commands += extra_args
commands += self.get_output_args(output)
mlog.debug('Running compile:')
mlog.debug('Working directory: ', tmpdirname)
mlog.debug('Command line: ', ' '.join(commands), '\n')
mlog.debug('Code:\n', code)
p = subprocess.Popen(commands, cwd=tmpdirname,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(stde, stdo) = p.communicate()
stde = stde.decode()
stdo = stdo.decode()
mlog.debug('Compiler stdout:\n', stdo)
mlog.debug('Compiler stderr:\n', stde)
p.input_name = srcname
p.output_name = output
yield p
except (PermissionError, OSError):
# On Windows antivirus programs and the like hold on to files so
# they can't be deleted. There's not much to do in this case. Also,
# catch OSError because the directory is then no longer empty.
pass
def compiles(self, code, env, extra_args=None, dependencies=None): def compiles(self, code, env, extra_args=None, dependencies=None):
if extra_args is None: if extra_args is None:
@ -670,11 +698,6 @@ int main () {{ {1}; }}'''
dependencies = [] dependencies = []
elif not isinstance(dependencies, list): elif not isinstance(dependencies, list):
dependencies = [dependencies] dependencies = [dependencies]
suflen = len(self.default_suffix)
(fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix)
os.close(fd)
with open(srcname, 'w') as ofile:
ofile.write(code)
cargs = [a for d in dependencies for a in d.get_compile_args()] cargs = [a for d in dependencies for a in d.get_compile_args()]
# Convert flags to the native type of the selected compiler # Convert flags to the native type of the selected compiler
args = self.unix_link_flags_to_native(cargs + extra_args) args = self.unix_link_flags_to_native(cargs + extra_args)
@ -682,17 +705,8 @@ int main () {{ {1}; }}'''
args += self.get_cross_extra_flags(env, compile=True, link=False) args += self.get_cross_extra_flags(env, compile=True, link=False)
# We only want to compile; not link # We only want to compile; not link
args += self.get_compile_only_args() args += self.get_compile_only_args()
p = self.compile(code, srcname, args) with self.compile(code, args) as p:
try: return p.returncode == 0
trial = srcname[:-suflen] + 'o'
os.remove(trial)
except FileNotFoundError:
pass
try:
os.remove(srcname[:-suflen] + 'obj')
except FileNotFoundError:
pass
return p.returncode == 0
def links(self, code, env, extra_args=None, dependencies=None): def links(self, code, env, extra_args=None, dependencies=None):
if extra_args is None: if extra_args is None:
@ -703,12 +717,6 @@ int main () {{ {1}; }}'''
dependencies = [] dependencies = []
elif not isinstance(dependencies, list): elif not isinstance(dependencies, list):
dependencies = [dependencies] dependencies = [dependencies]
(fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix)
os.close(fd)
(fd, dstname) = tempfile.mkstemp()
os.close(fd)
with open(srcname, 'w') as ofile:
ofile.write(code)
cargs = [a for d in dependencies for a in d.get_compile_args()] cargs = [a for d in dependencies for a in d.get_compile_args()]
link_args = [a for d in dependencies for a in d.get_link_args()] link_args = [a for d in dependencies for a in d.get_link_args()]
# Convert flags to the native type of the selected compiler # Convert flags to the native type of the selected compiler
@ -717,14 +725,8 @@ int main () {{ {1}; }}'''
args += self.get_linker_debug_crt_args() args += self.get_linker_debug_crt_args()
# Read c_args/c_link_args/cpp_args/cpp_link_args/etc from the cross-info file (if needed) # Read c_args/c_link_args/cpp_args/cpp_link_args/etc from the cross-info file (if needed)
args += self.get_cross_extra_flags(env, compile=True, link=True) args += self.get_cross_extra_flags(env, compile=True, link=True)
# Arguments specifying the output filename with self.compile(code, args) as p:
args += self.get_output_args(dstname) return p.returncode == 0
p = self.compile(code, srcname, args)
try:
os.remove(dstname)
except FileNotFoundError:
pass
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 extra_args is None: if extra_args is None:
@ -735,10 +737,6 @@ int main () {{ {1}; }}'''
dependencies = [dependencies] dependencies = [dependencies]
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.')
(fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix)
os.close(fd)
with open(srcname, 'w') as ofile:
ofile.write(code)
cargs = [a for d in dependencies for a in d.get_compile_args()] cargs = [a for d in dependencies for a in d.get_compile_args()]
link_args = [a for d in dependencies for a in d.get_link_args()] link_args = [a for d in dependencies for a in d.get_link_args()]
# Convert flags to the native type of the selected compiler # Convert flags to the native type of the selected compiler
@ -747,48 +745,30 @@ int main () {{ {1}; }}'''
args += self.get_linker_debug_crt_args() args += self.get_linker_debug_crt_args()
# Read c_link_args/cpp_link_args/etc from the cross-info file # Read c_link_args/cpp_link_args/etc from the cross-info file
args += self.get_cross_extra_flags(env, compile=True, link=True) args += self.get_cross_extra_flags(env, compile=True, link=True)
# Create command list with self.compile(code, args) as p:
exename = srcname + '.exe' # Is guaranteed to be executable on every platform. if p.returncode != 0:
commands = self.get_exelist() + args mlog.debug('Could not compile test file %s: %d\n' % (
commands.append(srcname) p.input_name,
commands += self.get_output_args(exename) p.returncode))
mlog.debug('Running code:\n\n', code) return RunResult(False)
mlog.debug('Command line:', ' '.join(commands)) if self.is_cross:
p = subprocess.Popen(commands, cwd=os.path.split(srcname)[0], stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmdlist = self.exe_wrapper + [p.output_name]
(stdo, stde) = p.communicate() else:
stde = stde.decode() cmdlist = p.output_name
stdo = stdo.decode() try:
mlog.debug('Compiler stdout:\n') pe = subprocess.Popen(cmdlist, stdout=subprocess.PIPE,
mlog.debug(stdo) stderr=subprocess.PIPE)
mlog.debug('Compiler stderr:\n') except Exception as e:
mlog.debug(stde) mlog.debug('Could not run: %s (error: %s)\n' % (cmdlist, e))
os.remove(srcname) return RunResult(False)
if p.returncode != 0:
return RunResult(False) (so, se) = pe.communicate()
if self.is_cross:
cmdlist = self.exe_wrapper + [exename]
else:
cmdlist = exename
try:
pe = subprocess.Popen(cmdlist, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except Exception as e:
mlog.debug('Could not run: %s (error: %s)\n' % (cmdlist, e))
return RunResult(False)
(so, se) = pe.communicate()
so = so.decode() so = so.decode()
se = se.decode() se = se.decode()
mlog.debug('Program stdout:\n') mlog.debug('Program stdout:\n')
mlog.debug(so) mlog.debug(so)
mlog.debug('Program stderr:\n') mlog.debug('Program stderr:\n')
mlog.debug(se) mlog.debug(se)
try:
os.remove(exename)
except PermissionError:
# On Windows antivirus programs and the like hold
# on to files so they can't be deleted. There's not
# much to do in this case.
pass
return RunResult(True, pe.returncode, so, se) return RunResult(True, pe.returncode, so, se)
def cross_sizeof(self, element, prefix, env, extra_args=None, dependencies=None): def cross_sizeof(self, element, prefix, env, extra_args=None, dependencies=None):

@ -699,8 +699,12 @@ class CompilerHolder(InterpreterObject):
def run_method(self, args, kwargs): def run_method(self, args, kwargs):
if len(args) != 1: if len(args) != 1:
raise InterpreterException('Run method takes exactly one positional argument.') raise InterpreterException('Run method takes exactly one positional argument.')
check_stringlist(args)
code = args[0] code = args[0]
if isinstance(code, mesonlib.File):
code = mesonlib.File.from_absolute_file(
code.rel_to_builddir(self.environment.source_dir))
elif not isinstance(code, str):
raise InvalidArguments('Argument must be string or file.')
testname = kwargs.get('name', '') testname = kwargs.get('name', '')
if not isinstance(testname, str): if not isinstance(testname, str):
raise InterpreterException('Testname argument must be a string.') raise InterpreterException('Testname argument must be a string.')
@ -820,8 +824,12 @@ class CompilerHolder(InterpreterObject):
def compiles_method(self, args, kwargs): def compiles_method(self, args, kwargs):
if len(args) != 1: if len(args) != 1:
raise InterpreterException('compiles method takes exactly one argument.') raise InterpreterException('compiles method takes exactly one argument.')
check_stringlist(args)
code = args[0] code = args[0]
if isinstance(code, mesonlib.File):
code = mesonlib.File.from_absolute_file(
code.rel_to_builddir(self.environment.source_dir))
elif not isinstance(code, str):
raise InvalidArguments('Argument must be string or file.')
testname = kwargs.get('name', '') testname = kwargs.get('name', '')
if not isinstance(testname, str): if not isinstance(testname, str):
raise InterpreterException('Testname argument must be a string.') raise InterpreterException('Testname argument must be a string.')
@ -839,8 +847,12 @@ class CompilerHolder(InterpreterObject):
def links_method(self, args, kwargs): def links_method(self, args, kwargs):
if len(args) != 1: if len(args) != 1:
raise InterpreterException('links method takes exactly one argument.') raise InterpreterException('links method takes exactly one argument.')
check_stringlist(args)
code = args[0] code = args[0]
if isinstance(code, mesonlib.File):
code = mesonlib.File.from_absolute_file(
code.rel_to_builddir(self.environment.source_dir))
elif not isinstance(code, str):
raise InvalidArguments('Argument must be string or file.')
testname = kwargs.get('name', '') testname = kwargs.get('name', '')
if not isinstance(testname, str): if not isinstance(testname, str):
raise InterpreterException('Testname argument must be a string.') raise InterpreterException('Testname argument must be a string.')

@ -0,0 +1,2 @@
#include<nonexisting.h>
void func() { printf("This won't work.\n"); }

@ -13,6 +13,14 @@ if compiler.compiles(code, name : 'should succeed') == false
error('Compiler is fail.') error('Compiler is fail.')
endif endif
if compiler.compiles(files('valid.c'), name : 'should succeed') == false
error('Compiler is fail.')
endif
if compiler.compiles(breakcode, name : 'should fail') if compiler.compiles(breakcode, name : 'should fail')
error('Compiler returned true on broken code.') error('Compiler returned true on broken code.')
endif endif
if compiler.compiles(files('invalid.c'), name : 'should fail')
error('Compiler returned true on broken code.')
endif

@ -0,0 +1,2 @@
#include<stdio.h>
void func() { printf("Something.\n"); }

@ -0,0 +1,3 @@
int main(int argc, char **argv) {
return 1;
}

@ -27,42 +27,50 @@ error_code = '''int main(int argc, char **argv) {
no_compile_code = '''int main(int argc, char **argv) { no_compile_code = '''int main(int argc, char **argv) {
''' '''
ok = cc.run(ok_code, name : 'should succeed') INPUTS = [
err = cc.run(error_code, name : 'should fail') ['String', ok_code, error_code, no_compile_code],
noc = cc.run(no_compile_code, name : 'does not compile') ['File', files('ok.c'), files('error.c'), files('no_compile.c')],
]
if noc.compiled() foreach input : INPUTS
error('Compilation fail test failed.') type = input[0]
else ok = cc.run(input[1], name : type + ' should succeed')
message('Fail detected properly.') err = cc.run(input[2], name : type + ' should fail')
endif noc = cc.run(input[3], name : type + ' does not compile')
if ok.compiled() if noc.compiled()
message('Compilation worked.') error(type + ' compilation fail test failed.')
else else
error('Compilation did not work.') message(type + ' fail detected properly.')
endif endif
if ok.returncode() == 0 if ok.compiled()
message('Return code ok.') message(type + ' compilation worked.')
else else
error('Return code fail') error(type + ' compilation did not work.')
endif endif
if err.returncode() == 1 if ok.returncode() == 0
message('Bad return code ok.') message(type + ' return code ok.')
else else
error('Bad return code fail.') error(type + ' return code fail')
endif endif
if ok.stdout().strip() == 'stdout' if err.returncode() == 1
message('Stdout ok.') message(type + ' bad return code ok.')
else else
message('Bad stdout.') error(type + ' bad return code fail.')
endif endif
if ok.stderr().strip() == 'stderr' if ok.stdout().strip() == 'stdout'
message('Stderr ok.') message(type + ' stdout ok.')
else else
message('Bad stderr.') message(type + ' bad stdout.')
endif endif
if ok.stderr().strip() == 'stderr'
message(type + ' stderr ok.')
else
message(type + ' bad stderr.')
endif
endforeach

@ -0,0 +1 @@
int main(int argc, char **argv) {

@ -0,0 +1,6 @@
#include<stdio.h>
int main(int argc, char **argv) {
printf("%s\n", "stdout");
fprintf(stderr, "%s\n", "stderr");
return 0;
}
Loading…
Cancel
Save