| 1 | import py |
| 2 | |
| 3 | from rpython.tool.udir import udir |
| 4 | from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 5 | from subprocess import Popen, PIPE, STDOUT |
| 6 | |
| 7 | class TestEci: |
| 8 | def setup_class(cls): |
| 9 | tmpdir = udir.ensure('testeci', dir=1) |
| 10 | c_file = tmpdir.join('module.c') |
| 11 | c_file.write(py.code.Source(''' |
| 12 | int sum(int x, int y) |
| 13 | { |
| 14 | return x + y; |
| 15 | } |
| 16 | ''')) |
| 17 | cls.modfile = c_file |
| 18 | cls.tmpdir = tmpdir |
| 19 | |
| 20 | def test_merge(self): |
| 21 | e1 = ExternalCompilationInfo( |
| 22 | pre_include_bits = ['1'], |
| 23 | includes = ['x.h'], |
| 24 | post_include_bits = ['p1'] |
| 25 | ) |
| 26 | e2 = ExternalCompilationInfo( |
| 27 | pre_include_bits = ['2'], |
| 28 | includes = ['x.h', 'y.h'], |
| 29 | post_include_bits = ['p2'], |
| 30 | ) |
| 31 | e3 = ExternalCompilationInfo( |
| 32 | pre_include_bits = ['3'], |
| 33 | includes = ['y.h', 'z.h'], |
| 34 | post_include_bits = ['p1', 'p3'] |
| 35 | ) |
| 36 | e = e1.merge(e2, e3) |
| 37 | assert e.pre_include_bits == ('1', '2', '3') |
| 38 | assert e.includes == ('x.h', 'y.h', 'z.h') |
| 39 | assert e.post_include_bits == ('p1', 'p2', 'p3') |
| 40 | |
| 41 | def test_merge2(self): |
| 42 | e1 = ExternalCompilationInfo( |
| 43 | pre_include_bits = ['1'], |
| 44 | link_files = ['1.c'] |
| 45 | ) |
| 46 | e2 = ExternalCompilationInfo( |
| 47 | pre_include_bits = ['2'], |
| 48 | link_files = ['1.c', '2.c'] |
| 49 | ) |
| 50 | e3 = ExternalCompilationInfo( |
| 51 | pre_include_bits = ['3'], |
| 52 | link_files = ['1.c', '2.c', '3.c'] |
| 53 | ) |
| 54 | e = e1.merge(e2) |
| 55 | e = e.merge(e3, e3) |
| 56 | assert e.pre_include_bits == ('1', '2', '3') |
| 57 | assert e.link_files == ('1.c', '2.c', '3.c') |
| 58 | |
| 59 | def test_convert_sources_to_c_files(self): |
| 60 | eci = ExternalCompilationInfo( |
| 61 | separate_module_sources = ['xxx'], |
| 62 | separate_module_files = ['x.c'], |
| 63 | ) |
| 64 | cache_dir = udir.join('test_convert_sources').ensure(dir=1) |
| 65 | neweci = eci.convert_sources_to_files(cache_dir) |
| 66 | assert not neweci.separate_module_sources |
| 67 | res = neweci.separate_module_files |
| 68 | assert len(res) == 2 |
| 69 | assert res[0] == 'x.c' |
| 70 | assert str(res[1]).startswith(str(cache_dir)) |
| 71 | e = ExternalCompilationInfo() |
| 72 | assert e.convert_sources_to_files() is e |
| 73 | |
| 74 | def test_make_shared_lib(self): |
| 75 | eci = ExternalCompilationInfo( |
| 76 | separate_module_sources = [''' |
| 77 | RPY_EXTERN int get() |
| 78 | { |
| 79 | return 42; |
| 80 | } |
| 81 | int shouldnt_export() |
| 82 | { |
| 83 | return 43; |
| 84 | }'''], |
| 85 | ) |
| 86 | neweci = eci.compile_shared_lib() |
| 87 | assert len(neweci.libraries) == 1 |
| 88 | try: |
| 89 | import ctypes |
| 90 | except ImportError: |
| 91 | py.test.skip("Need ctypes for that test") |
| 92 | assert ctypes.CDLL(neweci.libraries[0]).get() == 42 |
| 93 | assert not hasattr(ctypes.CDLL(neweci.libraries[0]), 'shouldnt_export') |
| 94 | assert not neweci.separate_module_sources |
| 95 | assert not neweci.separate_module_files |
| 96 | |
| 97 | def test_from_compiler_flags(self): |
| 98 | flags = ('-I/some/include/path -I/other/include/path ' |
| 99 | '-DMACRO1 -D_MACRO2=baz -?1 -!2') |
| 100 | eci = ExternalCompilationInfo.from_compiler_flags(flags) |
| 101 | assert eci.pre_include_bits == ('#define MACRO1 1', |
| 102 | '#define _MACRO2 baz') |
| 103 | assert eci.includes == () |
| 104 | assert eci.include_dirs == ('/some/include/path', |
| 105 | '/other/include/path') |
| 106 | assert eci.compile_extra == ('-?1', '-!2') |
| 107 | |
| 108 | def test_from_linker_flags(self): |
| 109 | flags = ('-L/some/lib/path -L/other/lib/path ' |
| 110 | '-lmylib1 -lmylib2 -?1 -!2') |
| 111 | eci = ExternalCompilationInfo.from_linker_flags(flags) |
| 112 | assert eci.libraries == ('mylib1', 'mylib2') |
| 113 | assert eci.library_dirs == ('/some/lib/path', |
| 114 | '/other/lib/path') |
| 115 | assert eci.link_extra == ('-?1', '-!2') |
| 116 | |
| 117 | def test_from_config_tool(self): |
| 118 | sdlconfig = py.path.local.sysfind('sdl-config') |
| 119 | if not sdlconfig: |
| 120 | py.test.skip("sdl-config not installed") |
| 121 | eci = ExternalCompilationInfo.from_config_tool('sdl-config') |
| 122 | assert 'SDL' in eci.libraries |
| 123 | |
| 124 | def test_from_missing_config_tool(self): |
| 125 | py.test.raises(ImportError, |
| 126 | ExternalCompilationInfo.from_config_tool, |
| 127 | 'dxowqbncpqympqhe-config') |
| 128 | |
| 129 | def test_from_pkg_config(self): |
| 130 | try: |
| 131 | cmd = ['pkg-config', 'ncurses', '--exists'] |
| 132 | popen = Popen(cmd) |
| 133 | result = popen.wait() |
| 134 | except OSError: |
| 135 | result = -1 |
| 136 | if result != 0: |
| 137 | py.test.skip("failed: %r" % (' '.join(cmd),)) |
| 138 | eci = ExternalCompilationInfo.from_pkg_config('ncurses') |
| 139 | assert 'ncurses' in eci.libraries |
| 140 | |
| 141 | def test_platforms(self): |
| 142 | from rpython.translator.platform import Platform |
| 143 | |
| 144 | class Maemo(Platform): |
| 145 | def __init__(self, cc=None): |
| 146 | self.cc = cc |
| 147 | |
| 148 | eci = ExternalCompilationInfo(platform=Maemo()) |
| 149 | eci2 = ExternalCompilationInfo() |
| 150 | assert eci != eci2 |
| 151 | assert hash(eci) != hash(eci2) |
| 152 | assert repr(eci) != repr(eci2) |
| 153 | py.test.raises(Exception, eci2.merge, eci) |
| 154 | assert eci.merge(eci).platform == Maemo() |
| 155 | assert (ExternalCompilationInfo(platform=Maemo(cc='xxx')) != |
| 156 | ExternalCompilationInfo(platform=Maemo(cc='yyy'))) |
| 157 | assert (repr(ExternalCompilationInfo(platform=Maemo(cc='xxx'))) != |
| 158 | repr(ExternalCompilationInfo(platform=Maemo(cc='yyy')))) |