def try_compile_cache(c_files, eci): path = cache_file_path(c_files, eci, 'try_compile_cache') try: return eval(path.read()) except py.error.Error: try: build_executable(c_files, eci) result = True except (distutils.errors.CompileError, distutils.errors.LinkError): result = False path.write(repr(result)) return result
def try_compile_cache(c_files, eci): path = cache_file_path(c_files, eci, "try_compile_cache") try: return eval(path.read()) except py.error.Error: try: build_executable(c_files, eci) result = True except (distutils.errors.CompileError, distutils.errors.LinkError): result = False path.write(repr(result)) return result
def try_compile_cache(c_files, eci): path = cache_file_path(c_files, eci, 'try_compile_cache') try: data = path.read() except py.error.Error: data = '' if not (data.startswith('True') or data.startswith('FAIL\n')): try: build_executable(c_files, eci) data = 'True' except CompilationError, e: data = 'FAIL\n%s\n' % (e,) path.write(data)
def build_executable_cache(c_files, eci): path = cache_file_path(c_files, eci, 'build_executable_cache') try: return path.read() except py.error.Error: result = py.process.cmdexec(build_executable(c_files, eci)) path.write(result) return result
def build_executable_cache(c_files, eci): path = cache_file_path(c_files, eci, "build_executable_cache") try: return path.read() except py.error.Error: result = py.process.cmdexec(build_executable(c_files, eci)) path.write(result) return result
def test_simple_executable(): print udir testpath = udir.join('testbuildexec') t = testpath.ensure("test.c") t.write(r""" #include <stdio.h> int main() { printf("hello world\n"); return 0; } """) testexec = build_executable([t]) out = py.process.cmdexec(testexec) assert out.startswith('hello world')
def test_simple_executable(): print udir testpath = udir.join('testbuildexec') t = testpath.ensure("test.c") t.write(r""" #include <stdio.h> int main() { printf("hello world\n"); return 0; } """) eci = ExternalCompilationInfo() testexec = build_executable([t], eci) out = py.process.cmdexec(testexec) assert out.startswith('hello world')
def run_example_code(filepath, include_dirs=[]): executable = build_executable([filepath], include_dirs=include_dirs) output = py.process.cmdexec(executable) section = None for line in output.splitlines(): line = line.strip() if line.startswith('-+- '): # start of a new section section = {} elif line == '---': # section end assert section is not None yield section section = None elif line: assert section is not None key, value = line.split(': ') section[key] = int(value)
def test_standalone(self): tmpdir = self.tmpdir c_file = tmpdir.join('stand1.c') c_file.write(''' #include <math.h> #include <stdio.h> int main() { printf("%f\\n", pow(2.0, 2.0)); }''') eci = ExternalCompilationInfo( libraries = ['m'], ) output = build_executable([c_file], eci) p = Popen(output, stdout=PIPE, stderr=STDOUT) p.wait() assert p.stdout.readline().startswith('4.0')
def test_standalone_maemo(self): # XXX skip if there is no scratchbox if not py.path.local('/scratchbox/login').check(): py.test.skip("No scratchbox detected") tmpdir = self.tmpdir c_file = tmpdir.join('stand1.c') c_file.write(''' #include <math.h> #include <stdio.h> int main() { printf("%f\\n", pow(2.0, 2.0)); return 0; }''') if sys.platform == 'win32': py.test.skip("No cross-compilation on windows yet") else: eci = ExternalCompilationInfo(platform='maemo', libraries=['m']) output = build_executable([c_file], eci) py.test.raises(py.process.cmdexec.Error, py.process.cmdexec, output) result = py.process.cmdexec(eci.get_emulator_for_platform() + output) assert result.startswith('4.0')
def sizeof_c_type(c_typename, includes={}, compiler_exe=None): from py.compat.subprocess import PIPE, Popen includes['stdio.h'] = True includes['sys' + os.path.sep + 'types.h'] = True include_string = "\n".join(["#include <%s>" % i for i in includes.keys()]) c_source = py.code.Source(''' // includes %s // checking code int main(void) { printf("%%d\\n", sizeof(%s)); return (0); } ''' % (include_string, c_typename)) c_file = udir.join("typetest.c") c_file.write(c_source) c_exec = build_executable([str(c_file)], compiler_exe=compiler_exe) pipe = Popen(c_exec, stdout=PIPE) pipe.wait() return int(pipe.stdout.read()) * 8