[pypy-svn] r58929 - in pypy/branch/cbuild-refactor/pypy: config translator translator/c translator/platform translator/platform/test translator/tool

fijal at codespeak.net fijal at codespeak.net
Fri Oct 10 19:33:42 CEST 2008


Author: fijal
Date: Fri Oct 10 19:33:42 2008
New Revision: 58929

Added:
   pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_posix.py   (contents, props changed)
Modified:
   pypy/branch/cbuild-refactor/pypy/config/translationoption.py
   pypy/branch/cbuild-refactor/pypy/translator/c/genc.py
   pypy/branch/cbuild-refactor/pypy/translator/driver.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/tool/cbuild.py
Log:
(pedronis, fijal)
Shave two yaks in order to gradually remove cbuild.py as it is.
Moved profopt to genc and some other things. Completely disable
cbuild.CCompiler for now.


Modified: pypy/branch/cbuild-refactor/pypy/config/translationoption.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/config/translationoption.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/config/translationoption.py	Fri Oct 10 19:33:42 2008
@@ -352,16 +352,6 @@
     'maemo',
 ]
 
-def set_platform(config, platform):
-    from pypy.rlib.pyplatform import Platform, Maemo
-    from pypy.rlib import pyplatform
-    from pypy.translator.tool.cbuild import ExternalCompilationInfo
-    if isinstance(platform, str):
-        if platform == 'maemo':
-            platform = Maemo(cc=config.translation.cc or None)
-        elif platform == 'host':
-            return
-        else:
-            raise NotImplementedError('Platform = %s' % (platform,))
-    assert isinstance(platform, Platform)
-    pyplatform.platform = platform
+def get_platform(config):
+    from pypy.translator.platform import host
+    return host

Modified: pypy/branch/cbuild-refactor/pypy/translator/c/genc.py
==============================================================================
--- pypy/branch/cbuild-refactor/pypy/translator/c/genc.py	(original)
+++ pypy/branch/cbuild-refactor/pypy/translator/c/genc.py	Fri Oct 10 19:33:42 2008
@@ -7,8 +7,6 @@
 from pypy.translator.llsupport.wrapper import new_wrapper
 from pypy.translator.gensupp import uniquemodulename, NameManager
 from pypy.translator.tool.cbuild import so_ext, ExternalCompilationInfo
-from pypy.translator.tool.cbuild import compile_c_module
-from pypy.translator.tool.cbuild import CCompiler, ProfOpt
 from pypy.translator.tool.cbuild import import_module_from_directory
 from pypy.translator.tool.cbuild import check_under_under_thread
 from pypy.rpython.lltypesystem import lltype
@@ -18,6 +16,60 @@
 from pypy.rpython.typesystem import getfunctionptr
 from pypy.translator.c import gc
 
+class ProfOpt(object):
+    #XXX assuming gcc style flags for now
+    name = "profopt"
+
+    def __init__(self, compiler):
+        self.compiler = compiler
+
+    def first(self):
+        return self.build('-fprofile-generate')
+
+    def probe(self, exe, args):
+        # 'args' is a single string typically containing spaces
+        # and quotes, which represents several arguments.
+        self.compiler.platform.execute(exe, args)
+
+    def after(self):
+        return self.build('-fprofile-use')
+
+    def build(self, option):
+        eci = ExternalCompilationInfo(compile_extra=[option],
+                                      link_extra=[option])
+        # XXX
+        #compiler.fix_gcc_random_seed = True
+        return self.compiler._build(eci)
+
+class CCompilerDriver(object):
+    def __init__(self, platform, cfiles, eci, outputfilename=None,
+                 profbased=False):
+        self.platform = platform
+        self.cfiles = cfiles
+        self.eci = eci
+        self.outputfilename = outputfilename
+        self.profbased = profbased
+
+    def _build(self, eci=ExternalCompilationInfo()):
+        return self.platform.compile(self.cfiles, self.eci.merge(eci),
+                                     outputfilename=self.outputfilename)
+
+    def build(self):
+        if self.profbased:
+            return self._do_profbased()
+        return self._build()
+
+    def _do_profbased(self):
+        ProfDriver, args = self.profbased
+        profdrv = ProfDriver(self)
+        dolog = getattr(log, profdrv.name)
+        dolog(args)
+        exename = profdrv.first()
+        dolog('Gathering profile data from: %s %s' % (
+            str(exename), args))
+        profdrv.probe(exename, args)
+        return profdrv.after()
+    
 class CBuilder(object):
     c_source_filename = None
     _compiled = False
@@ -132,7 +184,7 @@
         if self.config.translation.sandbox:
             defines['RPY_SANDBOXED'] = 1
         if CBuilder.have___thread is None:
