[pypy-svn] r58958 - in pypy/branch/cbuild-refactor/pypy: rpython/tool tool tool/test translator translator/c/test translator/goal translator/platform translator/platform/test translator/tool translator/tool/test

fijal at codespeak.net fijal at codespeak.net
Sat Oct 11 12:32:22 CEST 2008


Author: fijal
Date: Sat Oct 11 12:32:21 2008
New Revision: 58958

Modified:
   pypy/branch/cbuild-refactor/pypy/rpython/tool/rffi_platform.py
   pypy/branch/cbuild-refactor/pypy/tool/gcc_cache.py
   pypy/branch/cbuild-refactor/pypy/tool/test/test_gcc_cache.py
   pypy/branch/cbuild-refactor/pypy/translator/c/test/test_boehm.py
   pypy/branch/cbuild-refactor/pypy/translator/c/test/test_stackless.py
   pypy/branch/cbuild-refactor/pypy/translator/driver.py
   pypy/branch/cbuild-refactor/pypy/translator/goal/translate.py
   pypy/branch/cbuild-refactor/pypy/translator/platform/__init__.py
   pypy/branch/cbuild-refactor/pypy/translator/platform/linux.py
   pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py
   pypy/branch/cbuild-refactor/pypy/translator/tool/cbuild.py
   pypy/branch/cbuild-refactor/pypy/translator/tool/test/test_cbuild.py
Log:
(pedronis, fijal) Shuffle stuff around, shave some more yaks


Modified: pypy/branch/cbuild-refactor/pypy/rpython/tool/rffi_platform.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/rpython/tool/rffi_platform.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/rpython/tool/rffi_platform.py	Sat Oct 11 12:32:21 2008
@@ -561,6 +561,21 @@
     gcv = sysconfig.get_config_vars()
     return gcv['INCLUDEPY']
 
