Commits

Alexander Hesse  committed 0224394

Moved pypy.tool.gcc_cache to rpython

  • Participants
  • Parent commits f54abb7
  • Branches split-rpython

Comments (0)

Files changed (6)

File pypy/tool/gcc_cache.py

-from pypy.conftest import pypydir
-from rpython.translator.platform import CompilationError
-from rpython.translator.tool.cbuild import ExternalCompilationInfo
-from pypy.tool.compat import md5
-import py
-
-cache_dir_root = py.path.local(pypydir).join('_cache').ensure(dir=1)
-
-def cache_file_path(c_files, eci, cachename):
-    "Builds a filename to cache compilation data"
-    # Import 'platform' every time, the compiler may have been changed
-    from rpython.translator.platform import platform
-    cache_dir = cache_dir_root.join(cachename).ensure(dir=1)
-    filecontents = [c_file.read() for c_file in c_files]
-    key = repr((filecontents, eci, platform.key()))
-    hash = md5(key).hexdigest()
-    return cache_dir.join(hash)
-
-def build_executable_cache(c_files, eci, ignore_errors=False):
-    "Builds and run a program; caches the result"
-    # Import 'platform' every time, the compiler may have been changed
-    from rpython.translator.platform import platform
-    path = cache_file_path(c_files, eci, 'build_executable_cache')
-    try:
-        return path.read()
-    except py.error.Error:
-        _previous = platform.log_errors
-        try:
-            if ignore_errors:
-                platform.log_errors = False
-            result = platform.execute(platform.compile(c_files, eci))
-        finally:
-            if ignore_errors:
-                del platform.log_errors
-            # ^^^remove from the instance --- needed so that it can
-            # compare equal to another instance without it
-            if platform.log_errors != _previous:
-                platform.log_errors = _previous
-        path.write(result.out)
-        return result.out
-
-def try_compile_cache(c_files, eci):
-    "Try to compile a program; caches the result (starts with 'True' or 'FAIL')"
-    # Import 'platform' every time, the compiler may have been changed
-    from rpython.translator.platform import platform
-    path = cache_file_path(c_files, eci, 'try_compile_cache')
-    try:
-        data = path.read()
-    except py.error.Error:
-        data = ''
-    if not (data.startswith('True') or data.startswith('FAIL\n')):
-        try:
-            _previous = platform.log_errors
-            try:
-                platform.log_errors = False
-                platform.compile(c_files, eci)
-            finally:
-                del platform.log_errors
-                # ^^^remove from the instance --- needed so that it can
-                # compare equal to another instance without it
-                if platform.log_errors != _previous:
-                    platform.log_errors = _previous
-            data = 'True'
-            path.write(data)
-        except CompilationError, e:
-            data = 'FAIL\n%s\n' % (e,)
-    if data.startswith('True'):
-        return True
-    else:
-        assert data.startswith('FAIL\n')
-        msg = data[len('FAIL\n'):]
-        raise CompilationError(msg.strip(), '')

File pypy/tool/test/test_gcc_cache.py

