[pypy-svn] r59251 - pypy/branch/cbuild-refactor/pypy/translator/platform

fijal at codespeak.net fijal at codespeak.net
Mon Oct 20 10:42:33 CEST 2008


Author: fijal
Date: Mon Oct 20 10:42:32 2008
New Revision: 59251

Modified:
   pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py
   pypy/branch/cbuild-refactor/pypy/translator/platform/darwin.py
   pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py
   pypy/branch/cbuild-refactor/pypy/translator/platform/windows.py
Log:
shuffle stuff around not to pretend that every platform inherits from linux


Modified: pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py	Mon Oct 20 10:42:32 2008
@@ -49,10 +49,13 @@
     name = "abstract platform"
     
     def __init__(self, cc):
+        if self.__class__ is Platform:
+            raise TypeError("You should not instantiate Platform class directly")
         self.cc = cc
-    
+
     def compile(self, cfiles, eci, outputfilename=None, standalone=True):
-        raise NotImplementedError("Pure abstract baseclass")
+        ofiles = self._compile_o_files(cfiles, eci, standalone)
+        return self._finish_linking(ofiles, eci, outputfilename, standalone)
 
     def execute(self, executable, args=None, env=None):
         returncode, stdout, stderr = _run_subprocess(str(executable), args,
@@ -75,6 +78,42 @@
         return (self.__class__ is other.__class__ and
                 self.__dict__ == other.__dict__)
 
+    # some helpers which seem to be cross-platform enough
+
+    def _execute_c_compiler(self, cc, args, outname):
+        log.execute(cc + ' ' + ' '.join(args))
+        returncode, stdout, stderr = _run_subprocess(cc, args)
+        self._handle_error(returncode, stderr, stdout, outname)
+
+    def _handle_error(self, returncode, stderr, stdout, outname):
+        if returncode != 0:
+            errorfile = outname.new(ext='errors')
+            errorfile.write(stderr)
+            stderrlines = stderr.splitlines()
+            for line in stderrlines[:5]:
+                log.ERROR(line)
+            if len(stderrlines) > 5:
+                log.ERROR('...')
+            raise CompilationError(stdout, stderr)
+
+    
+    def _compile_args_from_eci(self, eci, standalone):
+        include_dirs = self._preprocess_dirs(eci.include_dirs)
+        args = self._includedirs(include_dirs)
+        if standalone:
+            extra = self.standalone_only
+        else:
+            extra = self.shared_only
+        cflags = self.cflags + extra
+        return (cflags + list(eci.compile_extra) + args)
+
+    def _link_args_from_eci(self, eci):
+        library_dirs = self._libdirs(eci.library_dirs)
+        libraries = self._libs(eci.libraries)
+        return (library_dirs + libraries + self.link_flags +
+                list(eci.link_extra))
+
+
     # below are some detailed informations for platforms
 
     def include_dirs_for_libffi(self):

Modified: pypy/branch/cbuild-refactor/pypy/translator/platform/darwin.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/platform/darwin.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/darwin.py	Mon Oct 20 10:42:32 2008
@@ -1,8 +1,8 @@
 
 import py, os
-from pypy.translator.platform import linux # xxx
+from pypy.translator.platform import posix
 
-class Darwin(linux.Linux): # xxx
+class Darwin(posix.BasePosix):
     name = "darwin"
     
     link_flags = []

Modified: pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py	Mon Oct 20 10:42:32 2008
@@ -3,96 +3,9 @@
 from pypy.translator.platform import Platform, CompilationError, ExecutionResult
 from pypy.translator.platform import log, _run_subprocess
 from pypy.tool import autopath
+from pypy.translator.platform.posix import GnuMakefile, BasePosix
 
-class Definition(object):
-    def __init__(self, name, value):
-        self.name = name
-        self.value = value
-
-    def write(self, f):
-        def write_list(prefix, lst):
-            for i, fn in enumerate(lst):
-                print >> f, prefix, fn,
-                if i < len(lst)-1:
-                    print >> f, '\\'
-                else:
-                    print >> f
-                prefix = ' ' * len(prefix)
-        name, value = self.name, self.value
-        if isinstance(value, str):
-            f.write('%s = %s\n' % (name, value))
-        else:
-            write_list('%s =' % (name,), value)
-        if value:
-            f.write('\n')
-        
-class Rule(object):
-    def __init__(self, target, deps, body):
-        self.target = target
-        self.deps   = deps
-        self.body   = body
-
-    def write(self, f):
-        target, deps, body = self.target, self.deps, self.body
-        if isinstance(deps, str):
-            dep_s = deps
-        else:
-            dep_s = ' '.join(deps)
-        f.write('%s: %s\n' % (target, dep_s))
-        if isinstance(body, str):
-            f.write('\t%s\n' % body)
-        elif body:
-            f.write('\t%s\n' % '\n\t'.join(body))
-        f.write('\n')
-
-class Comment(object):
-    def __init__(self, body):
-        self.body = body
-
-    def write(self, f):
-        f.write('# %s\n' % (self.body,))
-
-class GnuMakefile(object):
-    def __init__(self, path=None):
-        self.defs = {}
-        self.lines = []
-        self.makefile_dir = py.path.local(path)
-        
-    def pathrel(self, fpath):
-        if fpath.dirpath() == self.makefile_dir:
-            return fpath.basename
-        elif fpath.dirpath().dirpath() == self.makefile_dir.dirpath():
-            return '../' + fpath.relto(self.makefile_dir.dirpath())
-        else:
-            return str(fpath)
-
-    def definition(self, name, value):
-        defs = self.defs
-        defn = Definition(name, value)
-        if name in defs:
-            self.lines[defs[name]] = defn
-        else:
-            defs[name] = len(self.lines)
-            self.lines.append(defn)
-
-    def rule(self, target, deps, body):
-        self.lines.append(Rule(target, deps, body))
-
-    def comment(self, body):
-        self.lines.append(Comment(body))
-
-    def write(self, out=None):
-        if out is None:
-            f = self.makefile_dir.join('Makefile').open('w')
-        else:
-            f = out
-        for line in self.lines:
-            line.write(f)
-        f.flush()
-        if out is None:
-            f.close()
-
-class Linux(Platform):
+class Linux(BasePosix):
     name = "linux"
     
     link_flags = ['-pthread']
@@ -102,11 +15,6 @@
     so_ext = 'so'
     exe_ext = ''
     
-    def __init__(self, cc=None):
-        if cc is None:
-            cc = 'gcc'
-        self.cc = cc
-
     def _libs(self, libraries):
         return ['-l%s' % (lib,) for lib in libraries]
 
@@ -163,37 +71,7 @@
             exe_name += '.' + self.so_ext
         return self._link(self.cc, ofiles, self._link_args_from_eci(eci),
                           standalone, exe_name)
-
-    def _compile_c_file(self, cc, cfile, compile_args):
-        oname = cfile.new(ext='o')
-        args = ['-c'] + compile_args + [str(cfile), '-o', str(oname)]
-        self._execute_c_compiler(cc, args, oname)
-        return oname
-
-    def _execute_c_compiler(self, cc, args, outname):
-        log.execute(cc + ' ' + ' '.join(args))
-        returncode, stdout, stderr = _run_subprocess(cc, args)
-        self._handle_error(returncode, stderr, stdout, outname)
-
-    def _handle_error(self, returncode, stderr, stdout, outname):
-        if returncode != 0:
-            errorfile = outname.new(ext='errors')
-            errorfile.write(stderr)
-            stderrlines = stderr.splitlines()
-            for line in stderrlines[:5]:
-                log.ERROR(line)
-            if len(stderrlines) > 5:
-                log.ERROR('...')
-            raise CompilationError(stdout, stderr)
         
-    def _link(self, cc, ofiles, link_args, standalone, exe_name):
-        args = [str(ofile) for ofile in ofiles] + link_args
-        args += ['-o', str(exe_name)]
-        if not standalone:
-            args = self._args_for_shared(args)
-        self._execute_c_compiler(cc, args, exe_name)
-        return exe_name
-
     def gen_makefile(self, cfiles, eci, exe_name=None, path=None):
         cfiles = [py.path.local(f) for f in cfiles]
         cfiles += [py.path.local(f) for f in eci.separate_module_files]

Modified: pypy/branch/cbuild-refactor/pypy/translator/platform/windows.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/platform/windows.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/windows.py	Mon Oct 20 10:42:32 2008
@@ -1,8 +1,8 @@
 
 import py, os, sys
-from pypy.translator.platform import linux # xxx
 from pypy.translator.platform import CompilationError, ExecutionResult
 from pypy.translator.platform import log, _run_subprocess
+from pypy.translator.platform import Platform, posix
 from pypy.tool import autopath
 
 def _install_msvc_env():
@@ -44,7 +44,7 @@
     traceback.print_exc()
     # Assume that the compiler is already part of the environment
 
-class Windows(linux.Linux): # xxx
+class Windows(Platform):
     name = "win32"
     so_ext = 'dll'
     exe_ext = 'exe'
@@ -82,6 +82,15 @@
         self._execute_c_compiler(cc, args, oname)
         return oname
 
+    def _compile_o_files(self, cfiles, eci, standalone=True):
+        cfiles = [py.path.local(f) for f in cfiles]
+        cfiles += [py.path.local(f) for f in eci.separate_module_files]
+        compile_args = self._compile_args_from_eci(eci, standalone)
+        ofiles = []
+        for cfile in cfiles:
+            ofiles.append(self._compile_c_file(self.cc, cfile, compile_args))
+        return ofiles
+
     def _link(self, cc, ofiles, link_args, standalone, exe_name):
         args = ['/nologo'] + [str(ofile) for ofile in ofiles] + link_args
         args += ['/out:%s' % (exe_name,)]
@@ -90,6 +99,18 @@
         self._execute_c_compiler(self.link, args, exe_name)
         return exe_name
 
+    def _finish_linking(self, ofiles, eci, outputfilename, standalone):
+        if outputfilename is None:
+            outputfilename = ofiles[0].purebasename
+        exe_name = py.path.local(os.path.join(str(ofiles[0].dirpath()),
+                                              outputfilename))
+        if standalone:
+            exe_name += '.' + self.exe_ext
+        else:
+            exe_name += '.' + self.so_ext
+        return self._link(self.cc, ofiles, self._link_args_from_eci(eci),
+                          standalone, exe_name)
+
     def _handle_error(self, returncode, stderr, stdout, outname):
         if returncode != 0:
             # Microsoft compilers write compilation errors to stdout
@@ -178,5 +199,5 @@
             
         self._handle_error(returncode, stdout, stderr, path.join('make'))
 
-class NMakefile(linux.GnuMakefile):
+class NMakefile(posix.GnuMakefile):
     pass # for the moment



More information about the Pypy-commit mailing list