[pypy-svn] r58906 - in pypy/branch/cbuild-refactor/pypy/translator/platform: . test

fijal at codespeak.net fijal at codespeak.net
Fri Oct 10 14:57:48 CEST 2008


Author: fijal
Date: Fri Oct 10 14:57:48 2008
New Revision: 58906

Added:
   pypy/branch/cbuild-refactor/pypy/translator/platform/   (props changed)
   pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py   (contents, props changed)
   pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py   (contents, props changed)
   pypy/branch/cbuild-refactor/pypy/translator/platform/test/   (props changed)
   pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py   (contents, props changed)
Log:
(fijal, pedronis)
Starting target platform abstraction.


Added: pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py
==============================================================================
--- (empty file)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py	Fri Oct 10 14:57:48 2008
@@ -0,0 +1,36 @@
+
+""" Platform object that allows you to compile/execute C sources for given
+platform.
+"""
+
+import sys
+
+class CompilationError(Exception):
+    def __init__(self, out, err):
+        self.out = out
+        self.err = err
+
+class ExecutionResult(object):
+    def __init__(self, returncode, out, err):
+        self.returncode = returncode
+        self.out = out
+        self.err = err
+
+class Platform(object):
+    def __init__(self, cc):
+        self.cc = cc
+    
+    def compile(self, cfiles, eci):
+        raise NotImplementedError("Pure abstract baseclass")
+
+    def execute(self, file_to_exec):
+        raise NotImplementedError("Pure abstract baseclass")
+
+    def __repr__(self):
+        return '<%s cc=%s>' % (self.__class__.__name__, self.cc)
+
+if sys.platform == 'linux2':
+    from pypy.translator.platform.linux import Linux
+    host = Linux()
+else:
+    xxx

Added: pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py
==============================================================================
--- (empty file)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py	Fri Oct 10 14:57:48 2008
@@ -0,0 +1,29 @@
+
+import py
+from pypy.translator.platform import Platform, CompilationError, ExecutionResult
+from subprocess import PIPE, Popen
+
+def _run_subprocess(args):
+    pipe = Popen(args, executable=args[0],
+                 stdout=PIPE, stderr=PIPE, shell=False)
+    stdout, stderr = pipe.communicate()
+    return pipe.returncode, stdout, stderr
+
+class Linux(Platform):
+    def __init__(self, cc='gcc'):
+        self.cc = cc
+
+    def compile(self, cfiles, eci):
+        cfiles = [py.path.local(f) for f in cfiles]
+        # XXX ignore eci
+        args = [self.cc] + [str(f) for f in cfiles]
+        exe_name = cfiles[0].dirpath().join(cfiles[0].purebasename)
+        args += ['-o', str(exe_name)]
+        returncode, stdout, stderr = _run_subprocess(args)
+        if returncode != 0:
+            raise CompilationError(stdout, stderr)
+        return exe_name
+
+    def execute(self, executable):
+        returncode, stdout, stderr = _run_subprocess([str(executable)])
+        return ExecutionResult(returncode, stdout, stderr)

Added: pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py
==============================================================================
--- (empty file)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py	Fri Oct 10 14:57:48 2008
@@ -0,0 +1,19 @@
+
+from pypy.tool.udir import udir
+from pypy.translator.platform import host
+
+def test_simple_enough():
+    cfile = udir.join('test_simple_enough.c')
+    tmpdir = cfile.write('''
+    #include <stdio.h>
+    int main()
+    {
+        printf("42\\n");
+        return 0;
+    }
+    ''')
+    executable = host.compile([cfile], None)
+    res = host.execute(executable)
+    assert res.out == '42\n'
+    assert res.err == ''
+    assert res.returncode == 0



More information about the Pypy-commit mailing list