-import sys
-from pypy.tool.gcc_cache import *
-from rpython.tool.udir import udir
-import md5, cStringIO
-from rpython.translator.tool.cbuild import ExternalCompilationInfo
-
-localudir = udir.join('test_gcc_cache').ensure(dir=1)
-
-def test_gcc_exec():
-    f = localudir.join("x.c")
-    f.write("""
-    #include <stdio.h>
-    #include <test_gcc_exec.h>
-    int main()
-    {
-       printf("%d\\n", ANSWER);
-       return 0;
-    }
-    """)
-    dir1 = localudir.join('test_gcc_exec_dir1').ensure(dir=1)
-    dir2 = localudir.join('test_gcc_exec_dir2').ensure(dir=1)
-    dir1.join('test_gcc_exec.h').write('#define ANSWER 3\n')
-    dir2.join('test_gcc_exec.h').write('#define ANSWER 42\n')
-    eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
-    # remove cache
-    path = cache_file_path([f], eci, 'build_executable_cache')
-    if path.check():
-        path.remove()
-    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"
-    f.write("#error BOOM\n")
-    err = py.test.raises(CompilationError, build_executable_cache, [f], eci2)
-    print '<<<'
-    print err
-    print '>>>'
-
-def test_gcc_ask():
-    f = localudir.join("y.c")
-    f.write("""
-    #include <stdio.h>
-    #include <test_gcc_ask.h>
-    int main()
-    {
-       printf("hello\\n");
-       return 0;
-    }
-    """)
-    dir1 = localudir.join('test_gcc_ask_dir1').ensure(dir=1)
-    dir2 = localudir.join('test_gcc_ask_dir2').ensure(dir=1)
-    dir1.join('test_gcc_ask.h').write('/* hello world */\n')
-    dir2.join('test_gcc_ask.h').write('#error boom\n')
-    eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
-    # remove cache
-    path = cache_file_path([f], eci, 'try_compile_cache')
-    if path.check():
-        path.remove()
-    assert try_compile_cache([f], eci)
-    assert try_compile_cache([f], eci)
-    assert build_executable_cache([f], eci) == "hello\n"
-    eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
-    err = py.test.raises(CompilationError, try_compile_cache, [f], eci2)
-    print '<<<'
-    print err
-    print '>>>'
-
-def test_gcc_ask_doesnt_log_errors():
-    f = localudir.join('z.c')
-    f.write("""this file is not valid C code\n""")
-    eci = ExternalCompilationInfo()
-    oldstderr = sys.stderr
-    try:
-        sys.stderr = capture = cStringIO.StringIO()
-        py.test.raises(CompilationError, try_compile_cache, [f], eci)
-    finally:
-        sys.stderr = oldstderr
-    assert 'ERROR' not in capture.getvalue().upper()
-
-def test_execute_code_ignore_errors():
-    f = localudir.join('z.c')
-    f.write("""this file is not valid C code\n""")
-    eci = ExternalCompilationInfo()
-    oldstderr = sys.stderr
-    try:
-        sys.stderr = capture = cStringIO.StringIO()
-        py.test.raises(CompilationError, build_executable_cache,
-                       [f], eci, True)
-    finally:
-        sys.stderr = oldstderr
-    assert 'ERROR' not in capture.getvalue().upper()
-    

File rpython/rtyper/tool/rffi_platform.py

 from rpython.rtyper.lltypesystem import lltype
 from rpython.rtyper.lltypesystem import rffi
 from rpython.rtyper.lltypesystem import llmemory
-from pypy.tool.gcc_cache import build_executable_cache, try_compile_cache
+from rpython.tool.gcc_cache import build_executable_cache, try_compile_cache
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 from rpython.translator.platform import CompilationError
 from rpython.tool.udir import udir

File rpython/rtyper/tool/rfficache.py

 from rpython.tool.udir import udir
 from rpython.rlib import rarithmetic
 from rpython.rtyper.lltypesystem import lltype
-from pypy.tool.gcc_cache import build_executable_cache
+from rpython.tool.gcc_cache import build_executable_cache
 
 def ask_gcc(question, add_source="", ignore_errors=False):
     from rpython.translator.platform import platform

File rpython/tool/gcc_cache.py

+from pypy.conftest import pypydir
+from rpython.translator.platform import CompilationError
+from rpython.translator.tool.cbuild import ExternalCompilationInfo
+from pypy.tool.compat import md5
+import py
+
+cache_dir_root = py.path.local(pypydir).join('_cache').ensure(dir=1)
+
+def cache_file_path(c_files, eci, cachename):
+    "Builds a filename to cache compilation data"
+    # Import 'platform' every time, the compiler may have been changed
+    from rpython.translator.platform import platform
+    cache_dir = cache_dir_root.join(cachename).ensure(dir=1)
+    filecontents = [c_file.read() for c_file in c_files]
+    key = repr((filecontents, eci, platform.key()))
+    hash = md5(key).hexdigest()
+    return cache_dir.join(hash)
+
+def build_executable_cache(c_files, eci, ignore_errors=False):
+    "Builds and run a program; caches the result"
+    # Import 'platform' every time, the compiler may have been changed
+    from rpython.translator.platform import platform
+    path = cache_file_path(c_files, eci, 'build_executable_cache')
+    try:
+        return path.read()
+    except py.error.Error:
+        _previous = platform.log_errors
+        try:
+            if ignore_errors:
+                platform.log_errors = False
+            result = platform.execute(platform.compile(c_files, eci))
+        finally:
+            if ignore_errors:
+                del platform.log_errors
+            # ^^^remove from the instance --- needed so that it can
+            # compare equal to another instance without it
+            if platform.log_errors != _previous:
+                platform.log_errors = _previous
+        path.write(result.out)
+        return result.out
+
+def try_compile_cache(c_files, eci):
+    "Try to compile a program; caches the result (starts with 'True' or 'FAIL')"
+    # Import 'platform' every time, the compiler may have been changed
+    from rpython.translator.platform import platform
+    path = cache_file_path(c_files, eci, 'try_compile_cache')
+    try:
+        data = path.read()
+    except py.error.Error:
+        data = ''
+    if not (data.startswith('True') or data.startswith('FAIL\n')):
+        try:
+            _previous = platform.log_errors
+            try:
+                platform.log_errors = False
+                platform.compile(c_files, eci)
+            finally:
+                del platform.log_errors
+                # ^^^remove from the instance --- needed so that it can
+                # compare equal to another instance without it
+                if platform.log_errors != _previous:
+                    platform.log_errors = _previous
+            data = 'True'
+            path.write(data)
+        except CompilationError, e:
+            data = 'FAIL\n%s\n' % (e,)
+    if data.startswith('True'):
+        return True
+    else:
+        assert data.startswith('FAIL\n')
+        msg = data[len('FAIL\n'):]
+        raise CompilationError(msg.strip(), '')