-            CBuilder.have___thread = check_under_under_thread()
+            CBuilder.have___thread = self.translator.platform.check___thread()
         if not self.standalone:
             assert not self.config.translation.instrument
             cfile, extra = gen_source(db, modulename, targetdir, self.eci,
@@ -330,19 +382,20 @@
     def compile(self):
         assert self.c_source_filename
         assert not self._compiled
-        compiler = self.getccompiler()
+        compiler = CCompilerDriver(self.translator.platform,
+                                   [self.c_source_filename] + self.extrafiles,
+                                   self.eci, profbased=self.getprofbased())
         if self.config.translation.gcrootfinder == "asmgcc":
+            xxx
             # as we are gcc-only anyway, let's just use the Makefile.
             cmdline = "make -C '%s'" % (self.targetdir,)
             err = os.system(cmdline)
             if err != 0:
                 raise OSError("failed (see output): " + cmdline)
         else:
-            eci = self.eci.merge(ExternalCompilationInfo(includes=
-                                                         [str(self.targetdir)]))
-            self.adaptflags(compiler)
-            compiler.build()
-        self.executable_name = str(compiler.outputfilename)
+            #self.adaptflags(compiler)
+            self.executable_name = compiler.build()
+            assert self.executable_name
         self._compiled = True
         return self.executable_name
 
@@ -361,6 +414,7 @@
             compiler.link_extra.append(self.config.translation.linkerflags)
 
     def gen_makefile(self, targetdir):
+        return # XXX
         def write_list(lst, prefix):
             for i, fn in enumerate(lst):
                 print >> f, prefix, 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	Fri Oct 10 19:33:42 2008
@@ -53,13 +53,12 @@
         self.compiler = compiler
 
     def first(self):
-        self.compiler._build()
+        return self.compiler._build()
 
     def probe(self, exe, args):
-        from py.compat import subprocess
         env = os.environ.copy()
         env['_INSTRUMENT_COUNTERS'] = str(self.datafile)
-        subprocess.call("'%s' %s" % (exe, args), env=env, shell=True)
+        self.compiler.platform.execute(exe, args, env=env)
         
     def after(self):
         # xxx

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	Fri Oct 10 19:33:42 2008
@@ -16,6 +16,9 @@
         self.out = out
         self.err = err
 
+    def __repr__(self):
+        return "<ExecutionResult retcode=%d>" % (self.returncode,)
+
 class Platform(object):
     def __init__(self, cc):
         self.cc = cc
@@ -23,12 +26,15 @@
     def compile(self, cfiles, eci, outputfilename=None, standalone=True):
         raise NotImplementedError("Pure abstract baseclass")
 
-    def execute(self, file_to_exec):
+    def execute(self, file_to_exec, args=None, env=None):
         raise NotImplementedError("Pure abstract baseclass")
 
     def __repr__(self):
         return '<%s cc=%s>' % (self.__class__.__name__, self.cc)
 
+    def check___thread(self):
+        return True
+
 if sys.platform == 'linux2':
     from pypy.translator.platform.linux import Linux
     host = Linux()

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	Fri Oct 10 19:33:42 2008
@@ -7,9 +7,17 @@
 log = py.log.Producer("cbuild")
 py.log.setconsumer("cbuild", ansi_log)
 
-def _run_subprocess(args):
-    pipe = Popen(args, executable=args[0],
-                 stdout=PIPE, stderr=PIPE, shell=False)
+def _run_subprocess(executable, args, env=None):
+    if isinstance(args, str):
+        args = str(executable) + ' ' + args
+        shell = True
+    else:
+        if args is None:
+            args = [str(executable)]
+        else:
+            args = [str(executable)] + args
+        shell = False
+    pipe = Popen(args, stdout=PIPE, stderr=PIPE, shell=shell, env=env)
     stdout, stderr = pipe.communicate()
     return pipe.returncode, stdout, stderr
 
@@ -42,9 +50,8 @@
         args += ['-o', str(exe_name)]
         if not standalone:
             args = self._args_for_shared(args)
-        args = [self.cc] + args
-        log.execute(' '.join(args))
-        returncode, stdout, stderr = _run_subprocess(args)
+        log.execute(self.cc + ' ' + ' '.join(args))
+        returncode, stdout, stderr = _run_subprocess(self.cc, args)
         if returncode != 0:
             errorfile = exe_name.new(ext='errors')
             errorfile.write(stderr)
@@ -56,6 +63,7 @@
             raise CompilationError(stdout, stderr)
         return exe_name
 
-    def execute(self, executable):
-        returncode, stdout, stderr = _run_subprocess([str(executable)])
+    def execute(self, executable, args=None, env=None):
+        returncode, stdout, stderr = _run_subprocess(str(executable), args,
+                                                     env)
         return ExecutionResult(returncode, stdout, stderr)

Added: pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_posix.py
==============================================================================
--- (empty file)
+++ pypy/branch/cbuild-refactor/pypy/translator/platform/test/test_posix.py	Fri Oct 10 19:33:42 2008
@@ -0,0 +1,9 @@
+
+from pypy.translator.platform import host, CompilationError
+
+def test_echo():
+    res = host.execute('echo', '42 24')
+    assert res.out == '42 24\n'
+    res = host.execute('echo', ['42', '24'])
+    assert res.out == '42 24\n'
+    

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	Fri Oct 10 19:33:42 2008
@@ -341,35 +341,6 @@
     return spawn_and_log
 
 
-class ProfOpt(object):
-    #XXX assuming gcc style flags for now
-    name = "profopt"
-
-    def __init__(self, compiler):
-        self.compiler = compiler
-
-    def first(self):
-        self.build('-fprofile-generate')
-
-    def probe(self, exe, args):
-        # 'args' is a single string typically containing spaces
-        # and quotes, which represents several arguments.
-        os.system("'%s' %s" % (exe, args))
-
-    def after(self):
-        self.build('-fprofile-use')
-
-    def build(self, option):
-        compiler = self.compiler
-        compiler.fix_gcc_random_seed = True
-        compiler.compile_extra.append(option)
-        compiler.link_extra.append(option)
-        try:
-            compiler._build()
-        finally:
-            compiler.compile_extra.pop()
-            compiler.link_extra.pop()
-
 class CompilationError(Exception):
     pass
 
@@ -378,6 +349,7 @@
 
     def __init__(self, cfilenames, eci, outputfilename=None,
                  compiler_exe=None, profbased=None, standalone=True):
+        XXX
         self.cfilenames = cfilenames
         if standalone:
             ext = ''



More information about the Pypy-commit mailing list