+def check_boehm(platform=None, cache={}):
+    if platform is None:
+        from pypy.translator.platform import platform
+    try:
+        return cache[platform]
+    except KeyError:
+        class CConfig:
+            _compilation_info_ = ExternalCompilationInfo(
+                includes=['gc/gc.h'],
+                platform=platform,
+                )
+            HAS = Has('GC_init')
+        cache[platform] = configure(CConfig)['HAS']
+        return cache[platform]
+
 if __name__ == '__main__':
     doc = """Example:
     

Modified: pypy/branch/cbuild-refactor/pypy/tool/gcc_cache.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/tool/gcc_cache.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/tool/gcc_cache.py	Sat Oct 11 12:32:21 2008
@@ -1,8 +1,7 @@
 
 from pypy.tool.autopath import pypydir
-from pypy.translator.tool.cbuild import build_executable
+from pypy.translator.platform import platform, CompilationError
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.translator.tool.cbuild import CompilationError
 from pypy.tool.compat import md5
 import py
 
@@ -20,9 +19,9 @@
     try:
         return path.read()
     except py.error.Error:
-        result = eci.platform.execute(build_executable(c_files, eci))
-        path.write(result)
-        return result
+        result = platform.execute(platform.compile(c_files, eci))
+        path.write(result.out)
+        return result.out
 
 def try_compile_cache(c_files, eci):
     path = cache_file_path(c_files, eci, 'try_compile_cache')
@@ -32,7 +31,7 @@
         data = ''
     if not (data.startswith('True') or data.startswith('FAIL\n')):
         try:
-            build_executable(c_files, eci)
+            platform.compile(c_files, eci)
             data = 'True'
         except CompilationError, e:
             data = 'FAIL\n%s\n' % (e,)
@@ -42,4 +41,4 @@
     else:
         assert data.startswith('FAIL\n')
         msg = data[len('FAIL\n'):]
-        raise CompilationError(msg.strip())
+        raise CompilationError(msg.strip(), '')

Modified: pypy/branch/cbuild-refactor/pypy/tool/test/test_gcc_cache.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/tool/test/test_gcc_cache.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/tool/test/test_gcc_cache.py	Sat Oct 11 12:32:21 2008
@@ -24,7 +24,8 @@
     path = cache_file_path([f], eci, 'build_executable_cache')
     if path.check():
         path.remove()
-    assert build_executable_cache([f], eci) == "3\n"
+    res = build_executable_cache([f], eci)
+    assert res == "3\n"
     assert build_executable_cache([f], eci) == "3\n"
     eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
     assert build_executable_cache([f], eci2) == "42\n"

Modified: pypy/branch/cbuild-refactor/pypy/translator/c/test/test_boehm.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/c/test/test_boehm.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/c/test/test_boehm.py	Sat Oct 11 12:32:21 2008
@@ -2,12 +2,12 @@
 from pypy.translator.translator import TranslationContext
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.memory.test import snippet
-from pypy.translator.tool.cbuild import check_boehm_presence
+from pypy.rpython.tool.rffi_platform import check_boehm
 from pypy.translator.c.genc import CExtModuleBuilder
 from pypy import conftest
 
 def setup_module(mod):
-    if not check_boehm_presence():
+    if not check_boehm():
         py.test.skip("Boehm GC not present")
 
 class AbstractGCTestClass(object):

Modified: pypy/branch/cbuild-refactor/pypy/translator/c/test/test_stackless.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/c/test/test_stackless.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/c/test/test_stackless.py	Sat Oct 11 12:32:21 2008
@@ -20,8 +20,8 @@
             import py
             py.test.skip("stackless + refcounting doesn't work any more for now")
         elif cls.gcpolicy == "boehm":
-            from pypy.translator.tool.cbuild import check_boehm_presence
-            if not check_boehm_presence():
+            from pypy.rpython.tool.rffi_platform import check_boehm
+            if not check_boehm():
                 py.test.skip("Boehm GC not present")
 
     def wrap_stackless_function(self, fn):

Modified: pypy/branch/cbuild-refactor/pypy/translator/driver.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/driver.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/driver.py	Sat Oct 11 12:32:21 2008
@@ -457,13 +457,10 @@
 
     def possibly_check_for_boehm(self):
         if self.config.translation.gc == "boehm":
-            from pypy.translator.tool.cbuild import check_boehm_presence
-            from pypy.translator.tool.cbuild import CompilationError
-            try:
-                check_boehm_presence(noerr=False)
-            except CompilationError, e:
+            from pypy.rpython.tool.rffi_platform import check_boehm
+            if not check_boehm(self.translator.platform):
                 i = 'Boehm GC not installed.  Try e.g. "translate.py --gc=hybrid"'
-                raise CompilationError('%s\n--------------------\n%s' % (e, i))
+                raise Exception(i)
 
     def task_database_c(self):
         translator = self.translator
@@ -516,7 +513,7 @@
         if self.exe_name is not None:
             exename = mkexename(self.c_entryp)
             newexename = self.compute_exe_name()
-            shutil.copy(exename, newexename)
+            shutil.copy(str(exename), str(newexename))
             self.c_entryp = newexename
         self.log.info("created: %s" % (self.c_entryp,))
 

Modified: pypy/branch/cbuild-refactor/pypy/translator/goal/translate.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/goal/translate.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/goal/translate.py	Sat Oct 11 12:32:21 2008
@@ -163,7 +163,7 @@
                 translating=True)
 
     # apply the platform settings
-    set_platform(config, translateconfig.platform)
+    set_platform(translateconfig, config)
 
     # apply the optimization level settings
     set_opt_level(config, translateconfig.opt)

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	Sat Oct 11 12:32:21 2008
@@ -3,7 +3,11 @@
 platform.
 """
 
-import sys
+import sys, py
+
+from pypy.tool.ansi_print import ansi_log
+log = py.log.Producer("platform")
+py.log.setconsumer("platform", ansi_log)
 
 class CompilationError(Exception):
     def __init__(self, out, err):
@@ -32,11 +36,32 @@
     def __repr__(self):
         return '<%s cc=%s>' % (self.__class__.__name__, self.cc)
 
+    def __hash__(self):
+        return hash(self.__class__.__name__)
+
+    def __ne__(self, other):
+        return not self == other
+
+    def __eq__(self, other):
+        return (self.__class__ is other.__class__ and
+                self.__dict__ == other.__dict__)
+
     def check___thread(self):
         return True
 