File rpython/tool/test/test_gcc_cache.py

+import sys
+from rpython.tool.gcc_cache import *
+from rpython.tool.udir import udir
+import md5, cStringIO
+from rpython.translator.tool.cbuild import ExternalCompilationInfo
+
+localudir = udir.join('test_gcc_cache').ensure(dir=1)
+
+def test_gcc_exec():
+    f = localudir.join("x.c")
+    f.write("""
+    #include <stdio.h>
+    #include <test_gcc_exec.h>
+    int main()
+    {
+       printf("%d\\n", ANSWER);
+       return 0;
+    }
+    """)
+    dir1 = localudir.join('test_gcc_exec_dir1').ensure(dir=1)
+    dir2 = localudir.join('test_gcc_exec_dir2').ensure(dir=1)
+    dir1.join('test_gcc_exec.h').write('#define ANSWER 3\n')
+    dir2.join('test_gcc_exec.h').write('#define ANSWER 42\n')
+    eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
+    # remove cache
+    path = cache_file_path([f], eci, 'build_executable_cache')
+    if path.check():
+        path.remove()
+    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"
+    f.write("#error BOOM\n")
+    err = py.test.raises(CompilationError, build_executable_cache, [f], eci2)
+    print '<<<'
+    print err
+    print '>>>'
+
+def test_gcc_ask():
+    f = localudir.join("y.c")
+    f.write("""
+    #include <stdio.h>
+    #include <test_gcc_ask.h>
+    int main()
+    {
+       printf("hello\\n");
+       return 0;
+    }
+    """)
+    dir1 = localudir.join('test_gcc_ask_dir1').ensure(dir=1)
+    dir2 = localudir.join('test_gcc_ask_dir2').ensure(dir=1)
+    dir1.join('test_gcc_ask.h').write('/* hello world */\n')
+    dir2.join('test_gcc_ask.h').write('#error boom\n')
+    eci = ExternalCompilationInfo(include_dirs=[str(dir1)])
+    # remove cache
+    path = cache_file_path([f], eci, 'try_compile_cache')
+    if path.check():
+        path.remove()
+    assert try_compile_cache([f], eci)
+    assert try_compile_cache([f], eci)
+    assert build_executable_cache([f], eci) == "hello\n"
+    eci2 = ExternalCompilationInfo(include_dirs=[str(dir2)])
+    err = py.test.raises(CompilationError, try_compile_cache, [f], eci2)
+    print '<<<'
+    print err
+    print '>>>'
+
+def test_gcc_ask_doesnt_log_errors():
+    f = localudir.join('z.c')
+    f.write("""this file is not valid C code\n""")
+    eci = ExternalCompilationInfo()
+    oldstderr = sys.stderr
+    try:
+        sys.stderr = capture = cStringIO.StringIO()
+        py.test.raises(CompilationError, try_compile_cache, [f], eci)
+    finally:
+        sys.stderr = oldstderr
+    assert 'ERROR' not in capture.getvalue().upper()
+
+def test_execute_code_ignore_errors():
+    f = localudir.join('z.c')
+    f.write("""this file is not valid C code\n""")
+    eci = ExternalCompilationInfo()
+    oldstderr = sys.stderr
+    try:
+        sys.stderr = capture = cStringIO.StringIO()
+        py.test.raises(CompilationError, build_executable_cache,
+                       [f], eci, True)
+    finally:
+        sys.stderr = oldstderr
+    assert 'ERROR' not in capture.getvalue().upper()
+