[pypy-svn] r54818 - in pypy/dist/pypy/translator/tool: . test

arigo at codespeak.net arigo at codespeak.net
Sat May 17 13:16:57 CEST 2008


Author: arigo
Date: Sat May 17 13:16:55 2008
New Revision: 54818

Modified:
   pypy/dist/pypy/translator/tool/cbuild.py
   pypy/dist/pypy/translator/tool/test/test_cbuild.py
Log:
Some libraries have tools "xxx-config" that can be run to get
compiler paths and libraries information.  Add support for that
in ExternalCompilationInfo.


Modified: pypy/dist/pypy/translator/tool/cbuild.py
==============================================================================
--- pypy/dist/pypy/translator/tool/cbuild.py	(original)
+++ pypy/dist/pypy/translator/tool/cbuild.py	Sat May 17 13:16:55 2008
@@ -65,6 +65,38 @@
             assert isinstance(value, (list, tuple))
             setattr(self, name, tuple(value))
 
+    def from_compiler_flags(cls, flags):
+        """Returns a new ExternalCompilationInfo instance by parsing
+        the string 'flags', which is in the typical Unix compiler flags
+        format."""
+        pre_include_lines = []
+        include_dirs = []
+        libraries = []
+        library_dirs = []
+        for arg in flags.split():
+            if arg.startswith('-I'):
+                include_dirs.append(arg[2:])
+            elif arg.startswith('-L'):
+                library_dirs.append(arg[2:])
+            elif arg.startswith('-l'):
+                libraries.append(arg[2:])
+            elif arg.startswith('-D'):
+                pre_include_lines.append('#define ' + arg[2:])
+        return cls(pre_include_lines=pre_include_lines,
+                   include_dirs=include_dirs,
+                   libraries=libraries,
+                   library_dirs=library_dirs)
+    from_compiler_flags = classmethod(from_compiler_flags)
+
+    def from_config_tool(cls, execonfigtool):
+        """Returns a new ExternalCompilationInfo instance by executing
+        the 'execonfigtool' with --cflags and --libs arguments."""
+        execonfigtool = str(execonfigtool)
+        cflags = py.process.cmdexec([execonfigtool, '--cflags'])
+        libs = py.process.cmdexec([execonfigtool, '--libs'])
+        return cls.from_compiler_flags(cflags + ' ' + libs)
+    from_config_tool = classmethod(from_config_tool)
+
     def _value(self):
         return tuple([getattr(self, x) for x in self._ATTRIBUTES])
 

Modified: pypy/dist/pypy/translator/tool/test/test_cbuild.py
==============================================================================
--- pypy/dist/pypy/translator/tool/test/test_cbuild.py	(original)
+++ pypy/dist/pypy/translator/tool/test/test_cbuild.py	Sat May 17 13:16:55 2008
@@ -124,3 +124,27 @@
         assert ctypes.CDLL(neweci.libraries[0]).get() == 42
         assert not neweci.separate_module_sources
         assert not neweci.separate_module_files
+
+    def test_from_compiler_flags(self):
+        flags = ('-I/some/include/path -L/some/lib/path '
+                 '-I/other/include/path -L/other/lib/path '
+                 '-lmylib1 -lmylib2 '
+                 '-DMACRO1 -D_MACRO2')
+        eci = ExternalCompilationInfo.from_compiler_flags(flags)
+        assert eci.pre_include_lines == ('#define MACRO1',
+                                         '#define _MACRO2')
+        assert eci.includes == ()
+        assert eci.include_dirs == ('/some/include/path',
+                                    '/other/include/path')
+        assert eci.libraries == ('mylib1', 'mylib2')
+        assert eci.library_dirs == ('/some/lib/path',
+                                    '/other/lib/path')
+
+    def test_from_config_tool(self):
+        sdlconfig = py.path.local.sysfind('sdl-config')
+        if not sdlconfig:
+            py.test.skip("sdl-config not installed")
+        eci = ExternalCompilationInfo.from_config_tool(sdlconfig)
+        assert 'SDL' in eci.libraries
+        eci2 = ExternalCompilationInfo.from_config_tool(str(sdlconfig))
+        assert eci2 == eci



More information about the Pypy-commit mailing list