+    
 if sys.platform == 'linux2':
     from pypy.translator.platform.linux import Linux
     host = Linux()
 else:
     xxx
+
+platform = host
+
+def set_platform(new_platform, cc):
+    global platform
+    assert new_platform == 'host'
+    log.msg("Setting platform to %r cc=%s" % (new_platform,cc))
+    # XXX fix
+    platform = host
+

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	Sat Oct 11 12:32:21 2008
@@ -1,12 +1,9 @@
 
 import py, os
 from pypy.translator.platform import Platform, CompilationError, ExecutionResult
+from pypy.translator.platform import log
 from subprocess import PIPE, Popen
 
-from pypy.tool.ansi_print import ansi_log
-log = py.log.Producer("cbuild")
-py.log.setconsumer("cbuild", ansi_log)
-
 def _run_subprocess(executable, args, env=None):
     if isinstance(args, str):
         args = str(executable) + ' ' + args
@@ -22,17 +19,21 @@
     return pipe.returncode, stdout, stderr
 
 class Linux(Platform):
-    link_extra = ['-lpthread']
+    link_extra = ['-pthread']
+    cflags = ['-O3', '-pthread', '-fomit-frame-pointer']
     
     def __init__(self, cc='gcc'):
         self.cc = cc
 
-    def _args_from_eci(self, cfiles, eci):
+    def _compile_args_from_eci(self, eci):
         include_dirs = ['-I%s' % (idir,) for idir in eci.include_dirs]
+        return (self.cflags + list(eci.compile_extra) + include_dirs)
+
+    def _link_args_from_eci(self, eci):
         library_dirs = ['-L%s' % (ldir,) for ldir in eci.library_dirs]
         libraries = ['-l%s' % (lib,) for lib in eci.libraries]
-        return (include_dirs + [str(f) for f in cfiles] +
-                library_dirs + libraries + self.link_extra)
+        return (library_dirs + libraries + self.link_extra +
+                list(eci.link_extra))
 
     def _args_for_shared(self, args):
         return ['-shared'] + args
@@ -40,20 +41,30 @@
     def compile(self, cfiles, eci, outputfilename=None, standalone=True):
         cfiles = [py.path.local(f) for f in cfiles]
         cfiles += [py.path.local(f) for f in eci.separate_module_files]
-        args = self._args_from_eci(cfiles, eci)
+        compile_args = self._compile_args_from_eci(eci)
         if outputfilename is None:
             outputfilename = cfiles[0].purebasename
         exe_name = py.path.local(os.path.join(str(cfiles[0].dirpath()),
                                               outputfilename))
         if not standalone:
             exe_name += '.so'
-        args += ['-o', str(exe_name)]
-        if not standalone:
-            args = self._args_for_shared(args)
-        log.execute(self.cc + ' ' + ' '.join(args))
-        returncode, stdout, stderr = _run_subprocess(self.cc, args)
+        ofiles = []
+        for cfile in cfiles:
+            ofiles.append(self._compile_c_file(self.cc, cfile, compile_args))
+        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)
         if returncode != 0:
-            errorfile = exe_name.new(ext='errors')
+            errorfile = outname.new(ext='errors')
             errorfile.write(stderr)
             stderrlines = stderr.splitlines()
             for line in stderrlines[:5]:
@@ -61,6 +72,13 @@
             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 execute(self, executable, args=None, env=None):

Modified: pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_platform.py	Sat Oct 11 12:32:21 2008
@@ -1,7 +1,7 @@
 
 import py, sys
 from pypy.tool.udir import udir
-from pypy.translator.platform import host, CompilationError
+from pypy.translator.platform import host, CompilationError, Platform
 from pypy.translator.tool.cbuild import ExternalCompilationInfo
 
 def test_simple_enough():
@@ -20,6 +20,30 @@
     assert res.err == ''
     assert res.returncode == 0
 
+def test_two_files():
+    cfile = udir.join('test_two_files.c')
+    cfile.write('''
+    #include <stdio.h>
+    int func();
+    int main()
+    {
+        printf("%d\\n", func());
+        return 0;
+    }
+    ''')
+    cfile2 = udir.join('implement1.c')
+    cfile2.write('''
+    int func()
+    {
+        return 42;
+    }
+    ''')
+    executable = host.compile([cfile, cfile2], ExternalCompilationInfo())
+    res = host.execute(executable)
+    assert res.out == '42\n'
+    assert res.err == ''
+    assert res.returncode == 0
+
 def test_nice_errors():
     cfile = udir.join('test_nice_errors.c')
     cfile.write('')
@@ -72,3 +96,16 @@
     executable = host.compile([c_file], eci)
     res = host.execute(executable)
     assert res.out.startswith('4.0')
+
+
+def test_equality():
+    class X(Platform):
+        def __init__(self):
+            pass
+    class Y(Platform):
+        def __init__(self, x):
+            self.x = x
+
+    assert X() == X()
+    assert Y(3) == Y(3)
+    assert Y(2) != Y(3)

Modified: pypy/branch/cbuild-refactor/pypy/translator/tool/cbuild.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/tool/cbuild.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/tool/cbuild.py	Sat Oct 11 12:32:21 2008
@@ -90,8 +90,7 @@
             assert isinstance(value, (list, tuple))
             setattr(self, name, tuple(value))
         if platform is None:
-            from pypy.rlib import pyplatform
-            platform = pyplatform.platform
+            from pypy.translator.platform import platform
         self.platform = platform
 
     def from_compiler_flags(cls, flags):
@@ -489,40 +488,8 @@
             extra_preargs=self.link_extra,
             library_dirs=self.eci.library_dirs)
 
-def build_executable(*args, **kwds):
-    noerr = kwds.pop('noerr', False)
-    compiler = CCompiler(*args, **kwds)
-    compiler.build(noerr=noerr)
-    return str(compiler.outputfilename)
-
-def check_boehm_presence(noerr=True):
-    from pypy.tool.udir import udir
-    try:
-        cfile = udir.join('check_boehm.c')
-        cfname = str(cfile)
-        cfile = cfile.open('w')
-        cfile.write("""
-#include <gc/gc.h>
-
-int main() {
-  return 0;
-}
-""")
-        cfile.close()
-        if sys.platform == 'win32':
-            eci = ExternalCompilationInfo(libraries=['gc_pypy'])
-        else:
-            eci = ExternalCompilationInfo(libraries=['gc'])
-        build_executable([cfname], eci, noerr=noerr)
-    except CompilationError:
-        if noerr:
-            return False
-        else:
-            raise
-    else:
-        return True
-
 def check_under_under_thread():
+    xxx
     from pypy.tool.udir import udir
     cfile = py.path.local(autopath.this_dir).join('__thread_test.c')
     fsource = cfile.open('r')

Modified: pypy/branch/cbuild-refactor/pypy/translator/tool/test/test_cbuild.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/tool/test/test_cbuild.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/tool/test/test_cbuild.py	Sat Oct 11 12:32:21 2008
@@ -125,38 +125,6 @@
 
 
 class Stuff:
-    def test_platform(self):
-        from pypy.rlib.pyplatform import Platform
-        class Expected(Exception):
-            pass
-        
-        class X(Platform):
-            def get_compiler(self):
-                raise Expected
-
-            def execute(self):
-                return 3
-
-        eci = ExternalCompilationInfo(platform=X())
-        try:
-            build_executable([self.modfile], eci)
-        except Expected:
-            pass
-        else:
-            py.test.fail("Did not raise")
-        assert eci.platform.execute() == 3
-
-    def test_platform_equality(self):
-        from pypy.rlib.pyplatform import Platform
-        class X(Platform):
-            pass
-        class Y(Platform):
-            def __init__(self, x):
-                self.x = x
-
-        assert X() == X()
-        assert Y(3) == Y(3)
-        assert Y(2) != Y(3)
 
     def checkscratchbox(self):
         if not py.path.local('/scratchbox/login').check():



More information about the Pypy-commit mailing list